KickJava   Java API By Example, From Geeks To Geeks.

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


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
23 import junit.framework.TestCase;
24 import junit.framework.AssertionFailedError;
25
26 import java.io.File JavaDoc;
27
28 /**
29  * JUnit 3 testcases for org.apache.tools.ant.types.PatternSet.
30  *
31  * <p>This doesn't actually test much, mainly reference handling.</p>
32  *
33  */

34
35 public class PatternSetTest extends TestCase {
36
37     private Project project;
38
39     public PatternSetTest(String JavaDoc name) {
40         super(name);
41     }
42
43     public void setUp() {
44         project = new Project();
45         project.setBasedir(".");
46     }
47
48     public void testEmptyElementIfIsReference() {
49         PatternSet p = new PatternSet();
50         p.setIncludes("**/*.java");
51         try {
52             p.setRefid(new Reference("dummyref"));
53             fail("Can add reference to PatternSet with elements from setIncludes");
54         } catch (BuildException be) {
55             assertEquals("You must not specify more than one attribute when using refid",
56                          be.getMessage());
57         }
58
59         p = new PatternSet();
60         p.setRefid(new Reference("dummyref"));
61         try {
62             p.setIncludes("**/*.java");
63             fail("Can set includes in PatternSet that is a reference.");
64         } catch (BuildException be) {
65             assertEquals("You must not specify more than one attribute when using refid",
66                          be.getMessage());
67         }
68
69         p = new PatternSet();
70         p.setRefid(new Reference("dummyref"));
71         try {
72             p.setIncludesfile(new File JavaDoc("/a"));
73             fail("Can set includesfile in PatternSet that is a reference.");
74         } catch (BuildException be) {
75             assertEquals("You must not specify more than one attribute when using refid",
76                          be.getMessage());
77         }
78         try {
79             p.setExcludes("**/*.java");
80             fail("Can set excludes in PatternSet that is a reference.");
81         } catch (BuildException be) {
82             assertEquals("You must not specify more than one attribute when using refid",
83                          be.getMessage());
84         }
85         try {
86             p.setExcludesfile(new File JavaDoc("/a"));
87             fail("Can set excludesfile in PatternSet that is a reference.");
88         } catch (BuildException be) {
89             assertEquals("You must not specify more than one attribute when using refid",
90                          be.getMessage());
91         }
92         try {
93             p.createInclude();
94             fail("Can add nested include in PatternSet that is a reference.");
95         } catch (BuildException be) {
96             assertEquals("You must not specify nested elements when using refid",
97                          be.getMessage());
98         }
99         try {
100             p.createExclude();
101             fail("Can add nested exclude in PatternSet that is a reference.");
102         } catch (BuildException be) {
103             assertEquals("You must not specify nested elements when using refid",
104                          be.getMessage());
105         }
106         try {
107             p.createIncludesFile();
108             fail("Can add nested includesfile in PatternSet that is a reference.");
109         } catch (BuildException be) {
110             assertEquals("You must not specify nested elements when using refid",
111                          be.getMessage());
112         }
113         try {
114             p.createExcludesFile();
115             fail("Can add nested excludesfile in PatternSet that is a reference.");
116         } catch (BuildException be) {
117             assertEquals("You must not specify nested elements when using refid",
118                          be.getMessage());
119         }
120     }
121
122     public void testCircularReferenceCheck() {
123         PatternSet p = new PatternSet();
124         project.addReference("dummy", p);
125         p.setRefid(new Reference("dummy"));
126         try {
127             p.getIncludePatterns(project);
128             fail("Can make PatternSet a Reference to itself.");
129         } catch (BuildException be) {
130             assertEquals("This data type contains a circular reference.",
131                          be.getMessage());
132         }
133         try {
134             p.getExcludePatterns(project);
135             fail("Can make PatternSet a Reference to itself.");
136         } catch (BuildException be) {
137             assertEquals("This data type contains a circular reference.",
138                          be.getMessage());
139         }
140
141         // dummy1 --> dummy2 --> dummy3 --> dummy1
142
PatternSet p1 = new PatternSet();
143         project.addReference("dummy1", p1);
144         p1.setRefid(new Reference("dummy2"));
145         PatternSet p2 = new PatternSet();
146         project.addReference("dummy2", p2);
147         p2.setRefid(new Reference("dummy3"));
148         PatternSet p3 = new PatternSet();
149         project.addReference("dummy3", p3);
150         p3.setRefid(new Reference("dummy1"));
151         try {
152             p1.getIncludePatterns(project);
153             fail("Can make circular reference.");
154         } catch (BuildException be) {
155             assertEquals("This data type contains a circular reference.",
156                          be.getMessage());
157         }
158         try {
159             p1.getExcludePatterns(project);
160             fail("Can make circular reference.");
161         } catch (BuildException be) {
162             assertEquals("This data type contains a circular reference.",
163                          be.getMessage());
164         }
165
166         // dummy1 --> dummy2 --> dummy3
167
// (which holds patterns "include" and "exclude")
168
p1 = new PatternSet();
169         project.addReference("dummy1", p1);
170         p1.setRefid(new Reference("dummy2"));
171         p2 = new PatternSet();
172         project.addReference("dummy2", p2);
173         p2.setRefid(new Reference("dummy3"));
174         p3 = new PatternSet();
175         project.addReference("dummy3", p3);
176         p3.setIncludes("include");
177         p3.createExclude().setName("exclude");
178         String JavaDoc[] i = p1.getIncludePatterns(project);
179         assertEquals("One include pattern buried deep inside a nested patternset structure",
180                      1, i.length);
181         assertEquals("include", i[0]);
182         i = p3.getExcludePatterns(project);
183         assertEquals("One exclude pattern buried deep inside a nested patternset structure",
184                      1, i.length);
185         assertEquals("exclude", i[0]);
186     }
187
188     public void testNestedPatternset() {
189         PatternSet p = new PatternSet();
190         p.setIncludes("**/*.java");
191
192         PatternSet nested = new PatternSet();
193         nested.setExcludes("**/*.class");
194
195         p.addConfiguredPatternset(nested);
196
197         String JavaDoc[] excludes = p.getExcludePatterns(project);
198         String JavaDoc[] includes = p.getIncludePatterns(project);
199
200         assertEquals("Includes","**/*.java", includes[0]);
201         assertEquals("Excludes","**/*.class", excludes[0]);
202     }
203 }
204
Popular Tags