package com.perforce.p4java.util; import java.io.OutputStream; import java.lang.reflect.Field; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.nio.file.attribute.FileAttributeView; import java.nio.file.attribute.PosixFileAttributeView; import java.nio.file.attribute.PosixFileAttributes; import java.nio.file.attribute.PosixFilePermission; import java.nio.file.attribute.PosixFilePermissions; import java.util.Date; import java.util.EnumSet; import java.util.List; import java.util.Set; import org.apache.commons.io.FileUtils; import org.apache.log4j.Logger; import org.junit.Assert; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import com.perforce.p4java.core.ChangelistStatus; import com.perforce.p4java.core.IChangelist; import com.perforce.p4java.core.file.FileSpecBuilder; import com.perforce.p4java.core.file.IFileSpec; import com.perforce.p4java.exception.AccessException; import com.perforce.p4java.exception.RequestException; import com.perforce.p4java.exceptions.P4ConnectionException; import com.perforce.p4java.impl.generic.client.ClientView; import com.perforce.p4java.impl.generic.core.Changelist; import com.perforce.p4java.impl.generic.core.User; import com.perforce.p4java.impl.mapbased.client.Client; import com.perforce.p4java.option.client.AddFilesOptions; import com.perforce.p4java.option.client.SyncOptions; import com.perforce.p4java.server.IOptionsServer; import com.perforce.p4java.server.IServer; /** * Test class used to test connection functionality * to perforce using p4java * @author tpethiyagoda * */ //@Ignore public class ConnectionTest { private static final Logger LOGGER = Logger.getLogger(ConnectionTest.class); /** * Constant field referring to the path where p4d server software is installed. */ private static final String PATH_TO_P4D = ConnectionTest.class.getResource("/").getPath(); private static final String PATH_TO_P4D_BIN = PATH_TO_P4D + "p4dbin"; private static final String PATH_TO_WORKSPACE = PATH_TO_P4D + "p4dbin/workspace"; @Rule public ExpectedException exception = ExpectedException.none(); /** * Starts new p4d prcess on a given port. * @param port Port of the p4d process * @param db Path to the db files of the p4d process * @return The reference to the p4d process itself * @throws Exception */ private Process startHelix(String p4dVersion,String port, String db) throws Exception { LOGGER.info("Starting Helix server for test"); String p4dPath = PATH_TO_P4D_BIN;// + p4dVersion; if(Files.notExists(Paths.get(p4dPath))) { //Files.createDirectories(Paths.get(p4dPath)); //FileUtils.copyDirectoryToDirectory(Paths.get(PATH_TO_P4D + "p4dbin").toFile(), Paths.get(p4dPath).toFile()); } if(Files.notExists(Paths.get(p4dPath + "/" + db))) { Files.createDirectories(Paths.get(p4dPath + "/" + db)); } if(Files.notExists(Paths.get(p4dPath + "/journal"+port))) { Files.createDirectories(Paths.get(p4dPath + "/journal"+port)); } String [] cmd = {p4dPath + "/p4d", "-p", port, "-r", p4dPath + "/" + db}; Process p = Runtime.getRuntime().exec(cmd); return p; } /** * Stops currently running Helix process. * @param p A reference to a currently running Helix process. * @throws Exception */ private void stopHelix(Process p) throws Exception { int pid = getPID(p); p.destroy(); p.waitFor(); //killHelix(p, pid); } /** * Destroys a running Helix process using kill -9 command. * @param p * @param processName * @throws Exception */ private void killHelix(Process p, int pid) throws Exception { Runtime.getRuntime().exec("kill -15 " + pid); } /** * Extracts the PID pf the currently running p4d process. * @param p * @throws Exception */ private int getPID(Process p) throws Exception { Field f = p.getClass().getDeclaredField("pid"); f.setAccessible(true); int pid = f.getInt(p); return pid; } /** * Test method to test obtaining a successful connection to perforce. * Starts the server first and performs the following tests: * .Connection is made * .Client host is as expected * Finally stops the server. */ //@Test() public void connectSuccessfully() throws Exception { Process p = null; try { p = startHelix("v1776","1776", "db1776"); Assert.assertEquals("Check that the server is started programatically", true, p.isAlive()); P4JavaConnection p4JavaConnection = new P4JavaConnection(); Assert.assertNotNull(p4JavaConnection); IServer server = p4JavaConnection.connect("p4java://localhost:1776"); Assert.assertNotNull(server); Assert.assertEquals(server.getServerInfo().getClientHost(), "TPs-iMac.das.perforce.com", server.getServerInfo().getClientHost()); server.disconnect(); } finally { if(p != null) { stopHelix(p); FileUtils.deleteDirectory(Paths.get(PATH_TO_P4D_BIN + "/db1776").toFile()); FileUtils.deleteDirectory(Paths.get(PATH_TO_P4D_BIN + "/journal1776").toFile()); } } } /** * Tests a connection failure specifically when Helix is down. */ //@Test(expected = P4ConnectionException.class) public void testFailedConnection() { P4JavaConnection p4JavaConnection = new P4JavaConnection(); Assert.assertNotNull(p4JavaConnection); IServer server = p4JavaConnection.connect("p4java://localhost:1667"); Assert.assertNotNull(server); Assert.assertEquals(false, server.isConnected()); } /** * This method tests logging on to Helix by a user */ //@Test() public void testLoginWhenUserDoesNotExist() throws Exception { Process p = null; IServer server = null; try { p = startHelix("v1779","1779", "db1779"); Assert.assertEquals("Check that the server is started programatically", true, p.isAlive()); P4JavaConnection p4JavaConnection = new P4JavaConnection(); Assert.assertNotNull(p4JavaConnection); server = p4JavaConnection.connect("p4java://localhost:1779"); System.out.println(server.getUserName()); Assert.assertNotNull(server); Assert.assertEquals(true, server.isConnected()); exception.expect(RequestException.class); //exception.expect(AccessException.class); server.login("perforce1234"); } finally { if(server != null && server.isConnected()) { server.disconnect(); } if(p != null) { stopHelix(p); FileUtils.deleteDirectory(Paths.get(PATH_TO_P4D_BIN + "/db1779").toFile()); FileUtils.deleteDirectory(Paths.get(PATH_TO_P4D_BIN + "/journal1779").toFile()); } } } /** * This method tests logging on to Helix by a user */ //@Test() public void testLoginWhenUserExists() throws Exception { Process p = null; IServer server = null; try { p = startHelix("v1778","1778", "db1778"); P4JavaConnection p4JavaConnection = new P4JavaConnection(); Assert.assertNotNull(p4JavaConnection); server = p4JavaConnection.connect("p4java://localhost:1778"); System.out.println(server.getUserName()); Assert.assertNotNull(server); Assert.assertEquals(true, server.isConnected()); Assert.assertNull(server.getUserName()); if(server.getUserName() == null) { User user = new User("tpeths", "aa@aa.com","TP", null, null, "perforce1234", null, null); server.createUser(user, true); } server.setUserName("tpeths"); server.login("perforce1234"); Assert.assertEquals("tpeths", server.getUserName()); Client tpws = Client.newClient((IOptionsServer) server, "tp-ws", "test-client-ws",PATH_TO_WORKSPACE, null); server.createClient(tpws); Assert.assertNotNull(server.getClient("tp-ws")); Assert.assertEquals("tp-ws", server.getClient("tp-ws").getName()); } finally { if(server != null && server.isConnected()) { server.disconnect(); } if(p != null) { stopHelix(p); FileUtils.deleteDirectory(Paths.get(PATH_TO_P4D_BIN + "/db1778").toFile()); FileUtils.deleteDirectory(Paths.get(PATH_TO_P4D_BIN + "/journal1778").toFile()); } } } @Test public void testInitialCheckIn() throws Exception { Process p = null; IServer server = null; try { p = startHelix("v1775","1775", "db1775"); P4JavaConnection p4JavaConnection = new P4JavaConnection(); Assert.assertNotNull(p4JavaConnection); server = p4JavaConnection.connect("p4java://localhost:1775"); System.out.println(server.getUserName()); Assert.assertNotNull(server); Assert.assertEquals(true, server.isConnected()); Assert.assertNull(server.getUserName()); if(server.getUserName() == null) { User user = new User("tpeths", "aa@aa.com","TP", null, null, "perforce1234", null, null); server.createUser(user, true); } server.setUserName("tpeths"); server.login("perforce1234"); Assert.assertEquals("tpeths", server.getUserName()); Client tpws = Client.newClient((IOptionsServer) server, "tp-ws", "test-client-ws",PATH_TO_WORKSPACE + "/tp-ws", null); //Set the client spec view ClientView mapping = new ClientView(); String viewLeftSide = "//depot/tp-ws/..."; String viewRightSide = "//tp-ws/..."; mapping.addEntry(new ClientView.ClientViewMapping(0,viewLeftSide, viewRightSide)); tpws.setClientView(mapping); server.createClient(tpws); Assert.assertNotNull(server.getClient("tp-ws")); Assert.assertEquals("tp-ws", server.getClient("tp-ws").getName()); //create file Path newFilePath = Paths.get(PATH_TO_WORKSPACE + "/tp-ws/Test.c"); Files.createDirectory(Paths.get(PATH_TO_WORKSPACE + "/tp-ws")); Files.createFile(newFilePath); OutputStream os = Files.newOutputStream(newFilePath); String fileContents = "This is a test"; os.write(fileContents.getBytes()); os.flush(); os.close(); server.setCurrentClient(tpws); Changelist changeListImpl = new Changelist(); changeListImpl.setId(IChangelist.UNKNOWN); changeListImpl.setClientId(tpws.getName()); changeListImpl.setDate(new Date()); changeListImpl.setServer(server); changeListImpl.setDescription("initial check in"); changeListImpl.setUsername(server.getUserName()); changeListImpl.setShelved(false); changeListImpl.setStatus(ChangelistStatus.NEW); IChangelist changelist = tpws.createChangelist(changeListImpl); AddFilesOptions options = new AddFilesOptions(); options.setChangelistId(changelist.getId()); List<IFileSpec> returnList = tpws.addFiles(FileSpecBuilder.makeFileSpecList(newFilePath.toString()), options); List<IFileSpec> submitList = changelist.submit(false); submitList.size(); //note that trailing dots is a must here. List<IFileSpec> filespecList = FileSpecBuilder.makeFileSpecList(PATH_TO_WORKSPACE + "/tp-ws/..."); SyncOptions s0 = new SyncOptions(true, true, false, false); List<IFileSpec> synced = tpws.sync(filespecList, s0); synced.size(); tpws.openedFiles(filespecList, null); List<IFileSpec> editfilespecList = tpws.editFiles(filespecList, null); tpws.openedFiles(filespecList, null); } finally { if(server != null && server.isConnected()) { server.disconnect(); } if(p != null) { stopHelix(p); FileUtils.deleteDirectory(Paths.get(PATH_TO_P4D_BIN + "/db1775").toFile()); FileUtils.deleteDirectory(Paths.get(PATH_TO_P4D_BIN + "/journal1775").toFile()); FileUtils.deleteDirectory(Paths.get(PATH_TO_WORKSPACE + "/tp-ws").toFile()); } } } }