|
31 | 31 | * @author Edoardo Luppi
|
32 | 32 | */
|
33 | 33 | public class P2RepositoryFtpHandlerTest extends AbstractTychoIntegrationTest {
|
34 |
| - private static final String TEST_BASEDIR = "/p2Repository.ftp"; |
35 |
| - |
36 |
| - private File repoDir; |
37 |
| - private String repositoryUrl; |
38 |
| - private FakeFtpServer ftpServer; |
39 |
| - |
40 |
| - @Before |
41 |
| - public void setup() throws Exception { |
42 |
| - repoDir = new File(getBasedir(TEST_BASEDIR, null), "repository"); |
43 |
| - startFtpServer(); |
44 |
| - } |
45 |
| - |
46 |
| - @Test |
47 |
| - public void testFtpRepository() throws Exception { |
48 |
| - final Verifier verifier = getVerifier(TEST_BASEDIR, false); |
49 |
| - verifier.getSystemProperties().setProperty("p2.ftp.repository", repositoryUrl); |
50 |
| - verifier.executeGoal("package"); |
51 |
| - verifier.verifyErrorFreeLog(); |
52 |
| - } |
53 |
| - |
54 |
| - @After |
55 |
| - public void tearDown() { |
56 |
| - ftpServer.stop(); |
57 |
| - } |
58 |
| - |
59 |
| - private void startFtpServer() throws IOException { |
60 |
| - ftpServer = new FakeFtpServer(); |
61 |
| - ftpServer.setFileSystem(getFileSystem()); |
62 |
| - |
63 |
| - final UserAccount userAccount = new UserAccount("anonymous", "", "/"); |
64 |
| - userAccount.setPasswordRequiredForLogin(false); |
65 |
| - |
66 |
| - ftpServer.addUserAccount(userAccount); |
67 |
| - |
68 |
| - ftpServer.setCommandHandler("FEAT", new StaticReplyCommandHandler(STAT_SYSTEM_OK, getFeatResponse())); |
69 |
| - ftpServer.setCommandHandler("MDTM", new MdtmCommandHandler()); |
70 |
| - ftpServer.setCommandHandler("SIZE", new SizeCommandHandler()); |
71 |
| - |
72 |
| - ftpServer.setServerControlPort(0); |
73 |
| - ftpServer.start(); |
74 |
| - |
75 |
| - // Address example: ftp://localhost:21/ |
76 |
| - repositoryUrl = "ftp://localhost:" + ftpServer.getServerControlPort() + "/"; |
77 |
| - } |
78 |
| - |
79 |
| - private FileSystem getFileSystem() throws IOException { |
80 |
| - final FileSystem fileSystem = new UnixFakeFileSystem(); |
81 |
| - fileSystem.add(new DirectoryEntry("/")); |
82 |
| - fileSystem.add(getFileEntry("/content.xml", new File(repoDir, "content.xml"))); |
83 |
| - fileSystem.add(getFileEntry("/artifacts.xml", new File(repoDir, "artifacts.xml"))); |
84 |
| - fileSystem.add(new DirectoryEntry("/plugins")); |
85 |
| - fileSystem.add(getFileEntry( |
86 |
| - "/plugins/tycho.ftp.bundle_1.0.0.202303021030.jar", |
87 |
| - new File(repoDir, "plugins/tycho.ftp.bundle_1.0.0.202303021030.jar") |
88 |
| - )); |
89 |
| - |
90 |
| - return fileSystem; |
91 |
| - } |
92 |
| - |
93 |
| - private FileEntry getFileEntry(final String path, final File file) throws IOException { |
94 |
| - final FileEntry entry = new FileEntry(path); |
95 |
| - entry.setContents(FileUtils.readFileToByteArray(file)); |
96 |
| - return entry; |
97 |
| - } |
98 |
| - |
99 |
| - private String getFeatResponse() { |
100 |
| - return "Extensions supported:\r\n" + |
101 |
| - " MDTM\r\n" + |
102 |
| - " RETR\r\n" + |
103 |
| - " SIZE\r\n" + |
104 |
| - " UTF8\r\n" + |
105 |
| - " EPSV\r\n" + |
106 |
| - " EPRT\r\n" + |
107 |
| - "END"; |
108 |
| - } |
109 |
| - |
110 |
| - private static class MdtmCommandHandler extends AbstractFakeCommandHandler { |
111 |
| - @Override |
112 |
| - protected void handle(final Command command, final Session session) { |
113 |
| - verifyLoggedIn(session); |
114 |
| - |
115 |
| - final String path = getRealPath(session, command.getRequiredParameter(0)); |
116 |
| - final FileSystemEntry entry = getFileSystem().getEntry(path); |
117 |
| - |
118 |
| - verifyFileSystemCondition(entry != null, path, "filesystem.doesNotExist"); |
119 |
| - verifyFileSystemCondition(!Objects.requireNonNull(entry).isDirectory(), path, "filesystem.isNotAFile"); |
120 |
| - |
121 |
| - final FileEntry fileEntry = (FileEntry) entry; |
122 |
| - final DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); |
123 |
| - final String lastModified = format.format(fileEntry.getLastModified()); |
124 |
| - session.sendReply(STAT_FILE_OK, lastModified); |
125 |
| - } |
126 |
| - } |
127 |
| - |
128 |
| - private static class SizeCommandHandler extends AbstractFakeCommandHandler { |
129 |
| - @Override |
130 |
| - protected void handle(final Command command, final Session session) { |
131 |
| - verifyLoggedIn(session); |
132 |
| - |
133 |
| - final String path = getRealPath(session, command.getRequiredParameter(0)); |
134 |
| - final FileSystemEntry entry = getFileSystem().getEntry(path); |
135 |
| - |
136 |
| - verifyFileSystemCondition(entry != null, path, "filesystem.doesNotExist"); |
137 |
| - verifyFileSystemCondition(!Objects.requireNonNull(entry).isDirectory(), path, "filesystem.isNotAFile"); |
138 |
| - |
139 |
| - final FileEntry fileEntry = (FileEntry) entry; |
140 |
| - session.sendReply(STAT_FILE_OK, String.valueOf(fileEntry.getSize())); |
141 |
| - } |
142 |
| - } |
| 34 | + private static final String TEST_BASEDIR = "/p2Repository.ftp"; |
| 35 | + |
| 36 | + private File repoDir; |
| 37 | + private String repositoryUrl; |
| 38 | + private FakeFtpServer ftpServer; |
| 39 | + |
| 40 | + @Before |
| 41 | + public void setup() throws Exception { |
| 42 | + repoDir = new File(getBasedir(TEST_BASEDIR), "repository"); |
| 43 | + startFtpServer(); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testFtpRepository() throws Exception { |
| 48 | + final Verifier verifier = getVerifier(TEST_BASEDIR, false); |
| 49 | + verifier.getSystemProperties().setProperty("p2.ftp.repository", repositoryUrl); |
| 50 | + verifier.executeGoal("package"); |
| 51 | + verifier.verifyErrorFreeLog(); |
| 52 | + } |
| 53 | + |
| 54 | + @After |
| 55 | + public void tearDown() { |
| 56 | + ftpServer.stop(); |
| 57 | + } |
| 58 | + |
| 59 | + private void startFtpServer() throws IOException { |
| 60 | + ftpServer = new FakeFtpServer(); |
| 61 | + ftpServer.setFileSystem(getFileSystem()); |
| 62 | + |
| 63 | + final UserAccount userAccount = new UserAccount("anonymous", "", "/"); |
| 64 | + userAccount.setPasswordRequiredForLogin(false); |
| 65 | + |
| 66 | + ftpServer.addUserAccount(userAccount); |
| 67 | + |
| 68 | + ftpServer.setCommandHandler("FEAT", new StaticReplyCommandHandler(STAT_SYSTEM_OK, getFeatResponse())); |
| 69 | + ftpServer.setCommandHandler("MDTM", new MdtmCommandHandler()); |
| 70 | + ftpServer.setCommandHandler("SIZE", new SizeCommandHandler()); |
| 71 | + |
| 72 | + ftpServer.setServerControlPort(0); |
| 73 | + ftpServer.start(); |
| 74 | + |
| 75 | + // Address example: ftp://localhost:21/ |
| 76 | + repositoryUrl = "ftp://localhost:" + ftpServer.getServerControlPort() + "/"; |
| 77 | + } |
| 78 | + |
| 79 | + private FileSystem getFileSystem() throws IOException { |
| 80 | + final FileSystem fileSystem = new UnixFakeFileSystem(); |
| 81 | + fileSystem.add(new DirectoryEntry("/")); |
| 82 | + fileSystem.add(getFileEntry("/content.xml", new File(repoDir, "content.xml"))); |
| 83 | + fileSystem.add(getFileEntry("/artifacts.xml", new File(repoDir, "artifacts.xml"))); |
| 84 | + fileSystem.add(new DirectoryEntry("/plugins")); |
| 85 | + fileSystem.add(getFileEntry("/plugins/tycho.ftp.bundle_1.0.0.202303021030.jar", |
| 86 | + new File(repoDir, "plugins/tycho.ftp.bundle_1.0.0.202303021030.jar"))); |
| 87 | + |
| 88 | + return fileSystem; |
| 89 | + } |
| 90 | + |
| 91 | + private FileEntry getFileEntry(final String path, final File file) throws IOException { |
| 92 | + final FileEntry entry = new FileEntry(path); |
| 93 | + entry.setContents(FileUtils.readFileToByteArray(file)); |
| 94 | + return entry; |
| 95 | + } |
| 96 | + |
| 97 | + private String getFeatResponse() { |
| 98 | + return "Extensions supported:\r\n" + " MDTM\r\n" + " RETR\r\n" + " SIZE\r\n" + " UTF8\r\n" + " EPSV\r\n" |
| 99 | + + " EPRT\r\n" + "END"; |
| 100 | + } |
| 101 | + |
| 102 | + private static class MdtmCommandHandler extends AbstractFakeCommandHandler { |
| 103 | + @Override |
| 104 | + protected void handle(final Command command, final Session session) { |
| 105 | + verifyLoggedIn(session); |
| 106 | + |
| 107 | + final String path = getRealPath(session, command.getRequiredParameter(0)); |
| 108 | + final FileSystemEntry entry = getFileSystem().getEntry(path); |
| 109 | + |
| 110 | + verifyFileSystemCondition(entry != null, path, "filesystem.doesNotExist"); |
| 111 | + verifyFileSystemCondition(!Objects.requireNonNull(entry).isDirectory(), path, "filesystem.isNotAFile"); |
| 112 | + |
| 113 | + final FileEntry fileEntry = (FileEntry) entry; |
| 114 | + final DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); |
| 115 | + final String lastModified = format.format(fileEntry.getLastModified()); |
| 116 | + session.sendReply(STAT_FILE_OK, lastModified); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private static class SizeCommandHandler extends AbstractFakeCommandHandler { |
| 121 | + @Override |
| 122 | + protected void handle(final Command command, final Session session) { |
| 123 | + verifyLoggedIn(session); |
| 124 | + |
| 125 | + final String path = getRealPath(session, command.getRequiredParameter(0)); |
| 126 | + final FileSystemEntry entry = getFileSystem().getEntry(path); |
| 127 | + |
| 128 | + verifyFileSystemCondition(entry != null, path, "filesystem.doesNotExist"); |
| 129 | + verifyFileSystemCondition(!Objects.requireNonNull(entry).isDirectory(), path, "filesystem.isNotAFile"); |
| 130 | + |
| 131 | + final FileEntry fileEntry = (FileEntry) entry; |
| 132 | + session.sendReply(STAT_FILE_OK, String.valueOf(fileEntry.getSize())); |
| 133 | + } |
| 134 | + } |
143 | 135 | }
|
0 commit comments