1 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 23 24 abstract class ClientLibraryTest extends TestCase 25 { 26 final Model emptyModel = ModelFactory.createDefaultModel() ; 27 PrintWriter out ; 29 String modelURI ; boolean expectedToSucceed ; Model expectedResult = null ; 32 HttpException expectedException = null ; 33 34 static Log log = LogFactory.getLog(ClientLibraryTest.class) ; 35 36 private ClientLibraryTest(PrintWriter w, String testName, String targetURI) 37 { 38 super("Joseki HTTP test: "+testName) ; 39 out = w ; 40 modelURI = targetURI ; 41 } 42 43 50 51 ClientLibraryTest(PrintWriter w, String testName, String targetURI, Model resultModel) 52 { 53 this(w, testName, targetURI) ; 54 expectedToSucceed = true ; 55 expectedResult = resultModel ; 56 } 57 58 64 ClientLibraryTest(PrintWriter w, String testName, String targetURI, HttpException httpEx) 65 { 66 this(w, testName, targetURI) ; 67 expectedToSucceed = false ; 68 expectedException = httpEx ; 69 } 70 71 78 ClientLibraryTest(PrintWriter w, String testName, String targetURI, int responseCode) 79 { 80 this(w, testName, targetURI) ; 81 expectedToSucceed = false ; 82 expectedException = new HttpException(responseCode) ; 83 } 84 85 91 92 ClientLibraryTest(PrintWriter w, String testName, String targetURI, boolean shouldWork) 93 { 94 this(w, testName, targetURI) ; 95 expectedToSucceed = shouldWork ; 96 } 97 98 99 final protected void runTest() throws Throwable 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 ; 164 165 168 protected void setUpTest() throws Throwable 169 { } 170 171 174 protected void takeDownTest() throws Throwable 175 { } 176 177 178 179 protected void dumpModel(Model m) throws Exception 180 { 181 m.write(out, "N3") ; 182 out.flush() ; 183 } 184 } 185 186 187 213 214 215 | Popular Tags |