TestUtilities.java revision 332075ed0cd6330412de06f07be130b68a7eb7a9
0N/A/*
0N/A * CDDL HEADER START
0N/A *
0N/A * The contents of this file are subject to the terms of the
2362N/A * Common Development and Distribution License, Version 1.0 only
0N/A * (the "License"). You may not use this file except in compliance
0N/A * with the License.
0N/A *
0N/A * You can obtain a copy of the license at
2362N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE
0N/A * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
2362N/A * See the License for the specific language governing permissions
0N/A * and limitations under the License.
0N/A *
0N/A * When distributing Covered Code, include this CDDL HEADER in each
0N/A * file and include the License file at
0N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
0N/A * add the following below this CDDL HEADER, with the fields enclosed
0N/A * by brackets "[]" replaced with your own identifying information:
0N/A * Portions Copyright [yyyy] [name of copyright owner]
0N/A *
0N/A * CDDL HEADER END
0N/A *
2365N/A *
2365N/A * Portions Copyright 2007 Sun Microsystems, Inc.
2365N/A */
0N/A
0N/Apackage org.opends.quicksetup;
0N/A
0N/Aimport org.opends.quicksetup.util.ZipExtractor;
0N/Aimport org.opends.quicksetup.util.ServerController;
0N/Aimport org.opends.quicksetup.util.FileManager;
0N/Aimport org.opends.server.TestCaseUtils;
0N/Aimport org.opends.server.types.OperatingSystem;
0N/A
0N/Aimport java.io.File;
0N/Aimport java.io.FileNotFoundException;
0N/Aimport java.io.IOException;
0N/Aimport java.net.ServerSocket;
0N/Aimport java.util.List;
0N/Aimport java.util.ArrayList;
0N/A
0N/A/**
0N/A *
0N/A */
0N/Apublic class TestUtilities {
0N/A
0N/A /**
0N/A * The name of the system property that specifies the server build root.
0N/A */
0N/A public static final String PROPERTY_BUILD_ROOT =
0N/A "org.opends.server.BuildRoot";
0N/A
0N/A public static final String DIRECTORY_MANAGER_PASSWORD = "password";
0N/A
0N/A public static Integer ldapPort;
0N/A
0N/A public static Integer jmxPort;
0N/A
0N/A private static boolean initialized;
0N/A
0N/A static public void initServer()
0N/A throws IOException, ApplicationException, InterruptedException {
0N/A File qsServerRoot = getQuickSetupTestServerRootDir();
0N/A if (!initialized) {
0N/A if (qsServerRoot.exists()) {
0N/A stopServer();
0N/A new FileManager().deleteRecursively(qsServerRoot);
0N/A }
0N/A ZipExtractor extractor = new ZipExtractor(getInstallPackageFile());
0N/A extractor.extract(qsServerRoot);
0N/A setupServer();
0N/A initialized = true;
0N/A }
0N/A }
0N/A
0N/A static public Installation getInstallation() {
0N/A return new Installation(getQuickSetupTestServerRootDir());
0N/A }
0N/A
0N/A static private void setupServer() throws IOException, InterruptedException {
0N/A ServerSocket ldapSocket = TestCaseUtils.bindFreePort();
0N/A ldapPort = ldapSocket.getLocalPort();
0N/A ldapSocket.close();
0N/A
0N/A ServerSocket jmxSocket = TestCaseUtils.bindFreePort();
0N/A jmxPort = jmxSocket.getLocalPort();
0N/A jmxSocket.close();
0N/A
0N/A List<String> args = new ArrayList<String>();
0N/A File root = getQuickSetupTestServerRootDir();
0N/A if (OperatingSystem.isUNIXBased(
0N/A OperatingSystem.forName(System.getProperty("os.name")))) {
0N/A args.add(new File(root, "setup").getPath());
0N/A } else {
0N/A args.add(new File(root, "setup.bat").getPath());
0N/A }
0N/A args.add("--cli");
0N/A args.add("-Q");
0N/A args.add("-p");
0N/A args.add(Integer.toString(ldapPort));
0N/A args.add("-x");
0N/A args.add(Integer.toString(jmxPort));
0N/A args.add("-w");
0N/A args.add(DIRECTORY_MANAGER_PASSWORD);
0N/A
0N/A ProcessBuilder pb = new ProcessBuilder(args);
0N/A Process p = pb.start();
0N/A if (p.waitFor() != 0) {
0N/A throw new IllegalStateException("setup server failed");
0N/A }
0N/A }
0N/A
0N/A static public void stopServer() throws ApplicationException {
0N/A ServerController controller = new ServerController(getInstallation());
0N/A controller.stopServer();
0N/A }
0N/A
0N/A static public File getInstallPackageFile() throws FileNotFoundException {
0N/A File installPackageFile = null;
0N/A String buildRoot = System.getProperty(PROPERTY_BUILD_ROOT);
0N/A File buildDir = new File(buildRoot, "build");
0N/A File packageDir = new File(buildDir, "package");
0N/A if (!packageDir.exists()) {
0N/A throw new FileNotFoundException("Package directory " + packageDir +
0N/A " does not exist");
0N/A }
0N/A String[] files = packageDir.list();
0N/A if (files != null) {
0N/A for (String fileName : files) {
0N/A if (fileName.endsWith(".zip")) {
0N/A installPackageFile = new File(packageDir, fileName);
0N/A break;
0N/A }
0N/A }
0N/A } else {
0N/A throw new FileNotFoundException("No files in " + packageDir);
0N/A }
0N/A return installPackageFile;
0N/A }
0N/A
0N/A static public File getQuickSetupTestWorkspace() {
0N/A String buildRoot = System.getProperty(PROPERTY_BUILD_ROOT);
0N/A File buildDir = new File(buildRoot, "build");
0N/A File unitRootDir = new File(buildDir, "unit-tests");
0N/A return new File(unitRootDir, "quicksetup");
0N/A }
0N/A
0N/A static public File getQuickSetupTestServerRootDir() {
0N/A return new File(getQuickSetupTestWorkspace(), "OpenDS");
0N/A }
0N/A}
0N/A