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.Mapper;
024:
025: import java.io.File;
026:
027: /**
028: * Tests Present Selectors
029: *
030: */
031: public class PresentSelectorTest extends BaseSelectorTest {
032:
033: private Project project;
034:
035: public PresentSelectorTest(String name) {
036: super (name);
037: }
038:
039: /**
040: * Factory method from base class. This is overriden in child
041: * classes to return a specific Selector class.
042: */
043: public BaseSelector getInstance() {
044: return new PresentSelector();
045: }
046:
047: /**
048: * Test the code that validates the selector.
049: */
050: public void testValidate() {
051: PresentSelector s = (PresentSelector) getInstance();
052: try {
053: s.createMapper();
054: s.createMapper();
055: fail("PresentSelector allowed more than one nested mapper.");
056: } catch (BuildException be1) {
057: assertEquals("Cannot define more than one mapper", be1
058: .getMessage());
059: }
060:
061: s = (PresentSelector) getInstance();
062: try {
063: s.isSelected(basedir, filenames[0], files[0]);
064: fail("PresentSelector did not check for required fields");
065: } catch (BuildException be2) {
066: assertEquals("The targetdir attribute is required.", be2
067: .getMessage());
068: }
069:
070: }
071:
072: /**
073: * Tests to make sure that the selector is selecting files correctly.
074: */
075: public void testSelectionBehaviour() {
076: PresentSelector s;
077: String results;
078: Mapper m;
079: Mapper.MapperType identity = new Mapper.MapperType();
080: identity.setValue("identity");
081: Mapper.MapperType glob = new Mapper.MapperType();
082: glob.setValue("glob");
083: Mapper.MapperType merge = new Mapper.MapperType();
084: merge.setValue("merge");
085: Mapper.MapperType flatten = new Mapper.MapperType();
086: flatten.setValue("flatten");
087:
088: try {
089: makeBed();
090:
091: s = (PresentSelector) getInstance();
092: s.setTargetdir(beddir);
093: results = selectionString(s);
094: assertEquals("TTTTTTTTTTTT", results);
095:
096: s = (PresentSelector) getInstance();
097: s.setTargetdir(beddir);
098: m = s.createMapper();
099: m.setType(identity);
100: results = selectionString(s);
101: assertEquals("TTTTTTTTTTTT", results);
102:
103: s = (PresentSelector) getInstance();
104: File subdir = new File(System.getProperty("root"),
105: "src/etc/testcases/taskdefs/expected");
106: s.setTargetdir(subdir);
107: m = s.createMapper();
108: m.setType(flatten);
109: results = selectionString(s);
110: assertEquals("TTTTTTTTTTTF", results);
111:
112: s = (PresentSelector) getInstance();
113: s.setTargetdir(beddir);
114: m = s.createMapper();
115: m.setType(merge);
116: m.setTo("asf-logo.gif.gz");
117: results = selectionString(s);
118: assertEquals("TTTTTTTTTTTT", results);
119:
120: s = (PresentSelector) getInstance();
121: subdir = new File(beddir, "tar/bz2");
122: s.setTargetdir(subdir);
123: m = s.createMapper();
124: m.setType(glob);
125: m.setFrom("*.bz2");
126: m.setTo("*.tar.bz2");
127: results = selectionString(s);
128: assertEquals("FFTFFFFFFFFF", results);
129:
130: try {
131: makeMirror();
132:
133: s = (PresentSelector) getInstance();
134: subdir = getProject().resolveFile("selectortest2");
135: s.setTargetdir(subdir);
136: results = mirrorSelectionString(s);
137: assertEquals("TTTFFTTTTTTT", results);
138: results = selectionString(s);
139: assertEquals("TTTFFTTTTTTT", results);
140:
141: } finally {
142: cleanupMirror();
143: }
144:
145: } finally {
146: cleanupBed();
147: }
148:
149: }
150:
151: }
|