1 /*
2  * HomeControllerTest.java 6 juin 2006
3  *
4  * Copyright (c) 2006 Emmanuel PUYBARET / eTeks <info@eteks.com>. All Rights Reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 package com.eteks.sweethome3d.junit;
21 
22 import java.beans.PropertyChangeEvent;
23 import java.beans.PropertyChangeListener;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.List;
27 import java.util.concurrent.atomic.AtomicReference;
28 
29 import com.eteks.sweethome3d.model.CollectionEvent;
30 import com.eteks.sweethome3d.model.CollectionListener;
31 import com.eteks.sweethome3d.model.Home;
32 import com.eteks.sweethome3d.model.HomeObject;
33 import com.eteks.sweethome3d.model.Label;
34 import com.eteks.sweethome3d.model.Wall;
35 
36 import junit.framework.TestCase;
37 
38 /**
39  * Tests {@link com.eteks.sweethome3d.model.Home Home} class.
40  * @author Emmanuel Puybaret
41  */
42 public class HomeTest extends TestCase {
testHomeWalls()43   public void testHomeWalls() {
44     // Create a home and a wall listener that updates lists when notified
45     Home home = new Home();
46     final List<Wall> addedWalls = new ArrayList<Wall>();
47     final List<Wall> deletedWalls = new ArrayList<Wall>();
48     final List<Wall> updatedWalls = new ArrayList<Wall>();
49     final PropertyChangeListener wallChangeListener = new PropertyChangeListener() {
50         public void propertyChange(PropertyChangeEvent ev) {
51           updatedWalls.add((Wall)ev.getSource());
52         }
53       };
54     home.addWallsListener(new CollectionListener<Wall> () {
55       public void collectionChanged(CollectionEvent<Wall> ev) {
56         switch (ev.getType()) {
57           case ADD :
58             addedWalls.add(ev.getItem());
59             ev.getItem().addPropertyChangeListener(wallChangeListener);
60             break;
61           case DELETE :
62             deletedWalls.add(ev.getItem());
63             ev.getItem().removePropertyChangeListener(wallChangeListener);
64             break;
65         }
66       }
67     });
68 
69     // Create 2 walls
70     Wall wall1 = new Wall(0, 0, 100, 0, 0, home.getWallHeight());
71     Wall wall2 = new Wall(100, 0, 100, 100, 0, home.getWallHeight());
72     // Add them to home
73     home.addWall(wall1);
74     home.addWall(wall2);
75     // Check they were added and that wall listener received a notification for each wall
76     assertWallCollectionContains(home.getWalls(), wall1, wall2);
77     assertWallCollectionContains(addedWalls, wall1, wall2);
78 
79     // Join end point of first wall to start point of second wall
80     wall1.setWallAtEnd(wall2);
81     // Check wall1 end wall is wall2 and that wall listener received 1 notification
82     assertSame("Wall not joined", wall2, wall1.getWallAtEnd());
83     assertWallCollectionContains(updatedWalls, wall1);
84 
85     // Join start point of second wall to end point of first wall
86     updatedWalls.clear();
87     wall2.setWallAtStart(wall1);
88     // Check wall2 start wall is wall1 and that wall listener received 1 notification
89     assertSame("Wall not joined", wall1, wall2.getWallAtStart());
90     assertWallCollectionContains(updatedWalls, wall2);
91 
92     // Move end point of second wall
93     updatedWalls.clear();
94     wall2.setXEnd(60);
95     wall2.setYEnd(100);
96     // Check wall2 end position and that wall listener received 1 notifications
97     assertEquals("Incorrect abscissa", 60f, wall2.getXEnd());
98     assertEquals("Incorrect ordinate", 100f, wall2.getYEnd());
99     assertWallCollectionContains(updatedWalls, wall2);
100 
101     // Move point shared by the two walls
102     updatedWalls.clear();
103     wall2.setXStart(60);
104     wall2.setYStart(0);
105     // Check wall2 start point position
106     assertEquals("Incorrect abscissa", 60f, wall2.getXStart());
107     assertEquals("Incorrect ordinate", 0f, wall2.getYStart());
108     // Check that wall listener received 2 notifications
109     assertWallCollectionContains(updatedWalls, wall2);
110 
111     updatedWalls.clear();
112     wall1.setXEnd(60);
113     wall1.setYEnd(0);
114     // Check wall1 end point position
115     assertEquals("Incorrect abscissa", 60f, wall1.getXEnd());
116     assertEquals("Incorrect ordinate", 0f, wall1.getYEnd());
117     // Check that wall listener received 2 notifications
118     assertWallCollectionContains(updatedWalls, wall1);
119 
120     // Detach second wall from first wall
121     updatedWalls.clear();
122     wall2.setWallAtStart(null);
123     // Check wall2 and wall1 are not joined and that wall listener received 2 notifications
124     assertSame("Wall joined", null, wall1.getWallAtEnd());
125     assertSame("Wall joined", null, wall2.getWallAtStart());
126     assertWallCollectionContains(updatedWalls, wall1, wall2);
127 
128     // Delete second wall
129     home.deleteWall(wall2);
130     // Check it was removed and that wall listener received a notification
131     assertWallCollectionContains(home.getWalls(), wall1);
132     assertWallCollectionContains(deletedWalls, wall2);
133   }
134 
testProperties()135   public void testProperties() {
136     // Test properties management on a subclass of HomeObject
137     HomeObject object = new HomeObject() { };
138     assertTrue("Missing internal id", object.getId().startsWith("object-"));
139     final AtomicReference<String> propertyName = new AtomicReference<String>(null);
140     PropertyChangeListener userPropertyChangeListener = new PropertyChangeListener() {
141         public void propertyChange(PropertyChangeEvent ev) {
142           propertyName.set((String)ev.getPropertyName());
143         }
144       };
145     object.addPropertyChangeListener(userPropertyChangeListener);
146     object.setProperty("name", "My object");
147     assertEquals("Wrong count of properties", 1, object.getPropertyNames().size());
148     assertEquals("Wrong property name", "name", object.getPropertyNames().iterator().next());
149     assertEquals("Wrong property property name", "name", propertyName.get());
150     assertEquals("Wrong property value", "My object", object.getProperty("name"));
151     assertEquals("Wrong property value on clone", "My object", object.clone().getProperty("name"));
152     // Change the property to check it behaves correctly with one property
153     object.setProperty("name", "My other object");
154     assertEquals("Wrong property value", "My other object", object.getProperty("name"));
155     // Set a second property that should change internally the way properties are stored
156     object.setProperty("id", "Object1");
157     assertEquals("Wrong count of properties", 2, object.getPropertyNames().size());
158     assertEquals("Wrong property value", "Object1", object.getProperty("id"));
159     assertEquals("Wrong property value", "My other object", object.getProperty("name"));
160     assertEquals("Wrong properties count on clone", 2, object.clone().getPropertyNames().size());
161     object.setProperty("name", null);
162     object.setProperty("id", null);
163     assertEquals("Wrong count of properties", 0, object.getPropertyNames().size());
164     assertEquals("Wrong properties count on clone", 0, object.clone().getPropertyNames().size());
165 
166     Label label = new Label("Text", 0, 0);
167     PropertyChangeListener modelPropertyChangeListener = new PropertyChangeListener() {
168         public void propertyChange(PropertyChangeEvent ev) {
169           propertyName.set((String)ev.getPropertyName());
170         }
171       };
172     label.addPropertyChangeListener(modelPropertyChangeListener);
173     label.setText("Text2");
174     assertEquals("Wrong property property name", Label.Property.TEXT.name(), propertyName.get());
175     label.removePropertyChangeListener(modelPropertyChangeListener);
176     label.addPropertyChangeListener(userPropertyChangeListener);
177     label.setProperty("prop", "value");
178     assertEquals("Wrong property property name", "prop", propertyName.get());
179   }
180 
assertWallCollectionContains(Collection<Wall> wallCollection, Wall ... walls)181   private void assertWallCollectionContains(Collection<Wall> wallCollection, Wall ... walls) {
182     assertEquals("Walls incorrect count", walls.length, wallCollection.size());
183     for (Wall wall : walls) {
184       assertTrue("Wall doesn't belong to collection", wallCollection.contains(wall));
185     }
186   }
187 }
188