KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joseki > test > JosekiServerInternalTests


1 /*
2  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  */

5
6 package org.joseki.test;
7
8 import java.io.* ;
9 import org.apache.commons.logging.*;
10
11 import junit.framework.*;
12 import org.joseki.server.* ;
13 import org.joseki.server.processors.*;
14 import org.joseki.server.source.*;
15
16
17 import com.hp.hpl.jena.rdf.model.*;
18
19 /** Tests of the mechanisms (attach, dispatch) of the server side.
20  *
21  * @author Andy Seaborne
22  * @version $Id: JosekiServerInternalTests.java,v 1.8 2004/04/30 14:13:13 andy_seaborne Exp $
23  */

24 public class JosekiServerInternalTests extends TestSuite
25 {
26     static Log log = LogFactory.getLog(ClientLibraryTest.class) ;
27     
28     static public TestSuite suite() {
29         return new JosekiServerInternalTests() ;
30     }
31     
32     
33     static final String JavaDoc context = "Joseki Internal Tests" ;
34     static final String JavaDoc host = "http://test.org/" ;
35     static final String JavaDoc baseName = host+"namespace#" ;
36     int testCounter = 1 ;
37     
38     public JosekiServerInternalTests()
39     {
40         super("Joseki Internal Tests") ;
41         init() ;
42         PrintWriter writer = null ;
43         try {
44             writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out, "UTF-8"))) ;
45         } catch (java.io.UnsupportedEncodingException JavaDoc ex) {}
46         addTest(new AddTest(writer, "HttpAdd-"+(testCounter++))) ;
47         addTest(new RemoveTest(writer, "Remove-"+(testCounter++))) ;
48         addTest(new QueryTest(writer, "Query-"+(testCounter++))) ;
49     }
50     
51     private void init()
52     {
53     }
54     
55     abstract class Test extends TestCase
56     {
57         String JavaDoc modelURI = "/test" ;
58         String JavaDoc requestURL = "http://example.org/test" ;
59         Dispatcher dispatcher ;
60         Model targetModel = null ; // The server side model
61
PrintWriter out ;
62             
63         Test(PrintWriter w, String JavaDoc testName)
64         {
65             super("Joseki Internal test: "+testName) ;
66             out = w ;
67         }
68     
69         protected void setUp()
70         {
71             dispatcher = new Dispatcher() ;
72             DispatcherRegistry.getInstance().add(context, dispatcher) ;
73             dispatcher = DispatcherRegistry.getInstance().find(context) ;
74             targetModel = ModelFactory.createDefaultModel() ;
75             ModelSource aModel = new ModelSourcePermanent(null, targetModel, modelURI) ;
76             dispatcher.addModelSource(aModel, aModel.getServerURI()) ;
77             dispatcher.addQueryProcessor(aModel.getServerURI(), "RDQL", new QueryProcessorRDQL()) ;
78             dispatcher.addQueryProcessor(aModel.getServerURI(), "GET", new QueryProcessorGET()) ;
79             dispatcher.addProcessor(aModel.getServerURI(), "add", new AddProcessor()) ;
80             dispatcher.addProcessor(aModel.getServerURI(), "remove", new RemoveProcessor()) ;
81         }
82         
83         abstract protected void runTest() throws Throwable JavaDoc ;
84         
85         protected void tearDown()
86         {
87             dispatcher.removeModelSource(modelURI) ;
88             DispatcherRegistry.getInstance().remove(context) ;
89         }
90     
91         // Building block operations
92

93         protected Model perform(String JavaDoc opName, Model m) throws ExecutionException
94         {
95             log.info(getName()+": "+opName+" :: "+modelURI) ;
96             Request request = dispatcher.createOperation(modelURI, requestURL, opName) ;
97             if ( m != null )
98                 request.addArg(m) ;
99             Model resultModel = dispatcher.exec(request) ;
100             return resultModel ;
101         }
102         
103         protected void dumpModel(Model m) throws Exception JavaDoc
104         {
105             m.write(out, "N3") ;
106             out.flush() ;
107         }
108
109     }
110
111     
112     class AddTest extends Test
113     {
114         AddTest(PrintWriter w, String JavaDoc testName) { super(w, testName) ; }
115         
116         protected void runTest() throws Throwable JavaDoc
117         {
118             Model m = ModelFactory.createDefaultModel() ;
119             Property p = m.createProperty(baseName+"p") ;
120             Resource r = m.createResource(host+"res") ;
121             String JavaDoc object = "value" ;
122             m.add(r,p ,object) ;
123             Model resultModel = perform("add", m) ;
124             assertNotNull(resultModel) ;
125             assertTrue(targetModel.contains(r, p, object)) ;
126         }
127     }
128     
129     
130     class RemoveTest extends Test
131     {
132         RemoveTest(PrintWriter w, String JavaDoc testName) { super(w, testName) ; }
133         protected void runTest() throws Throwable JavaDoc
134         {
135             // HttpAdd one statement
136
{
137                 Model m = ModelFactory.createDefaultModel() ;
138                 Property p = m.createProperty(baseName+"p") ;
139                 Resource r = m.createResource(host+"res") ;
140                 String JavaDoc object = "value" ;
141                 m.add(r, p, object) ;
142                 Model resultModel = perform("add", m) ;
143
144                 assertNotNull(resultModel) ;
145                 assertTrue(targetModel.contains(r, p, object)) ;
146                 assertTrue(targetModel.isIsomorphicWith(m)) ;
147             }
148             // Now remove it.
149
{
150                 Model m = ModelFactory.createDefaultModel() ;
151                 Property p = m.createProperty(baseName+"p") ;
152                 Resource r = m.createResource(host+"res") ;
153                 String JavaDoc object = "value" ;
154                 m.add(r, p, object) ;
155                 Model resultModel = perform("remove", m) ;
156
157                 assertNotNull(resultModel) ;
158                 assertTrue(! targetModel.contains(r, p, object)) ;
159                 boolean passesTest = targetModel.isIsomorphicWith(ModelFactory.createDefaultModel()) ;
160                 
161                 if ( ! passesTest )
162                 {
163                     out.println(this.getName()+" Expected --------------------------") ;
164                     dumpModel(targetModel) ;
165                     out.println(this.getName()+" -----------------------------------") ;
166                 }
167                     
168                 assertTrue(passesTest) ;
169                 
170             }
171         }
172     }
173     
174     class QueryTest extends Test
175     {
176         QueryTest(PrintWriter w, String JavaDoc testName) { super(w, testName) ; }
177         protected void runTest() throws Throwable JavaDoc
178         {
179             // HttpAdd one statement
180
{
181                 Model m = ModelFactory.createDefaultModel() ;
182                 Property p = m.createProperty(baseName+"p") ;
183                 Resource r = m.createResource(host+"res") ;
184                 String JavaDoc object = "value" ;
185                 m.add(r, p, object) ;
186                 Model resultModel = perform("add", m) ;
187
188                 assertNotNull(resultModel) ;
189                 assertTrue(targetModel.contains(r, p, object)) ;
190                 assertTrue(targetModel.isIsomorphicWith(m)) ;
191             }
192             // Query it
193
{
194                 log.info(getName()+": query :: "+modelURI);
195                 Request request = dispatcher.createQueryRequest(modelURI, requestURL, "RDQL") ;
196                 request.setParam("lang", "RDQL") ;
197                 request.setParam("query", "SELECT * WHERE (?x, ?y, ?z)") ;
198                 Model resultModel = dispatcher.exec(request) ;
199                 assertNotNull(resultModel) ;
200                 boolean passesTest = targetModel.isIsomorphicWith(resultModel) ;
201                 if ( ! passesTest )
202                 {
203                     out.println(this.getName()+" Expected --------------------------") ;
204                     dumpModel(targetModel) ;
205                     out.println(this.getName()+" Got -------------------------------") ;
206                     dumpModel(resultModel) ;
207                     out.println(this.getName()+" -----------------------------------") ;
208                 }
209                 
210                 assertTrue(passesTest) ;
211             }
212         }
213     }
214 }
215
216
217 /*
218  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
219  * All rights reserved.
220  *
221  * Redistribution and use in source and binary forms, with or without
222  * modification, are permitted provided that the following conditions
223  * are met:
224  * 1. Redistributions of source code must retain the above copyright
225  * notice, this list of conditions and the following disclaimer.
226  * 2. Redistributions in binary form must reproduce the above copyright
227  * notice, this list of conditions and the following disclaimer in the
228  * documentation and/or other materials provided with the distribution.
229  * 3. The name of the author may not be used to endorse or promote products
230  * derived from this software without specific prior written permission.
231  *
232  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
233  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
234  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
235  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
236  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
237  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
238  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
239  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
240  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
241  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
242  */

243
Popular Tags