~ubuntu-branches/ubuntu/jaunty/ant/jaunty-proposed

« back to all changes in this revision

Viewing changes to src/testcases/org/apache/tools/ant/IntrospectionHelperTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Gybas
  • Date: 2002-02-14 14:28:48 UTC
  • Revision ID: james.westby@ubuntu.com-20020214142848-2ww7ynmqkj31vlmn
Tags: upstream-1.4.1
ImportĀ upstreamĀ versionĀ 1.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * The Apache Software License, Version 1.1
 
3
 *
 
4
 * Copyright (c) 2000 The Apache Software Foundation.  All rights
 
5
 * reserved.
 
6
 *
 
7
 * Redistribution and use in source and binary forms, with or without
 
8
 * modification, are permitted provided that the following conditions
 
9
 * are met:
 
10
 *
 
11
 * 1. Redistributions of source code must retain the above copyright
 
12
 *    notice, this list of conditions and the following disclaimer.
 
13
 *
 
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
 
17
 *    distribution.
 
18
 *
 
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.
 
25
 *
 
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.
 
30
 *
 
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.
 
34
 *
 
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
 
46
 * SUCH DAMAGE.
 
47
 * ====================================================================
 
48
 *
 
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/>.
 
53
 */
 
54
 
 
55
package org.apache.tools.ant;
 
56
 
 
57
import junit.framework.TestCase;
 
58
import junit.framework.AssertionFailedError;
 
59
import java.io.File;
 
60
import java.util.*;
 
61
 
 
62
/**
 
63
 * JUnit 3 testcases for org.apache.tools.ant.IntrospectionHelper.
 
64
 *
 
65
 * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> 
 
66
 */
 
67
 
 
68
public class IntrospectionHelperTest extends TestCase {
 
69
 
 
70
    private Project p;
 
71
 
 
72
    public static boolean isUnixStyle = File.pathSeparatorChar == ':';
 
73
 
 
74
    public IntrospectionHelperTest(String name) {
 
75
        super(name);
 
76
    }
 
77
    
 
78
    public void setUp() {
 
79
        p = new Project();
 
80
        p.setBasedir("/tmp");
 
81
    }
 
82
 
 
83
    public void testAddText() throws BuildException {
 
84
        IntrospectionHelper ih = IntrospectionHelper.getHelper(java.lang.String.class);
 
85
        try {
 
86
            ih.addText(p, "", "test");
 
87
            fail("String doesn\'t support addText");
 
88
        } catch (BuildException be) {
 
89
        }
 
90
 
 
91
        ih = IntrospectionHelper.getHelper(getClass());
 
92
        ih.addText(p, this, "test");
 
93
        try {
 
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);
 
98
        }
 
99
    }
 
100
 
 
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());
 
107
    }
 
108
 
 
109
    public void addText(String text) {
 
110
        assertEquals("test", text);
 
111
    }
 
112
    
 
113
    public void testElementCreators() throws BuildException {
 
114
        IntrospectionHelper ih = IntrospectionHelper.getHelper(getClass());
 
115
        try {
 
116
            ih.getElementType("one");
 
117
            fail("don't have element type one");
 
118
        } catch (BuildException be) {
 
119
        }
 
120
        try {
 
121
            ih.getElementType("two");
 
122
            fail("createTwo takes arguments");
 
123
        } catch (BuildException be) {
 
124
        }
 
125
        try {
 
126
            ih.getElementType("three");
 
127
            fail("createThree returns void");
 
128
        } catch (BuildException be) {
 
129
        }
 
130
        try {
 
131
            ih.getElementType("four");
 
132
            fail("createFour returns array");
 
133
        } catch (BuildException be) {
 
134
        }
 
135
        try {
 
136
            ih.getElementType("five");
 
137
            fail("createFive returns primitive type");
 
138
        } catch (BuildException be) {
 
139
        }
 
140
        assertEquals(java.lang.String.class, ih.getElementType("six"));
 
141
        assertEquals("test", ih.createElement(p, this, "six"));
 
142
 
 
143
        try {
 
144
            ih.getElementType("seven");
 
145
            fail("addSeven takes two arguments");
 
146
        } catch (BuildException be) {
 
147
        }
 
148
        try {
 
149
            ih.getElementType("eight");
 
150
            fail("addEight takes no arguments");
 
151
        } catch (BuildException be) {
 
152
        }
 
153
        try {
 
154
            ih.getElementType("nine");
 
155
            fail("nine return non void");
 
156
        } catch (BuildException be) {
 
157
        }
 
158
        try {
 
159
            ih.getElementType("ten");
 
160
            fail("addTen takes array argument");
 
161
        } catch (BuildException be) {
 
162
        }
 
163
        try {
 
164
            ih.getElementType("eleven");
 
165
            fail("addTen takes primitive argument");
 
166
        } catch (BuildException be) {
 
167
        }
 
168
        try {
 
169
            ih.getElementType("twelve");
 
170
            fail("no primitive constructor for java.lang.Class");
 
171
        } catch (BuildException be) {
 
172
        }
 
173
        assertEquals(java.lang.StringBuffer.class, ih.getElementType("thirteen"));
 
174
        assertEquals("test", ih.createElement(p, this, "thirteen").toString());
 
175
 
 
176
        try {
 
177
            ih.createElement(p, this, "fourteen");
 
178
            fail("fourteen throws NullPointerException");
 
179
        } catch (BuildException be) {
 
180
            assert(be.getException() instanceof NullPointerException);
 
181
        }
 
182
 
 
183
        try {
 
184
            ih.createElement(p, this, "fourteen");
 
185
            fail("fifteen throws NullPointerException");
 
186
        } catch (BuildException be) {
 
187
            assert(be.getException() instanceof NullPointerException);
 
188
        }
 
189
    }
 
190
    
 
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?",
 
203
                          expect);
 
204
            assertEquals("Return type of "+name, expect, ih.getElementType(name));
 
205
            h.remove(name);
 
206
        }
 
207
        assert("Found all", h.isEmpty());
 
208
    }
 
209
 
 
210
    public Object createTwo(String s) {
 
211
        return null;
 
212
    }
 
213
 
 
214
    public void createThree() {}
 
215
 
 
216
    public Object[] createFour() {
 
217
        return null;
 
218
    }
 
219
 
 
220
    public int createFive() {
 
221
        return 0;
 
222
    }
 
223
 
 
224
    public String createSix() {
 
225
        return "test";
 
226
    }
 
227
 
 
228
    public StringBuffer createFifteen() {
 
229
        throw new NullPointerException();
 
230
    }
 
231
 
 
232
    public void addSeven(String s, String s2) {}
 
233
 
 
234
    public void addEight() {}
 
235
 
 
236
    public String addNine(String s) {
 
237
        return null;
 
238
    }
 
239
 
 
240
    public void addTen(String[] s) {}
 
241
 
 
242
    public void addEleven(int i) {}
 
243
 
 
244
    public void addTwelve(Class c) {}
 
245
 
 
246
    public void addThirteen(StringBuffer sb) {
 
247
        sb.append("test");
 
248
    }
 
249
    
 
250
    public void addFourteen(StringBuffer s) {
 
251
        throw new NullPointerException();
 
252
    }
 
253
 
 
254
    public void testAttributeSetters() throws BuildException {
 
255
        IntrospectionHelper ih = IntrospectionHelper.getHelper(getClass());
 
256
        try {
 
257
            ih.setAttribute(p, this, "one", "test");
 
258
            fail("setOne doesn't exist");
 
259
        } catch (BuildException be) {
 
260
        }
 
261
        try {
 
262
            ih.setAttribute(p, this, "two", "test");
 
263
            fail("setTwo returns non void");
 
264
        } catch (BuildException be) {
 
265
        }
 
266
        try {
 
267
            ih.setAttribute(p, this, "three", "test");
 
268
            fail("setThree takes no args");
 
269
        } catch (BuildException be) {
 
270
        }
 
271
        try {
 
272
            ih.setAttribute(p, this, "four", "test");
 
273
            fail("setFour takes two args");
 
274
        } catch (BuildException be) {
 
275
        }
 
276
        try {
 
277
            ih.setAttribute(p, this, "five", "test");
 
278
            fail("setFive takes array arg");
 
279
        } catch (BuildException be) {
 
280
        }
 
281
        try {
 
282
            ih.setAttribute(p, this, "six", "test");
 
283
            fail("Project doesn't have a String constructor");
 
284
        } catch (BuildException be) {
 
285
        }
 
286
        ih.setAttribute(p, this, "seven", "2");
 
287
        try {
 
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);
 
292
        }
 
293
        ih.setAttribute(p, this, "eight", "2");
 
294
        try {
 
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);
 
299
        }
 
300
        ih.setAttribute(p, this, "nine", "2");
 
301
        try {
 
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);
 
306
        }
 
307
        ih.setAttribute(p, this, "ten", "2");
 
308
        try {
 
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);
 
313
        }
 
314
        ih.setAttribute(p, this, "eleven", "2");
 
315
        try {
 
316
            ih.setAttribute(p, this, "eleven", "on");
 
317
            fail("on shouldn't be false");
 
318
        } catch (BuildException be) {
 
319
            assert(be.getException() instanceof AssertionFailedError);
 
320
        }
 
321
        ih.setAttribute(p, this, "twelve", "2");
 
322
        try {
 
323
            ih.setAttribute(p, this, "twelve", "on");
 
324
            fail("on shouldn't be false");
 
325
        } catch (BuildException be) {
 
326
            assert(be.getException() instanceof AssertionFailedError);
 
327
        }
 
328
        ih.setAttribute(p, this, "thirteen", "org.apache.tools.ant.Project");
 
329
        try {
 
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);
 
334
        }
 
335
        try {
 
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);
 
340
        }
 
341
        ih.setAttribute(p, this, "fourteen", "2");
 
342
        try {
 
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);
 
347
        }
 
348
        ih.setAttribute(p, this, "fifteen", "abcd");
 
349
        try {
 
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);
 
354
        }
 
355
        ih.setAttribute(p, this, "sixteen", "abcd");
 
356
        try {
 
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);
 
361
        }
 
362
    }
 
363
 
 
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);
 
376
 
 
377
        /*
 
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.
 
381
         *
 
382
         * Simply add it here and remove it after the tests.
 
383
         */
 
384
        h.put("name", java.lang.String.class);
 
385
 
 
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?",
 
392
                          expect);
 
393
            assertEquals("Type of "+name, expect, ih.getAttributeType(name));
 
394
            h.remove(name);
 
395
        }
 
396
        h.remove("name");
 
397
        assert("Found all", h.isEmpty());
 
398
    }
 
399
 
 
400
    public int setTwo(String s) {
 
401
        return 0;
 
402
    }
 
403
 
 
404
    public void setThree() {}
 
405
 
 
406
    public void setFour(String s1, String s2) {}
 
407
 
 
408
    public void setFive(String[] s) {}
 
409
 
 
410
    public void setSix(Project p) {}
 
411
 
 
412
    public void setSeven(String s) {
 
413
        assertEquals("2", s);
 
414
    }
 
415
 
 
416
    public void setEight(int i) {
 
417
        assertEquals(2, i);
 
418
    }
 
419
 
 
420
    public void setNine(Integer i) {
 
421
        assertEquals(2, i.intValue());
 
422
    }
 
423
 
 
424
    public void setTen(File f) {
 
425
        if (isUnixStyle) { 
 
426
            assertEquals("/tmp/2", f.getAbsolutePath());
 
427
        } else {
 
428
            assertEquals(":\\tmp\\2", f.getAbsolutePath().toLowerCase().substring(1));
 
429
        }
 
430
    }
 
431
 
 
432
    public void setEleven(boolean b) {
 
433
        assert(!b);
 
434
    }
 
435
 
 
436
    public void setTwelve(Boolean b) {
 
437
        assert(!b.booleanValue());
 
438
    }
 
439
 
 
440
    public void setThirteen(Class c) {
 
441
        assertEquals(Project.class, c);
 
442
    }
 
443
 
 
444
    public void setFourteen(StringBuffer sb) {
 
445
        assertEquals("2", sb.toString());
 
446
    }
 
447
 
 
448
    public void setFifteen(char c) {
 
449
        assertEquals(c, 'a');
 
450
    }
 
451
 
 
452
    public void setSixteen(Character c) {
 
453
        assertEquals(c.charValue(), 'a');
 
454
    }
 
455
 
 
456
}// IntrospectionHelperTest