KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > util > ClassUtilsTest


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: ClassUtilsTest.java,v 1.3 2007/01/07 06:14:55 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.core.util;
23
24 import junit.framework.TestCase;
25
26 import org.opensubsystems.core.error.OSSDynamicClassException;
27
28 /**
29  * Tests for common operations with classes.
30  *
31  * @version $Id: ClassUtilsTest.java,v 1.3 2007/01/07 06:14:55 bastafidli Exp $
32  * @author Miro Halas
33  * @code.reviewer Miro Halas
34  * @code.reviewed Initial revision
35  */

36 public class ClassUtilsTest extends TestCase
37 {
38    /**
39     * Constructor for ClassUtilsTest.
40     * @param arg0 - name of the test
41     */

42    public ClassUtilsTest(
43       String JavaDoc arg0
44    )
45    {
46       super(arg0);
47    }
48
49    /**
50     * Set up environment for the test case.
51     *
52     * @throws Exception - an error has occured during setting up test
53     */

54    protected void setUp(
55    ) throws Exception JavaDoc
56    {
57       super.setUp();
58    }
59
60    /**
61     * Restore original environment after the test case.
62     *
63     * @throws Exception - an error has occured during tearing down up test
64     */

65    protected void tearDown() throws Exception JavaDoc
66    {
67       super.tearDown();
68    }
69
70    /**
71     * Test createNewInstance method with Class parameter
72     *
73     * @throws Exception - and error has occured
74     */

75    public void testCreateNewInstanceWithClass(
76    ) throws Exception JavaDoc
77    {
78       Object JavaDoc test;
79       Object JavaDoc test2;
80       
81       // First instantiate class which should be instantiated
82
test = ClassUtils.createNewInstance(String JavaDoc.class);
83       assertNotNull("Cannot instantiate String class", test);
84       
85       // Make sure that when instantiating two classes, the instance is not the same
86
test2 = ClassUtils.createNewInstance(String JavaDoc.class);
87       assertNotNull("Cannot instantiate String class", test2);
88       assertTrue("Cannot create the same instance twice", test != test2);
89       
90       // Now try to instantiate class which has no public constructor
91
try
92       {
93          test = ClassUtils.createNewInstance(java.io.File JavaDoc.class);
94          fail("Instantiated class with no public constructor");
95       }
96       catch (OSSDynamicClassException dceExc)
97       {
98          // This is expected exception so ignore it
99
}
100    }
101
102    /**
103     * Test createNewInstance method with String parameter
104     *
105     * @throws Exception - and error has occured
106     */

107    public void testCreateNewInstanceWithString(
108    ) throws Exception JavaDoc
109    {
110       Object JavaDoc test;
111       Object JavaDoc test2;
112       
113       // First instantiate class which should be instantiated
114
test = ClassUtils.createNewInstance("java.lang.String");
115       assertNotNull("Cannot instantiate String class", test);
116       
117       // Make sure that when instantiating two classes, the instance is not the same
118
test2 = ClassUtils.createNewInstance("java.lang.String");
119       assertNotNull("Cannot instantiate String class", test2);
120       assertTrue("Cannot create the same instance twice", test != test2);
121
122       // Now try to instantiate class which has no public constructor
123
try
124       {
125          test = ClassUtils.createNewInstance("java.io.File");
126          fail("Instantiated class with no public constructor");
127       }
128       catch (OSSDynamicClassException dceExc)
129       {
130          // This is expected exception so ignore it
131
}
132
133       // Now try to instantiate class which doesn't exist
134
try
135       {
136          test = ClassUtils.createNewInstance("class.which.doesnt.exist");
137          fail("Instantiated class which doesn't exist");
138       }
139       catch (OSSDynamicClassException dceExc)
140       {
141          // This is expected exception so ignore it
142
}
143    }
144 }
145
Popular Tags