KickJava   Java API By Example, From Geeks To Geeks.

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


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  * GenericGraphsTest.java
27  * --------------------------
28  * (C) Copyright 2006-2006, by HartmutBenz and Contributors.
29  *
30  * Original Author: Hartmut Benz
31  * Contributor(s): John V. Sichi
32  *
33  * $Id: GenericGraphsTest.java 504 2006-07-03 02:37:26Z perfecthash $
34  *
35  * Changes
36  * -------
37  * ??-???-2006 : Initial revision (HB);
38  *
39  */

40 package org.jgrapht.graph;
41
42 import org.jgrapht.*;
43
44
45 /**
46  * A unit test for graph generic vertex/edge parameters.
47  *
48  * @author Hartmut Benz
49  */

50 public class GenericGraphsTest
51     extends EnhancedTestCase
52 {
53
54     //~ Instance fields -------------------------------------------------------
55

56     Graph<Object JavaDoc, ? extends DefaultEdge> objectGraph;
57     Graph<FooVertex, FooEdge> fooFooGraph;
58     Graph<BarVertex, BarEdge> barBarGraph;
59     Graph<FooVertex, BarEdge> fooBarGraph;
60
61     //~ Constructors ----------------------------------------------------------
62

63     /**
64      * @see junit.framework.TestCase#TestCase(java.lang.String)
65      */

66     public GenericGraphsTest(String JavaDoc name)
67     {
68         super(name);
69     }
70
71     //~ Methods ---------------------------------------------------------------
72

73     // ~ Methods ---------------------------------------------------------------
74

75     public void testLegalInsertStringGraph()
76     {
77         String JavaDoc v1 = "Vertex1";
78         Object JavaDoc v2 = "Vertex2";
79         objectGraph.addVertex(v1);
80         objectGraph.addVertex(v2);
81         objectGraph.addEdge(v1, v2);
82     }
83
84     public void testLegalInsertFooGraph()
85     {
86         FooVertex v1 = new FooVertex();
87         FooVertex v2 = new FooVertex();
88         BarVertex vb1 = new BarVertex();
89         BarVertex vb2 = new BarVertex();
90         fooFooGraph.addVertex(v1);
91         fooFooGraph.addVertex(v2);
92         fooFooGraph.addVertex(vb1);
93         fooFooGraph.addVertex(vb2);
94         fooFooGraph.addEdge(v1, v2);
95         fooFooGraph.addEdge(vb1, vb2);
96         fooFooGraph.addEdge(v1, vb2);
97         fooFooGraph.addEdge(v1, v2, new BarEdge());
98         fooFooGraph.addEdge(v1, vb2, new BarEdge());
99         fooFooGraph.addEdge(vb1, vb2, new BarEdge());
100     }
101
102     public void testLegalInsertBarGraph()
103     {
104         BarVertex v1 = new BarVertex();
105         BarVertex v2 = new BarVertex();
106         barBarGraph.addVertex(v1);
107         barBarGraph.addVertex(v2);
108         barBarGraph.addEdge(v1, v2);
109     }
110
111     public void testLegalInsertFooBarGraph()
112     {
113         FooVertex v1 = new FooVertex();
114         FooVertex v2 = new FooVertex();
115         BarVertex vb1 = new BarVertex();
116         BarVertex vb2 = new BarVertex();
117         fooFooGraph.addVertex(v1);
118         fooFooGraph.addVertex(v2);
119         fooFooGraph.addVertex(vb1);
120         fooFooGraph.addVertex(vb2);
121         fooFooGraph.addEdge(v1, v2);
122         fooFooGraph.addEdge(vb1, vb2);
123         fooFooGraph.addEdge(v1, vb2);
124     }
125
126     public void testAlissaHacker()
127     {
128         DirectedGraph<String JavaDoc, CustomEdge> g =
129             new DefaultDirectedGraph<String JavaDoc, CustomEdge>(CustomEdge.class);
130         g.addVertex("a");
131         g.addVertex("b");
132         g.addEdge("a", "b");
133         CustomEdge custom = g.getEdge("a", "b");
134         String JavaDoc s = custom.toString();
135         assertEquals("Alissa P. Hacker", s);
136     }
137
138     /**
139      * .
140      */

141     protected void setUp()
142     {
143         objectGraph =
144             new DefaultDirectedGraph<Object JavaDoc, DefaultEdge>(
145                 DefaultEdge.class);
146         fooFooGraph = new SimpleGraph<FooVertex, FooEdge>(FooEdge.class);
147         barBarGraph = new SimpleGraph<BarVertex, BarEdge>(BarEdge.class);
148     }
149
150     //~ Inner Classes ---------------------------------------------------------
151

152     public static class CustomEdge
153         extends DefaultEdge
154     {
155         private static final long serialVersionUID = 1L;
156
157         public String JavaDoc toString()
158         {
159             return "Alissa P. Hacker";
160         }
161     }
162
163     public static class FooEdge
164         extends DefaultEdge
165     {
166         private static final long serialVersionUID = 1L;
167     }
168
169     private class FooVertex
170     {
171         String JavaDoc str;
172
173         public FooVertex()
174         {
175             super();
176             str = "empty foo";
177         }
178
179         public FooVertex(String JavaDoc s)
180         {
181             str = s;
182         }
183     }
184
185     public static class BarEdge
186         extends FooEdge
187     {
188         private static final long serialVersionUID = 1L;
189     }
190
191     private class BarVertex
192         extends FooVertex
193     {
194         public BarVertex()
195         {
196             super("empty bar");
197         }
198
199         public BarVertex(String JavaDoc s)
200         {
201             super(s);
202         }
203     }
204 }
205
Popular Tags