KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Set JavaDoc;
22
23 import net.sf.dozer.util.mapping.vo.A;
24 import net.sf.dozer.util.mapping.vo.Aliases;
25 import net.sf.dozer.util.mapping.vo.B;
26 import net.sf.dozer.util.mapping.vo.C;
27 import net.sf.dozer.util.mapping.vo.D;
28 import net.sf.dozer.util.mapping.vo.FlatIndividual;
29 import net.sf.dozer.util.mapping.vo.Individual;
30 import net.sf.dozer.util.mapping.vo.Individuals;
31
32 /**
33  * @author wojtek.kiersztyn
34  * @author dominic.peciuch
35  *
36  */

37 public class IndexMappingTest extends DozerTestBase {
38
39   private static DozerBeanMapper mapper;
40
41   static {
42     List JavaDoc myMappingFiles = new ArrayList JavaDoc();
43     myMappingFiles.add("IndividualMapping.xml");
44     mapper = new DozerBeanMapper();
45     mapper.setMappingFiles(myMappingFiles);
46   }
47
48   public void testMap1() throws Exception JavaDoc {
49     List JavaDoc userNames = new ArrayList JavaDoc();
50     userNames.add("username1");
51     userNames.add("username2");
52
53     String JavaDoc[] secondNames = new String JavaDoc[3];
54     secondNames[0] = "secondName1";
55     secondNames[1] = "secondName2";
56     secondNames[2] = "secondName3";
57
58     Individuals source = new Individuals();
59     source.setUsernames(userNames);
60     source.setSimpleField("a very simple field");
61     source.setSecondNames(secondNames);
62     
63     Set JavaDoc mySet = new HashSet JavaDoc();
64     mySet.add("myString");
65     
66     source.setAddressSet(mySet);
67
68     FlatIndividual dest = (FlatIndividual) mapper.map(source, FlatIndividual.class);
69
70     assertEquals(source.getUsernames().get(0), dest.getUsername1());
71     assertEquals(source.getSimpleField(), dest.getSimpleField());
72     assertEquals(source.getSecondNames()[1], dest.getSecondName1());
73     assertEquals(source.getSecondNames()[2], dest.getSecondName2());
74     assertEquals("myString", dest.getAddress());
75   }
76
77   public void testMap1Inv() {
78     FlatIndividual source = new FlatIndividual();
79     source.setUsername1("username1");
80     source.setUsername2("username2");
81     source.setSimpleField("a simple field");
82     source.setSecondName1("secondName1");
83     source.setSecondName2("secondName2");
84     source.setPrimaryAlias("aqqq");
85
86     Individuals dest = (Individuals) mapper.map(source, Individuals.class);
87
88     assertEquals(source.getUsername1(), dest.getUsernames().get(0));
89     assertEquals(dest.getIndividual().getUsername(), source.getUsername2());
90     assertEquals(dest.getAliases().getOtherAliases()[0], "aqqq");
91     assertEquals(source.getUsername2(), dest.getUsernames().get(1));
92     assertEquals(source.getSimpleField(), dest.getSimpleField());
93     assertEquals(source.getSecondName1(), dest.getSecondNames()[1]);
94     assertEquals(source.getSecondName2(), dest.getSecondNames()[2]);
95   }
96
97   public void testMap3() {
98     List JavaDoc userNames = new ArrayList JavaDoc();
99     userNames.add("username1");
100     userNames.add("username2");
101
102     Individual nestedIndividual = new Individual();
103     nestedIndividual.setUsername("nestedusername");
104
105     String JavaDoc[] secondNames = new String JavaDoc[3];
106     secondNames[0] = "secondName1";
107     secondNames[1] = "secondName2";
108     secondNames[2] = "secondName3";
109
110     Individuals source = new Individuals();
111     source.setUsernames(userNames);
112     source.setIndividual(nestedIndividual);
113     source.setSecondNames(secondNames);
114
115     FlatIndividual dest = (FlatIndividual) mapper.map(source, FlatIndividual.class);
116
117     assertEquals(source.getUsernames().get(0), dest.getUsername1());
118     assertEquals(source.getIndividual().getUsername(), dest.getUsername2());
119     assertEquals(source.getSecondNames()[1], dest.getSecondName1());
120     assertEquals(source.getSecondNames()[2], dest.getSecondName2());
121   }
122
123   public void testNulls() {
124     FlatIndividual source = new FlatIndividual();
125     source.setSimpleField("a simplefield");
126
127     Individuals dest = (Individuals) mapper.map(source, Individuals.class);
128     assertEquals(source.getSimpleField(), dest.getSimpleField());
129   }
130
131   public void testNullsInv() {
132     Individuals source = new Individuals();
133     source.setSimpleField("a simplefield");
134
135     FlatIndividual dest = (FlatIndividual) mapper.map(source, FlatIndividual.class);
136     assertEquals(source.getSimpleField(), dest.getSimpleField());
137   }
138
139  public void testNestedArray() {
140     Individuals source = new Individuals();
141     Aliases aliases = new Aliases();
142     aliases.setOtherAliases(new String JavaDoc[] { "other alias 1", "other alias 2" });
143     source.setAliases(aliases);
144
145     FlatIndividual dest = (FlatIndividual) mapper.map(source, FlatIndividual.class);
146     assertEquals("other alias 1", dest.getPrimaryAlias());
147   }
148
149   public void testNotNullNestedIndexAtoD() {
150     C c = new C();
151     c.setValue("value1");
152     B b = new B();
153     b.setCs(new C[] { c });
154     A a = new A();
155     a.setB(b);
156
157     D d = (D) mapper.map(a, D.class);
158     assertEquals("value not translated", "value1", d.getValue());
159   }
160
161   public void testNullNestedIndexAtoD() {
162     A a = new A();
163
164     D d = (D) mapper.map(a, D.class);
165     assertNull("value should not be translated", d.getValue());
166   }
167
168   public void testNotNullNestedIndexDtoA() {
169     D d = new D();
170     d.setValue("value1");
171
172     A a = (A) mapper.map(d, A.class);
173     assertEquals("value not translated", d.getValue(), a.getB().getCs()[0].getValue());
174   }
175
176   public void testNullNestedIndexDtoA() {
177     D d = new D();
178     A a = (A) mapper.map(d, A.class);
179     assertNotNull(a);
180   }
181 }
182
Popular Tags