| | |
| | | |
| | | import com.forgerock.opendj.util.OperatingSystem; |
| | | |
| | | import static org.mockito.Mockito.*; |
| | | import static org.opends.server.loggers.TextAccessLogPublisher.*; |
| | | import static org.opends.server.loggers.TextErrorLogPublisher.*; |
| | | import static org.opends.server.loggers.TextHTTPAccessLogPublisher.*; |
| | |
| | | import static org.opends.server.util.StaticUtils.*; |
| | | import static org.testng.Assert.*; |
| | | |
| | | /** |
| | | * This class defines some utility functions which can be used by test cases. |
| | | */ |
| | | /** This class defines some utility functions which can be used by test cases. */ |
| | | @SuppressWarnings("javadoc") |
| | | public final class TestCaseUtils { |
| | | /** The name of the system property that specifies the server build root. */ |
| | |
| | | copyDirectory(new File(installedRoot), testInstallRoot); |
| | | |
| | | // Get the instance location |
| | | |
| | | } |
| | | else |
| | | { |
| | |
| | | waitForOpsToComplete(); |
| | | } |
| | | |
| | | /** |
| | | * This can be made public if quiesceServer becomes too heavy-weight in |
| | | * some circumstance. |
| | | */ |
| | | /** This can be made public if quiesceServer becomes too heavy-weight in some circumstance. */ |
| | | private static void waitForOpsToComplete() |
| | | { |
| | | try { |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Binds to the given socket port on the local host. |
| | | * @return the bounded Server socket. |
| | |
| | | DirectoryServer.setSchema(schemaBeforeStartingFakeServer); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Shut down the server. This should only be called at the end of the test |
| | | * suite and not by any unit tests. |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Clears a memory-based backend. |
| | | */ |
| | | /** Clears a memory-based backend. */ |
| | | public static void clearMemoryBackend(String backendID) throws Exception |
| | | { |
| | | MemoryBackend memoryBackend = (MemoryBackend) DirectoryServer.getBackend(backendID); |
| | |
| | | if (clearBackend(b) && baseDN != null) |
| | | { |
| | | Entry e = createEntry(DN.valueOf(baseDN)); |
| | | DirectoryServer.getBackend(backendId).addEntry(e, null); |
| | | DirectoryServer.getBackend(backendId).addEntry(e, mock(AddOperation.class)); |
| | | } |
| | | } |
| | | |
| | |
| | | { |
| | | if (b instanceof BackendImpl) |
| | | { |
| | | final BackendImpl backend = (BackendImpl) b; |
| | | final BackendImpl<?> backend = (BackendImpl<?>) b; |
| | | final RootContainer rootContainer = backend.getRootContainer(); |
| | | if (rootContainer != null) |
| | | { |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Get the LDAP port the test environment Directory Server instance is |
| | | * running on. |
| | |
| | | // No implementation. |
| | | } |
| | | |
| | | |
| | | //////////////////////////////////////////////////////////////////////////// |
| | | //////////////////////////////////////////////////////////////////////////// |
| | | // Various methods for converting LDIF Strings to Entries |
| | | //////////////////////////////////////////////////////////////////////////// |
| | | //////////////////////////////////////////////////////////////////////////// |
| | | |
| | | |
| | | /** |
| | | * Returns a modifiable List of entries parsed from the provided LDIF. |
| | | * It's best to call this after the server has been initialized so |
| | |
| | | return entriesFromLdifString(makeLdif(lines)); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Adds the provided entry to the Directory Server using an internal |
| | | * operation. |
| | |
| | | assertEquals(addOperation.getResultCode(), ResultCode.SUCCESS); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Deletes the provided entry from the Directory Server using an |
| | | * internal operation. |
| | |
| | | assertEquals(deleteOperation.getResultCode(), ResultCode.SUCCESS); |
| | | } |
| | | |
| | | |
| | | |
| | | public static boolean canBind(String dn, String pw) throws Exception |
| | | { |
| | | // Check that the user can bind. |
| | | try (Socket s = new Socket("127.0.0.1", TestCaseUtils.getServerLdapPort());) |
| | | try (Socket s = new Socket("127.0.0.1", TestCaseUtils.getServerLdapPort())) |
| | | { |
| | | TestCaseUtils.configureSocket(s); |
| | | ASN1Reader r = ASN1.getReader(s.getInputStream()); |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Configures a socket for use in unit tests. This should only be used if the |
| | | * socket is not expected to timeout. |
| | |
| | | s.setSoTimeout(60 * 1000); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Adds the provided entry to the Directory Server using an internal |
| | | * operation. |
| | |
| | | return e; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Adds the provided entry to the Directory Server using an internal |
| | | * operation. |
| | |
| | | return addOperation.getResultCode(); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Adds the provided set of entries to the Directory Server using internal |
| | | * operations. |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Adds the provided set of entries to the Directory Server using internal |
| | | * operations. |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Applies a set of modifications to the server as described in the provided |
| | | * set of lines (using LDIF change form). The changes will be applied over |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Return a Map constructed via alternating key and value pairs. |
| | | */ |
| | | /** Return a Map constructed via alternating key and value pairs. */ |
| | | public static Map<String, String> makeMap(String... keyValuePairs) |
| | | { |
| | | Map<String, String> map = new LinkedHashMap<>(); |
| | |
| | | * must write something to System.out. */ |
| | | public static final PrintStream originalSystemOut = System.out; |
| | | |
| | | /** System.err is redirected to here so that we can only print it out |
| | | * if a test fails. */ |
| | | /** System.err is redirected to here so that we can only print it out if a test fails. */ |
| | | private static final ByteArrayOutputStream redirectedSystemErr = new ByteArrayOutputStream(); |
| | | |
| | | /** System.out is redirected to here so that we can only print it out |
| | | * if a test fails. */ |
| | | /** System.out is redirected to here so that we can only print it out if a test fails. */ |
| | | private static final ByteArrayOutputStream redirectedSystemOut = new ByteArrayOutputStream(); |
| | | |
| | | public static synchronized void suppressOutput() { |
| | |
| | | redirectedSystemErr.reset(); |
| | | } |
| | | |
| | | /** |
| | | * Clear everything written to the Access, Error, or Debug loggers. |
| | | */ |
| | | /** Clear everything written to the Access, Error, or Debug loggers. */ |
| | | public static synchronized void clearLoggersContents() { |
| | | ACCESS_TEXT_WRITER.clear(); |
| | | ERROR_TEXT_WRITER.clear(); |
| | |
| | | return lines; |
| | | } |
| | | |
| | | |
| | | /** Read the contents of a file and return it as a String. */ |
| | | private static byte[] readFileBytes(File file) throws IOException { |
| | | FileInputStream fis = new FileInputStream(file); |
| | |
| | | return bytes; |
| | | } |
| | | |
| | | |
| | | /** Store the contents of a String in a file. */ |
| | | public static void writeFile(File file, String contents) throws IOException { |
| | | writeFile(file.getAbsolutePath(), contents); |
| | |
| | | assertEquals(DSConfig.main(fullArgs, System.out, System.err), 0); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Gets the root configuration associated with the active server |
| | | * instance. This root configuration can then be used to access and |
| | |
| | | return context.getRootConfiguration(); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Return a String representation of all of the current threads. |
| | | * @return a dump of all Threads on the server |
| | |
| | | { |
| | | Map<Thread,StackTraceElement[]> threadStacks = Thread.getAllStackTraces(); |
| | | |
| | | |
| | | // Re-arrange all of the elements by thread ID so that there is some logical order. |
| | | Map<Long, Map.Entry<Thread, StackTraceElement[]>> orderedStacks = new TreeMap<>(); |
| | | for (Map.Entry<Thread,StackTraceElement[]> e : threadStacks.entrySet()) |
| | |
| | | /** FIXME Replace with {@link Assert#assertNotEquals(Object, Object, String)} once we upgrade to testng >= 6.1. */ |
| | | public static void assertNotEquals(Object actual1, Object actual2, String message) |
| | | { |
| | | boolean fail = false; |
| | | try |
| | | { |
| | | Assert.assertEquals(actual1, actual2); |
| | | fail = true; |
| | | Assert.fail(message); |
| | | } |
| | | catch (AssertionError e) |
| | | { |
| | | } |
| | | if (fail) |
| | | { |
| | | Assert.fail(message); |
| | | // this is good: they are not equals |
| | | return; |
| | | } |
| | | } |
| | | } |