001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.ant.grammar;
043:
044: import java.util.Arrays;
045: import java.util.Collections;
046: import java.util.List;
047: import org.netbeans.junit.NbTestCase;
048: import org.w3c.dom.Element;
049:
050: // XXX testSpecials (what does this mean actually?)
051: // XXX testAddTarget
052: // XXX testDescriptionCanBeAddedOnlyOnce
053:
054: /**
055: * Test functionality of AntGrammar.
056: * @author Jesse Glick
057: */
058: public class AntGrammarTest extends NbTestCase {
059:
060: public AntGrammarTest(String name) {
061: super (name);
062: }
063:
064: private AntGrammar g;
065:
066: protected @Override
067: void setUp() throws Exception {
068: super .setUp();
069: g = new AntGrammar();
070: }
071:
072: public void testTypeOf() throws Exception {
073: String simpleProject = "<project default='all'><target name='all'/></project>";
074: Element e = TestUtil.createElementInDocument(simpleProject,
075: "project", null);
076: AntGrammar.ElementType type = AntGrammar.typeOf(e);
077: assertEquals(AntGrammar.Kind.PROJECT, type.kind);
078: // XXX other specials...
079: String projectWithTasks = "<project default='all'><target name='all'><echo>hello</echo></target></project>";
080: e = TestUtil.createElementInDocument(projectWithTasks, "echo",
081: null);
082: type = AntGrammar.typeOf(e);
083: assertEquals(AntGrammar.Kind.TASK, type.kind);
084: assertEquals("org.apache.tools.ant.taskdefs.Echo", type.name);
085: String projectWithTypes = "<project default='all'><path id='foo'/><target name='all'/></project>";
086: e = TestUtil.createElementInDocument(projectWithTypes, "path",
087: null);
088: type = AntGrammar.typeOf(e);
089: assertEquals(AntGrammar.Kind.TYPE, type.kind);
090: assertEquals("org.apache.tools.ant.types.Path", type.name);
091: // XXX more...
092: }
093:
094: public void testTaskCompletion() throws Exception {
095: String p = "<project default='x'><target name='x'><ecHERE/></target></project>";
096: List<String> l = TestUtil.grammarResultValues(g
097: .queryElements(TestUtil.createCompletion(p)));
098: assertTrue("matched <echo>", l.contains("echo"));
099: // XXX more...
100: }
101:
102: public void testTypeCompletion() throws Exception {
103: String p = "<project default='x'><target name='x'><paHERE/></target></project>";
104: List<String> l = TestUtil.grammarResultValues(g
105: .queryElements(TestUtil.createCompletion(p)));
106: assertTrue("matched <path>", l.contains("path"));
107: p = "<project default='x'><filHERE/><target name='x'/></project>";
108: l = TestUtil.grammarResultValues(g.queryElements(TestUtil
109: .createCompletion(p)));
110: assertTrue("matched <fileset>", l.contains("fileset"));
111: // XXX more...
112: }
113:
114: public void testRegularAttrCompletion() throws Exception {
115: String p = "<project default='x'><target name='x'><javac srcdHERE=''/></target></project>";
116: List<String> l = TestUtil.grammarResultValues(g
117: .queryAttributes(TestUtil.createCompletion(p)));
118: assertTrue("matched srcdir on <javac>: " + l, l
119: .contains("srcdir"));
120: // XXX more...
121: }
122:
123: public void testSpecialAttrCompletion() throws Exception {
124: String p = "<project default='x'><target nHERE=''/></project>";
125: List<String> l = TestUtil.grammarResultValues(g
126: .queryAttributes(TestUtil.createCompletion(p)));
127: assertEquals("matched name on <target>", Collections
128: .singletonList("name"), l);
129: p = "<project default='x'><target dHERE=''/></project>";
130: l = TestUtil.grammarResultValues(g.queryAttributes(TestUtil
131: .createCompletion(p)));
132: Collections.sort(l);
133: assertEquals("matched depends and description on <target>",
134: Arrays.asList("depends", "description"), l);
135: // XXX more...
136: }
137:
138: public void testEnumeratedValueCompletion() throws Exception {
139: String p = "<project default='x'><target><echo level='vHERE'/></target></project>";
140: List<String> l = TestUtil.grammarResultValues(g
141: .queryValues(TestUtil.createCompletion(p)));
142: assertEquals("matched level='verbose' on <echo>", Collections
143: .singletonList("verbose"), l);
144: }
145:
146: public void testBooleanValueCompletion() throws Exception {
147: String p = "<project default='x'><target><echo append='HERE'/></target></project>";
148: List<String> l = TestUtil.grammarResultValues(g
149: .queryValues(TestUtil.createCompletion(p)));
150: Collections.sort(l);
151: assertEquals("true or false for append on <echo>", Arrays
152: .asList("false", "true"), l);
153: }
154:
155: public void testStockProperties() throws Exception {
156: String p = "<project default='x'><target><echo message='${HERE'/></target></project>";
157: List<String> l = TestUtil.grammarResultValues(g
158: .queryValues(TestUtil.createCompletion(p)));
159: assertTrue("matched ${ant.home}: " + l, l
160: .contains("${ant.home}"));
161: assertTrue("matched ${basedir}: " + l, l.contains("${basedir}"));
162: assertTrue("matched ${java.home}: " + l, l
163: .contains("${java.home}"));
164: }
165:
166: public void testPropertiesWithoutBrace() throws Exception {
167: String p = "<project default='x'><target><echo message='$HERE'/></target></project>";
168: List<String> l = TestUtil.grammarResultValues(g
169: .queryValues(TestUtil.createCompletion(p)));
170: assertTrue("matched ${basedir}: " + l, l.contains("${basedir}"));
171: }
172:
173: public void testPropertiesInText() throws Exception {
174: String p = "<project default='x'><target><echo>basedir=${baseHERE</echo></target></project>";
175: List<String> l = TestUtil.grammarResultValues(g
176: .queryValues(TestUtil.createCompletion(p)));
177: assertTrue("matched ${basedir}: " + l, l.contains("dir}"));
178: }
179:
180: public void testPropertiesInInterior() throws Exception {
181: String p = "<project default='x'><target><echo message='basedir=${baseHERE'/></target></project>";
182: List<String> l = TestUtil.grammarResultValues(g
183: .queryValues(TestUtil.createCompletion(p)));
184: assertTrue("matched ${basedir} after prefix: " + l, l
185: .contains("basedir=${basedir}"));
186: p = "<project default='x'><target><echo message='foo=${foo} basedir=${baseHERE'/></target></project>";
187: l = TestUtil.grammarResultValues(g.queryValues(TestUtil
188: .createCompletion(p)));
189: assertTrue("matched ${basedir} after other props: " + l, l
190: .contains("foo=${foo} basedir=${basedir}"));
191: p = "<project default='x'><target><echo>foo=${foo} basedir=${baseHERE</echo></target></project>";
192: l = TestUtil.grammarResultValues(g.queryValues(TestUtil
193: .createCompletion(p)));
194: assertTrue(
195: "matched ${basedir} after other props in text: " + l, l
196: .contains("dir}"));
197: }
198:
199: public void testAlreadyUsedProperties() throws Exception {
200: String p = "<project default='x'><target><echo message='${foo}'/><echo message='${HERE'/></target></project>";
201: List<String> l = TestUtil.grammarResultValues(g
202: .queryValues(TestUtil.createCompletion(p)));
203: assertTrue("matched already used property ${foo}: " + l, l
204: .contains("${foo}"));
205: p = "<project default='x'><target><echo message='${HERE'/></target><target><echo message='${foo}'/></target></project>";
206: l = TestUtil.grammarResultValues(g.queryValues(TestUtil
207: .createCompletion(p)));
208: assertTrue("matched property ${foo} used later: " + l, l
209: .contains("${foo}"));
210: p = "<project default='x'><target><echo message='${HERE'/></target><target><echo>${foo}</echo></target></project>";
211: l = TestUtil.grammarResultValues(g.queryValues(TestUtil
212: .createCompletion(p)));
213: assertTrue("matched property ${foo} used in a text node: " + l,
214: l.contains("${foo}"));
215: p = "<project default='x'><target><echo message='prefix${foo}suffix'/><echo message='${HERE'/></target></project>";
216: l = TestUtil.grammarResultValues(g.queryValues(TestUtil
217: .createCompletion(p)));
218: assertTrue("matched property ${foo} used inside a value: " + l,
219: l.contains("${foo}"));
220: p = "<project default='x'><target><echo message='${foo}:${bar}'/><echo message='${HERE'/></target></project>";
221: l = TestUtil.grammarResultValues(g.queryValues(TestUtil
222: .createCompletion(p)));
223: assertTrue("matched property ${foo} used before another prop: "
224: + l, l.contains("${foo}"));
225: assertTrue("matched property ${bar} used after another prop: "
226: + l, l.contains("${bar}"));
227: }
228:
229: public void testAddedProperties() throws Exception {
230: String p = "<project default='x'><property name='foo' value='whatever'/><target><echo message='${HERE'/></target></project>";
231: List<String> l = TestUtil.grammarResultValues(g
232: .queryValues(TestUtil.createCompletion(p)));
233: assertTrue("matched defined property ${foo}: " + l, l
234: .contains("${foo}"));
235: }
236:
237: public void testImpliedProperties() throws Exception {
238: String p = "<project default='x'><target if='someprop'><echo message='${HERE'/></target></project>";
239: List<String> l = TestUtil.grammarResultValues(g
240: .queryValues(TestUtil.createCompletion(p)));
241: assertTrue("matched property ${someprop} from <target if>: "
242: + l, l.contains("${someprop}"));
243: p = "<project default='x'><target><junit errorproperty='failed'/><echo message='${HERE'/></target></project>";
244: l = TestUtil.grammarResultValues(g.queryValues(TestUtil
245: .createCompletion(p)));
246: assertTrue(
247: "matched property ${failed} from <junit errorproperty>: "
248: + l, l.contains("${failed}"));
249: // XXX could also test other standard names
250: }
251:
252: public void testImplicitProperties() throws Exception {
253: String p = "<project default='x'><target><buildnumber/><echo message='${HERE'/></target></project>";
254: List<String> l = TestUtil.grammarResultValues(g
255: .queryValues(TestUtil.createCompletion(p)));
256: assertTrue(
257: "matched property ${build.number} from <buildnumber>: "
258: + l, l.contains("${build.number}"));
259: // XXX could also test other standard names
260: }
261:
262: public void testIndirectProperties() throws Exception {
263: String p = "<project default='x'><target><property name='${foo}' value='bar'/><echo message='${HERE'/></target></project>";
264: List<String> l = TestUtil.grammarResultValues(g
265: .queryValues(TestUtil.createCompletion(p)));
266: assertFalse("did not match non-property ${${foo}}: " + l, l
267: .contains("${${foo}}"));
268: }
269:
270: public void testNonProperties() throws Exception {
271: String p = "<project default='x'><target><echo>${foo</echo><echo message='${HERE'/></target></project>";
272: List<String> l = TestUtil.grammarResultValues(g
273: .queryValues(TestUtil.createCompletion(p)));
274: assertFalse("did not match broken property ref '${foo': " + l,
275: l.contains("${foo}"));
276: p = "<project default='x'><target><echo>$${foo}</echo><echo message='${HERE'/></target></project>";
277: l = TestUtil.grammarResultValues(g.queryValues(TestUtil
278: .createCompletion(p)));
279: assertFalse("did not match escaped property nonref '$${foo}': "
280: + l, l.contains("${foo}"));
281: p = "<project default='x'><target><echo>${}</echo><echo message='${HERE'/></target></project>";
282: l = TestUtil.grammarResultValues(g.queryValues(TestUtil
283: .createCompletion(p)));
284: assertFalse("did not match empty property name: " + l, l
285: .contains("${}"));
286: p = "<project default='x'><target><echo>$$${foo}</echo><echo message='${HERE'/></target></project>";
287: l = TestUtil.grammarResultValues(g.queryValues(TestUtil
288: .createCompletion(p)));
289: assertTrue(
290: "but '$$${foo}' is a property ref after an escaped shell: "
291: + l, l.contains("${foo}"));
292: }
293:
294: public void testNonCompletingProperties() throws Exception {
295: String p = "<project default='x'><target><echo message='$${baseHERE'/></target></project>";
296: List<String> l = TestUtil.grammarResultValues(g
297: .queryValues(TestUtil.createCompletion(p)));
298: assertFalse("did not match property non-ref $${basedir}: " + l,
299: l.contains("$${basedir}"));
300: assertEquals("in fact there are no completions here",
301: Collections.EMPTY_LIST, l);
302: p = "<project default='x'><target><echo message='$$${baseHERE'/></target></project>";
303: l = TestUtil.grammarResultValues(g.queryValues(TestUtil
304: .createCompletion(p)));
305: assertTrue("but did match property ref $$${basedir}: " + l, l
306: .contains("$$${basedir}"));
307: p = "<project default='x'><target><echo message='${basedir}HERE'/></target></project>";
308: l = TestUtil.grammarResultValues(g.queryValues(TestUtil
309: .createCompletion(p)));
310: assertFalse("${basedir} is already complete: " + l, l
311: .contains("${basedir}"));
312: assertEquals("in fact there are no completions here",
313: Collections.emptyList(), l);
314: }
315:
316: public void testCompleteImpliedProperties() throws Exception {
317: String p = "<project default='x'><target if='baseHERE'/></project>";
318: List<String> l = TestUtil.grammarResultValues(g
319: .queryValues(TestUtil.createCompletion(p)));
320: assertTrue("completing <target if>: " + l, l
321: .contains("basedir"));
322: p = "<project default='x'><target><condition><isset property='baseHERE'/></condition></target></project>";
323: l = TestUtil.grammarResultValues(g.queryValues(TestUtil
324: .createCompletion(p)));
325: assertTrue("completing <isset property>: " + l, l
326: .contains("basedir"));
327: // XXX could also test other standard names
328: }
329:
330: public void testImport() throws Exception {
331: String p = "<project default='x'><impHERE/></project>";
332: List<String> l = TestUtil.grammarResultValues(g
333: .queryElements(TestUtil.createCompletion(p)));
334: assertTrue("matched <import>", l.contains("import"));
335: p = "<project default='x'><import fHERE=''/></project>";
336: l = TestUtil.grammarResultValues(g.queryAttributes(TestUtil
337: .createCompletion(p)));
338: assertTrue("matched file on <import>: " + l, l.contains("file"));
339: p = "<project default='x'><import file='y' optHERE=''/></project>";
340: l = TestUtil.grammarResultValues(g.queryAttributes(TestUtil
341: .createCompletion(p)));
342: assertTrue("matched optional on <import>: " + l, l
343: .contains("optional"));
344: p = "<project default='x'><import file='y' optional='HERE'/></project>";
345: l = TestUtil.grammarResultValues(g.queryValues(TestUtil
346: .createCompletion(p)));
347: Collections.sort(l);
348: assertEquals("true or false for optional on <import>", Arrays
349: .asList("false", "true"), l);
350: }
351:
352: }
|