KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > MapperTest


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;
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
25 import junit.framework.TestCase;
26 import junit.framework.AssertionFailedError;
27
28 import java.io.File JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Arrays JavaDoc;
31
32 /**
33  * JUnit 3 testcases for org.apache.tools.ant.types.Mapper.
34  *
35  */

36
37 public class MapperTest extends TestCase {
38
39     private Project project;
40
41     public MapperTest(String JavaDoc name) {
42         super(name);
43     }
44
45     public void setUp() {
46         project = new Project();
47         project.setBasedir(".");
48     }
49
50     public void testEmptyElementIfIsReference() {
51         Mapper m = new Mapper(project);
52         m.setFrom("*.java");
53         try {
54             m.setRefid(new Reference("dummyref"));
55             fail("Can add reference to Mapper with from attribute set");
56         } catch (BuildException be) {
57             assertEquals("You must not specify more than one attribute when using refid",
58                          be.getMessage());
59         }
60
61         m = new Mapper(project);
62         m.setRefid(new Reference("dummyref"));
63         try {
64             m.setFrom("*.java");
65             fail("Can set from in Mapper that is a reference.");
66         } catch (BuildException be) {
67             assertEquals("You must not specify more than one attribute when using refid",
68                          be.getMessage());
69         }
70
71         m = new Mapper(project);
72         m.setRefid(new Reference("dummyref"));
73         try {
74             m.setTo("*.java");
75             fail("Can set to in Mapper that is a reference.");
76         } catch (BuildException be) {
77             assertEquals("You must not specify more than one attribute when using refid",
78                          be.getMessage());
79         }
80         try {
81             Mapper.MapperType mt = new Mapper.MapperType();
82             mt.setValue("glob");
83             m.setType(mt);
84             fail("Can set type in Mapper that is a reference.");
85         } catch (BuildException be) {
86             assertEquals("You must not specify more than one attribute when using refid",
87                          be.getMessage());
88         }
89     }
90
91     public void testCircularReferenceCheck() {
92         Mapper m = new Mapper(project);
93         project.addReference("dummy", m);
94         m.setRefid(new Reference("dummy"));
95         try {
96             m.getImplementation();
97             fail("Can make Mapper a Reference to itself.");
98         } catch (BuildException be) {
99             assertEquals("This data type contains a circular reference.",
100                          be.getMessage());
101         }
102
103         // dummy1 --> dummy2 --> dummy3 --> dummy1
104
Mapper m1 = new Mapper(project);
105         project.addReference("dummy1", m1);
106         m1.setRefid(new Reference("dummy2"));
107         Mapper m2 = new Mapper(project);
108         project.addReference("dummy2", m2);
109         m2.setRefid(new Reference("dummy3"));
110         Mapper m3 = new Mapper(project);
111         project.addReference("dummy3", m3);
112         m3.setRefid(new Reference("dummy1"));
113         try {
114             m1.getImplementation();
115             fail("Can make circular reference.");
116         } catch (BuildException be) {
117             assertEquals("This data type contains a circular reference.",
118                          be.getMessage());
119         }
120
121         // dummy1 --> dummy2 --> dummy3
122
// (which holds a glob mapper from "*.java" to "*.class"
123
m1 = new Mapper(project);
124         project.addReference("dummy1", m1);
125         m1.setRefid(new Reference("dummy2"));
126         m2 = new Mapper(project);
127         project.addReference("dummy2", m2);
128         m2.setRefid(new Reference("dummy3"));
129         m3 = new Mapper(project);
130         project.addReference("dummy3", m3);
131         Mapper.MapperType mt = new Mapper.MapperType();
132         mt.setValue("glob");
133         m3.setType(mt);
134         m3.setFrom("*.java");
135         m3.setTo("*.class");
136         FileNameMapper fmm = m1.getImplementation();
137         assertTrue("should be glob", fmm instanceof GlobPatternMapper);
138         String JavaDoc[] result = fmm.mapFileName("a.java");
139         assertEquals("a.java should match", 1, result.length);
140         assertEquals("a.class", result[0]);
141     }
142
143     public void testNested() {
144         Mapper mapper1 = new Mapper(project);
145         Mapper.MapperType mt = new Mapper.MapperType();
146         mt.setValue("glob");
147         mapper1.setType(mt);
148         mapper1.setFrom("from*");
149         mapper1.setTo("to*");
150
151         //mix element types
152
FileNameMapper mapper2 = new FlatFileNameMapper();
153         FileNameMapper mapper3 = new MergingMapper();
154         mapper3.setTo("mergefile");
155
156         Mapper container = new Mapper(project);
157         container.addConfiguredMapper(mapper1);
158         container.add(mapper2);
159         container.add(mapper3);
160
161         FileNameMapper fileNameMapper = container.getImplementation();
162         String JavaDoc[] targets = fileNameMapper.mapFileName("fromfilename");
163         assertNotNull("no filenames mapped", targets);
164         assertEquals("wrong number of filenames mapped", 3, targets.length);
165         List JavaDoc list = Arrays.asList(targets);
166         assertTrue("cannot find expected target \"tofilename\"",
167             list.contains("tofilename"));
168         assertTrue("cannot find expected target \"fromfilename\"",
169             list.contains("fromfilename"));
170         assertTrue("cannot find expected target \"mergefile\"",
171             list.contains("mergefile"));
172     }
173
174     public void testChained() {
175
176         // a --> b --> c --- def
177
// \-- ghi
178

179         FileNameMapper mapperAB = new GlobPatternMapper();
180         mapperAB.setFrom("a");
181         mapperAB.setTo("b");
182
183         FileNameMapper mapperBC = new GlobPatternMapper();
184         mapperBC.setFrom("b");
185         mapperBC.setTo("c");
186
187         //implicit composite
188
Mapper mapperCX = new Mapper(project);
189
190         FileNameMapper mapperDEF = new GlobPatternMapper();
191         mapperDEF.setFrom("c");
192         mapperDEF.setTo("def");
193
194         FileNameMapper mapperGHI = new GlobPatternMapper();
195         mapperGHI.setFrom("c");
196         mapperGHI.setTo("ghi");
197
198         mapperCX.add(mapperDEF);
199         mapperCX.add(mapperGHI);
200
201         Mapper chained = new Mapper(project);
202         chained.setClassname(ChainedMapper.class.getName());
203         chained.add(mapperAB);
204         chained.add(mapperBC);
205         chained.addConfiguredMapper(mapperCX);
206
207         FileNameMapper fileNameMapper = chained.getImplementation();
208         String JavaDoc[] targets = fileNameMapper.mapFileName("a");
209         assertNotNull("no filenames mapped", targets);
210         assertEquals("wrong number of filenames mapped", 2, targets.length);
211         List JavaDoc list = Arrays.asList(targets);
212         assertTrue("cannot find expected target \"def\"", list.contains("def"));
213         assertTrue("cannot find expected target \"ghi\"", list.contains("ghi"));
214     }
215
216     public void testCopyTaskWithTwoFilesets() {
217         TaskdefForCopyTest t = new TaskdefForCopyTest("test1");
218         try {
219             t.setUp();
220             t.test1();
221         } finally {
222             t.tearDown();
223         }
224     }
225
226     private class TaskdefForCopyTest extends BuildFileTest {
227         TaskdefForCopyTest(String JavaDoc name) {
228             super(name);
229         }
230
231         public void setUp() {
232             configureProject("src/etc/testcases/types/mapper.xml");
233         }
234
235         public void tearDown() {
236             executeTarget("cleanup");
237         }
238
239         public void test1() {
240             executeTarget("test1");
241         }
242     }
243 }
244
Popular Tags