001: /**********************************************************************
002: Copyright (c) 26-Oct-2004 Andy Jefferson and others.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015:
016:
017: Contributors:
018: ...
019: **********************************************************************/package org.jpox.util;
020:
021: import java.net.MalformedURLException;
022: import java.util.AbstractCollection;
023: import java.util.AbstractList;
024: import java.util.Collection;
025:
026: import javax.jdo.PersistenceManager;
027: import javax.jdo.spi.PersistenceCapable;
028: import javax.jdo.spi.StateManager;
029:
030: import junit.framework.TestCase;
031:
032: import org.apache.log4j.Category;
033: import org.jpox.ClassLoaderResolver;
034: import org.jpox.JDOClassLoaderResolver;
035:
036: /**
037: * Tests for class utilities.
038: * @version $Revision: 1.8 $
039: */
040: public class ClassUtilsTest extends TestCase {
041: protected static final Category LOG = Category
042: .getInstance("JPOX.TEST");
043:
044: /**
045: * Start of the test, so log it and initialise.
046: * @param name Name of the test case (not used)
047: */
048: public ClassUtilsTest(String name) {
049: super (name);
050: }
051:
052: /**
053: * Test of Boolean object retrieval.
054: */
055: public void testBooleanValueOf() {
056: assertTrue("True value was incorrect", ClassUtils
057: .booleanValueOf(true).booleanValue());
058: assertTrue("False value was incorrect", !ClassUtils
059: .booleanValueOf(false).booleanValue());
060: }
061:
062: /**
063: * Test of whether a class is an inner class
064: */
065: public void testIsInnerClass() {
066: assertTrue("Should have identified inner class, but failed",
067: ClassUtils.isInnerClass("java.text.DateFormat$Field"));
068: }
069:
070: /**
071: * Test of whether a class has a default constructor
072: */
073: public void testHasDefaultConstructor() {
074: assertTrue(
075: "Class java.lang.String should have been identified as having a default constructor, but failed",
076: ClassUtils
077: .hasDefaultConstructor(java.lang.String.class));
078: assertTrue(
079: "Class java.sql.DriverPropertyInfo should have been identified as not having a default constructor, but passed",
080: !ClassUtils
081: .hasDefaultConstructor(java.sql.DriverPropertyInfo.class));
082: }
083:
084: /**
085: * Test for the superclasses of a class.
086: */
087: public void testGetSuperclasses() {
088: Collection super classes = ClassUtils
089: .getSuperclasses(java.util.ArrayList.class);
090: assertTrue(
091: "java.util.ArrayList should have had 3 superclasses, but had "
092: + super classes.size(), super classes.size() == 3);
093: assertTrue(
094: "java.util.ArrayList should have had a superclass of AbstractList, but didn't !",
095: super classes.contains(AbstractList.class));
096: assertTrue(
097: "java.util.ArrayList should have had a superclass of AbstractCollection, but didn't !",
098: super classes.contains(AbstractCollection.class));
099: assertTrue(
100: "java.util.ArrayList should have had a superclass of Object, but didn't !",
101: super classes.contains(Object.class));
102: }
103:
104: /**
105: * Test for the creation of a fully specified class name.
106: */
107: public void testCreateFullClassName() {
108: assertTrue("Full classname is incorrect", ClassUtils
109: .createFullClassName("org.jpox.samples", "MyClass")
110: .equals("org.jpox.samples.MyClass"));
111: assertTrue("Full classname is incorrect", ClassUtils
112: .createFullClassName(" ", "MyClass").equals(
113: "MyClass"));
114: assertTrue("Full classname is incorrect", ClassUtils
115: .createFullClassName("org", "MyClass").equals(
116: "org.MyClass"));
117: }
118:
119: /**
120: * Test of the retrieval of the classname for a filename, given the root directory name.
121: * @throws MalformedURLException
122: */
123: public void testGetClassnameForFilename()
124: throws MalformedURLException {
125: String externalForm = getClass().getResource(
126: "/org/jpox/util/ClassUtilsTest.class").toExternalForm();
127: externalForm = StringUtils.replaceAll(externalForm, "file:/",
128: "");
129: String path = externalForm.substring(0, externalForm
130: .indexOf("/org/jpox/util/ClassUtilsTest.class"));
131: externalForm = StringUtils.replaceAll(externalForm, "/", System
132: .getProperty("file.separator"));
133: externalForm = StringUtils.replaceAll(externalForm, "\\",
134: System.getProperty("file.separator"));
135: assertTrue("Classname for filename is incorrect", ClassUtils
136: .getClassnameForFilename(externalForm, path).equals(
137: "org.jpox.util.ClassUtilsTest"));
138: }
139:
140: /**
141: * Test for whether classes are descendents of each other.
142: */
143: public void testClassesAreDescendents() {
144: ClassLoaderResolver clr = new JDOClassLoaderResolver(null);
145:
146: assertTrue(
147: "java.util.Collection and java.util.ArrayList should have been direct descendents but weren't",
148: ClassUtils.classesAreDescendents(clr,
149: "java.util.Collection", "java.util.ArrayList"));
150: assertTrue(
151: "java.util.ArrayList and java.util.Collection should have been direct descendents but weren't",
152: ClassUtils.classesAreDescendents(clr,
153: "java.util.ArrayList", "java.util.Collection"));
154: assertTrue(
155: "java.util.ArrayList and java.lang.String shouldn't have been direct descendents but were",
156: !ClassUtils.classesAreDescendents(clr,
157: "java.util.ArrayList", "java.lang.String"));
158: }
159:
160: /**
161: * Test for java bean getter name generator.
162: */
163: public void testJavaBeanGetterName() {
164: assertEquals("Incorrect Java Bean getter name", "getParam",
165: ClassUtils.getJavaBeanGetterName("param", false));
166: assertEquals("Incorrect Java Bean getter name", "getABC",
167: ClassUtils.getJavaBeanGetterName("ABC", false));
168: assertEquals("Incorrect Java Bean getter name", "getA",
169: ClassUtils.getJavaBeanGetterName("a", false));
170: }
171:
172: /**
173: * Test for java bean setter name generator.
174: */
175: public void testJavaBeanSetterName() {
176: assertEquals("Incorrect Java Bean setter name", "setParam",
177: ClassUtils.getJavaBeanSetterName("param"));
178: assertEquals("Incorrect Java Bean setter name", "setABC",
179: ClassUtils.getJavaBeanSetterName("ABC"));
180: assertEquals("Incorrect Java Bean setter name", "setA",
181: ClassUtils.getJavaBeanSetterName("a"));
182: }
183:
184: /**
185: * Test for field name for java bean getter.
186: */
187: public void testFieldNameForJavaBeanGetter() {
188: assertEquals("Incorrect field name for Java Bean getter",
189: "param", ClassUtils
190: .getFieldNameForJavaBeanGetter("getParam"));
191: assertEquals("Incorrect field name for Java Bean getter",
192: "ABC", ClassUtils
193: .getFieldNameForJavaBeanGetter("getABC"));
194: assertEquals("Incorrect field name for Java Bean getter", "a",
195: ClassUtils.getFieldNameForJavaBeanGetter("getA"));
196: }
197:
198: /**
199: * Test for field name for java bean setter.
200: */
201: public void testFieldNameForJavaBeanSetter() {
202: assertEquals("Incorrect field name for Java Bean setter",
203: "param", ClassUtils
204: .getFieldNameForJavaBeanSetter("setParam"));
205: assertEquals("Incorrect field name for Java Bean setter",
206: "ABC", ClassUtils
207: .getFieldNameForJavaBeanSetter("setABC"));
208: assertEquals("Incorrect field name for Java Bean setter", "a",
209: ClassUtils.getFieldNameForJavaBeanSetter("setA"));
210: }
211:
212: public class MyPCClass implements PersistenceCapable {
213:
214: public PersistenceManager jdoGetPersistenceManager() {
215: return null;
216: }
217:
218: public void jdoReplaceStateManager(StateManager arg0)
219: throws SecurityException {
220: }
221:
222: public void jdoProvideField(int arg0) {
223: }
224:
225: public void jdoProvideFields(int[] arg0) {
226: }
227:
228: public void jdoReplaceField(int arg0) {
229: }
230:
231: public void jdoReplaceFields(int[] arg0) {
232: }
233:
234: public void jdoReplaceFlags() {
235: }
236:
237: public void jdoCopyFields(Object arg0, int[] arg1) {
238: }
239:
240: public void jdoMakeDirty(String arg0) {
241: }
242:
243: public Object jdoGetObjectId() {
244: return null;
245: }
246:
247: public Object jdoGetTransactionalObjectId() {
248: return null;
249: }
250:
251: public Object jdoGetVersion() {
252: return null;
253: }
254:
255: public boolean jdoIsDirty() {
256: return false;
257: }
258:
259: public boolean jdoIsTransactional() {
260: return false;
261: }
262:
263: public boolean jdoIsPersistent() {
264: return false;
265: }
266:
267: public boolean jdoIsNew() {
268: return false;
269: }
270:
271: public boolean jdoIsDeleted() {
272: return false;
273: }
274:
275: public boolean jdoIsDetached() {
276: return false;
277: }
278:
279: public PersistenceCapable jdoNewInstance(StateManager arg0) {
280: return null;
281: }
282:
283: public PersistenceCapable jdoNewInstance(StateManager arg0,
284: Object arg1) {
285: return null;
286: }
287:
288: public Object jdoNewObjectIdInstance() {
289: return null;
290: }
291:
292: public Object jdoNewObjectIdInstance(Object arg0) {
293: return null;
294: }
295:
296: public void jdoCopyKeyFieldsToObjectId(Object arg0) {
297: }
298:
299: public void jdoCopyKeyFieldsToObjectId(
300: ObjectIdFieldSupplier arg0, Object arg1) {
301: }
302:
303: public void jdoCopyKeyFieldsFromObjectId(
304: ObjectIdFieldConsumer arg0, Object arg1) {
305: }
306: }
307:
308: public class MyChildPCClass extends MyPCClass {
309: }
310: }
|