📄 modifiedselectortest.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */package org.apache.tools.ant.types.selectors;// Javaimport java.io.File;import java.text.RuleBasedCollator;import java.util.Comparator;import java.util.Iterator;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.Project;import org.apache.tools.ant.Target;import org.apache.tools.ant.Task;import org.apache.tools.ant.types.Parameter;import org.apache.tools.ant.types.Path;import org.apache.tools.ant.types.selectors.modifiedselector.Algorithm;import org.apache.tools.ant.types.selectors.modifiedselector.Cache;import org.apache.tools.ant.types.selectors.modifiedselector.ChecksumAlgorithm;import org.apache.tools.ant.types.selectors.modifiedselector.DigestAlgorithm;import org.apache.tools.ant.types.selectors.modifiedselector.EqualComparator;import org.apache.tools.ant.types.selectors.modifiedselector.HashvalueAlgorithm;import org.apache.tools.ant.types.selectors.modifiedselector.ModifiedSelector;import org.apache.tools.ant.types.selectors.modifiedselector.PropertiesfileCache;import org.apache.tools.ant.util.FileUtils;/** * Unit tests for ModifiedSelector. * * @since Ant 1.6 */public class ModifiedSelectorTest extends BaseSelectorTest { /** Utilities used for file operations */ private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); // ===================== attributes ===================== /** Package of the CacheSelector classes. */ private static String pkg = "org.apache.tools.ant.types.selectors.modifiedselector"; /** Path where the testclasses are. */ private Path testclasses = null; // ===================== constructors, factories ===================== public ModifiedSelectorTest(String name) { super(name); } /** * Factory method from base class. This should be overriden in child * classes to return a specific Selector class (like here). */ public BaseSelector getInstance() { return new ModifiedSelector(); } // ===================== JUnit stuff ===================== public void setUp() { // project reference is set in super.setUp() super.setUp(); // init the testclasses path object Project prj = getProject(); if (prj != null) { testclasses = new Path(prj, prj.getProperty("build.tests.value")); } } /* * / // for test only - ignore tests where we arent work at the moment public static junit.framework.Test suite() { junit.framework.TestSuite suite= new junit.framework.TestSuite(); suite.addTest(new ModifiedSelectorTest("testValidateWrongCache")); return suite; } /* */ // ======= testcases for the attributes and nested elements of the selector ===== /** Test right use of cache names. */ public void testValidateWrongCache() { String name = "this-is-not-a-valid-cache-name"; try { ModifiedSelector.CacheName cacheName = new ModifiedSelector.CacheName(); cacheName.setValue(name); fail("CacheSelector.CacheName accepted invalid value."); } catch (BuildException be) { assertEquals(name + " is not a legal value for this attribute", be.getMessage()); } } /** Test right use of cache names. */ public void testValidateWrongAlgorithm() { String name = "this-is-not-a-valid-algorithm-name"; try { ModifiedSelector.AlgorithmName algoName = new ModifiedSelector.AlgorithmName(); algoName.setValue(name); fail("CacheSelector.AlgorithmName accepted invalid value."); } catch (BuildException be) { assertEquals(name + " is not a legal value for this attribute", be.getMessage()); } } /** Test right use of comparator names. */ public void testValidateWrongComparator() { String name = "this-is-not-a-valid-comparator-name"; try { ModifiedSelector.ComparatorName compName = new ModifiedSelector.ComparatorName(); compName.setValue(name); fail("ModifiedSelector.ComparatorName accepted invalid value."); } catch (BuildException be) { assertEquals(name + " is not a legal value for this attribute", be.getMessage()); } } public void testIllegalCustomAlgorithm() { try { getAlgoName("java.lang.Object"); fail("Illegal classname used."); } catch (Exception e) { assertTrue("Wrong exception type: " + e.getClass().getName(), e instanceof BuildException); assertEquals("Wrong exception message.", "Specified class (java.lang.Object) is not an Algorithm.", e.getMessage()); } } public void testNonExistentCustomAlgorithm() { boolean noExcThrown = false; try { getAlgoName("non.existent.custom.Algorithm"); noExcThrown = true; } catch (Exception e) { if (noExcThrown) { fail("does 'non.existent.custom.Algorithm' really exist?"); } assertTrue("Wrong exception type: " + e.getClass().getName(), e instanceof BuildException); assertEquals("Wrong exception message.", "Specified class (non.existent.custom.Algorithm) not found.", e.getMessage()); } } public void testCustomAlgorithm() { String algo = getAlgoName("org.apache.tools.ant.types.selectors.modifiedselector.HashvalueAlgorithm"); assertTrue("Wrong algorithm used: "+algo, algo.startsWith("HashvalueAlgorithm")); } public void testCustomAlgorithm2() { String algo = getAlgoName("org.apache.tools.ant.types.selectors.MockAlgorithm"); assertTrue("Wrong algorithm used: "+algo, algo.startsWith("MockAlgorithm")); } public void testCustomClasses() { BFT bft = new BFT(); bft.setUp(); try { // do the actions bft.doTarget("modifiedselectortest-customClasses"); // do the checks - the buildfile stores the fileset as property String fsFullValue = bft.getProperty("fs.full.value"); String fsModValue = bft.getProperty("fs.mod.value"); assertNotNull("'fs.full.value' must be set.", fsFullValue); assertTrue("'fs.full.value' must not be null.", !"".equals(fsFullValue)); assertTrue("'fs.full.value' must contain ant.bat.", fsFullValue.indexOf("ant.bat")>-1); assertNotNull("'fs.mod.value' must be set.", fsModValue); // must be empty according to the Mock* implementations assertTrue("'fs.mod.value' must be empty.", "".equals(fsModValue)); // don't catch the JUnit exceptions } finally { bft.doTarget("modifiedselectortest-scenario-clean"); bft.deletePropertiesfile(); bft.tearDown(); } } public void testDelayUpdateTaskFinished() { doDelayUpdateTest(1); } public void testDelayUpdateTargetFinished() { doDelayUpdateTest(2); } public void testDelayUpdateBuildFinished() { doDelayUpdateTest(3); } public void doDelayUpdateTest(int kind) { // no check for 1<=kind<=3 - only internal use therefore check it // while development // readable form of parameter kind String[] kinds = {"task", "target", "build"}; // setup the "Ant project" MockProject project = new MockProject(); File base = new File("base"); File file1 = new File("file1"); File file2 = new File("file2"); // setup the selector ModifiedSelector sel = new ModifiedSelector(); sel.setProject(project); sel.setUpdate(true); sel.setDelayUpdate(true); // sorry - otherwise we will get a ClassCastException because the MockCache // is loaded by two different classloader ... sel.setClassLoader(this.getClass().getClassLoader()); sel.addClasspath(testclasses); sel.setAlgorithmClass("org.apache.tools.ant.types.selectors.MockAlgorithm"); sel.setCacheClass("org.apache.tools.ant.types.selectors.MockCache"); sel.configure(); // get the cache, so we can check our things MockCache cache = (MockCache)sel.getCache(); // the test assertFalse("Cache must not be saved before 1st selection.", cache.saved); sel.isSelected(base, "file1", file1); assertFalse("Cache must not be saved after 1st selection.", cache.saved); sel.isSelected(base, "file2", file2); assertFalse("Cache must not be saved after 2nd selection.", cache.saved); switch (kind) { case 1 : project.fireTaskFinished(); break; case 2 : project.fireTargetFinished(); break; case 3 : project.fireBuildFinished(); break; } assertTrue("Cache must be saved after " + kinds[kind-1] + "Finished-Event.", cache.saved); // MockCache doesnt create a file - therefore no cleanup needed } /** * Extracts the real used algorithm name from the ModifiedSelector using * its toString() method. * @param classname the classname from the algorithm to use * @return the algorithm part from the toString() (without brackets) */ private String getAlgoName(String classname) { ModifiedSelector sel = new ModifiedSelector(); // add the test classes to its classpath sel.addClasspath(testclasses); sel.setAlgorithmClass(classname); // let the selector do its checks sel.validate(); // extract the algorithm name (and config) from the selectors output String s1 = sel.toString(); int posStart = s1.indexOf("algorithm=") + 10; int posEnd = s1.indexOf(" comparator="); String algo = s1.substring(posStart, posEnd); // '<' and '>' are only used if the algorithm has properties if (algo.startsWith("<")) algo = algo.substring(1); if (algo.endsWith(">")) algo = algo.substring(0, algo.length()-1); // return the clean value return algo; } // ================ testcases for the cache implementations ================ /** * Propertycache must have a set 'cachefile' attribute. * The default in ModifiedSelector "cache.properties" is set by the selector. */ public void testPropcacheInvalid() { Cache cache = new PropertiesfileCache(); if (cache.isValid()) fail("PropertyfilesCache does not check its configuration."); } public void testPropertyfileCache() { PropertiesfileCache cache = new PropertiesfileCache(); File cachefile = new File("cache.properties"); cache.setCachefile(cachefile); doTest(cache); assertFalse("Cache file not deleted.", cachefile.exists()); } /** Checks whether a cache file is created. */ public void testCreatePropertiesCacheDirect() { File cachefile = new File(basedir, "cachefile.properties"); PropertiesfileCache cache = new PropertiesfileCache(); cache.setCachefile(cachefile);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -