File: constructorTest2.java

package info (click to toggle)
mauve 20080616-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 26,856 kB
  • ctags: 21,952
  • sloc: java: 234,107; sh: 2,834; xml: 208; makefile: 59
file content (704 lines) | stat: -rw-r--r-- 20,442 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
// Tags: JDK1.1

//Copyright (C) 1998 Anthony Green <green@cygnus.com>
//Copyright (C) 1998 Tom Tromey <tromey@cygnus.com>
//Copyright (C) 2004 Robert Schuster <thebohemian@gmx.net>

//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.

//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING.  If not, write to
//the Free Software Foundation, 59 Temple Place - Suite 330,
//Boston, MA 02111-1307, USA.  */

package gnu.testlet.java.beans.PropertyDescriptor;
import gnu.testlet.Testlet;
import gnu.testlet.TestHarness;
import java.beans.PropertyDescriptor;
import java.beans.IntrospectionException;
import java.lang.reflect.Method;

/**
 * 
 * @author Anthony Green <green@cygnus.com>
 * @author Tom Tromey <tromey@cygnus.com>
 * @author Robert Schuster <thebohemian@gmx.net>
 */
public class constructorTest2 implements Testlet
{
    /** Read method of <code>int</code> property "prop1".
     * 
     * @return 0.
     */
    public int getProp1()
    {
        return 0;
    }

    /** Write method of <code>int</code> property "prop1".
     * 
     * @param val Unused.
     */
    public void setProp1(int val)
    {}

    /** Read method of <code>boolean</code> property "prop2".
     *
     * This is the "get" variant of a <code>boolean</code> getter. 
     * 
     * @return <code>false</code>.
     */
    public boolean getProp2()
    {
        return false;
    }

    /** Write method of <code>boolean</code> property "prop2".
     * 
     * @param val Unused.
     */
    public void setProp2(boolean val)
    {}

    /** Read method of <code>boolean</code> property "prop3".
     *
     * This is the "is" variant of a <code>boolean</code> getter. 
     * 
     * @return <code>false</code>.
     */
    public boolean isProp3()
    {
        return false;
    }

    /** Write method of <code>boolean</code> property "prop3".
     * 
     * @param val Unused.
     */
    public void setProp3(boolean val)
    {}

    /** Read method of <code>int</code> property "prop4".
     *
     * This demonstrates an "is" variant of a <code>int</code> getter,
     * which is allowed in Sun's implementation of <code>PropertyDescriptor</code> 
     * 
     * @return <code>0</code>.
     */
    public int isProp4()
    {
        return 0;
    }

    /** Write method of <code>int</code> property "prop4".
     * 
     * @param val Unused.
     */
    public void setProp4(int val)
    {}

    /** Read method of <code>boolean</code> property "prop5".<br/>
     * <br>
     * This is the "is" variant of the "getter". The "get" variant exists, too.
     * Sun's implementation of <code>PropertyDescriptor</code> allows that.<br/>
     * <br/> 
     * Note: The difference to "prop6" is the order in which the methods are declared
     * in the source code.
     * 
     * @return <code>false</code>.
     */
    public boolean isProp5()
    {
        return false;
    }

    /** Read method of <code>boolean</code> property "prop5".<br/>
     * <br>
     * This is the "get" variant of the "getter". The "is" variant exists, too.
     * Sun's implementation of <code>PropertyDescriptor</code> allows that.<br/>
     * <br/> 
     * Note: The difference to "prop6" is the order in which the methods are declared
     * in the source code.
     * 
     * @return <code>false</code>.
     */
    public boolean getProp5()
    {
        return false;
    }

    /** Write method of <code>boolean</code> property "prop5".
     * 
     * @param val Unused.
     */
    public void setProp5(boolean val)
    {}

    /** Read method of <code>boolean</code> property "prop6".<br/>
     * <br>
     * This is the "get" variant of the "getter". The "is" variant exists, too.
     * Sun's implementation of <code>PropertyDescriptor</code> allows that.<br/>
     * <br/> 
     * Note: The difference to "prop5" is the order in which the methods are declared
     * in the source code.
     * 
     * @return <code>false</code>.
     */
    public boolean getProp6()
    {
        return false;
    }

    /** Read method of <code>boolean</code> property "prop6".<br/>
     * <br>
     * This is the "is" variant of the "getter". The "get" variant exists, too.
     * Sun's implementation of <code>PropertyDescriptor</code> allows that.<br/>
     * <br/> 
     * Note: The difference to "prop5" is the order in which the methods are declared
     * in the source code.
     * 
     * @return <code>false</code>.
     */
    public boolean isProp6()
    {
        return false;
    }

    /** Write method of <code>boolean</code> property "prop6".
     * 
     * @param val Unused.
     */
    public void setProp6(boolean val)
    {}

    /** <code>boolean</code> read method of property "prop7" that
     * has multiple "setter" or write methods with different argument types:
     * {@link setProp7(boolean)} and {@link setProp(int)}.<br/>
     * <br>
     * Sun's implementation of <code>PropertyDescriptor</code> allows that.<br/>
     * 
     * @return <code>false</code>.
     */
    public boolean getProp7()
    {
        return false;
    }

    /** <code>boolean</code> write method of property "prop7" that
     * has multiple "setter" or write methods with different argument types.
     * See {@link setProp(int)} for the other write method.<br/>
     * <br>
     * Sun's implementation of <code>PropertyDescriptor</code> allows that.<br/>
     * 
     * @param val Unused.
     */
    public void setProp7(boolean val)
    {}

    /** <code>int</code> write method of property "prop7" that
     * has multiple "setter" or write methods with different argument types.
     * See {@link setProp(boolean)} for the other write method.<br/>
     * <br>
     * Sun's implementation of <code>PropertyDescriptor</code> allows that.<br/>
     * 
     * @param val Unused.
     */
    public void setProp7(int val)
    {}

    /** Read method of <code>boolean</code> property "error1".
     * This is a read-only property which is not allowed when using the
     * <code>(String, Class)</code> constructor of <code>PropertyDescriptor</code>.<br/>
     * <br/>
     * This is the "get" variant of a read method.
     * 
     * @return <code>false</code>.
     */
    public boolean getError1()
    {
        return false;
    }

    /** Read method of <code>boolean</code> property "error2".
     * This is a read-only property which is not allowed when using the
     * <code>(String, Class)</code> constructor of <code>PropertyDescriptor</code>.<br/>
     * <br/>
     * This is the "is" variant of a read method.
     * 
     * @return <code>false</code>.
     */
    public boolean isError2()
    {
        return false;
    }

    /** Write method of <code>boolean</code> property "error3".
     * This is a write-only property which is not allowed when using the
     * <code>(String, Class)</code> constructor of <code>PropertyDescriptor</code>.<br/>
     * <br/>
     * 
     * @param val Unused.
     */
    public void setError3(boolean val)
    {}

    /** Invalid read method of property "error4". It is illegal
     * because read methods are not allowed to have a <code>void</code>
     * return type.<br/>
     * <br/>
     * This method is the "get" variant of a read method. 
     */
    public void getError4()
    {}

    /** Write method of erroneous property "error4".
     * 
     * @param val Unused.
     */
    public void setError4(int val)
    {}

    /** Invalid read method of property "error5". It is illegal
     * because read methods are not allowed to have a <code>void</code>
     * return type.<br/>
     * <br/>
     * This method is the "is" variant of a read method. 
     */
    public void isError5()
    {}

    /** Write method of erroneous property "error5".
     * 
     * @param val Unused.
     */
    public void setError5(int val)
    {}

    /** Invalid read method of property "error6". It is illegal
     * because its return type is not compatible to the write method's 
     * argument type.<br/>
     * <br/>
     * This method is the "get" variant of a read method.
     * 
     * @return 0. 
     */
    public int getError6()
    {
        return 0;
    }

    /** Invalid write method of property "error6". It is illegal
     * because its argument type is not compatible to the read method's 
     * return type.<br/>
     * <br/>
     * 
     * @param val Unused. 
     */
    public void setError6(boolean val)
    {}

    /** Invalid read method of property "error7". It is illegal
     * because its return type is not compatible to the write method's 
     * argument type.<br/>
     * <br/>
     * This method is the "is" variant of a read method.
     * 
     * @return 0. 
     */
    public boolean isError7()
    {
        return false;
    }

    /** Invalid write method of property "error7". It is illegal
     * because its argument type is not compatible to the read method's 
     * return type.<br/>
     * <br/>
     * 
     * @param val Unused. 
     */
    public void setError7(int val)
    {}

    /** Invalid read method of property "error8". It is illegal
     * because its return type is not compatible to the write method's 
     * argument type. This demonstrates a type mismatch with non-primitives.<br/>
     * <br/>
     * This method is the "get" variant of a read method.
     *
     * XXX: I doubt this is legal because the return type of the read method is assignable
     * from the write method's argument type.
     *  
     * @return 0. 
     */
    public Object getError8()
    {
        return null;
    }

    /** Invalid write method of property "error8". It is illegal
    * because its argument type is not compatible to the read method's 
    * return type. This demonstrates a type mismatch with non-primitives.<br/>
    * <br/>
    * 
    * @param val Unused. 
    */
    public void setError8(String val)
    {}

	/** Invalid read method of property "error9". It is illegal
	 * because its return type is not compatible to the write method's 
	 * argument type. This demonstrates a type mismatch with non-primitives.<br/>
	 * <br/>
	 * This method is the "get" variant of a read method.
	 *  
	 * @return 0. 
	 */
    public String getError9()
    {
        return null;
    }

	/** Invalid write method of property "error9". It is illegal
	* because its argument type is not compatible to the read method's 
	* return type. This demonstrates a type mismatch with non-primitives.<br/>
	* <br/>
	* 
	* @param val Unused. 
	*/
    public void setError9(Object val)
    {}

	/** Read method of <code>boolean</code> property "error10".
	 * The property is invalid because its write method does not accept
	 * a single argument.
	 * 
	 * @return <code>false</code>.
	 */
    public boolean isError10()
    {
        return false;
    }
    
	/** Invalid write method of property "error10". It is illegal
	* because it does not accept a single argument.
	*/
    public void setError10()
    {}

	/** Read method of <code>boolean</code> property "error11".
	 * The property is invalid because its read method accepts an argument.<br/>
	 * <br>
	 * This is the "get" variant of a read method.
	 *
	 * @param arg Unused. 
	 * @return <code>false</code>.
	 */
    public boolean getError11(int arg)
    {
        return false;
    }
    
    /** Write method of <code>boolean</code> property "error11".
     * The property is invalid because of its read method.
     * 
     * @param val Unused.
     */
    public void setError11(boolean val)
    {}

	/** Read method of <code>boolean</code> property "error12".
	 * The property is invalid because its read method accepts an argument.<br/>
	 * <br>
	 * This is the "is" variant of a read method.
	 *
	 * @param arg Unused. 
	 * @return <code>false</code>.
	 */
    public boolean isError12(int arg)
    {
        return false;
    }

	/** Write method of <code>boolean</code> property "error12".
	 * The property is invalid because of its read method.
	 * 
	 * @param val Unused.
	 */
    public void setError12(boolean val)
    {}

	/** Read method of <code>boolean</code> property "error13".
	 * The property is invalid because of its write method.
	 * <br>
	 *
	 * @return <code>false</code>.
	 */
    public boolean isError13()
    {
        return false;
    }
    
	/** Write method of <code>boolean</code> property "error13".
	 * The property is invalid because this method accepts an
	 * extra argument.<br/>
	 * 
	 * @param val Unused.
	 * @param arg Unused.
	 */
    public void setError13(boolean val, int arg)
    {}

	/** Read method of illegally named <code>Object</code> property.
	 * 
	 * @return <code>null</code>.
	 */
    public Object get()
    {
        return null;
    }

	/** Write method of illegally named <code>Object</code> property.
	 * 
	 * @return <code>null</code>.
	 */
    public void set(Object val)
    {}

	/** Read method of illegally named <code>Object</code> property.
	 * The method name does not respect the requirements of a JavaBean.
	 * 
	 * @return <code>null</code>.
	 */
    public Object getknotted()
    {
        return null;
    }

	/** Write method of illegally named <code>Object</code> property.
	 * The method name does not respect the requirements of a JavaBean.
	 * 
	 * @param val Unused.
	 */
    public void setknotted(Object val)
    {}

    // Test the PropertyDescriptor(Class, String) constructor
    private void constructor1Tests(TestHarness harness)
    {
        String[] goodProperties =
            { "prop1", "prop2", "prop3", "prop4", "prop5", "prop6", "prop7" };
        for (int i = 0; i < goodProperties.length; i++)
        {
            harness.checkPoint(
                "new PropertyDescriptor("
                    + goodProperties[i]
                    + ") (should succeed)");
            PropertyDescriptor pd;
            try
            {
                pd =
                    new PropertyDescriptor(
                        goodProperties[i],
                        constructorTest2.class);
                harness.check(true);
            }
            catch (IntrospectionException e)
            {
                harness.debug(e);
                harness.check(false);
                continue;
            }
            harness.check(pd.getReadMethod() != null);
            harness.check(pd.getWriteMethod() != null);
        }

        harness.checkPoint("string constructor - binding");
        String[] bindProperties = { "prop5", "prop6" };
        for (int i = 0; i < bindProperties.length; i++)
        {
            harness.checkPoint(
                "new PropertyDescriptor("
                    + bindProperties[i]
                    + ") (check bindings)");
            try
            {
                PropertyDescriptor pd =
                    new PropertyDescriptor(
                        bindProperties[i],
                        constructorTest2.class);

                Class propType = pd.getPropertyType();

                Method readMethod = pd.getReadMethod();

                harness.check(readMethod != null);

                if (readMethod == null)
                {
                    continue;
                }

                harness.check(readMethod.getName().startsWith("is"));
                Class resType = readMethod.getReturnType();
                harness.check(resType.equals(propType));

                Method writeMethod = pd.getWriteMethod();
                harness.check(writeMethod != null);

                if (writeMethod == null)
                {
                    continue;
                }

                Class argType = writeMethod.getParameterTypes()[0];
                harness.check(argType.equals(propType));

            }
            catch (IntrospectionException e)
            {}

        }

        String[] badProperties =
            {
                "",
                "foo",
                "knotted",
                "???",
                "error1",
                "error2",
                "error3",
                "error4",
                "error5",
                "error6",
                "error7",
                "error8",
                "error9",
                "error10",
                "error11",
                "error12",
                "error13" };

        for (int i = 0; i < badProperties.length; i++)
        {
            harness.checkPoint(
                "new PropertyDescriptor("
                    + badProperties[i]
                    + ") (should fail)");
            boolean ok = false;
            try
            {
                new PropertyDescriptor(
                    badProperties[i],
                    constructorTest2.class);
            }
            catch (IntrospectionException e)
            {
                ok = true;
            }
            harness.check(ok);
        }
    }

    private void constructor2Tests(TestHarness harness)
    {
        String[][] goodProperties = { { "prop1", "getProp1", "setProp1" }, {
                "prop2", "getProp2", "setProp2" }, {
                "prop3", "isProp3", "setProp3" }, {
                "prop4", "isProp4", "setProp4" }, {
                "prop5", "isProp5", "setProp5" }, {
                "prop5", "getProp5", "setProp5" }, {
                "prop6", "isProp6", "setProp6" }, {
                "prop6", "getProp6", "setProp6" }, {
                "prop7", "getProp7", "setProp7" }, {
                "prop7", "getProp7", null }, {
                "prop7", null, "setProp7" }, {
                "prop7", null, null }, };

        for (int i = 0; i < goodProperties.length; i++)
        {
            harness.checkPoint(
                "new PropertyDescriptor("
                    + goodProperties[i][0]
                    + ", "
                    + goodProperties[i][1]
                    + ", "
                    + goodProperties[i][2]
                    + ") (should succeed)");

            boolean ok = true;

            try
            {
                new PropertyDescriptor(
                    goodProperties[i][0],
                    constructorTest2.class,
                    goodProperties[i][1],
                    goodProperties[i][2]);
            }
            catch (IntrospectionException e)
            {
                harness.debug(e);
                ok = false;
            }
            harness.check(ok);
        }

        String[][] badProperties = { { "foo", "getFoo", "setFoo" }, {
                "foo2", null, "setFoo2" }, {
                "foo3", "getFoo3", null }, {
                "bar", "", "" }, {
                "error4", "getError4", "setError4" }, {
                "error5", "isError5", "setError5" }, {
                "error6", "getError6", "setError6" }, {
                "error7", "isError7", "setError7" }, {
                "error8", "getError8", "setError8" }, {
                "error9", "getError9", "setError9" }, {
                "error10", "isError10", "setError10" }, {
                "error11", "getError11", "setError11" }, {
                "error12", "isError12", "setError12" }, {
                "error13", "isError13", "setError13" }, };

        for (int i = 0; i < badProperties.length; i++)
        {
            harness.checkPoint(
                "new PropertyDescriptor("
                    + badProperties[i][0]
                    + ", "
                    + badProperties[i][1]
                    + ", "
                    + badProperties[i][2]
                    + ") (should fail)");
            boolean ok = false;
            try
            {
                new PropertyDescriptor(
                    badProperties[i][0],
                    constructorTest2.class,
                    badProperties[i][1],
                    badProperties[i][2]);
            }
            catch (IntrospectionException e)
            {
                ok = true;
            }
            harness.check(ok);
        }
    }

    public void test(TestHarness harness)
    {
        constructor1Tests(harness);
        constructor2Tests(harness);
    }

}