KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdf > model > test > TestModelMakerImpl


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: TestModelMakerImpl.java,v 1.22 2005/03/10 14:35:35 chris-dollin Exp $
5 */

6
7 package com.hp.hpl.jena.rdf.model.test;
8
9 import com.hp.hpl.jena.rdf.model.impl.*;
10 import com.hp.hpl.jena.rdf.model.*;
11 import com.hp.hpl.jena.shared.*;
12 import com.hp.hpl.jena.util.iterator.*;
13 import com.hp.hpl.jena.graph.*;
14 import com.hp.hpl.jena.graph.test.*;
15
16 import java.util.*;
17
18 import junit.framework.*;
19
20 /**
21     Test ModelMakerImpl using a mock GraphMaker. This is as much an
22     exercise in learning testing technique as it is in actually doing the test ....
23
24     @author hedgehog
25 */

26 public class TestModelMakerImpl extends ModelTestBase
27     {
28     public TestModelMakerImpl(String JavaDoc name)
29         { super(name); }
30
31     public static TestSuite suite()
32         { return new TestSuite( TestModelMakerImpl.class ); }
33
34     private ModelMaker maker;
35     private Graph graph;
36     private GraphMaker graphMaker;
37
38     public void setUp()
39         {
40         graph = GraphTestBase.graphWith( "" );
41         graphMaker = new MockGraphMaker( graph );
42         maker = new ModelMakerImpl( graphMaker );
43         }
44
45     public void testClose()
46         {
47         maker.close();
48         checkHistory( one( "close()") );
49         }
50
51     public void testRemove()
52         {
53         maker.removeModel( "London" );
54         checkHistory( one( "remove(London)" ) );
55         }
56
57     public void testCreate()
58         {
59         maker.createModel();
60         checkHistory( one( "create()" ) );
61         }
62
63     public void testGet()
64         {
65         maker.getModel();
66         checkHistory( one( "get()" ) );
67         }
68
69     public void testCreateNamed()
70         {
71         Model m = maker.createModel( "petal" );
72         checkHistory( one("create(petal,false)" ) );
73         assertTrue( m.getGraph() == graph );
74         }
75
76     public void testCreateTrue()
77         {
78         Model m = maker.createModel( "stem", true );
79         checkHistory( one("create(stem,true)" ) );
80         assertTrue( m.getGraph() == graph );
81         }
82
83     public void testCreateFalse()
84         {
85         Model m = maker.createModel( "leaf", false );
86         checkHistory( one("create(leaf,false)" ) );
87         assertTrue( m.getGraph() == graph );
88         }
89
90     public void testOpen()
91         {
92         Model m = maker.openModel( "trunk" );
93         checkHistory( one("open(trunk,false)" ) );
94         assertTrue( m.getGraph() == graph );
95         }
96
97     public void testOpenFalse()
98         {
99         Model m = maker.openModel( "branch", false );
100         checkHistory( one("open(branch,false)" ) );
101         assertTrue( m.getGraph() == graph );
102         }
103
104     public void testOpenTrue()
105         {
106         Model m = maker.openModel( "bark", true );
107         checkHistory( one("open(bark,true)" ) );
108         assertTrue( m.getGraph() == graph );
109         }
110
111     public void testListGraphs()
112         {
113         maker.listModels().close();
114         checkHistory( one("listModels()" ) );
115         }
116
117     public void testGetGraphMaker()
118         {
119         assertTrue( maker.getGraphMaker() == graphMaker );
120         }
121
122     public void testGetDescription()
123         {
124         maker.getDescription();
125         checkHistory( one( "getDescription()" ) );
126         }
127
128     private void checkHistory( List expected )
129         { assertEquals( expected, history() ); }
130
131     private List history()
132         { return ((MockGraphMaker) maker.getGraphMaker()).history; }
133
134     private List one( String JavaDoc s )
135         {
136         List result = new ArrayList();
137         result.add( s );
138         return result;
139         }
140
141     static class MockGraphMaker implements GraphMaker
142         {
143         List history = new ArrayList();
144         Graph graph;
145
146         public MockGraphMaker( Graph graph )
147             { this.graph = graph; }
148
149         public ReificationStyle getReificationStyle()
150             {
151             history.add( "getReificationStyle()" );
152             return null;
153             }
154
155         public Graph getGraph()
156             {
157             history.add( "get()" );
158             return graph;
159             }
160
161         public Graph createGraph()
162             {
163             history.add( "create()" );
164             return graph;
165             }
166
167         public Graph createGraph( String JavaDoc name, boolean strict )
168             {
169             history.add( "create(" + name + "," + strict + ")" );
170             return graph;
171             }
172
173         public Graph createGraph( String JavaDoc name )
174             {
175             history.add( "create(" + name + ")" );
176             return graph;
177             }
178
179         public Graph openGraph( String JavaDoc name, boolean strict )
180             {
181             history.add( "open(" + name + "," + strict + ")" );
182             return graph;
183             }
184
185         public Graph openGraph( String JavaDoc name )
186             {
187             history.add( "open(" + name + ")" );
188             return graph;
189             }
190
191         public void removeGraph( String JavaDoc name )
192             {
193             history.add( "remove(" + name + ")" );
194             }
195
196         public boolean hasGraph( String JavaDoc name )
197             {
198             history.add( "has(" + name + ")" );
199             return false;
200             }
201
202         public Graph getDescription()
203             {
204             history.add( "getDescription()" );
205             return graphWith( "" );
206             }
207
208         public Graph getDescription( Node root )
209             {
210             history.add( "getDescription(Node)" );
211             return graphWith( "" );
212             }
213
214         public Graph addDescription( Graph desc, Node self )
215             {
216             history.add( "addDescription()" );
217             return desc;
218             }
219
220         public void close()
221             {
222             history.add( "close()" );
223             }
224
225         public ExtendedIterator listGraphs()
226             {
227             history.add( "listModels()" );
228             return NullIterator.instance;
229             }
230         }
231     }
232
233
234 /*
235     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
236     All rights reserved.
237
238     Redistribution and use in source and binary forms, with or without
239     modification, are permitted provided that the following conditions
240     are met:
241
242     1. Redistributions of source code must retain the above copyright
243        notice, this list of conditions and the following disclaimer.
244
245     2. Redistributions in binary form must reproduce the above copyright
246        notice, this list of conditions and the following disclaimer in the
247        documentation and/or other materials provided with the distribution.
248
249     3. The name of the author may not be used to endorse or promote products
250        derived from this software without specific prior written permission.
251
252     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
253     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
254     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
255     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
256     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
257     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
258     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
259     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
260     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
261     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
262 */

263
Popular Tags