File: InterfaceTestCaseUTest.java

package info (click to toggle)
libgroboutils-java 5-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 8,496 kB
  • sloc: java: 59,880; xml: 12,762; sh: 377; perl: 104; makefile: 20
file content (461 lines) | stat: -rw-r--r-- 13,153 bytes parent folder | download | duplicates (3)
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
/*
 * @(#)InterfaceTestCaseUTest.java
 *
 * Copyright (C) 2002-2003 Matt Albrecht
 * groboclown@users.sourceforge.net
 * http://groboutils.sourceforge.net
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a
 *  copy of this software and associated documentation files (the "Software"),
 *  to deal in the Software without restriction, including without limitation
 *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
 *  and/or sell copies of the Software, and to permit persons to whom the
 *  Software is furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 *  DEALINGS IN THE SOFTWARE.
 */

package net.sourceforge.groboutils.junit.v1.iftc;

import org.easymock.EasyMock;
import org.easymock.MockControl;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.framework.AssertionFailedError;


/**
 * Tests the InterfaceTestCase class.
 *
 * @author    Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
 * @since     March 1, 2002
 * @version   $Date: 2003/05/29 13:05:54 $
 */
public class InterfaceTestCaseUTest extends TestCase
{
    
    //-------------------------------------------------------------------------
    // Standard JUnit Class-specific declarations
    
    private static final Class THIS_CLASS = InterfaceTestCaseUTest.class;
    private static final org.apache.log4j.Logger LOG =
        org.apache.log4j.Logger.getLogger( InterfaceTestCaseUTest.class );
    
    public InterfaceTestCaseUTest( String name )
    {
        super( name );
    }

    


    //-------------------------------------------------------------------------
    // Tests
    
    public static class MyInterfaceTestCase extends InterfaceTestCase
    {
        public MyInterfaceTestCase( String name, Class interfaceClass,
                ImplFactory f )
        {
            super( name, interfaceClass, f );
        }
    }
    
    public void testConstructor1()
    {
        LOG.debug( "Entering testConstructor1" );
        ImplFactory f = new ImplFactory() {
            public Object createImplObject() {
                return new Runnable() { public void run() {} };
            }
        };
        InterfaceTestCase itc = new MyInterfaceTestCase( "test",
            Runnable.class, f );
        assertEquals(
            "Did not store the interface class correctly.",
            Runnable.class,
            itc.getInterfaceClass() );
        LOG.debug( "Leaving testConstructor1" );
    }
    
    
    public void testConstructor2()
    {
        LOG.debug( "Entering testConstructor2" );
        try
        {
            new MyInterfaceTestCase( "test", null, null );
            fail("Did not throw IllegalArgumentException.");
        }
        catch (IllegalArgumentException e)
        {
            // test exception?
        }
        LOG.debug( "Leaving testConstructor2" );
    }
    
    
    public void testConstructor3()
    {
        LOG.debug( "Entering testConstructor3" );
        try
        {
            new MyInterfaceTestCase( "test", Runnable.class, null );
            fail("Did not throw IllegalArgumentException.");
        }
        catch (IllegalArgumentException e)
        {
            // test exception?
        }
        LOG.debug( "Leaving testConstructor3" );
    }
    
    
    public void testConstructor4()
    {
        LOG.debug( "Entering testConstructor4" );
        ImplFactory f = new ImplFactory() {
            public Object createImplObject() {
                return new Runnable() { public void run() {} };
            }
        };
        try
        {
            new MyInterfaceTestCase( "test", null, f );
            fail("Did not throw IllegalArgumentException.");
        }
        catch (IllegalArgumentException e)
        {
            // test exception?
        }
        LOG.debug( "Leaving testConstructor4" );
    }
    
    
    public void testCreate1()
    {
        LOG.debug( "Entering testCreate1" );
        ImplFactory f = new ImplFactory() {
            public Object createImplObject() {
                return "a string";
            }
        };
        InterfaceTestCase itc = new MyInterfaceTestCase( "test",
            Runnable.class, f );
        try
        {
            itc.createImplObject();
            fail("Did not fail.");
        }
        catch (AssertionFailedError e)
        {
            // test error?
        }
        LOG.debug( "Leaving testCreate1" );
    }
    
    
    public void testCreate2()
    {
        LOG.debug( "Entering testCreate2" );
        ImplFactory f = new ImplFactory() {
            public Object createImplObject() {
                return null;
            }
        };
        InterfaceTestCase itc = new MyInterfaceTestCase( "test",
            Runnable.class, f );
        try
        {
            itc.createImplObject();
            fail("Did not fail.");
        }
        catch (AssertionFailedError e)
        {
            // test error?
        }
        LOG.debug( "Leaving testCreate2" );
    }
    
    
    public void testCreate3()
    {
        LOG.debug( "Entering testCreate3" );
        ImplFactory f = new ImplFactory() {
            public Object createImplObject() {
                throw new IllegalArgumentException("IAE");
            }
        };
        InterfaceTestCase itc = new MyInterfaceTestCase( "test",
            Runnable.class, f );
        try
        {
            itc.createImplObject();
            fail("Did not throw exception.");
        }
        catch (AssertionFailedError e)
        {
            // test error
            assertTrue(
                "Does not contain the correct exception text.",
                e.getMessage().indexOf( " threw exception "+
                    "java.lang.IllegalArgumentException: IAE during "+
                    "creation:" ) > 0 );
                
        }
    }
    
    
    final String teststring = "a string";
    public void testCreate4()
    {
        LOG.debug( "Entering testCreate4" );
        ImplFactory f = new ImplFactory() {
            public Object createImplObject() {
                return teststring;
            }
        };
        InterfaceTestCase itc = new MyInterfaceTestCase( "test",
            String.class, f );
        Object o = itc.createImplObject();
        assertSame(
            "Did not return the exact object we thought we returned.",
            teststring,
            o );
    }
    
    
    final static String factoryname = "TestFactoryName";
    public void testToString1()
    {
        LOG.debug( "Entering testToString1" );
        ImplFactory f = new ImplFactory() {
            public Object createImplObject() {
                return "";
            }
            public String toString() {
                return factoryname;
            }
        };
        InterfaceTestCase itc = new MyInterfaceTestCase( "test",
            String.class, f );
        itc.setUseClassInName( false );
        assertEquals(
            "Did not return the correct text.",
            "test["+factoryname+"]("+itc.getClass().getName()+")",
            itc.toString() );
    }
    
    
    public void testGetName1()
    {
        LOG.debug( "Entering testGetName1" );
        ImplFactory f = new ImplFactory() {
            public Object createImplObject() {
                return "";
            }
            public String toString() {
                return factoryname;
            }
        };
        InterfaceTestCase itc = new MyInterfaceTestCase( "test",
            String.class, f );
        itc.setUseClassInName( false );
        assertEquals(
            "Did not return the correct text.",
            "test["+factoryname+"]",
            itc.getName() );
    }
    
    
    public void testGetName2()
    {
        LOG.debug( "Entering testGetName1" );
        ImplFactory f = new ImplFactory() {
            public Object createImplObject() {
                return "";
            }
            public String toString() {
                return factoryname;
            }
        };
        InterfaceTestCase itc = new MyInterfaceTestCase( "test",
            String.class, f );
        itc.setUseClassInName( true );
        assertEquals(
            "Did not return the correct text.",
            "InterfaceTestCaseUTest$MyInterfaceTestCase.test["+factoryname+"]",
            itc.getName() );
    }
    
    
    public void testName1()
    {
        LOG.debug( "Entering testName1" );
        ImplFactory f = new ImplFactory() {
            public Object createImplObject() {
                return "";
            }
            public String toString() {
                return factoryname;
            }
        };
        InterfaceTestCase itc = new MyInterfaceTestCase( "test",
            String.class, f );
        itc.setUseClassInName( false );
        assertEquals(
            "Did not return the correct text.",
            itc.getName(),
            itc.name() );
    }
    
    //---------
    
    private static class MyCxFactory implements ICxFactory
    {
        public int createCount = 0;
        public int tearDownCount = 0;
        
        public Object createImplObject()
        {
            ++this.createCount;
            return new Integer( this.createCount );
        }
        
        public void tearDown( Object o )
        {
            int i = ((Integer)o).intValue();
            assertEquals(
                "Did not tear down in the right order.",
                createCount - tearDownCount,
                i );
            ++tearDownCount;
        }
    }
    
    public void testTearDown1() throws Exception
    {
        LOG.debug( "Entering testTearDown1()" );
        MyCxFactory f = new MyCxFactory();
        InterfaceTestCase itc = new MyInterfaceTestCase( "test",
            Integer.class, f );
        int instantCount = 100;
        for (int i = 0; i < instantCount; ++i)
        {
            itc.createImplObject();
        }
        itc.tearDown();
        assertEquals(
            "Did not tear down all expected instantiated objects.",
            instantCount,
            f.tearDownCount );
    }
    
    //---------
    
    private static class MyCxFactory2 implements ICxFactory
    {
        public Object createImplObject()
        {
            return new Integer( 0 );
        }
        
        public void tearDown( Object o ) throws Exception
        {
            throw new IllegalStateException();
        }
    }
    
    public void testTearDown2() throws Exception
    {
        LOG.debug( "Entering testTearDown2()" );
        MyCxFactory2 f = new MyCxFactory2();
        InterfaceTestCase itc = new MyInterfaceTestCase( "test",
            Integer.class, f );
        int instantCount = 100;
        for (int i = 0; i < instantCount; ++i)
        {
            itc.createImplObject();
        }
        try
        {
            itc.tearDown();
            fail( "tearDown did not propigate any exceptions to the top." );
        }
        catch (AssertionFailedError ex)
        {
            String s = ex.toString();
            int count = -1;
            int pos = 0;
            while (pos >= 0)
            {
                ++count;
                pos = s.indexOf( IllegalStateException.class.getName(),
                    pos+1 );
            }
            assertEquals(
                "Did not catch or report all exceptions.",
                instantCount,
                count );
        }
    }
    
    
    
    //-------------------------------------------------------------------------
    // Standard JUnit declarations
    
    
    public static Test suite()
    {
        TestSuite suite = new TestSuite( THIS_CLASS );
        
        return suite;
    }
    
    public static void main( String[] args )
    {
        String[] name = { THIS_CLASS.getName() };
        
        // junit.textui.TestRunner.main( name );
        // junit.swingui.TestRunner.main( name );
        
        junit.textui.TestRunner.main( name );
    }
    
    
    /**
     * 
     * @exception Exception thrown under any exceptional condition.
     */
    protected void setUp() throws Exception
    {
        super.setUp();
        
        // set ourself up
    }
    
    
    /**
     * 
     * @exception Exception thrown under any exceptional condition.
     */
    protected void tearDown() throws Exception
    {
        // tear ourself down
        
        
        super.tearDown();
    }
}