2
* The Apache Software License, Version 1.1
4
* Copyright (c) 2000 The Apache Software Foundation. All rights
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions
11
* 1. Redistributions of source code must retain the above copyright
12
* notice, this list of conditions and the following disclaimer.
14
* 2. Redistributions in binary form must reproduce the above copyright
15
* notice, this list of conditions and the following disclaimer in
16
* the documentation and/or other materials provided with the
19
* 3. The end-user documentation included with the redistribution, if
20
* any, must include the following acknowlegement:
21
* "This product includes software developed by the
22
* Apache Software Foundation (http://www.apache.org/)."
23
* Alternately, this acknowlegement may appear in the software itself,
24
* if and wherever such third-party acknowlegements normally appear.
26
* 4. The names "The Jakarta Project", "Ant", and "Apache Software
27
* Foundation" must not be used to endorse or promote products derived
28
* from this software without prior written permission. For written
29
* permission, please contact apache@apache.org.
31
* 5. Products derived from this software may not be called "Apache"
32
* nor may "Apache" appear in their names without prior written
33
* permission of the Apache Group.
35
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47
* ====================================================================
49
* This software consists of voluntary contributions made by many
50
* individuals on behalf of the Apache Software Foundation. For more
51
* information on the Apache Software Foundation, please see
52
* <http://www.apache.org/>.
55
package org.apache.tools.ant;
57
import junit.framework.TestCase;
58
import junit.framework.AssertionFailedError;
63
* JUnit 3 testcases for org.apache.tools.ant.IntrospectionHelper.
65
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
68
public class IntrospectionHelperTest extends TestCase {
72
public static boolean isUnixStyle = File.pathSeparatorChar == ':';
74
public IntrospectionHelperTest(String name) {
83
public void testAddText() throws BuildException {
84
IntrospectionHelper ih = IntrospectionHelper.getHelper(java.lang.String.class);
86
ih.addText(p, "", "test");
87
fail("String doesn\'t support addText");
88
} catch (BuildException be) {
91
ih = IntrospectionHelper.getHelper(getClass());
92
ih.addText(p, this, "test");
94
ih.addText(p, this, "test2");
95
fail("test2 shouldn\'t be equal to test");
96
} catch (BuildException be) {
97
assert(be.getException() instanceof AssertionFailedError);
101
public void testSupportsCharacters() {
102
IntrospectionHelper ih = IntrospectionHelper.getHelper(java.lang.String.class);
103
assert("String doesn\'t support addText", !ih.supportsCharacters());
104
ih = IntrospectionHelper.getHelper(getClass());
105
assert("IntrospectionHelperTest supports addText",
106
ih.supportsCharacters());
109
public void addText(String text) {
110
assertEquals("test", text);
113
public void testElementCreators() throws BuildException {
114
IntrospectionHelper ih = IntrospectionHelper.getHelper(getClass());
116
ih.getElementType("one");
117
fail("don't have element type one");
118
} catch (BuildException be) {
121
ih.getElementType("two");
122
fail("createTwo takes arguments");
123
} catch (BuildException be) {
126
ih.getElementType("three");
127
fail("createThree returns void");
128
} catch (BuildException be) {
131
ih.getElementType("four");
132
fail("createFour returns array");
133
} catch (BuildException be) {
136
ih.getElementType("five");
137
fail("createFive returns primitive type");
138
} catch (BuildException be) {
140
assertEquals(java.lang.String.class, ih.getElementType("six"));
141
assertEquals("test", ih.createElement(p, this, "six"));
144
ih.getElementType("seven");
145
fail("addSeven takes two arguments");
146
} catch (BuildException be) {
149
ih.getElementType("eight");
150
fail("addEight takes no arguments");
151
} catch (BuildException be) {
154
ih.getElementType("nine");
155
fail("nine return non void");
156
} catch (BuildException be) {
159
ih.getElementType("ten");
160
fail("addTen takes array argument");
161
} catch (BuildException be) {
164
ih.getElementType("eleven");
165
fail("addTen takes primitive argument");
166
} catch (BuildException be) {
169
ih.getElementType("twelve");
170
fail("no primitive constructor for java.lang.Class");
171
} catch (BuildException be) {
173
assertEquals(java.lang.StringBuffer.class, ih.getElementType("thirteen"));
174
assertEquals("test", ih.createElement(p, this, "thirteen").toString());
177
ih.createElement(p, this, "fourteen");
178
fail("fourteen throws NullPointerException");
179
} catch (BuildException be) {
180
assert(be.getException() instanceof NullPointerException);
184
ih.createElement(p, this, "fourteen");
185
fail("fifteen throws NullPointerException");
186
} catch (BuildException be) {
187
assert(be.getException() instanceof NullPointerException);
191
public void testGetNestedElements() {
192
Hashtable h = new Hashtable();
193
h.put("six", java.lang.String.class);
194
h.put("thirteen", java.lang.StringBuffer.class);
195
h.put("fourteen", java.lang.StringBuffer.class);
196
h.put("fifteen", java.lang.StringBuffer.class);
197
IntrospectionHelper ih = IntrospectionHelper.getHelper(getClass());
198
Enumeration enum = ih.getNestedElements();
199
while (enum.hasMoreElements()) {
200
String name = (String) enum.nextElement();
201
Class expect = (Class) h.get(name);
202
assertNotNull("Support for "+name+" in IntrospectioNHelperTest?",
204
assertEquals("Return type of "+name, expect, ih.getElementType(name));
207
assert("Found all", h.isEmpty());
210
public Object createTwo(String s) {
214
public void createThree() {}
216
public Object[] createFour() {
220
public int createFive() {
224
public String createSix() {
228
public StringBuffer createFifteen() {
229
throw new NullPointerException();
232
public void addSeven(String s, String s2) {}
234
public void addEight() {}
236
public String addNine(String s) {
240
public void addTen(String[] s) {}
242
public void addEleven(int i) {}
244
public void addTwelve(Class c) {}
246
public void addThirteen(StringBuffer sb) {
250
public void addFourteen(StringBuffer s) {
251
throw new NullPointerException();
254
public void testAttributeSetters() throws BuildException {
255
IntrospectionHelper ih = IntrospectionHelper.getHelper(getClass());
257
ih.setAttribute(p, this, "one", "test");
258
fail("setOne doesn't exist");
259
} catch (BuildException be) {
262
ih.setAttribute(p, this, "two", "test");
263
fail("setTwo returns non void");
264
} catch (BuildException be) {
267
ih.setAttribute(p, this, "three", "test");
268
fail("setThree takes no args");
269
} catch (BuildException be) {
272
ih.setAttribute(p, this, "four", "test");
273
fail("setFour takes two args");
274
} catch (BuildException be) {
277
ih.setAttribute(p, this, "five", "test");
278
fail("setFive takes array arg");
279
} catch (BuildException be) {
282
ih.setAttribute(p, this, "six", "test");
283
fail("Project doesn't have a String constructor");
284
} catch (BuildException be) {
286
ih.setAttribute(p, this, "seven", "2");
288
ih.setAttribute(p, this, "seven", "3");
289
fail("2 shouldn't be equals to three");
290
} catch (BuildException be) {
291
assert(be.getException() instanceof AssertionFailedError);
293
ih.setAttribute(p, this, "eight", "2");
295
ih.setAttribute(p, this, "eight", "3");
296
fail("2 shouldn't be equals to three - as int");
297
} catch (BuildException be) {
298
assert(be.getException() instanceof AssertionFailedError);
300
ih.setAttribute(p, this, "nine", "2");
302
ih.setAttribute(p, this, "nine", "3");
303
fail("2 shouldn't be equals to three - as Integer");
304
} catch (BuildException be) {
305
assert(be.getException() instanceof AssertionFailedError);
307
ih.setAttribute(p, this, "ten", "2");
309
ih.setAttribute(p, this, "ten", "3");
310
fail("/tmp/2 shouldn't be equals to /tmp/3");
311
} catch (BuildException be) {
312
assert(be.getException() instanceof AssertionFailedError);
314
ih.setAttribute(p, this, "eleven", "2");
316
ih.setAttribute(p, this, "eleven", "on");
317
fail("on shouldn't be false");
318
} catch (BuildException be) {
319
assert(be.getException() instanceof AssertionFailedError);
321
ih.setAttribute(p, this, "twelve", "2");
323
ih.setAttribute(p, this, "twelve", "on");
324
fail("on shouldn't be false");
325
} catch (BuildException be) {
326
assert(be.getException() instanceof AssertionFailedError);
328
ih.setAttribute(p, this, "thirteen", "org.apache.tools.ant.Project");
330
ih.setAttribute(p, this, "thirteen", "org.apache.tools.ant.ProjectHelper");
331
fail("org.apache.tools.ant.Project shouldn't be equal to org.apache.tools.ant.ProjectHelper");
332
} catch (BuildException be) {
333
assert(be.getException() instanceof AssertionFailedError);
336
ih.setAttribute(p, this, "thirteen", "org.apache.tools.ant.Project2");
337
fail("org.apache.tools.ant.Project2 doesn't exist");
338
} catch (BuildException be) {
339
assert(be.getException() instanceof ClassNotFoundException);
341
ih.setAttribute(p, this, "fourteen", "2");
343
ih.setAttribute(p, this, "fourteen", "on");
344
fail("2 shouldn't be equals to three - as StringBuffer");
345
} catch (BuildException be) {
346
assert(be.getException() instanceof AssertionFailedError);
348
ih.setAttribute(p, this, "fifteen", "abcd");
350
ih.setAttribute(p, this, "fifteen", "on");
351
fail("o shouldn't be equal to a");
352
} catch (BuildException be) {
353
assert(be.getException() instanceof AssertionFailedError);
355
ih.setAttribute(p, this, "sixteen", "abcd");
357
ih.setAttribute(p, this, "sixteen", "on");
358
fail("o shouldn't be equal to a");
359
} catch (BuildException be) {
360
assert(be.getException() instanceof AssertionFailedError);
364
public void testGetAttributes() {
365
Hashtable h = new Hashtable();
366
h.put("seven", java.lang.String.class);
367
h.put("eight", java.lang.Integer.TYPE);
368
h.put("nine", java.lang.Integer.class);
369
h.put("ten", java.io.File.class);
370
h.put("eleven", java.lang.Boolean.TYPE);
371
h.put("twelve", java.lang.Boolean.class);
372
h.put("thirteen", java.lang.Class.class);
373
h.put("fourteen", java.lang.StringBuffer.class);
374
h.put("fifteen", java.lang.Character.TYPE);
375
h.put("sixteen", java.lang.Character.class);
378
* JUnit 3.7 adds a getName method to TestCase - so we now
379
* have a name attribute in IntrospectionHelperTest if we run
380
* under JUnit 3.7 but not in earlier versions.
382
* Simply add it here and remove it after the tests.
384
h.put("name", java.lang.String.class);
386
IntrospectionHelper ih = IntrospectionHelper.getHelper(getClass());
387
Enumeration enum = ih.getAttributes();
388
while (enum.hasMoreElements()) {
389
String name = (String) enum.nextElement();
390
Class expect = (Class) h.get(name);
391
assertNotNull("Support for "+name+" in IntrospectionHelperTest?",
393
assertEquals("Type of "+name, expect, ih.getAttributeType(name));
397
assert("Found all", h.isEmpty());
400
public int setTwo(String s) {
404
public void setThree() {}
406
public void setFour(String s1, String s2) {}
408
public void setFive(String[] s) {}
410
public void setSix(Project p) {}
412
public void setSeven(String s) {
413
assertEquals("2", s);
416
public void setEight(int i) {
420
public void setNine(Integer i) {
421
assertEquals(2, i.intValue());
424
public void setTen(File f) {
426
assertEquals("/tmp/2", f.getAbsolutePath());
428
assertEquals(":\\tmp\\2", f.getAbsolutePath().toLowerCase().substring(1));
432
public void setEleven(boolean b) {
436
public void setTwelve(Boolean b) {
437
assert(!b.booleanValue());
440
public void setThirteen(Class c) {
441
assertEquals(Project.class, c);
444
public void setFourteen(StringBuffer sb) {
445
assertEquals("2", sb.toString());
448
public void setFifteen(char c) {
449
assertEquals(c, 'a');
452
public void setSixteen(Character c) {
453
assertEquals(c.charValue(), 'a');
456
}// IntrospectionHelperTest