KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgrapht > graph > ListenableGraphTest


1 /* ==========================================
2  * JGraphT : a free Java graph-theory library
3  * ==========================================
4  *
5  * Project Info: http://jgrapht.sourceforge.net/
6  * Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
7  *
8  * (C) Copyright 2003-2006, by Barak Naveh and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18  * License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this library; if not, write to the Free Software Foundation,
22  * Inc.,
23  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
24  */

25 /* ------------------------
26  * ListenableGraphTest.java
27  * ------------------------
28  * (C) Copyright 2003-2006, by Barak Naveh and Contributors.
29  *
30  * Original Author: Barak Naveh
31  * Contributor(s): -
32  *
33  * $Id: ListenableGraphTest.java 504 2006-07-03 02:37:26Z perfecthash $
34  *
35  * Changes
36  * -------
37  * 03-Aug-2003 : Initial revision (BN);
38  * 10-Aug-2003 : Adaptation to new event model (BN);
39  *
40  */

41 package org.jgrapht.graph;
42
43 import junit.framework.*;
44
45 import org.jgrapht.*;
46 import org.jgrapht.event.*;
47
48
49 /**
50  * Unit test for {@link ListenableGraph} class.
51  *
52  * @author Barak Naveh
53  * @since Aug 3, 2003
54  */

55 public class ListenableGraphTest
56     extends TestCase
57 {
58
59     //~ Instance fields -------------------------------------------------------
60

61     DefaultEdge lastAddedEdge;
62     DefaultEdge lastRemovedEdge;
63     Object JavaDoc lastAddedVertex;
64     Object JavaDoc lastRemovedVertex;
65
66     //~ Constructors ----------------------------------------------------------
67

68     /**
69      * @see junit.framework.TestCase#TestCase(java.lang.String)
70      */

71     public ListenableGraphTest(String JavaDoc name)
72     {
73         super(name);
74     }
75
76     //~ Methods ---------------------------------------------------------------
77

78     /**
79      * Tests GraphListener listener.
80      */

81     public void testGraphListener()
82     {
83         init();
84
85         ListenableGraph<Object JavaDoc, DefaultEdge> g =
86             new ListenableUndirectedGraph<Object JavaDoc, DefaultEdge>(
87                 DefaultEdge.class);
88         GraphListener<Object JavaDoc, DefaultEdge> listener = new MyGraphListner();
89         g.addGraphListener(listener);
90
91         String JavaDoc v1 = "v1";
92         String JavaDoc v2 = "v2";
93
94         // test vertex notification
95
g.addVertex(v1);
96         assertEquals(v1, lastAddedVertex);
97         assertEquals(null, lastRemovedVertex);
98
99         init();
100         g.removeVertex(v1);
101         assertEquals(v1, lastRemovedVertex);
102         assertEquals(null, lastAddedVertex);
103
104         // test edge notification
105
g.addVertex(v1);
106         g.addVertex(v2);
107
108         init();
109
110         DefaultEdge e = g.addEdge(v1, v2);
111         assertEquals(e, lastAddedEdge);
112         assertEquals(null, lastRemovedEdge);
113
114         init();
115         assertTrue(g.removeEdge(e));
116         assertEquals(e, lastRemovedEdge);
117         assertEquals(null, lastAddedEdge);
118
119         g.removeVertex(v1);
120         g.removeVertex(v2);
121
122         //
123
// test notification stops when removing listener
124
//
125
g.removeGraphListener(listener);
126         init();
127         g.addVertex(v1);
128         g.addVertex(v2);
129         e = g.addEdge(v1, v2);
130         g.removeEdge(e);
131
132         assertEquals(null, lastAddedEdge);
133         assertEquals(null, lastAddedVertex);
134         assertEquals(null, lastRemovedEdge);
135         assertEquals(null, lastRemovedVertex);
136     }
137
138     /**
139      * Tests VertexSetListener listener.
140      */

141     public void testVertexSetListener()
142     {
143         init();
144
145         ListenableGraph<Object JavaDoc, DefaultEdge> g =
146             new ListenableUndirectedGraph<Object JavaDoc, DefaultEdge>(
147                 DefaultEdge.class);
148         VertexSetListener<Object JavaDoc> listener = new MyGraphListner();
149         g.addVertexSetListener(listener);
150
151         String JavaDoc v1 = "v1";
152         String JavaDoc v2 = "v2";
153
154         // test vertex notification
155
g.addVertex(v1);
156         assertEquals(v1, lastAddedVertex);
157         assertEquals(null, lastRemovedVertex);
158
159         init();
160         g.removeVertex(v1);
161         assertEquals(v1, lastRemovedVertex);
162         assertEquals(null, lastAddedVertex);
163
164         // test edge notification
165
g.addVertex(v1);
166         g.addVertex(v2);
167
168         init();
169
170         DefaultEdge e = g.addEdge(v1, v2);
171         assertEquals(null, lastAddedEdge);
172         assertEquals(null, lastRemovedEdge);
173
174         init();
175         assertTrue(g.removeEdge(e));
176         assertEquals(null, lastRemovedEdge);
177         assertEquals(null, lastAddedEdge);
178
179         g.removeVertex(v1);
180         g.removeVertex(v2);
181
182         //
183
// test notification stops when removing listener
184
//
185
g.removeVertexSetListener(listener);
186         init();
187         g.addVertex(v1);
188         g.addVertex(v2);
189         e = g.addEdge(v1, v2);
190         g.removeEdge(e);
191
192         assertEquals(null, lastAddedEdge);
193         assertEquals(null, lastAddedVertex);
194         assertEquals(null, lastRemovedEdge);
195         assertEquals(null, lastRemovedVertex);
196     }
197
198     private void init()
199     {
200         lastAddedEdge = null;
201         lastAddedVertex = null;
202         lastRemovedEdge = null;
203         lastRemovedVertex = null;
204     }
205
206     //~ Inner Classes ---------------------------------------------------------
207

208     /**
209      * A listener on the tested graph.
210      *
211      * @author Barak Naveh
212      * @since Aug 3, 2003
213      */

214     private class MyGraphListner
215         implements GraphListener<Object JavaDoc, DefaultEdge>
216     {
217         /**
218          * @see GraphListener#edgeAdded(GraphEdgeChangeEvent)
219          */

220         public void edgeAdded(GraphEdgeChangeEvent<Object JavaDoc, DefaultEdge> e)
221         {
222             lastAddedEdge = e.getEdge();
223         }
224
225         /**
226          * @see GraphListener#edgeRemoved(GraphEdgeChangeEvent)
227          */

228         public void edgeRemoved(GraphEdgeChangeEvent<Object JavaDoc, DefaultEdge> e)
229         {
230             lastRemovedEdge = e.getEdge();
231         }
232
233         /**
234          * @see VertexSetListener#vertexAdded(GraphVertexChangeEvent)
235          */

236         public void vertexAdded(GraphVertexChangeEvent<Object JavaDoc> e)
237         {
238             lastAddedVertex = e.getVertex();
239         }
240
241         /**
242          * @see VertexSetListener#vertexRemoved(GraphVertexChangeEvent)
243          */

244         public void vertexRemoved(GraphVertexChangeEvent<Object JavaDoc> e)
245         {
246             lastRemovedVertex = e.getVertex();
247         }
248     }
249 }
250
Popular Tags