001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.ivy.util;
019:
020: import java.util.ArrayList;
021: import java.util.List;
022: import java.util.Map;
023:
024: import junit.framework.TestCase;
025:
026: /**
027: *
028: */
029: public class ConfiguratorTest extends TestCase {
030: public static class City {
031: private List _housings = new ArrayList();
032:
033: private List _streets = new ArrayList();
034:
035: private String _name;
036:
037: public String getName() {
038: return _name;
039: }
040:
041: public void setName(String name) {
042: _name = name;
043: }
044:
045: public List getHousings() {
046: return _housings;
047: }
048:
049: public List getStreets() {
050: return _streets;
051: }
052:
053: public void add(Housing h) {
054: _housings.add(h);
055: }
056:
057: public void add(Street s) {
058: _streets.add(s);
059: }
060: }
061:
062: public static class Street {
063: private Class _clazz;
064:
065: private List _trees = new ArrayList();
066:
067: private List _walkers = new ArrayList();
068:
069: public List getTrees() {
070: return _trees;
071: }
072:
073: public void addConfiguredTree(Tree tree) {
074: _trees.add(tree);
075: }
076:
077: public List getWalkers() {
078: return _walkers;
079: }
080:
081: public void addConfiguredWalker(Map walkerAttributes) {
082: _walkers.add(new Person((String) walkerAttributes
083: .get("name")));
084: }
085:
086: public Class getClazz() {
087: return _clazz;
088: }
089:
090: public void setClazz(Class clazz) {
091: _clazz = clazz;
092: }
093: }
094:
095: public static abstract class Housing {
096: private List _rooms = new ArrayList();
097:
098: private boolean _isEmpty;
099:
100: private Person _proprietary;
101:
102: public List getRooms() {
103: return _rooms;
104: }
105:
106: public void addRoom(Room r) {
107: _rooms.add(r);
108: }
109:
110: public boolean isEmpty() {
111: return _isEmpty;
112: }
113:
114: public void setEmpty(boolean isEmpty) {
115: _isEmpty = isEmpty;
116: }
117:
118: public Person getProprietary() {
119: return _proprietary;
120: }
121:
122: public void setProprietary(Person proprietary) {
123: _proprietary = proprietary;
124: }
125: }
126:
127: public static class House extends Housing {
128: }
129:
130: public static class Tree {
131: private short _age;
132:
133: public short getAge() {
134: return _age;
135: }
136:
137: public void setAge(short age) {
138: _age = age;
139: }
140: }
141:
142: public static class Flat extends Housing {
143: private int _stage;
144:
145: public int getStage() {
146: return _stage;
147: }
148:
149: public void setStage(int stage) {
150: _stage = stage;
151: }
152: }
153:
154: public static class Room {
155: private short _surface;
156:
157: public short getSurface() {
158: return _surface;
159: }
160:
161: public void setSurface(short surface) {
162: _surface = surface;
163: }
164: }
165:
166: public static class Person {
167: private String _name;
168:
169: public Person(String name) {
170: _name = name;
171: }
172:
173: public String getName() {
174: return _name;
175: }
176: }
177:
178: private Configurator _conf;
179:
180: protected void setUp() throws Exception {
181: _conf = new Configurator();
182: }
183:
184: public void testSetRoot() {
185: City city = new City();
186: _conf.setRoot(city);
187: assertEquals(city, _conf.getCurrent());
188: }
189:
190: public void testStringAttribute() {
191: City city = new City();
192: _conf.setRoot(city);
193: _conf.setAttribute("name", "bordeaux");
194: assertEquals("bordeaux", city.getName());
195: }
196:
197: public void testIntAttribute() {
198: Flat flat = new Flat();
199: _conf.setRoot(flat);
200: _conf.setAttribute("stage", "4");
201: assertEquals(4, flat.getStage());
202: }
203:
204: public void testBooleanAttribute() {
205: Housing housing = new House();
206: _conf.setRoot(housing);
207: _conf.setAttribute("empty", "true");
208: assertEquals(true, housing.isEmpty());
209: _conf.setAttribute("empty", "false");
210: assertEquals(false, housing.isEmpty());
211: _conf.setAttribute("empty", "yes");
212: assertEquals(true, housing.isEmpty());
213: _conf.setAttribute("empty", "no");
214: assertEquals(false, housing.isEmpty());
215: _conf.setAttribute("empty", "on");
216: assertEquals(true, housing.isEmpty());
217: _conf.setAttribute("empty", "off");
218: assertEquals(false, housing.isEmpty());
219: }
220:
221: public void testClassAttribute() {
222: Street street = new Street();
223: _conf.setRoot(street);
224: _conf.setAttribute("clazz", getClass().getName());
225: assertEquals(getClass(), street.getClazz());
226: }
227:
228: public void testPersonAttribute() {
229: Housing housing = new House();
230: _conf.setRoot(housing);
231: _conf.setAttribute("proprietary", "jean");
232: assertEquals("jean", housing.getProprietary().getName());
233: }
234:
235: public void testAddRoom() {
236: Housing housing = new House();
237: _conf.setRoot(housing);
238: _conf.startCreateChild("room");
239: assertEquals(1, housing.getRooms().size());
240: _conf.setAttribute("surface", "24");
241: assertEquals(24, ((Room) housing.getRooms().get(0))
242: .getSurface());
243: _conf.endCreateChild();
244: assertEquals(housing, _conf.getCurrent());
245: }
246:
247: public void testAddConfiguredTree() {
248: Street street = new Street();
249: _conf.setRoot(street);
250: _conf.startCreateChild("tree");
251: assertTrue(street.getTrees().isEmpty());
252: _conf.setAttribute("age", "400");
253: _conf.endCreateChild();
254: assertEquals(1, street.getTrees().size());
255: assertEquals(400, ((Tree) street.getTrees().get(0)).getAge());
256: assertEquals(street, _conf.getCurrent());
257: }
258:
259: public void testAddConfiguredWalker() {
260: Street street = new Street();
261: _conf.setRoot(street);
262: _conf.startCreateChild("walker");
263: assertTrue(street.getWalkers().isEmpty());
264: _conf.setAttribute("name", "xavier");
265: _conf.endCreateChild();
266: assertEquals(1, street.getWalkers().size());
267: assertEquals("xavier", ((Person) street.getWalkers().get(0))
268: .getName());
269: assertEquals(street, _conf.getCurrent());
270: }
271:
272: public void testAddWithTypeDef() throws Exception {
273: City city = new City();
274: _conf.typeDef("house", House.class.getName());
275: _conf.typeDef("flat", Flat.class.getName());
276: _conf.typeDef("street", Street.class.getName());
277: _conf.setRoot(city);
278: _conf.startCreateChild("house");
279: assertEquals(1, city.getHousings().size());
280: assertTrue(city.getHousings().get(0) instanceof House);
281: _conf.endCreateChild();
282: _conf.startCreateChild("flat");
283: assertEquals(2, city.getHousings().size());
284: assertTrue(city.getHousings().get(1) instanceof Flat);
285: _conf.endCreateChild();
286: _conf.startCreateChild("street");
287: assertEquals(1, city.getStreets().size());
288: _conf.endCreateChild();
289: assertEquals(city, _conf.getCurrent());
290: }
291:
292: public void testNested() throws Exception {
293: City city = new City();
294: _conf.typeDef("house", House.class.getName());
295: _conf.setRoot(city);
296: _conf.startCreateChild("house");
297: _conf.startCreateChild("room");
298: _conf.setAttribute("surface", "20");
299: _conf.endCreateChild();
300: _conf.startCreateChild("room");
301: _conf.setAttribute("surface", "25");
302: _conf.endCreateChild();
303: _conf.endCreateChild();
304: assertEquals(city, _conf.getCurrent());
305: assertEquals(2, ((Housing) city.getHousings().get(0))
306: .getRooms().size());
307: assertEquals(20, ((Room) ((Housing) city.getHousings().get(0))
308: .getRooms().get(0)).getSurface());
309: assertEquals(25, ((Room) ((Housing) city.getHousings().get(0))
310: .getRooms().get(1)).getSurface());
311: }
312:
313: public void testMacro() throws Exception {
314: City city = new City();
315: _conf.typeDef("house", House.class.getName());
316:
317: _conf.startMacroDef("castle");
318: _conf.addMacroAttribute("surface", "40");
319: _conf.addMacroElement("addroom", true);
320: _conf.startCreateChild("house");
321: _conf.startCreateChild("room");
322: _conf.setAttribute("surface", "@{surface}");
323: _conf.endCreateChild();
324: _conf.startCreateChild("room");
325: _conf.setAttribute("surface", "@{surface}");
326: _conf.endCreateChild();
327: _conf.startCreateChild("addroom");
328: _conf.endCreateChild();
329: _conf.endCreateChild();
330: _conf.endMacroDef();
331:
332: _conf.setRoot(city);
333: _conf.startCreateChild("castle");
334: _conf.setAttribute("surface", "10");
335: _conf.endCreateChild();
336:
337: _conf.startCreateChild("castle");
338: _conf.startCreateChild("addroom");
339: _conf.startCreateChild("room");
340: _conf.setAttribute("surface", "20");
341: _conf.endCreateChild();
342: _conf.endCreateChild();
343: _conf.endCreateChild();
344:
345: assertEquals(city, _conf.getCurrent());
346: assertEquals(2, city.getHousings().size());
347:
348: // first castle : 2 default rooms of 10 of surface
349: assertEquals(2, ((Housing) city.getHousings().get(0))
350: .getRooms().size());
351: assertEquals(10, ((Room) ((Housing) city.getHousings().get(0))
352: .getRooms().get(0)).getSurface());
353: assertEquals(10, ((Room) ((Housing) city.getHousings().get(0))
354: .getRooms().get(1)).getSurface());
355:
356: // second castle : 2 default rooms of default surface 40, + one addroom of surface 20
357: assertEquals(3, ((Housing) city.getHousings().get(1))
358: .getRooms().size());
359: assertEquals(40, ((Room) ((Housing) city.getHousings().get(1))
360: .getRooms().get(0)).getSurface());
361: assertEquals(40, ((Room) ((Housing) city.getHousings().get(1))
362: .getRooms().get(1)).getSurface());
363: assertEquals(20, ((Room) ((Housing) city.getHousings().get(1))
364: .getRooms().get(2)).getSurface());
365: }
366: }
|