KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > grammar > AntGrammarTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.ant.grammar;
21
22 import java.util.Arrays JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.List JavaDoc;
25 import org.netbeans.junit.NbTestCase;
26 import org.w3c.dom.Element JavaDoc;
27
28 // XXX testSpecials (what does this mean actually?)
29
// XXX testAddTarget
30
// XXX testDescriptionCanBeAddedOnlyOnce
31

32 /**
33  * Test functionality of AntGrammar.
34  * @author Jesse Glick
35  */

36 public class AntGrammarTest extends NbTestCase {
37
38     public AntGrammarTest(String JavaDoc name) {
39         super(name);
40     }
41
42     private AntGrammar g;
43
44     protected void setUp() throws Exception JavaDoc {
45         super.setUp();
46         g = new AntGrammar();
47     }
48     
49     public void testTypeOf() throws Exception JavaDoc {
50         String JavaDoc simpleProject = "<project default='all'><target name='all'/></project>";
51         Element JavaDoc e = TestUtil.createElementInDocument(simpleProject, "project", null);
52         String JavaDoc[] type = AntGrammar.typeOf(e);
53         assertEquals("is special", AntGrammar.KIND_SPECIAL, type[0]);
54         assertEquals("is project", AntGrammar.SPECIAL_PROJECT, type[1]);
55         // XXX other specials...
56
String JavaDoc projectWithTasks = "<project default='all'><target name='all'><echo>hello</echo></target></project>";
57         e = TestUtil.createElementInDocument(projectWithTasks, "echo", null);
58         type = AntGrammar.typeOf(e);
59         assertEquals("is task", AntGrammar.KIND_TASK, type[0]);
60         assertEquals("is <echo>", "org.apache.tools.ant.taskdefs.Echo", type[1]);
61         String JavaDoc projectWithTypes = "<project default='all'><path id='foo'/><target name='all'/></project>";
62         e = TestUtil.createElementInDocument(projectWithTypes, "path", null);
63         type = AntGrammar.typeOf(e);
64         assertEquals("is type", AntGrammar.KIND_TYPE, type[0]);
65         assertEquals("is <path>", "org.apache.tools.ant.types.Path", type[1]);
66         // XXX more...
67
}
68     
69     public void testTaskCompletion() throws Exception JavaDoc {
70         String JavaDoc p = "<project default='x'><target name='x'><ecHERE/></target></project>";
71         List JavaDoc l = TestUtil.grammarResultValues(g.queryElements(TestUtil.createCompletion(p)));
72         assertTrue("matched <echo>", l.contains("echo"));
73         // XXX more...
74
}
75     
76     public void testTypeCompletion() throws Exception JavaDoc {
77         String JavaDoc p = "<project default='x'><target name='x'><paHERE/></target></project>";
78         List JavaDoc l = TestUtil.grammarResultValues(g.queryElements(TestUtil.createCompletion(p)));
79         assertTrue("matched <path>", l.contains("path"));
80         p = "<project default='x'><filHERE/><target name='x'/></project>";
81         l = TestUtil.grammarResultValues(g.queryElements(TestUtil.createCompletion(p)));
82         assertTrue("matched <fileset>", l.contains("fileset"));
83         // XXX more...
84
}
85     
86     public void testRegularAttrCompletion() throws Exception JavaDoc {
87         String JavaDoc p = "<project default='x'><target name='x'><javac srcdHERE=''/></target></project>";
88         List JavaDoc l = TestUtil.grammarResultValues(g.queryAttributes(TestUtil.createCompletion(p)));
89         assertTrue("matched srcdir on <javac>: " + l, l.contains("srcdir"));
90         // XXX more...
91
}
92     
93     public void testSpecialAttrCompletion() throws Exception JavaDoc {
94         String JavaDoc p = "<project default='x'><target nHERE=''/></project>";
95         List JavaDoc l = TestUtil.grammarResultValues(g.queryAttributes(TestUtil.createCompletion(p)));
96         assertEquals("matched name on <target>", Collections.singletonList("name"), l);
97         p = "<project default='x'><target dHERE=''/></project>";
98         l = TestUtil.grammarResultValues(g.queryAttributes(TestUtil.createCompletion(p)));
99         Collections.sort(l);
100         assertEquals("matched depends and description on <target>",
101             Arrays.asList(new String JavaDoc[] {"depends", "description"}), l);
102         // XXX more...
103
}
104     
105     public void testEnumeratedValueCompletion() throws Exception JavaDoc {
106         String JavaDoc p = "<project default='x'><target><echo level='vHERE'/></target></project>";
107         List JavaDoc l = TestUtil.grammarResultValues(g.queryValues(TestUtil.createCompletion(p)));
108         assertEquals("matched level='verbose' on <echo>", Collections.singletonList("verbose"), l);
109     }
110     
111     public void testBooleanValueCompletion() throws Exception JavaDoc {
112         String JavaDoc p = "<project default='x'><target><echo append='HERE'/></target></project>";
113         List JavaDoc l = TestUtil.grammarResultValues(g.queryValues(TestUtil.createCompletion(p)));
114         Collections.sort(l);
115         assertEquals("true or false for append on <echo>",
116             Arrays.asList(new String JavaDoc[] {"false", "true"}), l);
117     }
118     
119     public void testStockProperties() throws Exception JavaDoc {
120         String JavaDoc p = "<project default='x'><target><echo message='${HERE'/></target></project>";
121         List JavaDoc l = TestUtil.grammarResultValues(g.queryValues(TestUtil.createCompletion(p)));
122         assertTrue("matched ${ant.home}: " + l, l.contains("${ant.home}"));
123         assertTrue("matched ${basedir}: " + l, l.contains("${basedir}"));
124         assertTrue("matched ${java.home}: " + l, l.contains("${java.home}"));
125     }
126     
127     public void testPropertiesWithoutBrace() throws Exception JavaDoc {
128         String JavaDoc p = "<project default='x'><target><echo message='$HERE'/></target></project>";
129         List JavaDoc l = TestUtil.grammarResultValues(g.queryValues(TestUtil.createCompletion(p)));
130         assertTrue("matched ${basedir}: " + l, l.contains("${basedir}"));
131     }
132     
133     public void testPropertiesInText() throws Exception JavaDoc {
134         String JavaDoc p = "<project default='x'><target><echo>basedir=${baseHERE</echo></target></project>";
135         List JavaDoc l = TestUtil.grammarResultValues(g.queryValues(TestUtil.createCompletion(p)));
136         assertTrue("matched ${basedir}: " + l, l.contains("dir}"));
137     }
138     
139     public void testPropertiesInInterior() throws Exception JavaDoc {
140         String JavaDoc p = "<project default='x'><target><echo message='basedir=${baseHERE'/></target></project>";
141         List JavaDoc l = TestUtil.grammarResultValues(g.queryValues(TestUtil.createCompletion(p)));
142         assertTrue("matched ${basedir} after prefix: " + l, l.contains("basedir=${basedir}"));
143         p = "<project default='x'><target><echo message='foo=${foo} basedir=${baseHERE'/></target></project>";
144         l = TestUtil.grammarResultValues(g.queryValues(TestUtil.createCompletion(p)));
145         assertTrue("matched ${basedir} after other props: " + l, l.contains("foo=${foo} basedir=${basedir}"));
146         p = "<project default='x'><target><echo>foo=${foo} basedir=${baseHERE</echo></target></project>";
147         l = TestUtil.grammarResultValues(g.queryValues(TestUtil.createCompletion(p)));
148         assertTrue("matched ${basedir} after other props in text: " + l, l.contains("dir}"));
149     }
150     
151     public void testAlreadyUsedProperties() throws Exception JavaDoc {
152         String JavaDoc p = "<project default='x'><target><echo message='${foo}'/><echo message='${HERE'/></target></project>";
153         List JavaDoc l = TestUtil.grammarResultValues(g.queryValues(TestUtil.createCompletion(p)));
154         assertTrue("matched already used property ${foo}: " + l, l.contains("${foo}"));
155         p = "<project default='x'><target><echo message='${HERE'/></target><target><echo message='${foo}'/></target></project>";
156         l = TestUtil.grammarResultValues(g.queryValues(TestUtil.createCompletion(p)));
157         assertTrue("matched property ${foo} used later: " + l, l.contains("${foo}"));
158         p = "<project default='x'><target><echo message='${HERE'/></target><target><echo>${foo}</echo></target></project>";
159         l = TestUtil.grammarResultValues(g.queryValues(TestUtil.createCompletion(p)));
160         assertTrue("matched property ${foo} used in a text node: " + l, l.contains("${foo}"));
161         p = "<project default='x'><target><echo message='prefix${foo}suffix'/><echo message='${HERE'/></target></project>";
162         l = TestUtil.grammarResultValues(g.queryValues(TestUtil.createCompletion(p)));
163         assertTrue("matched property ${foo} used inside a value: " + l, l.contains("${foo}"));
164         p = "<project default='x'><target><echo message='${foo}:${bar}'/><echo message='${HERE'/></target></project>";
165         l = TestUtil.grammarResultValues(g.queryValues(TestUtil.createCompletion(p)));
166         assertTrue("matched property ${foo} used before another prop: " + l, l.contains("${foo}"));
167         assertTrue("matched property ${bar} used after another prop: " + l, l.contains("${bar}"));
168     }
169     
170     public void testAddedProperties() throws Exception JavaDoc {
171         String JavaDoc p = "<project default='x'><property name='foo' value='whatever'/><target><echo message='${HERE'/></target></project>";
172         List JavaDoc l = TestUtil.grammarResultValues(g.queryValues(TestUtil.createCompletion(p)));
173         assertTrue("matched defined property ${foo}: " + l, l.contains("${foo}"));
174     }
175     
176     public void testImpliedProperties() throws Exception JavaDoc {
177         String JavaDoc p = "<project default='x'><target if='someprop'><echo message='${HERE'/></target></project>";
178         List JavaDoc l = TestUtil.grammarResultValues(g.queryValues(TestUtil.createCompletion(p)));
179         assertTrue("matched property ${someprop} from <target if>: " + l, l.contains("${someprop}"));
180         p = "<project default='x'><target><junit errorproperty='failed'/><echo message='${HERE'/></target></project>";
181         l = TestUtil.grammarResultValues(g.queryValues(TestUtil.createCompletion(p)));
182         assertTrue("matched property ${failed} from <junit errorproperty>: " + l, l.contains("${failed}"));
183         // XXX could also test other standard names
184
}
185     
186     public void testImplicitProperties() throws Exception JavaDoc {
187         String JavaDoc p = "<project default='x'><target><buildnumber/><echo message='${HERE'/></target></project>";
188         List JavaDoc l = TestUtil.grammarResultValues(g.queryValues(TestUtil.createCompletion(p)));
189         assertTrue("matched property ${build.number} from <buildnumber>: " + l, l.contains("${build.number}"));
190         // XXX could also test other standard names
191
}
192     
193     public void testIndirectProperties() throws Exception JavaDoc {
194         String JavaDoc p = "<project default='x'><target><property name='${foo}' value='bar'/><echo message='${HERE'/></target></project>";
195         List JavaDoc l = TestUtil.grammarResultValues(g.queryValues(TestUtil.createCompletion(p)));
196         assertFalse("did not match non-property ${${foo}}: " + l, l.contains("${${foo}}"));
197     }
198     
199     public void testNonProperties() throws Exception JavaDoc {
200         String JavaDoc p = "<project default='x'><target><echo>${foo</echo><echo message='${HERE'/></target></project>";
201         List JavaDoc l = TestUtil.grammarResultValues(g.queryValues(TestUtil.createCompletion(p)));
202         assertFalse("did not match broken property ref '${foo': " + l, l.contains("${foo}"));
203         p = "<project default='x'><target><echo>$${foo}</echo><echo message='${HERE'/></target></project>";
204         l = TestUtil.grammarResultValues(g.queryValues(TestUtil.createCompletion(p)));
205         assertFalse("did not match escaped property nonref '$${foo}': " + l, l.contains("${foo}"));
206         p = "<project default='x'><target><echo>${}</echo><echo message='${HERE'/></target></project>";
207         l = TestUtil.grammarResultValues(g.queryValues(TestUtil.createCompletion(p)));
208         assertFalse("did not match empty property name: " + l, l.contains("${}"));
209         p = "<project default='x'><target><echo>$$${foo}</echo><echo message='${HERE'/></target></project>";
210         l = TestUtil.grammarResultValues(g.queryValues(TestUtil.createCompletion(p)));
211         assertTrue("but '$$${foo}' is a property ref after an escaped shell: " + l, l.contains("${foo}"));
212     }
213     
214     public void testNonCompletingProperties() throws Exception JavaDoc {
215         String JavaDoc p = "<project default='x'><target><echo message='$${baseHERE'/></target></project>";
216         List JavaDoc l = TestUtil.grammarResultValues(g.queryValues(TestUtil.createCompletion(p)));
217         assertFalse("did not match property non-ref $${basedir}: " + l, l.contains("$${basedir}"));
218         assertEquals("in fact there are no completions here", Collections.EMPTY_LIST, l);
219         p = "<project default='x'><target><echo message='$$${baseHERE'/></target></project>";
220         l = TestUtil.grammarResultValues(g.queryValues(TestUtil.createCompletion(p)));
221         assertTrue("but did match property ref $$${basedir}: " + l, l.contains("$$${basedir}"));
222         p = "<project default='x'><target><echo message='${basedir}HERE'/></target></project>";
223         l = TestUtil.grammarResultValues(g.queryValues(TestUtil.createCompletion(p)));
224         assertFalse("${basedir} is already complete: " + l, l.contains("${basedir}"));
225         assertEquals("in fact there are no completions here", Collections.EMPTY_LIST, l);
226     }
227     
228     public void testCompleteImpliedProperties() throws Exception JavaDoc {
229         String JavaDoc p = "<project default='x'><target if='baseHERE'/></project>";
230         List JavaDoc l = TestUtil.grammarResultValues(g.queryValues(TestUtil.createCompletion(p)));
231         assertTrue("completing <target if>: " + l, l.contains("basedir"));
232         p = "<project default='x'><target><condition><isset property='baseHERE'/></condition></target></project>";
233         l = TestUtil.grammarResultValues(g.queryValues(TestUtil.createCompletion(p)));
234         assertTrue("completing <isset property>: " + l, l.contains("basedir"));
235         // XXX could also test other standard names
236
}
237     
238     public void testImport() throws Exception JavaDoc {
239         String JavaDoc p = "<project default='x'><impHERE/></project>";
240         List JavaDoc l = TestUtil.grammarResultValues(g.queryElements(TestUtil.createCompletion(p)));
241         assertTrue("matched <import>", l.contains("import"));
242         p = "<project default='x'><import fHERE=''/></project>";
243         l = TestUtil.grammarResultValues(g.queryAttributes(TestUtil.createCompletion(p)));
244         assertTrue("matched file on <import>: " + l, l.contains("file"));
245         p = "<project default='x'><import file='y' optHERE=''/></project>";
246         l = TestUtil.grammarResultValues(g.queryAttributes(TestUtil.createCompletion(p)));
247         assertTrue("matched optional on <import>: " + l, l.contains("optional"));
248         p = "<project default='x'><import file='y' optional='HERE'/></project>";
249         l = TestUtil.grammarResultValues(g.queryValues(TestUtil.createCompletion(p)));
250         Collections.sort(l);
251         assertEquals("true or false for optional on <import>",
252             Arrays.asList(new String JavaDoc[] {"false", "true"}), l);
253     }
254
255 }
256
Popular Tags