📄 ftptest.java
字号:
* of times a message has been emitted. */ private class LogCounter extends DefaultLogger { private Map searchMap = new HashMap(); private int matchCount; public void addLogMessageToSearch(String message) { searchMap.put(message, new Integer(0)); } /* * @param event the build event that is being logged. */ public void messageLogged(BuildEvent event) { String message = event.getMessage(); Integer mcnt = (Integer) searchMap.get(message); if (null != mcnt) { searchMap.put(message, new Integer(mcnt.intValue() + 1)); } super.messageLogged(event); } /** * @return the number of times that the looked for message was sent * to the log */ public int getMatchCount(String message) { Integer mcnt = (Integer) searchMap.get(message); if (null != mcnt) { return mcnt.intValue(); } return 0; } } /** * Tests the combination of the newer parameter and the * serverTimezoneConfig parameter in the PUT action. The default * configuration is an ftp server on localhost which formats * timestamps as GMT. */ public void testTimezonePut() { CountLogListener log = new CountLogListener("(\\d+) files? sent"); getProject().executeTarget("timed.test.setup"); getProject().addBuildListener(log); getProject().executeTarget("timed.test.put.older"); assertEquals(1, log.getCount()); } /** * Tests the combination of the newer parameter and the * serverTimezoneConfig parameter in the GET action. The default * configuration is an ftp server on localhost which formats * timestamps as GMT. */ public void testTimezoneGet() { CountLogListener log = new CountLogListener("(\\d+) files? retrieved"); getProject().executeTarget("timed.test.setup"); getProject().addBuildListener(log); getProject().executeTarget("timed.test.get.older"); assertEquals(3, log.getCount()); } /** * Tests that the presence of one of the server config params forces * the system type to Unix if not specified. */ public void testConfiguration1() { int[] expectedCounts = { 1,1,0,1,0,0,0 }; performConfigTest("configuration.1", expectedCounts); } /** * Tests the systemTypeKey attribute. */ public void testConfiguration2() { int[] expectedCounts = { 1,0,0,1,1,0,0 }; performConfigTest("configuration.2", expectedCounts); } /** * Tests the systemTypeKey attribute with UNIX specified. */ public void testConfiguration3() { int[] expectedCounts = { 1,0,1,0,0,1,0 }; performConfigTest("configuration.3", expectedCounts); } public void testConfigurationLang() { int[] expectedCounts = { 1,1,0,0,0,0,1 }; performConfigTest("configuration.lang.good", expectedCounts); try { performConfigTest("configuration.lang.bad", expectedCounts); fail("BuildException Expected"); } catch (Exception bx) { assertTrue(bx instanceof BuildException); } } /** * Tests the systemTypeKey attribute. */ public void testConfigurationNone() { int[] expectedCounts = { 0,0,0,0,0,0,0 }; performConfigTest("configuration.none", expectedCounts); } private void performConfigTest(String target, int[] expectedCounts) { String[] messages = new String[]{ "custom configuration", "custom config: system key = default (UNIX)", "custom config: system key = UNIX", "custom config: server time zone ID = " + getProject().getProperty("ftp.server.timezone"), "custom config: system key = WINDOWS", "custom config: default date format = yyyy/MM/dd HH:mm", "custom config: server language code = de" }; LogCounter counter = new LogCounter(); for (int i=0; i < messages.length; i++) { counter.addLogMessageToSearch(messages[i]); } getProject().addBuildListener(counter); getProject().executeTarget(target); for (int i=0; i < messages.length; i++) { assertEquals("target "+target+":message "+ i, expectedCounts[i], counter.getMatchCount(messages[i])); } } /** * this test is inspired by a user reporting that deletions of directories with the ftp task do not work */ public void testFTPDelete() { getProject().executeTarget("ftp-delete"); } private void compareFiles(DirectoryScanner ds, String[] expectedFiles, String[] expectedDirectories) { String includedFiles[] = ds.getIncludedFiles(); String includedDirectories[] = ds.getIncludedDirectories(); assertEquals("file present: ", expectedFiles.length, includedFiles.length); assertEquals("directories present: ", expectedDirectories.length, includedDirectories.length); for (int counter=0; counter < includedFiles.length; counter++) { includedFiles[counter] = includedFiles[counter].replace(File.separatorChar, '/'); } Arrays.sort(includedFiles); for (int counter=0; counter < includedDirectories.length; counter++) { includedDirectories[counter] = includedDirectories[counter] .replace(File.separatorChar, '/'); } Arrays.sort(includedDirectories); for (int counter=0; counter < includedFiles.length; counter++) { assertEquals(expectedFiles[counter], includedFiles[counter]); } for (int counter=0; counter < includedDirectories.length; counter++) { assertEquals(expectedDirectories[counter], includedDirectories[counter]); counter++; } } private static class myFTP extends FTP { public FTP.FTPDirectoryScanner newScanner(FTPClient client) { return new FTP.FTPDirectoryScanner(client); } // provide public visibility public String resolveFile(String file) { return super.resolveFile(file); } } public abstract static class myRetryableFTP extends FTP { private final int numberOfFailuresToSimulate; private int simulatedFailuresLeft; protected myRetryableFTP(int numberOfFailuresToSimulate) { this.numberOfFailuresToSimulate = numberOfFailuresToSimulate; this.simulatedFailuresLeft = numberOfFailuresToSimulate; } protected void getFile(FTPClient ftp, String dir, String filename) throws IOException, BuildException { if (this.simulatedFailuresLeft > 0) { this.simulatedFailuresLeft--; throw new IOException("Simulated failure for testing"); } super.getFile(ftp, dir, filename); } protected void executeRetryable(RetryHandler h, Retryable r, String filename) throws IOException { this.simulatedFailuresLeft = this.numberOfFailuresToSimulate; super.executeRetryable(h, r, filename); } } public static class oneFailureFTP extends myRetryableFTP { public oneFailureFTP() { super(1); } } public static class twoFailureFTP extends myRetryableFTP { public twoFailureFTP() { super(2); } } public static class threeFailureFTP extends myRetryableFTP { public threeFailureFTP() { super(3); } } public static class randomFailureFTP extends myRetryableFTP { public randomFailureFTP() { super(new Random().nextInt(Short.MAX_VALUE)); } } public void testGetWithSelectorRetryable1() { getProject().addTaskDefinition("ftp", oneFailureFTP.class); try { getProject().executeTarget("ftp-get-with-selector-retryable"); } catch (BuildException bx) { fail("Two retries expected, failed after one."); } } public void testGetWithSelectorRetryable2() { getProject().addTaskDefinition("ftp", twoFailureFTP.class); try { getProject().executeTarget("ftp-get-with-selector-retryable"); } catch (BuildException bx) { fail("Two retries expected, failed after two."); } } public void testGetWithSelectorRetryable3() { getProject().addTaskDefinition("ftp", threeFailureFTP.class); try { getProject().executeTarget("ftp-get-with-selector-retryable"); fail("Two retries expected, continued after two."); } catch (BuildException bx) { } } public void testGetWithSelectorRetryableRandom() { getProject().addTaskDefinition("ftp", randomFailureFTP.class); try { getProject().setProperty("ftp.retries", "forever"); getProject().executeTarget("ftp-get-with-selector-retryable"); } catch (BuildException bx) { fail("Retry forever specified, but failed."); } } public void testInitialCommand() { performCommandTest("test-initial-command", new int[] { 1,0 }); } public void testSiteAction() { performCommandTest("test-site-action", new int[] { 1,0 }); } private void performCommandTest(String target, int[] expectedCounts) { String[] messages = new String[]{ "Doing Site Command: umask 222", "Failed to issue Site Command: umask 222", }; LogCounter counter = new LogCounter(); for (int i=0; i < messages.length; i++) { counter.addLogMessageToSearch(messages[i]); } getProject().addBuildListener(counter); getProject().executeTarget(target); for (int i=0; i < messages.length; i++) { assertEquals("target "+target+":message "+ i, expectedCounts[i], counter.getMatchCount(messages[i])); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -