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