1 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 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 context = "Joseki Internal Tests" ; 34 static final String host = "http://test.org/" ; 35 static final String 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 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 modelURI = "/test" ; 58 String requestURL = "http://example.org/test" ; 59 Dispatcher dispatcher ; 60 Model targetModel = null ; PrintWriter out ; 62 63 Test(PrintWriter w, String 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 ; 84 85 protected void tearDown() 86 { 87 dispatcher.removeModelSource(modelURI) ; 88 DispatcherRegistry.getInstance().remove(context) ; 89 } 90 91 93 protected Model perform(String 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 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 testName) { super(w, testName) ; } 115 116 protected void runTest() throws Throwable 117 { 118 Model m = ModelFactory.createDefaultModel() ; 119 Property p = m.createProperty(baseName+"p") ; 120 Resource r = m.createResource(host+"res") ; 121 String 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 testName) { super(w, testName) ; } 133 protected void runTest() throws Throwable 134 { 135 { 137 Model m = ModelFactory.createDefaultModel() ; 138 Property p = m.createProperty(baseName+"p") ; 139 Resource r = m.createResource(host+"res") ; 140 String 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 { 150 Model m = ModelFactory.createDefaultModel() ; 151 Property p = m.createProperty(baseName+"p") ; 152 Resource r = m.createResource(host+"res") ; 153 String 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 testName) { super(w, testName) ; } 177 protected void runTest() throws Throwable 178 { 179 { 181 Model m = ModelFactory.createDefaultModel() ; 182 Property p = m.createProperty(baseName+"p") ; 183 Resource r = m.createResource(host+"res") ; 184 String 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 { 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 243 | Popular Tags |