001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.types.selectors;
020:
021: import org.apache.tools.ant.BuildException;
022: import org.apache.tools.ant.Project;
023: import org.apache.tools.ant.types.Parameter;
024:
025: /**
026: * Tests Filename Selectors
027: *
028: */
029: public class FilenameSelectorTest extends BaseSelectorTest {
030:
031: private Project project;
032:
033: public FilenameSelectorTest(String name) {
034: super (name);
035: }
036:
037: /**
038: * Factory method from base class. This is overriden in child
039: * classes to return a specific Selector class.
040: */
041: public BaseSelector getInstance() {
042: return new FilenameSelector();
043: }
044:
045: /**
046: * Test the code that validates the selector.
047: */
048: public void testValidate() {
049: FilenameSelector s = (FilenameSelector) getInstance();
050: try {
051: s.isSelected(basedir, filenames[0], files[0]);
052: fail("FilenameSelector did not check for required fields");
053: } catch (BuildException be1) {
054: assertEquals("The name attribute is required", be1
055: .getMessage());
056: }
057:
058: s = (FilenameSelector) getInstance();
059: Parameter param = new Parameter();
060: param.setName("garbage in");
061: param.setValue("garbage out");
062: Parameter[] params = { param };
063: s.setParameters(params);
064: try {
065: s.isSelected(basedir, filenames[0], files[0]);
066: fail("FilenameSelector did not check for valid parameter element");
067: } catch (BuildException be2) {
068: assertEquals("Invalid parameter garbage in", be2
069: .getMessage());
070: }
071:
072: }
073:
074: /**
075: * Tests to make sure that the selector is selecting files correctly.
076: */
077: public void testSelectionBehaviour() {
078: FilenameSelector s;
079: String results;
080:
081: try {
082: makeBed();
083:
084: s = (FilenameSelector) getInstance();
085: s.setName("no match possible");
086: results = selectionString(s);
087: assertEquals("FFFFFFFFFFFF", results);
088:
089: s = (FilenameSelector) getInstance();
090: s.setName("*.gz");
091: results = selectionString(s);
092: // This is turned off temporarily. There appears to be a bug
093: // in SelectorUtils.matchPattern() where it is recursive on
094: // Windows even if no ** is in pattern.
095: //assertEquals("FFFTFFFFFFFF", results); // Unix
096: // vs
097: //assertEquals("FFFTFFFFTFFF", results); // Windows
098:
099: s = (FilenameSelector) getInstance();
100: s.setName("**/*.gz");
101: s.setNegate(true);
102: results = selectionString(s);
103: assertEquals("TTTFTTTFFTTT", results);
104:
105: s = (FilenameSelector) getInstance();
106: s.setName("**/*.GZ");
107: s.setCasesensitive(false);
108: results = selectionString(s);
109: assertEquals("FFFTFFFTTFFF", results);
110:
111: s = (FilenameSelector) getInstance();
112: Parameter param1 = new Parameter();
113: param1.setName("name");
114: param1.setValue("**/*.bz2");
115: Parameter[] params = { param1 };
116: s.setParameters(params);
117: results = selectionString(s);
118: assertEquals("FFTFFFFFFTTF", results);
119:
120: } finally {
121: cleanupBed();
122: }
123:
124: }
125:
126: }
|