~ refactoring
authorAlexandre Bertails <bertails@gmail.com>
Sat, 27 Aug 2011 17:54:12 -0400
changeset 7 e6bf64866274
parent 6 392d9bd433b7
child 8 a0f7ac3d2722
~ refactoring
src/main/scala/Main.scala
src/test/scala/Test.scala
src/test/scala/utiltest.scala
--- a/src/main/scala/Main.scala	Fri Aug 26 14:02:12 2011 -0400
+++ b/src/main/scala/Main.scala	Sat Aug 27 17:54:12 2011 -0400
@@ -32,8 +32,7 @@
     case req @ Path(path) => {
       val baseURI = req.underlying.getRequestURL.toString  
       val fileOnDisk = new File(base, path)
-      
-      
+       
       def foo():(OutputStream, Model) = {
         // get empty model if file not on disk
         val model = ModelFactory.createDefaultModel()
@@ -48,7 +47,8 @@
           val parent = fileOnDisk.getParentFile
           if (! parent.exists) println(parent.mkdirs)
           val r = fileOnDisk.createNewFile()
-          logger.debug("Create file %s with success: %s" format (fileOnDisk.getAbsolutePath, r.toString))
+          logger.debug("Create file %s with success: %s" format
+              (fileOnDisk.getAbsolutePath, r.toString))
         }
         val fos = new FileOutputStream(fileOnDisk)
         (fos, model)
@@ -59,7 +59,7 @@
         try {
           m.read(fis, baseURI)
         } catch {
-          case je:JenaException => logger.error("File %s was eitheir empty or corrupted: considered as empty graph" format fileOnDisk.getAbsolutePath)
+          case je:JenaException => logger.error("File %s was either empty or corrupted: considered as empty graph" format fileOnDisk.getAbsolutePath)
         }
         fis.close()
         m
--- a/src/test/scala/Test.scala	Fri Aug 26 14:02:12 2011 -0400
+++ b/src/test/scala/Test.scala	Sat Aug 27 17:54:12 2011 -0400
@@ -11,21 +11,26 @@
 import com.hp.hpl.jena.query._
 import com.hp.hpl.jena.update._
 
-// a Handler that returns the http code
-object HttpCode {
-  def apply(req:Request):Handler[Int] = new Handler(req, (c, r, e) => c, null)
-}
+import org.w3.readwriteweb.utiltest._
 
 object ReadWriteWebSpec extends Specification with unfiltered.spec.jetty.Served {
 
   val base = new File("src/main/resources")
+  val joe = host / "2007/wiki/people/JoeLambda"
+  val joeOnDisk = new File(base, "2007/wiki/people/JoeLambda")
   
-  def post(req:Request, body:String) = (req <<< body).copy(method="POST")
-
+  doBeforeSpec {
+    if (joeOnDisk.exists) joeOnDisk.delete()    
+  }
+  
+  doAfterSpec {
+//    if (joeOnDisk.exists) joeOnDisk.delete()
+  }
+  
   def setup = { _.filter(new ReadWriteWeb(base).read) }
 
   val timBL = host / "People/Berners-Lee/card#i"
-    
+  
   "a GET on TimBL's FOAF profile" should {
     val (via, body) = Http(timBL >+ { req =>
       (req >:> { _("MS-Author-Via").head }, req as_str)
@@ -38,33 +43,51 @@
     }
   }
 
-  val joe = host / "2007/wiki/people/JoeLambda"
-  val joeOnDisk = new File(base, "2007/wiki/people/JoeLambda")
-  if (joeOnDisk.exists) joeOnDisk.delete()
     
-  val insert =
+  val insertQuery =
 """
-INSERT DATA { <http://dig.csail.xvm.mit.edu/2007/wiki/people/JoeLambda#JL> <http://xmlns.com/foaf/0.1/age> 66 }
+PREFIX foaf: <http://xmlns.com/foaf/0.1/>
+INSERT DATA { <http://dig.csail.xvm.mit.edu/2007/wiki/people/JoeLambda#JL> foaf:name "Joe Lambda" }
 """
         
-  "POSTing an INSERT query on Joe's URI" should {
+  "POSTing an INSERT query on Joe's URI (which does not exist yet)" should {
     "return a 200" in {
-      val httpCode:Int = Http(HttpCode(post(joe, insert)))
+      val httpCode:Int = Http(joe.post(insertQuery) get_statusCode)
       httpCode must_== 200
     }
     "create the corresponding file on disk" in {
       joeOnDisk must exist
     }
     "create a valid rdf document with exactly one triple" in {
-      val model = Http(joe >> { is => {
-        val m = ModelFactory.createDefaultModel()
-        m.read(is, joe.path)
-        m
-      } } )
+      val model = Http(joe as_model(joe.path) )
       model.size must_== 1
     }
   }
+  
+  val joeRDF =
+"""
+<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> 
+  <foaf:Person rdf:about="#JL" xmlns:foaf="http://xmlns.com/foaf/0.1/">
+    <foaf:name>Joe Lambda</foaf:name>
+    <foaf:homepage rdf:resource="/2007/wiki/people/JoeLambda" />
+  </foaf:Person>
+</rdf:RDF>
+"""
 
-  if (joeOnDisk.exists) joeOnDisk.delete()
-  
+//        <foaf:openid rdf:resource="/2007/wiki/people/JoeLambda" />
+//    <foaf:img rdf:resource="/2007/wiki/people/JoeLambda/images/me.jpg" />
+
+//  "PUTing an RDF document on Joe's URI (which now does exist)" should {
+//    "return a 200" in {
+//      val httpCode:Int = Http(joe.post(joeRDF) get_statusCode)
+//      httpCode must_== 200
+//    }
+//    "create a valid rdf document with exactly XXX triple" in {
+//      val model = Http(joe as_model(joe.path))
+//      model.size must_== 1
+//    }
+//  }
+    
+    
+    
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/test/scala/utiltest.scala	Sat Aug 27 17:54:12 2011 -0400
@@ -0,0 +1,58 @@
+package org.w3.readwriteweb
+
+import javax.servlet._
+import javax.servlet.http._
+import unfiltered.request._
+import unfiltered.response._
+import unfiltered.jetty._
+
+import java.io._
+import scala.io.Source
+
+import org.slf4j.{Logger, LoggerFactory}
+
+import com.hp.hpl.jena.rdf.model._
+import com.hp.hpl.jena.query._
+import com.hp.hpl.jena.update._
+
+import unfiltered.request._
+import unfiltered.response._
+import unfiltered.jetty._
+
+import dispatch._
+
+package object utiltest {
+
+  class RequestW(req:Request) {
+
+    def as_model(base:String, lang:String = "RDF/XML-ABBREV"):Handler[Model] =
+      req >> { is => modelFromInputStream(is, base) }
+
+    def post(body:String):Request =
+      (req <<< body).copy(method="POST")
+      
+    def get_statusCode:Handler[Int] = new Handler(req, (c, r, e) => c, null)
+    
+    def get:Request = req.copy(method="GET")
+    
+  }
+  
+  implicit def wrapRequest(req:Request):RequestW = new RequestW(req)
+  
+  def modelFromInputStream(is:InputStream, base:String, lang:String = "RDF/XML-ABBREV"):Model = {
+    val m = ModelFactory.createDefaultModel()
+    m.read(is, base, lang)
+    m
+  }
+  
+  def modelFromString(s:String, base:String, lang:String = "RDF/XML-ABBREV"):Model = {
+    val m = ModelFactory.createDefaultModel()
+    m.read(s, base, lang)
+    m
+  }
+  
+
+
+
+
+}
\ No newline at end of file