KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > util > ConfiguratorTest


1 /*
2  * This file is subject to the license found in LICENCE.TXT in the root directory of the project.
3  *
4  * #SNAPSHOT#
5  */

6 package fr.jayasoft.ivy.util;
7
8 import java.util.ArrayList JavaDoc;
9 import java.util.List JavaDoc;
10
11 import fr.jayasoft.ivy.util.Configurator;
12
13 import junit.framework.TestCase;
14
15 /**
16  * @author x.hanin
17  *
18  */

19 public class ConfiguratorTest extends TestCase {
20     public static class City {
21         private List JavaDoc _housings = new ArrayList JavaDoc();
22         private List JavaDoc _streets = new ArrayList JavaDoc();
23
24         private String JavaDoc _name;
25         public String JavaDoc getName() {
26             return _name;
27         }
28         public void setName(String JavaDoc name) {
29             _name = name;
30         }
31         public List JavaDoc getHousings() {
32             return _housings;
33         }
34         public List JavaDoc getStreets() {
35             return _streets;
36         }
37         public void add(Housing h) {
38             _housings.add(h);
39         }
40         public void add(Street s) {
41             _streets.add(s);
42         }
43     }
44
45     public static class Street {
46         private Class JavaDoc _clazz;
47         private List JavaDoc _trees = new ArrayList JavaDoc();
48         public List JavaDoc getTrees() {
49             return _trees;
50         }
51         public void addConfiguredTree(Tree tree) {
52             _trees.add(tree);
53         }
54         public Class JavaDoc getClazz() {
55             return _clazz;
56         }
57         public void setClazz(Class JavaDoc clazz) {
58             _clazz = clazz;
59         }
60     }
61
62     public static abstract class Housing {
63         private List JavaDoc _rooms = new ArrayList JavaDoc();
64         private boolean _isEmpty;
65         private Person _proprietary;
66
67         public List JavaDoc getRooms() {
68             return _rooms;
69         }
70         public void addRoom(Room r) {
71             _rooms.add(r);
72         }
73         public boolean isEmpty() {
74             return _isEmpty;
75         }
76         public void setEmpty(boolean isEmpty) {
77             _isEmpty = isEmpty;
78         }
79         public Person getProprietary() {
80             return _proprietary;
81         }
82         public void setProprietary(Person proprietary) {
83             _proprietary = proprietary;
84         }
85     }
86
87     public static class House extends Housing {
88     }
89     
90     public static class Tree {
91         private short _age;
92         public short getAge() {
93             return _age;
94         }
95         public void setAge(short age) {
96             _age = age;
97         }
98     }
99
100     public static class Flat extends Housing {
101         private int _stage;
102         public int getStage() {
103             return _stage;
104         }
105         public void setStage(int stage) {
106             _stage = stage;
107         }
108     }
109
110     public static class Room {
111         private short _surface;
112         public short getSurface() {
113             return _surface;
114         }
115         public void setSurface(short surface) {
116             _surface = surface;
117         }
118     }
119     
120     public static class Person {
121         private String JavaDoc _name;
122
123         public Person(String JavaDoc name) {
124             _name = name;
125         }
126         public String JavaDoc getName() {
127             return _name;
128         }
129     }
130
131     private Configurator _conf;
132     
133     protected void setUp() throws Exception JavaDoc {
134         _conf = new Configurator();
135     }
136     
137     public void testSetRoot() {
138         City city = new City();
139         _conf.setRoot(city);
140         assertEquals(city, _conf.getCurrent());
141     }
142     
143     public void testStringAttribute() {
144         City city = new City();
145         _conf.setRoot(city);
146         _conf.setAttribute("name", "bordeaux");
147         assertEquals("bordeaux", city.getName());
148     }
149     
150     public void testIntAttribute() {
151         Flat flat = new Flat();
152         _conf.setRoot(flat);
153         _conf.setAttribute("stage", "4");
154         assertEquals(4, flat.getStage());
155     }
156     
157     public void testBooleanAttribute() {
158         Housing housing = new House();
159         _conf.setRoot(housing);
160         _conf.setAttribute("empty", "true");
161         assertEquals(true, housing.isEmpty());
162         _conf.setAttribute("empty", "false");
163         assertEquals(false, housing.isEmpty());
164         _conf.setAttribute("empty", "yes");
165         assertEquals(true, housing.isEmpty());
166         _conf.setAttribute("empty", "no");
167         assertEquals(false, housing.isEmpty());
168         _conf.setAttribute("empty", "on");
169         assertEquals(true, housing.isEmpty());
170         _conf.setAttribute("empty", "off");
171         assertEquals(false, housing.isEmpty());
172     }
173
174     public void testClassAttribute() {
175         Street street = new Street();
176         _conf.setRoot(street);
177         _conf.setAttribute("clazz", getClass().getName());
178         assertEquals(getClass(), street.getClazz());
179     }
180     
181     public void testPersonAttribute() {
182         Housing housing = new House();
183         _conf.setRoot(housing);
184         _conf.setAttribute("proprietary", "jean");
185         assertEquals("jean", housing.getProprietary().getName());
186     }
187     
188     public void testAddRoom() {
189         Housing housing = new House();
190         _conf.setRoot(housing);
191         _conf.startCreateChild("room");
192         assertEquals(1, housing.getRooms().size());
193         _conf.setAttribute("surface", "24");
194         assertEquals(24, ((Room)housing.getRooms().get(0)).getSurface());
195         _conf.endCreateChild();
196         assertEquals(housing, _conf.getCurrent());
197     }
198     public void testAddConfiguredTree() {
199         Street street = new Street();
200         _conf.setRoot(street);
201         _conf.startCreateChild("tree");
202         assertTrue(street.getTrees().isEmpty());
203         _conf.setAttribute("age", "400");
204         _conf.endCreateChild();
205         assertEquals(1, street.getTrees().size());
206         assertEquals(400, ((Tree)street.getTrees().get(0)).getAge());
207         assertEquals(street, _conf.getCurrent());
208     }
209     public void testAddWithTypeDef() throws Exception JavaDoc {
210         City city = new City();
211         _conf.typeDef("house", House.class.getName());
212         _conf.typeDef("flat", Flat.class.getName());
213         _conf.typeDef("street", Street.class.getName());
214         _conf.setRoot(city);
215         _conf.startCreateChild("house");
216         assertEquals(1, city.getHousings().size());
217         assertTrue(city.getHousings().get(0) instanceof House);
218         _conf.endCreateChild();
219         _conf.startCreateChild("flat");
220         assertEquals(2, city.getHousings().size());
221         assertTrue(city.getHousings().get(1) instanceof Flat);
222         _conf.endCreateChild();
223         _conf.startCreateChild("street");
224         assertEquals(1, city.getStreets().size());
225         _conf.endCreateChild();
226         assertEquals(city, _conf.getCurrent());
227     }
228     public void testNested() throws Exception JavaDoc {
229         City city = new City();
230         _conf.typeDef("house", House.class.getName());
231         _conf.setRoot(city);
232         _conf.startCreateChild("house");
233         _conf.startCreateChild("room");
234         _conf.setAttribute("surface", "20");
235         _conf.endCreateChild();
236         _conf.startCreateChild("room");
237         _conf.setAttribute("surface", "25");
238         _conf.endCreateChild();
239         _conf.endCreateChild();
240         assertEquals(city, _conf.getCurrent());
241         assertEquals(2, ((Housing)city.getHousings().get(0)).getRooms().size());
242         assertEquals(20, ((Room)((Housing)city.getHousings().get(0)).getRooms().get(0)).getSurface());
243         assertEquals(25, ((Room)((Housing)city.getHousings().get(0)).getRooms().get(1)).getSurface());
244     }
245     
246     public void testMacro() throws Exception JavaDoc {
247         City city = new City();
248         _conf.typeDef("house", House.class.getName());
249         
250         _conf.startMacroDef("castle");
251         _conf.addMacroAttribute("surface", "40");
252         _conf.addMacroElement("addroom", true);
253         _conf.startCreateChild("house");
254         _conf.startCreateChild("room");
255         _conf.setAttribute("surface", "@{surface}");
256         _conf.endCreateChild();
257         _conf.startCreateChild("room");
258         _conf.setAttribute("surface", "@{surface}");
259         _conf.endCreateChild();
260         _conf.startCreateChild("addroom");
261         _conf.endCreateChild();
262         _conf.endCreateChild();
263         _conf.endMacroDef();
264         
265         _conf.setRoot(city);
266         _conf.startCreateChild("castle");
267         _conf.setAttribute("surface", "10");
268         _conf.endCreateChild();
269
270         _conf.startCreateChild("castle");
271         _conf.startCreateChild("addroom");
272         _conf.startCreateChild("room");
273         _conf.setAttribute("surface", "20");
274         _conf.endCreateChild();
275         _conf.endCreateChild();
276         _conf.endCreateChild();
277         
278         assertEquals(city, _conf.getCurrent());
279         assertEquals(2, city.getHousings().size());
280
281         // first castle : 2 default rooms of 10 of surface
282
assertEquals(2, ((Housing)city.getHousings().get(0)).getRooms().size());
283         assertEquals(10, ((Room)((Housing)city.getHousings().get(0)).getRooms().get(0)).getSurface());
284         assertEquals(10, ((Room)((Housing)city.getHousings().get(0)).getRooms().get(1)).getSurface());
285         
286         // second castle : 2 default rooms of default surface 40, + one addroom of surface 20
287
assertEquals(3, ((Housing)city.getHousings().get(1)).getRooms().size());
288         assertEquals(40, ((Room)((Housing)city.getHousings().get(1)).getRooms().get(0)).getSurface());
289         assertEquals(40, ((Room)((Housing)city.getHousings().get(1)).getRooms().get(1)).getSurface());
290         assertEquals(20, ((Room)((Housing)city.getHousings().get(1)).getRooms().get(2)).getSurface());
291     }
292 }
293
Popular Tags