KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > dozer > util > mapping > MappingUtilsTest


1 /*
2  * Copyright 2005-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package net.sf.dozer.util.mapping;
17
18 import java.util.Iterator JavaDoc;
19
20 import net.sf.dozer.util.mapping.fieldmap.ClassMap;
21 import net.sf.dozer.util.mapping.fieldmap.FieldMap;
22 import net.sf.dozer.util.mapping.fieldmap.Mappings;
23 import net.sf.dozer.util.mapping.util.MappingFileReader;
24 import net.sf.dozer.util.mapping.util.MappingUtils;
25 import net.sf.dozer.util.mapping.util.MappingsParser;
26
27 /**
28  * @author tierney.matt
29  */

30 public class MappingUtilsTest extends DozerTestBase {
31
32   MappingUtils mappingUtils = new MappingUtils();
33
34   public void testIsBlankOrNull() throws Exception JavaDoc {
35     assertTrue(mappingUtils.isBlankOrNull(null));
36     assertTrue(mappingUtils.isBlankOrNull(""));
37     assertTrue(mappingUtils.isBlankOrNull(" "));
38   }
39
40   public void testOverridenFields() throws Exception JavaDoc {
41     MappingFileReader fileReader = new MappingFileReader("overridemapping.xml");
42     Mappings mappings = fileReader.read();
43     MappingsParser mappingsParser = new MappingsParser();
44     mappingsParser.parseMappings(mappings);
45     // validate class mappings
46
Iterator JavaDoc iter = mappings.getMapping().iterator();
47     while (iter.hasNext()) {
48       ClassMap classMap = (ClassMap) iter.next();
49       if (classMap.getSourceClass().getName().equals("net.sf.dozer.util.mapping.vo.FurtherTestObject")) {
50         assertTrue(classMap.getStopOnErrors());
51       }
52       if (classMap.getSourceClass().getName().equals("net.sf.dozer.util.mapping.vo.SuperSuperSuperClass")) {
53         assertTrue(classMap.getWildcard());
54       }
55       if (classMap.getSourceClass().getName().equals("net.sf.dozer.util.mapping.vo.TestObject")) {
56         assertTrue(!((FieldMap) classMap.getFieldMaps().get(0)).getCopyByReference());
57       }
58     }
59   }
60
61   public void testGetClassWithoutPackage() throws Exception JavaDoc {
62     String JavaDoc result = mappingUtils.getClassNameWithoutPackage(String JavaDoc.class);
63     assertNotNull("result should not be null", result);
64     assertEquals("invalid result value", "String", result);
65   }
66   
67   public void testThrowMappingException_MappingException() {
68     MappingException ex = new MappingException(String.valueOf(System.currentTimeMillis()));
69     try {
70       mappingUtils.throwMappingException(ex);
71       fail("should have thrown exception");
72     } catch (MappingException e) {
73       assertEquals("invalid ex", ex, e);
74     }
75   }
76   
77   public void testThrowMappingException_RuntimeException() {
78     //Runtime ex should not get wrapped in MappingException
79
NullPointerException JavaDoc ex = new NullPointerException JavaDoc(String.valueOf(System.currentTimeMillis()));
80     try {
81       mappingUtils.throwMappingException(ex);
82       fail("should have thrown exception");
83     } catch (NullPointerException JavaDoc e) {
84       assertEquals("invalid ex", ex, e);
85     } catch (Throwable JavaDoc e) {
86       fail("NullPointerException should have been thrown");
87     }
88   }
89   
90   public void testThrowMappingException_CheckedException() {
91     //Checked exception should get wrapped in MappingException
92
NoSuchFieldException JavaDoc ex = new NoSuchFieldException JavaDoc(String.valueOf(System.currentTimeMillis()));
93     try {
94       mappingUtils.throwMappingException(ex);
95       fail("should have thrown exception");
96     } catch (MappingException e) {
97       assertEquals("invalid nested ex", ex, e.getCause());
98     } catch (Throwable JavaDoc e) {
99       fail("MappingException should have been thrown");
100     }
101   }
102   
103 }
104
Popular Tags