📄 modifiedselectortest.java
字号:
* <li> try fist time --> should select all </li> * <li> try second time --> should select no files (only directories) </li> * <li> modify timestamp of one file and content of a nother one </li> * <li> try third time --> should select only the file with modified * content </li> */ public void testScenario1() { BFT bft = null; ModifiedSelector s = null; try { // // ***** initialize test environment (called "bed") ***** // makeBed(); String results = null; // Configure the selector - only defaults are used s = (ModifiedSelector)getSelector(); // // ***** First Run ***** // the first call should get all files, because nothing is in // the cache // performTests(s, "TTTTTTTTTTTT"); // // ***** Second Run ***** // the second call should get no files, because no content // has changed // performTests(s, "TFFFFFFFFFFT"); // // ***** make some files dirty ***** // // these files are made dirty --> 3+4 with different content String f2name = "tar/bz2/asf-logo-huge.tar.bz2"; String f3name = "asf-logo.gif.md5"; String f4name = "copy.filterset.filtered"; // AccessObject to the test-Ant-environment bft = new BFT(); // give some values (via property file) to that environment bft.writeProperties("f2name="+f2name); bft.writeProperties("f3name="+f3name); bft.writeProperties("f4name="+f4name); // call the target for making the files dirty bft.doTarget("modifiedselectortest-makeDirty"); // // ***** Third Run ***** // third call should get only those files, which CONTENT changed // (no timestamp changes required!) results = selectionString(s); // // ***** Check the result ***** // // Mark all files which should be selected as (T)rue and all others // as (F)alse. Directories are always selected so they always are // (T)rue. StringBuffer expected = new StringBuffer(); for (int i=0; i<filenames.length; i++) { String ch = "F"; if (files[i].isDirectory()) ch = "T"; // f2name shouldn't be selected: only timestamp has changed! if (filenames[i].equalsIgnoreCase(f3name)) ch = "T"; if (filenames[i].equalsIgnoreCase(f4name)) ch = "T"; expected.append(ch); } assertEquals( "Wrong files selected. Differing files: " // info text + resolve(diff(expected.toString(), results)), // list of files expected.toString(), // expected result results // result ); } finally { // cleanup the environment cleanupBed(); if (s!=null) s.getCache().delete(); if (bft!=null) bft.deletePropertiesfile(); } } /** * This scenario is based on scenario 1, but does not use any * default value and its based on <custom> selector. Used values are:<ul> * <li><b>Cache: </b> Propertyfile, * cachefile={java.io.tmpdir}/mycache.txt </li> * <li><b>Algorithm: </b> Digest * algorithm=SHA, Provider=null </li> * <li><b>Comparator: </b> java.text.RuleBasedCollator * <li><b>Update: </b> true </li> */ public void _testScenario2() { // RuleBasedCollator not yet supported - see Selector:375 note ExtendSelector s = new ExtendSelector(); BFT bft = new BFT(); String cachefile = System.getProperty("java.io.tmpdir")+"/mycache.txt"; try { makeBed(); s.setClassname("org.apache.tools.ant.types.selectors.modifiedselector.ModifiedSelector"); s.addParam(createParam("cache.cachefile", cachefile)); //s.addParam(createParam("algorithm.provider","---")); // i don't know any valid s.addParam(createParam("cache","propertyfile")); s.addParam(createParam("update","true")); s.addParam(createParam("comparator","rule")); s.addParam(createParam("algorithm.name","sha")); s.addParam(createParam("algorithm","digest")); // first and second run performTests(s, "TTTTTTTTTTTT"); performTests(s, "TFFFFFFFFFFT"); // make dirty String f2name = "tar/bz2/asf-logo-huge.tar.bz2"; String f3name = "asf-logo.gif.md5"; String f4name = "copy.filterset.filtered"; bft.writeProperties("f2name="+f2name); bft.writeProperties("f3name="+f3name); bft.writeProperties("f4name="+f4name); bft.doTarget("modifiedselectortest-makeDirty"); // third run String results = selectionString(s); StringBuffer expected = new StringBuffer(); for (int i=0; i<filenames.length; i++) { String ch = "F"; if (files[i].isDirectory()) ch = "T"; if (filenames[i].equalsIgnoreCase(f3name)) ch = "T"; if (filenames[i].equalsIgnoreCase(f4name)) ch = "T"; expected.append(ch); } assertEquals( "Wrong files selected. Differing files: " // info text + resolve(diff(expected.toString(), results)), // list of files expected.toString(), // expected result results // result ); } finally { // cleanup the environment cleanupBed(); (new java.io.File(cachefile)).delete(); if (bft!=null) bft.deletePropertiesfile(); } } public void testScenarioCoreSelectorDefaults() { doScenarioTest("modifiedselectortest-scenario-coreselector-defaults", "cache.properties"); } public void testScenarioCoreSelectorSettings() { doScenarioTest("modifiedselectortest-scenario-coreselector-settings", "core.cache.properties"); } public void testScenarioCustomSelectorSettings() { doScenarioTest("modifiedselectortest-scenario-customselector-settings", "core.cache.properties"); } public void doScenarioTest(String target, String cachefilename) { BFT bft = new BFT(); bft.setUp(); File cachefile = new File(basedir, cachefilename); try { // do the actions bft.doTarget("modifiedselectortest-scenario-clean"); bft.doTarget(target); // the directories to check File to1 = new File(basedir, "selectortest/to-1"); File to2 = new File(basedir, "selectortest/to-2"); File to3 = new File(basedir, "selectortest/to-3"); // do the checks assertTrue("Cache file not created.", cachefile.exists()); assertTrue("Not enough files copied on first time.", to1.list().length>5); assertTrue("Too much files copied on second time.", to2.list().length==0); assertTrue("Too much files copied on third time.", to3.list().length==2); // don't catch the JUnit exceptions } finally { bft.doTarget("modifiedselectortest-scenario-clean"); bft.deletePropertiesfile(); bft.tearDown(); cachefile.delete(); } } // ===================== helper methods and classes ==================== /** * Creates a configured parameter object. * @param name name of the parameter * @param value value of the parameter * @return the parameter object */ private Parameter createParam(String name, String value) { Parameter p = new Parameter(); p.setName(name); p.setValue(value); return p; } /** * The BFT class wrapps the selector test-builfile inside an * ant project (BuildFileTest). It supports target execution * and property transfer to that project. */ private class BFT extends org.apache.tools.ant.BuildFileTest { String buildfile = "src/etc/testcases/types/selectors.xml"; BFT() { super("nothing"); } BFT(String name) { super(name); } String propfile = "ModifiedSelectorTest.properties"; boolean isConfigured = false; public void setUp() { configureProject(buildfile); isConfigured = true; } /** * This stub teardown is here because the outer class needs to call the * tearDown method, and in the superclass it is protected. */ public void tearDown() { try { super.tearDown(); } catch (Exception e) { // ignore } } public void doTarget(String target) { if (!isConfigured) setUp(); executeTarget(target); } public String getProperty(String property) { return project.getProperty(property); } public void writeProperties(String line) { if (!isConfigured) setUp(); File dir = getProject().getBaseDir(); File file = new File(dir, propfile); try { java.io.FileWriter out = new java.io.FileWriter(file.getAbsolutePath(), true); out.write(line); out.write(System.getProperty("line.separator")); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } } public void deletePropertiesfile() { if (!isConfigured) setUp(); new File(getProject().getBaseDir(), propfile).delete(); } public void deleteCachefile() { File basedir = new File(buildfile).getParentFile(); File cacheFile = new File(basedir, "cache.properties"); cacheFile.delete(); } public String getBuildfile() { return buildfile; } public void setBuildfile(String buildfile) { this.buildfile = buildfile; } }//class-BFT /** * MockProject wrappes a very small ant project (one target, one task) * but provides public methods to fire the build events. */ private class MockProject extends Project { private Task task; private Target target; public MockProject() { task = new Task(){ public void execute() { } }; task.setTaskName("testTask"); target = new Target(); target.setName("testTarget"); target.setProject(this); target.addTask(task); task.setOwningTarget(target); } public void fireBuildFinished() { super.fireBuildFinished(null); } public void fireSubBuildFinished() { super.fireSubBuildFinished(null); } public void fireTargetStarted() { super.fireTargetStarted(target); } public void fireTargetFinished() { super.fireTargetFinished(target, null); } public void fireTaskStarted() { super.fireTaskStarted(task); } public void fireTaskFinished() { super.fireTaskFinished(task, null); } }//class-MockProject}//class-ModifiedSelectorTest
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -