From 10ece6b20983eeb185f6b5cb6fc2ad71e596c993 Mon Sep 17 00:00:00 2001
From: Jean-Noël Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Mon, 31 Oct 2016 15:41:17 +0000
Subject: [PATCH] Make more use of the Args class
---
opendj-server-legacy/src/test/java/org/opends/server/util/Args.java | 59 ++++++++
opendj-server-legacy/src/test/java/org/opends/server/tools/UpgradeTestCase.java | 18 -
opendj-server-legacy/src/test/java/org/opends/server/core/RejectUnauthReqTests.java | 41 +----
opendj-server-legacy/src/test/java/org/opends/server/backends/SchemaBackendTestCase.java | 18 +-
opendj-server-legacy/src/test/java/org/opends/server/extensions/DigestMD5SASLMechanismHandlerTestCase.java | 277 +++++----------------------------------
5 files changed, 117 insertions(+), 296 deletions(-)
diff --git a/opendj-server-legacy/src/test/java/org/opends/server/backends/SchemaBackendTestCase.java b/opendj-server-legacy/src/test/java/org/opends/server/backends/SchemaBackendTestCase.java
index 131b9e0..a99b122 100644
--- a/opendj-server-legacy/src/test/java/org/opends/server/backends/SchemaBackendTestCase.java
+++ b/opendj-server-legacy/src/test/java/org/opends/server/backends/SchemaBackendTestCase.java
@@ -70,7 +70,7 @@
import org.opends.server.types.LDIFImportConfig;
import org.opends.server.types.LDIFImportResult;
import org.opends.server.types.SearchFilter;
-import org.opends.server.util.CollectionUtils;
+import org.opends.server.util.Args;
import org.opends.server.util.ServerConstants;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
@@ -2066,18 +2066,16 @@
private static String[] args(boolean usePermissiveModifyControl)
{
- final List<String> args = CollectionUtils.newArrayList(
- "-h", "127.0.0.1",
- "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
- "-D", "cn=Directory Manager",
- "-w", "password"
- );
+ final Args args = new Args()
+ .add("-h", "127.0.0.1")
+ .add("-p", TestCaseUtils.getServerLdapPort())
+ .add("-D", "cn=Directory Manager")
+ .add("-w", "password");
if (usePermissiveModifyControl)
{
- args.add("-J");
- args.add(ServerConstants.OID_PERMISSIVE_MODIFY_CONTROL);
+ args.add("-J", ServerConstants.OID_PERMISSIVE_MODIFY_CONTROL);
}
- return args.toArray(new String[0]);
+ return args.toArray();
}
/**
diff --git a/opendj-server-legacy/src/test/java/org/opends/server/core/RejectUnauthReqTests.java b/opendj-server-legacy/src/test/java/org/opends/server/core/RejectUnauthReqTests.java
index ab71f03..46a1a97 100644
--- a/opendj-server-legacy/src/test/java/org/opends/server/core/RejectUnauthReqTests.java
+++ b/opendj-server-legacy/src/test/java/org/opends/server/core/RejectUnauthReqTests.java
@@ -17,7 +17,6 @@
package org.opends.server.core;
import java.util.ArrayList;
-import java.util.List;
import com.forgerock.opendj.ldap.tools.LDAPCompare;
import org.forgerock.opendj.ldap.ByteString;
@@ -33,6 +32,7 @@
import org.opends.server.types.AuthenticationInfo;
import org.opends.server.types.Control;
import org.opends.server.types.LDAPException;
+import org.opends.server.util.Args;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
@@ -49,33 +49,6 @@
@SuppressWarnings("javadoc")
public class RejectUnauthReqTests extends CoreTestCase
{
- private class Args
- {
- private final List<String> args = new ArrayList<>();
-
- public void add(String arg)
- {
- args.add(arg);
- }
-
- public void add(String arg, Object value)
- {
- args.add(arg);
- args.add(value.toString());
- }
-
- public String[] toArray()
- {
- return args.toArray(new String[args.size()]);
- }
-
- @Override
- public String toString()
- {
- return args.toString();
- }
- }
-
/**
* Utility method which is called by the testcase sending an ADD request.
*
@@ -101,14 +74,14 @@
private String[] args(boolean authenticate, String filePath)
{
- Args args = new Args();
- args.add("--noPropertiesFile");
- args.add("-h", "127.0.0.1");
- args.add("-p", TestCaseUtils.getServerLdapPort());
+ Args args = new Args()
+ .add("--noPropertiesFile")
+ .add("-h", "127.0.0.1")
+ .add("-p", TestCaseUtils.getServerLdapPort());
if (authenticate)
{
- args.add("-D", "cn=directory manager");
- args.add("-w", "password");
+ args.add("-D", "cn=directory manager")
+ .add("-w", "password");
}
args.add("-f", filePath);
return args.toArray();
diff --git a/opendj-server-legacy/src/test/java/org/opends/server/extensions/DigestMD5SASLMechanismHandlerTestCase.java b/opendj-server-legacy/src/test/java/org/opends/server/extensions/DigestMD5SASLMechanismHandlerTestCase.java
index fc3b3eb..14e8f7d 100644
--- a/opendj-server-legacy/src/test/java/org/opends/server/extensions/DigestMD5SASLMechanismHandlerTestCase.java
+++ b/opendj-server-legacy/src/test/java/org/opends/server/extensions/DigestMD5SASLMechanismHandlerTestCase.java
@@ -20,18 +20,19 @@
import org.forgerock.opendj.config.server.ConfigException;
import org.forgerock.opendj.ldap.ByteString;
+import org.forgerock.opendj.ldap.DN;
import org.forgerock.opendj.ldap.ResultCode;
-import org.opends.server.TestCaseUtils;
import org.forgerock.opendj.server.config.meta.DigestMD5SASLMechanismHandlerCfgDefn;
+import org.opends.server.TestCaseUtils;
import org.opends.server.core.BindOperation;
import org.opends.server.core.DeleteOperation;
import org.opends.server.core.DirectoryServer;
import org.opends.server.protocols.internal.InternalClientConnection;
import com.forgerock.opendj.ldap.tools.LDAPSearch;
import org.opends.server.types.AuthenticationInfo;
-import org.forgerock.opendj.ldap.DN;
import org.opends.server.types.Entry;
import org.opends.server.types.InitializationException;
+import org.opends.server.util.Args;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
@@ -43,9 +44,7 @@
import static org.opends.server.util.ServerConstants.*;
import static org.testng.Assert.*;
-/**
- * A set of test cases for the DIGEST-MD5 SASL mechanism handler.
- */
+/** A set of test cases for the DIGEST-MD5 SASL mechanism handler. */
public class DigestMD5SASLMechanismHandlerTestCase
extends ExtensionsTestCase
{
@@ -205,19 +204,7 @@
"ds-pwp-password-policy-dn: cn=Clear UserPassword Policy," +
"cn=Password Policies,cn=config");
- String[] args =
- {
- "--noPropertiesFile",
- "-h", "127.0.0.1",
- "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
- "-o", "mech=DIGEST-MD5",
- "-o", "authid=u:test.user",
- "-o", "authzid=u:test.user",
- "-w", "password",
- "-b", "",
- "-s", "base",
- "(objectClass=*)"
- };
+ String[] args = args("authid=u:test.user", "authzid=u:test.user", "password");
assertEquals(LDAPSearch.run(nullPrintStream(), System.err, args), 0);
}
@@ -248,19 +235,7 @@
"ds-pwp-password-policy-dn: cn=Clear UserPassword Policy," +
"cn=Password Policies,cn=config");
- String[] args =
- {
- "--noPropertiesFile",
- "-h", "127.0.0.1",
- "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
- "-o", "mech=DIGEST-MD5",
- "-o", "authid=dn:uid=test.user,o=test",
- "-o", "authzid=dn:uid=test.user,o=test",
- "-w", "password",
- "-b", "",
- "-s", "base",
- "(objectClass=*)"
- };
+ String[] args = args("authid=dn:uid=test.user,o=test", "authzid=dn:uid=test.user,o=test", "password");
assertEquals(LDAPSearch.run(nullPrintStream(), System.err, args), 0);
}
@@ -291,19 +266,7 @@
"ds-pwp-password-policy-dn: cn=Clear UserPassword Policy," +
"cn=Password Policies,cn=config");
- String[] args =
- {
- "--noPropertiesFile",
- "-h", "127.0.0.1",
- "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
- "-o", "mech=DIGEST-MD5",
- "-o", "authid=u:test.user",
- "-o", "authzid=u:test.user",
- "-w", "wrongpassword",
- "-b", "",
- "-s", "base",
- "(objectClass=*)"
- };
+ String[] args = args("authid=u:test.user", "authzid=u:test.user", "wrongpassword");
assertFalse(LDAPSearch.run(nullPrintStream(), nullPrintStream(), args) == 0);
}
@@ -334,19 +297,7 @@
"ds-pwp-password-policy-dn: cn=Clear UserPassword Policy," +
"cn=Password Policies,cn=config");
- String[] args =
- {
- "--noPropertiesFile",
- "-h", "127.0.0.1",
- "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
- "-o", "mech=DIGEST-MD5",
- "-o", "authid=dn:uid=test.user,o=test",
- "-o", "authzid=dn:uid=test.user,o=test",
- "-w", "wrongpassword",
- "-b", "",
- "-s", "base",
- "(objectClass=*)"
- };
+ String[] args = args("authid=dn:uid=test.user,o=test", "authzid=dn:uid=test.user,o=test", "wrongpassword");
assertFalse(LDAPSearch.run(nullPrintStream(), nullPrintStream(), args) == 0);
}
@@ -375,19 +326,7 @@
"cn: Test User",
"userPassword: password");
- String[] args =
- {
- "--noPropertiesFile",
- "-h", "127.0.0.1",
- "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
- "-o", "mech=DIGEST-MD5",
- "-o", "authid=u:test.user",
- "-o", "authzid=u:test.user",
- "-w", "password",
- "-b", "",
- "-s", "base",
- "(objectClass=*)"
- };
+ String[] args = args("authid=u:test.user", "authzid=u:test.user", "password");
assertFalse(LDAPSearch.run(nullPrintStream(), nullPrintStream(), args) == 0);
}
@@ -416,19 +355,7 @@
"cn: Test User",
"userPassword: password");
- String[] args =
- {
- "--noPropertiesFile",
- "-h", "127.0.0.1",
- "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
- "-o", "mech=DIGEST-MD5",
- "-o", "authid=dn:uid=test.user,o=test",
- "-o", "authzid=dn:uid=test.user,o=test",
- "-w", "password",
- "-b", "",
- "-s", "base",
- "(objectClass=*)"
- };
+ String[] args = args("authid=dn:uid=test.user,o=test", "authzid=dn:uid=test.user,o=test", "password");
assertFalse(LDAPSearch.run(nullPrintStream(), nullPrintStream(), args) == 0);
}
@@ -457,19 +384,7 @@
"cn: Test User",
"userPassword: password");
- String[] args =
- {
- "--noPropertiesFile",
- "-h", "127.0.0.1",
- "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
- "-o", "mech=DIGEST-MD5",
- "-o", "authid=dn:invaliddn",
- "-o", "authzid=dn:invaliddn",
- "-w", "password",
- "-b", "",
- "-s", "base",
- "(objectClass=*)"
- };
+ String[] args = args("authid=dn:invaliddn", "authzid=dn:invaliddn", "password");
assertFalse(LDAPSearch.run(nullPrintStream(), nullPrintStream(), args) == 0);
}
@@ -498,19 +413,7 @@
"cn: Test User",
"userPassword: password");
- String[] args =
- {
- "--noPropertiesFile",
- "-h", "127.0.0.1",
- "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
- "-o", "mech=DIGEST-MD5",
- "-o", "authid=u:doesntexist",
- "-o", "authzid=u:doesntexist",
- "-w", "password",
- "-b", "",
- "-s", "base",
- "(objectClass=*)"
- };
+ String[] args = args("authid=u:doesntexist", "authzid=u:doesntexist", "password");
assertFalse(LDAPSearch.run(nullPrintStream(), nullPrintStream(), args) == 0);
}
@@ -539,19 +442,7 @@
"cn: Test User",
"userPassword: password");
- String[] args =
- {
- "--noPropertiesFile",
- "-h", "127.0.0.1",
- "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
- "-o", "mech=DIGEST-MD5",
- "-o", "authid=dn:uid=doesntexist,o=test",
- "-o", "authzid=dn:uid=doesntexist,o=test",
- "-w", "password",
- "-b", "",
- "-s", "base",
- "(objectClass=*)"
- };
+ String[] args = args("authid=dn:uid=doesntexist,o=test", "authzid=dn:uid=doesntexist,o=test", "password");
assertFalse(LDAPSearch.run(nullPrintStream(), nullPrintStream(), args) == 0);
}
@@ -569,19 +460,7 @@
{
TestCaseUtils.initializeTestBackend(true);
- String[] args =
- {
- "--noPropertiesFile",
- "-h", "127.0.0.1",
- "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
- "-o", "mech=DIGEST-MD5",
- "-o", "authid=u:",
- "-o", "authzid=u:",
- "-w", "",
- "-b", "",
- "-s", "base",
- "(objectClass=*)"
- };
+ String[] args = args("authid=u:", "authzid=u:", "");
assertFalse(LDAPSearch.run(nullPrintStream(), nullPrintStream(), args) == 0);
}
@@ -599,19 +478,7 @@
{
TestCaseUtils.initializeTestBackend(true);
- String[] args =
- {
- "--noPropertiesFile",
- "-h", "127.0.0.1",
- "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
- "-o", "mech=DIGEST-MD5",
- "-o", "authid=dn:",
- "-o", "authzid=dn:",
- "-w", "",
- "-b", "",
- "-s", "base",
- "(objectClass=*)"
- };
+ String[] args = args("authid=dn:", "authzid=dn:", "");
assertFalse(LDAPSearch.run(nullPrintStream(), nullPrintStream(), args) == 0);
}
@@ -629,19 +496,7 @@
{
TestCaseUtils.initializeTestBackend(true);
- String[] args =
- {
- "--noPropertiesFile",
- "-h", "127.0.0.1",
- "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
- "-o", "mech=DIGEST-MD5",
- "-o", "authid=",
- "-o", "authzid=",
- "-w", "",
- "-b", "",
- "-s", "base",
- "(objectClass=*)"
- };
+ String[] args = args("authid=", "authzid=", "");
assertFalse(LDAPSearch.run(nullPrintStream(), nullPrintStream(), args) == 0);
}
@@ -673,19 +528,7 @@
"ds-pwp-password-policy-dn: cn=Clear UserPassword Policy," +
"cn=Password Policies,cn=config");
- String[] args =
- {
- "--noPropertiesFile",
- "-h", "127.0.0.1",
- "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
- "-o", "mech=DIGEST-MD5",
- "-o", "authid=dn:uid=test.user,o=test",
- "-o", "authzid=",
- "-w", "password",
- "-b", "",
- "-s", "base",
- "(objectClass=*)"
- };
+ String[] args = args("authid=dn:uid=test.user,o=test", "authzid=", "password");
assertFalse(LDAPSearch.run(nullPrintStream(), nullPrintStream(), args) == 0);
}
@@ -702,19 +545,7 @@
public void testLDAPBindFailIrreversiblePasswordWithRootDN()
throws Exception
{
- String[] args =
- {
- "--noPropertiesFile",
- "-h", "127.0.0.1",
- "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
- "-o", "mech=DIGEST-MD5",
- "-o", "authid=dn:cn=Directory Manager",
- "-o", "authzid=dn:cn=Directory Manager",
- "-w", "password",
- "-b", "",
- "-s", "base",
- "(objectClass=*)"
- };
+ String[] args = args("authid=dn:cn=Directory Manager", "authzid=dn:cn=Directory Manager", "password");
assertFalse(LDAPSearch.run(nullPrintStream(), nullPrintStream(), args) == 0);
}
@@ -746,19 +577,7 @@
"ds-pwp-password-policy-dn: cn=Clear UserPassword Policy," +
"cn=Password Policies,cn=config");
- String[] args =
- {
- "--noPropertiesFile",
- "-h", "127.0.0.1",
- "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
- "-o", "mech=DIGEST-MD5",
- "-o", "authid=dn:cn=Second Root DN",
- "-o", "authzid=dn:cn=Second Root DN",
- "-w", "password",
- "-b", "",
- "-s", "base",
- "(objectClass=*)"
- };
+ String[] args = args("authid=dn:cn=Second Root DN", "authzid=dn:cn=Second Root DN", "password");
assertEquals(LDAPSearch.run(nullPrintStream(), System.err, args), 0);
@@ -794,19 +613,7 @@
"ds-pwp-password-policy-dn: cn=Clear UserPassword Policy," +
"cn=Password Policies,cn=config");
- String[] args =
- {
- "--noPropertiesFile",
- "-h", "127.0.0.1",
- "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
- "-o", "mech=DIGEST-MD5",
- "-o", "authid=dn:uid=test.user,o=test",
- "-o", "authzid=dn:uid=nonexistent,o=test",
- "-w", "password",
- "-b", "",
- "-s", "base",
- "(objectClass=*)"
- };
+ String[] args = args("authid=dn:uid=test.user,o=test", "authzid=dn:uid=nonexistent,o=test", "password");
assertFalse(LDAPSearch.run(nullPrintStream(), nullPrintStream(), args) == 0);
}
@@ -838,19 +645,7 @@
"ds-pwp-password-policy-dn: cn=Clear UserPassword Policy," +
"cn=Password Policies,cn=config");
- String[] args =
- {
- "--noPropertiesFile",
- "-h", "127.0.0.1",
- "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
- "-o", "mech=DIGEST-MD5",
- "-o", "authid=dn:uid=test.user,o=test",
- "-o", "authzid=u:nonexistent",
- "-w", "password",
- "-b", "",
- "-s", "base",
- "(objectClass=*)"
- };
+ String[] args = args("authid=dn:uid=test.user,o=test", "authzid=u:nonexistent", "password");
assertFalse(LDAPSearch.run(nullPrintStream(), nullPrintStream(), args) == 0);
}
@@ -882,23 +677,25 @@
"ds-pwp-password-policy-dn: cn=Clear UserPassword Policy," +
"cn=Password Policies,cn=config");
- String[] args =
- {
- "--noPropertiesFile",
- "-h", "127.0.0.1",
- "-p", String.valueOf(TestCaseUtils.getServerLdapPort()),
- "-o", "mech=DIGEST-MD5",
- "-o", "authid=dn:uid=test.user,o=test",
- "-o", "authzid=dn:malformed",
- "-w", "password",
- "-b", "",
- "-s", "base",
- "(objectClass=*)"
- };
+ String[] args = args("authid=dn:uid=test.user,o=test", "authzid=dn:malformed", "password");
assertFalse(LDAPSearch.run(nullPrintStream(), nullPrintStream(), args) == 0);
}
-
+ private String[] args(String authid, String authzid, String password)
+ {
+ return new Args()
+ .add("--noPropertiesFile")
+ .add("-h", "127.0.0.1")
+ .add("-p", TestCaseUtils.getServerLdapPort())
+ .add("-o", "mech=DIGEST-MD5")
+ .add("-o", authid)
+ .add("-o", authzid)
+ .add("-w", password)
+ .add("-b", "")
+ .add("-s", "base")
+ .add("(objectClass=*)")
+ .toArray();
+ }
/**
* Verifies that the server will reject a DIGEST-MD5 bind in which the first
diff --git a/opendj-server-legacy/src/test/java/org/opends/server/tools/UpgradeTestCase.java b/opendj-server-legacy/src/test/java/org/opends/server/tools/UpgradeTestCase.java
index 8faafa8..ed63c67 100644
--- a/opendj-server-legacy/src/test/java/org/opends/server/tools/UpgradeTestCase.java
+++ b/opendj-server-legacy/src/test/java/org/opends/server/tools/UpgradeTestCase.java
@@ -18,20 +18,18 @@
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.PrintStream;
-import java.util.Collections;
-import java.util.LinkedList;
-import java.util.List;
import org.assertj.core.api.Assertions;
import org.forgerock.i18n.LocalizableMessage;
import org.opends.server.TestCaseUtils;
import org.opends.server.core.DirectoryServer;
import org.opends.server.tools.upgrade.UpgradeCli;
+import org.opends.server.util.Args;
import org.opends.server.util.StaticUtils;
import org.testng.annotations.Test;
-import static org.opends.messages.ToolMessages.*;
import static com.forgerock.opendj.cli.ArgumentConstants.*;
+import static org.opends.messages.ToolMessages.*;
import static org.testng.Assert.*;
/** A set of test cases for the Upgrade tool. */
@@ -56,14 +54,10 @@
*/
private String[] setArgs(String... args)
{
- final List<String> argsList = new LinkedList<>();
- argsList.add("--configFile");
- argsList.add(configFilePath);
- if (args != null)
- {
- Collections.addAll(argsList, args);
- }
- return argsList.toArray(new String[argsList.size()]);
+ return new Args()
+ .add("--configFile", configFilePath)
+ .addAll(args)
+ .toArray();
}
/**
diff --git a/opendj-server-legacy/src/test/java/org/opends/server/util/Args.java b/opendj-server-legacy/src/test/java/org/opends/server/util/Args.java
new file mode 100644
index 0000000..b1eb0d4
--- /dev/null
+++ b/opendj-server-legacy/src/test/java/org/opends/server/util/Args.java
@@ -0,0 +1,59 @@
+/*
+ * The contents of this file are subject to the terms of the Common Development and
+ * Distribution License (the License). You may not use this file except in compliance with the
+ * License.
+ *
+ * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
+ * specific language governing permission and limitations under the License.
+ *
+ * When distributing Covered Software, include this CDDL Header Notice in each file and include
+ * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
+ * Header, with the fields enclosed by brackets [] replaced by your own identifying
+ * information: "Portions Copyright [year] [name of copyright owner]".
+ *
+ * Copyright 2016 ForgeRock AS.
+ */
+package org.opends.server.util;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+/** Helper class to represent arguments for command-line programs. */
+public class Args
+{
+ private final List<String> args = new ArrayList<>();
+
+ public Args add(String arg)
+ {
+ args.add(arg);
+ return this;
+ }
+
+ public Args add(String arg, Object value)
+ {
+ args.add(arg);
+ args.add(value.toString());
+ return this;
+ }
+
+ public Args addAll(String... additionalArgs)
+ {
+ if (additionalArgs != null)
+ {
+ Collections.addAll(args, additionalArgs);
+ }
+ return this;
+ }
+
+ public String[] toArray()
+ {
+ return args.toArray(new String[0]);
+ }
+
+ @Override
+ public String toString()
+ {
+ return args.toString();
+ }
+}
\ No newline at end of file
--
Gitblit v1.10.0