KickJava   Java API By Example, From Geeks To Geeks.

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


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 import junit.framework.*;
11 import com.hp.hpl.jena.joseki.* ;
12
13 import com.hp.hpl.jena.rdf.model.* ;
14
15 /** Harness for execution client library tests.
16  * Abstraction is that an operation should be performed
17  * and result in either a result model (all operations are
18  * operation: model->model) or an HttpExpection.
19  *
20  * @author Andy Seaborne
21  * @version $Id: ClientLibraryTest.java,v 1.6 2004/04/30 14:13:13 andy_seaborne Exp $
22  */

23
24 abstract class ClientLibraryTest extends TestCase
25 {
26     final Model emptyModel = ModelFactory.createDefaultModel() ;
27     PrintWriter out ; // Reporting channel
28

29     String JavaDoc modelURI ; // Target For operation
30
boolean expectedToSucceed ; // Expected outcome
31
Model expectedResult = null ;
32     HttpException expectedException = null ;
33     
34     static Log log = LogFactory.getLog(ClientLibraryTest.class) ;
35     
36     private ClientLibraryTest(PrintWriter w, String JavaDoc testName, String JavaDoc targetURI)
37     {
38         super("Joseki HTTP test: "+testName) ;
39         out = w ;
40         modelURI = targetURI ;
41     }
42     
43     /** Test an operation that should succed with
44      * a specific result.
45      * @param w
46      * @param testName
47      * @param targetURI
48      * @param resultModel
49      */

50     
51     ClientLibraryTest(PrintWriter w, String JavaDoc testName, String JavaDoc targetURI, Model resultModel)
52     {
53         this(w, testName, targetURI) ;
54         expectedToSucceed = true ;
55         expectedResult = resultModel ;
56     }
57
58     /** Test an operation that should fail with a specific exception
59      * @param w
60      * @param testName
61      * @param targetURI
62      * @param httpEx
63      */

64     ClientLibraryTest(PrintWriter w, String JavaDoc testName, String JavaDoc targetURI, HttpException httpEx)
65     {
66         this(w, testName, targetURI) ;
67         expectedToSucceed = false ;
68         expectedException = httpEx ;
69     }
70
71     /** Test an operation that should fail with a specific response code
72      *
73      * @param w
74      * @param testName
75      * @param targetURI
76      * @param responseCode
77      */

78     ClientLibraryTest(PrintWriter w, String JavaDoc testName, String JavaDoc targetURI, int responseCode)
79     {
80         this(w, testName, targetURI) ;
81         expectedToSucceed = false ;
82         expectedException = new HttpException(responseCode) ;
83     }
84
85     /** ClientLibraryTest an operation but don't know the outcome expcept
86      * whether it sould work or not.
87      * @param w
88      * @param testName
89      * @param shouldWork
90      */

91     
92     ClientLibraryTest(PrintWriter w, String JavaDoc testName, String JavaDoc targetURI, boolean shouldWork)
93     {
94         this(w, testName, targetURI) ;
95         expectedToSucceed = shouldWork ;
96     }
97
98
99     final protected void runTest() throws Throwable JavaDoc
100     {
101         try {
102             setUpTest() ;
103         } catch (HttpException httpEx)
104         {
105             assertTrue("Setting up failed", false) ;
106         }
107         
108         try {
109             log.info(getName()+" :: "+modelURI) ;
110             Model model = performTest() ;
111             assertTrue(this.getName()+" Test succeed but should not have", expectedToSucceed) ;
112             assertNull(this.getName()+" Test succeed but expection registered", expectedException) ;
113             if ( expectedResult != null )
114             {
115                 boolean passesTest = model.isIsomorphicWith(expectedResult) ;
116                  
117                 if ( ! passesTest )
118                 {
119                     out.println(this.getName()+" Expected --------------------------") ;
120                     dumpModel(expectedResult) ;
121                     out.println(this.getName()+" Got -------------------------------") ;
122                     dumpModel(model) ;
123                     out.println(this.getName()+" -----------------------------------") ;
124                 }
125                 assertTrue(this.getName()+" Result models not as expected", passesTest) ;
126             }
127         } catch (HttpException httpEx)
128         {
129             if ( expectedToSucceed )
130                 System.err.println(getName()+":: HttpException: "+org.joseki.util.Convert.decWWWForm(httpEx.getMessage())) ;
131             assertTrue(getName()+" was supposed to work! "+httpEx, !expectedToSucceed) ;
132             if ( expectedException != null )
133             {
134                 if ( expectedException.getResponseCode() != httpEx.getResponseCode() )
135                 {
136                     out.println(
137                         this.getName()+" Mismatch in response codes: Expected: "
138                             + expectedException.getResponseCode()
139                             + " Got:"
140                             + httpEx.getResponseCode());
141                     out.flush() ;
142                 }
143                 assertTrue(
144                     this.getName()
145                         + " [E:"
146                         + expectedException.getResponseCode()
147                         + "/G:"
148                         + httpEx.getResponseCode()
149                         + "]",
150                     expectedException.getResponseCode() == httpEx.getResponseCode());
151             }
152         }
153         
154         try {
155             takeDownTest() ;
156         } catch (HttpException httpEx)
157         {
158             assertTrue("Clearing up failed", false) ;
159         }
160         
161     }
162
163     abstract protected Model performTest() throws Throwable JavaDoc ;
164     
165     /** setUpTest is like JUnit setUp expect it is inside the test
166      * @throws Throwable
167      */

168     protected void setUpTest() throws Throwable JavaDoc
169     { }
170     
171     /** takeDownTest is like Junit tearDown expect it is inside the test
172      * @throws Throwable
173      */

174     protected void takeDownTest() throws Throwable JavaDoc
175     { }
176     
177     
178     
179     protected void dumpModel(Model m) throws Exception JavaDoc
180     {
181         m.write(out, "N3") ;
182         out.flush() ;
183     }
184 }
185
186
187 /*
188  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
189  * All rights reserved.
190  *
191  * Redistribution and use in source and binary forms, with or without
192  * modification, are permitted provided that the following conditions
193  * are met:
194  * 1. Redistributions of source code must retain the above copyright
195  * notice, this list of conditions and the following disclaimer.
196  * 2. Redistributions in binary form must reproduce the above copyright
197  * notice, this list of conditions and the following disclaimer in the
198  * documentation and/or other materials provided with the distribution.
199  * 3. The name of the author may not be used to endorse or promote products
200  * derived from this software without specific prior written permission.
201  *
202  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
203  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
204  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
205  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
206  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
207  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
208  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
209  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
210  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
211  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
212  */

213  
214
215
Popular Tags