package com.perforce.p4simulink.test; import com.mathworks.cmlink.api.ApplicationInteractor; import com.mathworks.cmlink.api.CMAdapter; import com.mathworks.cmlink.api.CMAdapterFactory; import com.mathworks.cmlink.api.ConfigurationManagementException; import com.mathworks.cmlink.sdk.tests.CMTestEnvironment; import com.mathworks.cmlink.util.interactor.NullApplicationInteractor; import com.mathworks.cmlink.util.system.SystemCommandException; import com.mathworks.cmlink.util.system.commandexecution.CommandBuilder; import com.mathworks.cmlink.util.system.commandexecution.StreamServedSystemCommandExecutor; import com.mathworks.cmlink.util.system.commandexecution.SystemCommandExecutor; import com.perforce.p4java.exception.AccessException; import com.perforce.p4java.server.IOptionsServer; import com.perforce.p4simulink.P4Adapter; import com.perforce.p4simulink.P4AdapterFactory; import com.perforce.p4simulink.P4Interactor; import com.perforce.p4simulink.connection.Config; import org.apache.commons.exec.CommandLine; import org.apache.commons.exec.DefaultExecuteResultHandler; import org.apache.commons.exec.DefaultExecutor; import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; public class P4TestEnvironment implements CMTestEnvironment { private P4AdapterFactory factory; private StreamServedSystemCommandExecutor executor; private File testDir; private Config config; private P4Interactor p4; private Path basePath; public P4TestEnvironment(File testDir, Config config) throws Exception { if (!testDir.exists()) { if (!testDir.mkdir()) { throw new Exception("Failed to create test directory for Perforce tests: " + testDir.getAbsolutePath()); } } this.testDir = testDir; this.config = config; factory = new P4AdapterFactory(); executor = new StreamServedSystemCommandExecutor(); basePath = Paths.get(""); setUp(); p4 = (P4Interactor) factory.getAdapterForThisSandboxDir(testDir, new NullApplicationInteractor(), config); p4.connect(); } /** * Create a repository in the specified repository. * * @param repositoryDir The directory in which to create a repository. * @return A repository specifier String. * @throws Exception */ public String createRepository(File repositoryDir) throws Exception { File parentDir = repositoryDir.getParentFile(); CommandBuilder command = new CommandBuilder() .add("p4admin") .add("create") .add(repositoryDir.toString()); try { executor.execute(command, parentDir); } catch (SystemCommandException e) { throw new RuntimeException( "Could not create a Perforce depot. Got the following error: " + e.getMessage() ); } return ""; } /** * Creates a sandbox for a specified repository location. * * @param repositorySpecifierString The repository for which the sandbox will be created. * @param sandboxDir The location of the sandbox to create. * @throws Exception */ public void createSandbox(String repositorySpecifierString, File sandboxDir) throws Exception { } /** * Gets the CMAdapterFactory for the instance being tested. * * @return The instance of CMAdapterFactory to be tested. */ public P4AdapterFactory getAdapterFactory() { return factory; } private void setUp() throws Exception { // get base dir and generate a suffix for this directory String baseDir = testDir.getAbsolutePath(); String suffix = "foo"; // required directories String serverRoot = baseDir + "/server-" + suffix; String clientRoot = baseDir + "/client-" + suffix; String[] directories = {baseDir, serverRoot, clientRoot, clientRoot + "/superuser", clientRoot + "/testuser"}; // clean up existing test directories (subdirectories of our testDir) FileUtils.deleteDirectory(testDir); // create required directories and open up permissions for (String directory : directories) { File file = new File(directory); if (!file.isDirectory()) { FileUtils.forceMkdir(file); FilesystemTools.chmodFile(file, true, true, true); } } // figure out which binary to use String systemName = System.getProperty("os.name").toLowerCase(); String p4Binary = null; if (systemName.contains("linux")) { p4Binary = basePath.toAbsolutePath().toString() + "/p4-bin/bin.linux26x86_64/p4d_r14.1"; } else if (systemName.contains("mac")) { p4Binary = basePath.toAbsolutePath().toString() + "/p4-bin/bin.darwin90x86_64/p4d_r14.1"; } else { throw new Exception("Unknown system: " + systemName + " Only linux and darwin (mac) supported."); } // spin up a server instance CommandLine cmdLine = new CommandLine(p4Binary); // cmdLine.addArgument("-vserver=5"); cmdLine.addArgument("-C1"); cmdLine.addArgument("-r"); cmdLine.addArgument(testDir.getAbsolutePath()); cmdLine.addArgument("-p"); cmdLine.addArgument(config.getPort()); cmdLine.addArgument("-Llog"); DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); DefaultExecutor executor = new DefaultExecutor(); executor.execute(cmdLine, resultHandler); System.out.println("start signal sent..."); Thread.sleep(100); } public void tearDown() throws ConfigurationManagementException, InterruptedException { System.out.println("Shutting down..."); Thread.sleep(200); // disconnect from the server if (p4 != null) { try { // stop server IOptionsServer server = p4.getServer(); System.out.println("SERVER: " + server); p4.getServer().execMapCmd("admin", new String[] {"stop"}, null); } catch (Exception e) { System.out.println("Could not stop server."); System.out.println(e.getMessage()); throw new ConfigurationManagementException(e); } } // ensure nothing is left holding file descriptions open System.gc(); try { FileUtils.deleteDirectory(testDir); } catch (IOException e) { throw new ConfigurationManagementException(e); } } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 11633 | Paul Allen |
Various fixes including workaround for no “admin” commands in p4java. Transferred from p4://perforce.perforce.com:1666@938159 |
||
#1 | 11632 | Paul Allen |
Added commons-exec library, server startup/shutdown for unit tests, fixed a bunch of NPEs. Transferred from p4://perforce.perforce.com:1666@937331 |