1 | /*
|
---|
2 | * Copyright 2000-2002,2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant.types.selectors;
|
---|
19 |
|
---|
20 | import org.apache.tools.ant.BuildException;
|
---|
21 | import org.apache.tools.ant.Project;
|
---|
22 | import org.apache.tools.ant.util.*;
|
---|
23 | import org.apache.tools.ant.BuildFileTest;
|
---|
24 | import org.apache.tools.ant.types.Mapper;
|
---|
25 | import org.apache.tools.ant.util.FileNameMapper;
|
---|
26 | import org.apache.tools.ant.util.IdentityMapper;
|
---|
27 | import org.apache.tools.ant.util.GlobPatternMapper;
|
---|
28 |
|
---|
29 | import java.io.File;
|
---|
30 |
|
---|
31 | import junit.framework.TestCase;
|
---|
32 | import junit.framework.AssertionFailedError;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Tests Present Selectors
|
---|
36 | *
|
---|
37 | */
|
---|
38 | public class PresentSelectorTest extends BaseSelectorTest {
|
---|
39 |
|
---|
40 | private Project project;
|
---|
41 |
|
---|
42 | public PresentSelectorTest(String name) {
|
---|
43 | super(name);
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Factory method from base class. This is overriden in child
|
---|
48 | * classes to return a specific Selector class.
|
---|
49 | */
|
---|
50 | public BaseSelector getInstance() {
|
---|
51 | return new PresentSelector();
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Test the code that validates the selector.
|
---|
56 | */
|
---|
57 | public void testValidate() {
|
---|
58 | PresentSelector s = (PresentSelector)getInstance();
|
---|
59 | try {
|
---|
60 | s.createMapper();
|
---|
61 | s.createMapper();
|
---|
62 | fail("PresentSelector allowed more than one nested mapper.");
|
---|
63 | } catch (BuildException be1) {
|
---|
64 | assertEquals("Cannot define more than one mapper",
|
---|
65 | be1.getMessage());
|
---|
66 | }
|
---|
67 |
|
---|
68 | s = (PresentSelector)getInstance();
|
---|
69 | try {
|
---|
70 | s.isSelected(basedir,filenames[0],files[0]);
|
---|
71 | fail("PresentSelector did not check for required fields");
|
---|
72 | } catch (BuildException be2) {
|
---|
73 | assertEquals("The targetdir attribute is required.",
|
---|
74 | be2.getMessage());
|
---|
75 | }
|
---|
76 |
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Tests to make sure that the selector is selecting files correctly.
|
---|
81 | */
|
---|
82 | public void testSelectionBehaviour() {
|
---|
83 | PresentSelector s;
|
---|
84 | String results;
|
---|
85 | Mapper m;
|
---|
86 | Mapper.MapperType identity = new Mapper.MapperType();
|
---|
87 | identity.setValue("identity");
|
---|
88 | Mapper.MapperType glob = new Mapper.MapperType();
|
---|
89 | glob.setValue("glob");
|
---|
90 | Mapper.MapperType merge = new Mapper.MapperType();
|
---|
91 | merge.setValue("merge");
|
---|
92 | Mapper.MapperType flatten = new Mapper.MapperType();
|
---|
93 | flatten.setValue("flatten");
|
---|
94 |
|
---|
95 | try {
|
---|
96 | makeBed();
|
---|
97 |
|
---|
98 | s = (PresentSelector)getInstance();
|
---|
99 | s.setTargetdir(beddir);
|
---|
100 | results = selectionString(s);
|
---|
101 | assertEquals("TTTTTTTTTTTT", results);
|
---|
102 |
|
---|
103 | s = (PresentSelector)getInstance();
|
---|
104 | s.setTargetdir(beddir);
|
---|
105 | m = s.createMapper();
|
---|
106 | m.setType(identity);
|
---|
107 | results = selectionString(s);
|
---|
108 | assertEquals("TTTTTTTTTTTT", results);
|
---|
109 |
|
---|
110 | s = (PresentSelector)getInstance();
|
---|
111 | File subdir = new File("src/etc/testcases/taskdefs/expected");
|
---|
112 | s.setTargetdir(subdir);
|
---|
113 | m = s.createMapper();
|
---|
114 | m.setType(flatten);
|
---|
115 | results = selectionString(s);
|
---|
116 | if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1)) {
|
---|
117 | assertEquals("TTTTTFFFFFFF", results);
|
---|
118 | } else {
|
---|
119 | assertEquals("TTTTTTTTTTTF", results);
|
---|
120 | }
|
---|
121 |
|
---|
122 | s = (PresentSelector)getInstance();
|
---|
123 | s.setTargetdir(beddir);
|
---|
124 | m = s.createMapper();
|
---|
125 | m.setType(merge);
|
---|
126 | m.setTo("asf-logo.gif.gz");
|
---|
127 | results = selectionString(s);
|
---|
128 | assertEquals("TTTTTTTTTTTT", results);
|
---|
129 |
|
---|
130 | s = (PresentSelector)getInstance();
|
---|
131 | subdir = new File(beddir, "tar/bz2");
|
---|
132 | s.setTargetdir(subdir);
|
---|
133 | m = s.createMapper();
|
---|
134 | m.setType(glob);
|
---|
135 | m.setFrom("*.bz2");
|
---|
136 | m.setTo("*.tar.bz2");
|
---|
137 | results = selectionString(s);
|
---|
138 | assertEquals("FFTFFFFFFFFF", results);
|
---|
139 |
|
---|
140 | try {
|
---|
141 | makeMirror();
|
---|
142 |
|
---|
143 | s = (PresentSelector)getInstance();
|
---|
144 | subdir = getProject().resolveFile("selectortest2");
|
---|
145 | s.setTargetdir(subdir);
|
---|
146 | results = mirrorSelectionString(s);
|
---|
147 | assertEquals("TTTFFTTTTTTT", results);
|
---|
148 | results = selectionString(s);
|
---|
149 | assertEquals("TTTFFTTTTTTT", results);
|
---|
150 |
|
---|
151 |
|
---|
152 | }
|
---|
153 | finally {
|
---|
154 | cleanupMirror();
|
---|
155 | }
|
---|
156 |
|
---|
157 | }
|
---|
158 | finally {
|
---|
159 | cleanupBed();
|
---|
160 | }
|
---|
161 |
|
---|
162 | }
|
---|
163 |
|
---|
164 | }
|
---|