| | |
| | | import static org.forgerock.opendj.server.embedded.EmbeddedDirectoryServer.manageEmbeddedDirectoryServer; |
| | | |
| | | import static org.opends.server.loggers.TextAccessLogPublisher.getStartupTextAccessPublisher; |
| | | import static org.opends.server.loggers.TextErrorLogPublisher.getToolStartupTextErrorPublisher; |
| | | import static org.opends.server.loggers.TextErrorLogPublisher.*; |
| | | import static org.opends.server.loggers.TextHTTPAccessLogPublisher.getStartupTextHTTPAccessPublisher; |
| | | import static org.opends.server.types.NullOutputStream.nullPrintStream; |
| | | import static org.opends.server.util.ServerConstants.PROPERTY_RUNNING_UNIT_TESTS; |
| | |
| | | import java.nio.file.Paths; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.Collections; |
| | | import java.util.Date; |
| | | import java.util.Enumeration; |
| | |
| | | import org.opends.server.util.BuildVersion; |
| | | import org.opends.server.util.DynamicConstants; |
| | | import org.opends.server.util.LDIFReader; |
| | | import org.testng.Assert; |
| | | |
| | | import com.forgerock.opendj.util.OperatingSystem; |
| | | |
| | |
| | | private static int serverRestarts; |
| | | |
| | | /** The paths to directories and files used in the tests. */ |
| | | private static TestPaths paths = new TestPaths(); |
| | | public static TestPaths paths = new TestPaths(); |
| | | |
| | | /** The ports used in the tests. */ |
| | | private static TestPorts ports; |
| | |
| | | */ |
| | | public static void startFakeServer() throws Exception |
| | | { |
| | | DirectoryServer.bootstrapClient(); |
| | | schemaBeforeStartingFakeServer = DirectoryServer.getInstance().getServerContext().getSchema(); |
| | | DirectoryServer.getInstance().getServerContext().getSchemaHandler().updateSchema(Schema.getDefaultSchema()); |
| | | } |
| | | |
| | | static class TestPaths |
| | | public static class TestPaths |
| | | { |
| | | final String buildRoot; |
| | | final File buildDir; |
| | | final File unitRoot; |
| | | final String installedRoot; |
| | | final File testInstallRoot; |
| | | final File testInstanceRoot; |
| | | public final File testInstanceRoot; |
| | | final File testConfigDir; |
| | | final File configFile; |
| | | final File testSrcRoot; |
| | |
| | | buildRoot = System.getProperty(PROPERTY_BUILD_ROOT,System.getProperty("user.dir")); |
| | | String buildDirStr = System.getProperty(PROPERTY_BUILD_DIR, buildRoot + File.separator + "target"); |
| | | buildDir = new File(buildDirStr); |
| | | unitRoot = new File(buildDir, "unit-tests"); |
| | | unitRoot = new File(buildDir, "unit-tests"+((testName!=null)?"/"+testName:"")); |
| | | if (installedRoot == null) |
| | | { |
| | | testInstallRoot = new File(unitRoot, "package-install"); |
| | |
| | | private static void initializePortsAndServer() throws Exception |
| | | { |
| | | ports = new TestPorts(); |
| | | hostname = InetAddress.getLocalHost().getHostName(); |
| | | hostname = "127.0.0.1"; |
| | | server = manageEmbeddedDirectoryServer( |
| | | configParams() |
| | | .serverRootDirectory(paths.testInstallRoot.getPath()) |
| | |
| | | System.out, |
| | | System.err); |
| | | } |
| | | |
| | | public static void cleanTestPath() throws IOException { |
| | | deleteDirectory(paths.unitRoot); |
| | | } |
| | | |
| | | /** |
| | | * Setup the directory server in separate install root directory and instance root directory. |
| | | * After this method the directory server should be ready to be started. |
| | |
| | | String cleanupRequiredString = System.getProperty(PROPERTY_CLEANUP_REQUIRED, "true"); |
| | | boolean cleanupRequired = !"false".equalsIgnoreCase(cleanupRequiredString); |
| | | |
| | | //originalSystemErr.println("start "+paths.unitRoot); |
| | | if (cleanupRequired) { |
| | | deleteDirectory(paths.testInstallRoot); |
| | | deleteDirectory(paths.testInstanceRoot); |
| | |
| | | File unitClassesDir = new File(paths.unitRoot, "classes"); |
| | | File libDir = new File(paths.buildDir.getPath() + "/package/opendj/lib"); |
| | | File upgradeDir = new File(paths.buildDir.getPath() + "/package/opendj/template/config/upgrade"); |
| | | System.out.println("libDir=" + libDir); |
| | | File resourceDir = new File(paths.buildRoot, "resource"); |
| | | File testResourceDir = new File(paths.testSrcRoot, "resource"); |
| | | // Set the class variable |
| | |
| | | // Enable more verbose error logger. |
| | | ErrorLogger.getInstance().addLogPublisher( |
| | | (ErrorLogPublisher) getToolStartupTextErrorPublisher(ERROR_TEXT_WRITER)); |
| | | |
| | | ErrorLogger.getInstance().addLogPublisher( |
| | | (ErrorLogPublisher) getServerStartupTextErrorPublisher(ERROR_TEXT_WRITER)); |
| | | |
| | | DebugLogger.getInstance().addPublisherIfRequired(DEBUG_TEXT_WRITER); |
| | | } |
| | |
| | | private static ServerSocket bindPort(int port) |
| | | throws IOException |
| | | { |
| | | ServerSocket serverLdapSocket = new ServerSocket(); |
| | | ServerSocket serverLdapSocket; |
| | | |
| | | serverLdapSocket = new ServerSocket(); |
| | | serverLdapSocket.setReuseAddress(true); |
| | | serverLdapSocket.bind(new InetSocketAddress(port)); |
| | | serverLdapSocket.close(); |
| | | |
| | | serverLdapSocket = new ServerSocket(); |
| | | serverLdapSocket.setReuseAddress(true); |
| | | serverLdapSocket.bind(new InetSocketAddress("localhost",port)); |
| | | serverLdapSocket.close(); |
| | | |
| | | |
| | | serverLdapSocket = new ServerSocket(); |
| | | serverLdapSocket.setReuseAddress(true); |
| | | serverLdapSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(),port)); |
| | | serverLdapSocket.close(); |
| | | |
| | | serverLdapSocket = new ServerSocket(); |
| | | serverLdapSocket.setReuseAddress(true); |
| | | serverLdapSocket.bind(new InetSocketAddress("127.0.0.1",port)); |
| | | return serverLdapSocket; |
| | | } |
| | | |
| | | static int port = 30000; |
| | | /** |
| | | * Find and binds to a free server socket port on the local host. Avoid allocating ephemeral ports since these may |
| | | * be used by client applications such as dsconfig. Instead scan through ports starting from a reasonably high number |
| | |
| | | * |
| | | * @throws IOException in case of underlying exception. |
| | | */ |
| | | public static ServerSocket bindFreePort() throws IOException |
| | | public synchronized static ServerSocket bindFreePort() throws IOException |
| | | { |
| | | for (int port = 10000; port < 32768; port++) |
| | | { |
| | | try |
| | | { |
| | | return bindPort(port); |
| | | } |
| | | catch (BindException e) |
| | | { |
| | | // Try next port. |
| | | } |
| | | } |
| | | throw new BindException("Unable to bind to a free port"); |
| | | for (; port > 15000;) |
| | | { |
| | | try |
| | | { |
| | | return bindPort(port--); |
| | | } |
| | | catch (BindException e){} |
| | | } |
| | | throw new BindException("Unable to bind to a free port"); |
| | | } |
| | | |
| | | /** |
| | |
| | | final int[] ports = new int[nb]; |
| | | for (int i = 0; i < nb; i++) |
| | | { |
| | | final ServerSocket socket = bindFreePort(); |
| | | sockets[i] = socket; |
| | | ports[i] = socket.getLocalPort(); |
| | | sockets[i] = bindFreePort(); |
| | | ports[i] = sockets[i].getLocalPort(); |
| | | } |
| | | close(sockets); |
| | | return ports; |
| | | } |
| | | finally |
| | | { |
| | | close(sockets); |
| | | |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | public static void configureSocket(Socket s) throws Exception |
| | | { |
| | | s.setSoTimeout(60 * 1000); |
| | | s.setReuseAddress(true); |
| | | s.setSoTimeout(60 * 1000); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | public static void appendLogsContents(StringBuilder logsContents) |
| | | { |
| | | appendMessages(logsContents, TestCaseUtils.ACCESS_TEXT_WRITER, "Access Log Messages:"); |
| | | appendMessages(logsContents, TestCaseUtils.ERROR_TEXT_WRITER, "Error Log Messages:"); |
| | | appendMessages(logsContents, TestCaseUtils.DEBUG_TEXT_WRITER, "Debug Log Messages:"); |
| | | |
| | | appendMessages(logsContents, TestCaseUtils.ACCESS_TEXT_WRITER, "Access Log Messages:"); |
| | | |
| | | appendStreamContent(logsContents, TestCaseUtils.getSystemOutContents(), "System.out"); |
| | | appendStreamContent(logsContents, TestCaseUtils.getSystemErrContents(), "System.err"); |
| | | |
| | | for (final File logFile : Arrays.asList(new File(paths.testInstanceRoot, "logs").listFiles())) { |
| | | try { |
| | | appendStreamContent(logsContents, readFile(logFile.getPath()), logFile.getPath()); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | private static void appendStreamContent(StringBuilder out, String content, String name) |
| | |
| | | } |
| | | |
| | | public static synchronized void unsupressOutput() { |
| | | System.setOut(originalSystemOut); |
| | | System.setErr(originalSystemErr); |
| | | |
| | | for (Map.Entry<Logger, Handler> entry : disabledLogHandlers.entrySet()) |
| | | { |
| | | Logger l = entry.getKey(); |
| | | Handler h = entry.getValue(); |
| | | l.addHandler(h); |
| | | } |
| | | disabledLogHandlers.clear(); |
| | | String suppressStr = System.getProperty("org.opends.test.suppressOutput"); |
| | | if ("true".equalsIgnoreCase(suppressStr)) |
| | | { |
| | | System.setOut(originalSystemOut); |
| | | System.setErr(originalSystemErr); |
| | | |
| | | for (Map.Entry<Logger, Handler> entry : disabledLogHandlers.entrySet()) |
| | | { |
| | | Logger l = entry.getKey(); |
| | | Handler h = entry.getValue(); |
| | | l.addHandler(h); |
| | | } |
| | | disabledLogHandlers.clear(); |
| | | } |
| | | } |
| | | |
| | | /** Read the contents of a file and return it as a String. */ |
| | |
| | | System.setIn(stdin); |
| | | } |
| | | } |
| | | |
| | | static String testName=null; |
| | | public static void setTestName(String name) { |
| | | testName=name; |
| | | paths=new TestPaths(); |
| | | //originalSystemErr.println(paths.unitRoot); |
| | | } |
| | | } |