001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestSiteModel.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.gui.model;
009:
010: import java.util.Iterator;
011:
012: import junit.framework.TestCase;
013:
014: import com.uwyn.rife.gui.model.exceptions.GuiModelException;
015:
016: public class TestSiteModel extends TestCase implements
017: ParticleModelListener {
018: private Object mChildAdded = null;
019:
020: public TestSiteModel(String name) {
021: super (name);
022: }
023:
024: public void testInstantiation() {
025: SiteModel sitemodel_instance = new SiteModel();
026:
027: assertNotNull(sitemodel_instance);
028: assertTrue(sitemodel_instance instanceof SiteModel);
029: }
030:
031: public void testNoInitialElements() {
032: SiteModel sitemodel_instance = new SiteModel();
033:
034: assertEquals(sitemodel_instance.getElements().size(), 0);
035: }
036:
037: public void testInitialElementCountIsZero() {
038: SiteModel sitemodel_instance = new SiteModel();
039:
040: assertEquals(sitemodel_instance.countElements(), 0);
041: }
042:
043: public void testAddOneElement() {
044: SiteModel sitemodel_instance = null;
045: ElementModel elementmodel_instance = null;
046:
047: sitemodel_instance = new SiteModel();
048: assertTrue(sitemodel_instance.addParticleListener(this ));
049: assertNull(mChildAdded);
050:
051: try {
052: elementmodel_instance = new ElementModel("elementmodel1");
053: sitemodel_instance.addElement(elementmodel_instance);
054: } catch (GuiModelException e) {
055: assertTrue(e.getMessage(), false);
056: }
057: assertSame(mChildAdded, elementmodel_instance);
058:
059: Iterator<ElementModel> element_it = sitemodel_instance
060: .getElements().iterator();
061: assertTrue(element_it.hasNext());
062: Object element = element_it.next();
063: assertEquals(element_it.hasNext(), false);
064: assertSame(element, elementmodel_instance);
065: }
066:
067: public void testAddAnotherEqualElement() {
068: SiteModel sitemodel_instance = null;
069: ElementModel elementmodel_instance1 = null;
070: ElementModel elementmodel_instance2 = null;
071:
072: sitemodel_instance = new SiteModel();
073: assertTrue(sitemodel_instance.addParticleListener(this ));
074: assertNull(mChildAdded);
075:
076: try {
077: elementmodel_instance1 = new ElementModel("elementmodel1");
078: elementmodel_instance2 = new ElementModel("elementmodel1");
079: sitemodel_instance.addElement(elementmodel_instance1);
080: } catch (GuiModelException e) {
081: assertTrue(e.getMessage(), false);
082: }
083: assertSame(mChildAdded, elementmodel_instance1);
084:
085: try {
086: sitemodel_instance.addElement(elementmodel_instance2);
087: fail();
088: } catch (GuiModelException e) {
089: assertTrue(true);
090: assertSame(mChildAdded, elementmodel_instance1);
091: }
092:
093: Iterator<ElementModel> element_it = sitemodel_instance
094: .getElements().iterator();
095: assertTrue(element_it.hasNext());
096: Object element = element_it.next();
097: assertEquals(element_it.hasNext(), false);
098: assertSame(element, elementmodel_instance1);
099: }
100:
101: public void testAddTwoElements() {
102: SiteModel sitemodel_instance = null;
103: ElementModel elementmodel_instance1 = null;
104: ElementModel elementmodel_instance2 = null;
105:
106: sitemodel_instance = new SiteModel();
107: assertTrue(sitemodel_instance.addParticleListener(this ));
108: assertNull(mChildAdded);
109:
110: try {
111: elementmodel_instance1 = new ElementModel("elementmodel1");
112: elementmodel_instance2 = new ElementModel("elementmodel2");
113: sitemodel_instance.addElement(elementmodel_instance1);
114: assertSame(mChildAdded, elementmodel_instance1);
115: sitemodel_instance.addElement(elementmodel_instance2);
116: assertSame(mChildAdded, elementmodel_instance2);
117: } catch (GuiModelException e) {
118: assertTrue(e.getMessage(), false);
119: }
120: Iterator<ElementModel> element_it = sitemodel_instance
121: .getElements().iterator();
122: assertTrue(element_it.hasNext());
123: Object element1 = element_it.next();
124: assertTrue(element_it.hasNext());
125: Object element2 = element_it.next();
126: assertEquals(element_it.hasNext(), false);
127: assertTrue((element1 == elementmodel_instance1 && element2 == elementmodel_instance2)
128: || (element2 == elementmodel_instance1 && element1 == elementmodel_instance2));
129: }
130:
131: public void testCountElements() {
132: SiteModel sitemodel_instance = null;
133: ElementModel elementmodel_instance1 = null;
134: ElementModel elementmodel_instance2 = null;
135:
136: sitemodel_instance = new SiteModel();
137:
138: try {
139: elementmodel_instance1 = new ElementModel("elementmodel1");
140: elementmodel_instance2 = new ElementModel("elementmodel2");
141: sitemodel_instance.addElement(elementmodel_instance1);
142: sitemodel_instance.addElement(elementmodel_instance2);
143: } catch (GuiModelException e) {
144: assertTrue(e.getMessage(), false);
145: }
146: assertEquals(sitemodel_instance.countElements(), 2);
147: }
148:
149: public void parentChanged() {
150: }
151:
152: public void childAdded(ParticleModel child) {
153: mChildAdded = child;
154: }
155:
156: public void childRemoved(ParticleModel child) {
157: }
158:
159: public void propertyAdded(ParticlePropertyModel property) {
160: }
161:
162: public void propertyRemoved(ParticlePropertyModel property) {
163: }
164:
165: public void propertyRenamed(ParticlePropertyModel property) {
166: }
167: }
|