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 Depend Selectors
029: *
030: */
031: public class DependSelectorTest extends BaseSelectorTest {
032:
033: private Project project;
034:
035: public DependSelectorTest(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 DependSelector();
045: }
046:
047: /**
048: * Test the code that validates the selector.
049: */
050: public void testValidate() {
051: DependSelector s = (DependSelector) getInstance();
052: try {
053: s.createMapper();
054: s.createMapper();
055: fail("DependSelector 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 = (DependSelector) getInstance();
062: try {
063: s.isSelected(basedir, filenames[0], files[0]);
064: fail("DependSelector 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: DependSelector s;
077: String results;
078: File subdir;
079: Mapper m;
080: Mapper.MapperType identity = new Mapper.MapperType();
081: identity.setValue("identity");
082: Mapper.MapperType glob = new Mapper.MapperType();
083: glob.setValue("glob");
084: Mapper.MapperType merge = new Mapper.MapperType();
085: merge.setValue("merge");
086:
087: try {
088: makeBed();
089:
090: s = (DependSelector) getInstance();
091: s.setTargetdir(beddir);
092: results = selectionString(s);
093: assertEquals("FFFFFFFFFFFF", results);
094:
095: s = (DependSelector) getInstance();
096: s.setTargetdir(beddir);
097: m = s.createMapper();
098: m.setType(identity);
099: results = selectionString(s);
100: assertEquals("FFFFFFFFFFFF", results);
101:
102: s = (DependSelector) getInstance();
103: s.setTargetdir(beddir);
104: m = s.createMapper();
105: m.setType(merge);
106: m.setTo("asf-logo.gif.gz");
107: results = selectionString(s);
108: assertEquals("TFFFFTTTFFF", results.substring(0, 11));
109:
110: s = (DependSelector) getInstance();
111: s.setTargetdir(beddir);
112: m = s.createMapper();
113: m.setType(merge);
114: m.setTo("asf-logo.gif.bz2");
115: results = selectionString(s);
116: assertEquals("TTFTTTTTTTTT", results);
117:
118: // Test for path relative to project base directory
119: s = (DependSelector) getInstance();
120: subdir = new File("selectortest/tar/bz2");
121: s.setTargetdir(subdir);
122: m = s.createMapper();
123: m.setType(glob);
124: m.setFrom("*.bz2");
125: m.setTo("*.tar.bz2");
126: results = selectionString(s);
127: assertEquals("FFTFFFFFFTTF", results);
128:
129: s = (DependSelector) getInstance();
130: subdir = new File(beddir, "tar/bz2");
131: s.setTargetdir(subdir);
132: m = s.createMapper();
133: m.setType(glob);
134: m.setFrom("*.bz2");
135: m.setTo("*.tar.bz2");
136: results = selectionString(s);
137: assertEquals("FFFFFFFFFTTF", results);
138:
139: try {
140: makeMirror();
141:
142: s = (DependSelector) getInstance();
143: File testdir = getProject()
144: .resolveFile("selectortest2");
145: s.setTargetdir(testdir);
146: results = selectionString(s);
147: assertEquals("FFFTTFFFFFFF", results);
148:
149: s = (DependSelector) getInstance();
150: testdir = getProject().resolveFile(
151: "selectortest2/tar/bz2");
152: s.setTargetdir(testdir);
153: m = s.createMapper();
154: m.setType(glob);
155: m.setFrom("*.bz2");
156: m.setTo("*.tar.bz2");
157: results = mirrorSelectionString(s);
158: assertEquals("FFFFFFFFFTTF", results);
159: results = selectionString(s);
160: assertEquals("FFFFFFFFFTTF", results);
161: } finally {
162: cleanupMirror();
163: }
164:
165: } finally {
166: cleanupBed();
167: }
168:
169: }
170:
171: }
|