[infinispan-commits] Infinispan SVN: r1863 - in trunk/server/rest/src: test/scala/org/infinispan/rest and 1 other directory.
infinispan-commits at lists.jboss.org
infinispan-commits at lists.jboss.org
Thu May 27 02:55:31 EDT 2010
Author: galder.zamarreno at jboss.com
Date: 2010-05-27 02:55:29 -0400 (Thu, 27 May 2010)
New Revision: 1863
Modified:
trunk/server/rest/src/main/scala/org/infinispan/rest/Server.scala
trunk/server/rest/src/main/scala/org/infinispan/rest/StartupListener.scala
trunk/server/rest/src/test/scala/org/infinispan/rest/IntegrationTest.scala
Log:
[ISPN-460] (REST server should pre-start caches to avoid concurrent startup issues) Fixed.
Modified: trunk/server/rest/src/main/scala/org/infinispan/rest/Server.scala
===================================================================
--- trunk/server/rest/src/main/scala/org/infinispan/rest/Server.scala 2010-05-26 17:18:47 UTC (rev 1862)
+++ trunk/server/rest/src/main/scala/org/infinispan/rest/Server.scala 2010-05-27 06:55:29 UTC (rev 1863)
@@ -9,118 +9,124 @@
import core.Response.{ResponseBuilder, Status}
import org.infinispan.remoting.MIMECacheEntry
import org.infinispan.manager._
-import org.infinispan.Cache
import org.codehaus.jackson.map.ObjectMapper
+import org.infinispan.{CacheException, Cache}
+/**
+ * // TODO
+ *
+ * @author Michael Neale
+ * @author Galder Zamarreno
+ * @since 4.0
+ */
@Path("/rest")
class Server(@Context request: Request, @HeaderParam("performAsync") useAsync: Boolean) {
- @GET
- @Path("/{cacheName}/{cacheKey}")
- def getEntry(@PathParam("cacheName") cacheName: String, @PathParam("cacheKey") key: String) = {
+ @GET
+ @Path("/{cacheName}/{cacheKey}")
+ def getEntry(@PathParam("cacheName") cacheName: String, @PathParam("cacheKey") key: String) = {
ManagerInstance.getEntry(cacheName, key) match {
- case b: MIMECacheEntry => {
- val lastMod = new Date(b.lastModified)
- request.evaluatePreconditions(lastMod, calcETAG(b)) match {
- case bldr: ResponseBuilder => bldr.build
- case null => Response.ok(b.data, b.contentType).lastModified(lastMod).tag(calcETAG(b)).build
- }
- }
- case s: String => Response.ok(s, "text/plain").build
- case obj: Any => {
- val variant = request.selectVariant(variantList)
- val selectedMediaType = if (variant != null) variant.getMediaType.toString else "application/x-java-serialized-object"
- selectedMediaType match {
- case MediaType.APPLICATION_JSON => Response.ok.`type`(selectedMediaType).entity(streamIt(jsonMapper.writeValue(_, obj))).build
- case MediaType.APPLICATION_XML => Response.ok.`type`(selectedMediaType).entity(streamIt(xstream.toXML(obj, _))).build
- case _ =>
- obj match {
- case ser: Serializable =>
- Response.ok.`type`("application/x-java-serialized-object").entity(streamIt(new ObjectOutputStream(_).writeObject(ser))).build
- case _ => Response.notAcceptable(variantList).build
- }
+ case b: MIMECacheEntry => {
+ val lastMod = new Date(b.lastModified)
+ request.evaluatePreconditions(lastMod, calcETAG(b)) match {
+ case bldr: ResponseBuilder => bldr.build
+ case null => Response.ok(b.data, b.contentType).lastModified(lastMod).tag(calcETAG(b)).build
+ }
+ }
+ case s: String => Response.ok(s, "text/plain").build
+ case obj: Any => {
+ val variant = request.selectVariant(variantList)
+ val selectedMediaType = if (variant != null) variant.getMediaType.toString else "application/x-java-serialized-object"
+ selectedMediaType match {
+ case MediaType.APPLICATION_JSON => Response.ok.`type`(selectedMediaType).entity(streamIt(jsonMapper.writeValue(_, obj))).build
+ case MediaType.APPLICATION_XML => Response.ok.`type`(selectedMediaType).entity(streamIt(xstream.toXML(obj, _))).build
+ case _ =>
+ obj match {
+ case ser: Serializable =>
+ Response.ok.`type`("application/x-java-serialized-object").entity(streamIt(new ObjectOutputStream(_).writeObject(ser))).build
+ case _ => Response.notAcceptable(variantList).build
+ }
- }
- }
- case null => Response status(Status.NOT_FOUND) build
+ }
+ }
+ case null => Response status (Status.NOT_FOUND) build
}
- }
+ }
- /** create a JAX-RS streaming output */
- def streamIt(action: (OutputStream) => Unit) = new StreamingOutput { def write(o: OutputStream) = {action(o)}}
+ /**create a JAX-RS streaming output */
+ def streamIt(action: (OutputStream) => Unit) = new StreamingOutput {def write(o: OutputStream) = {action(o)}}
-
- @HEAD
- @Path("/{cacheName}/{cacheKey}")
- def headEntry(@PathParam("cacheName") cacheName: String, @PathParam("cacheKey") key: String) = {
+ @HEAD
+ @Path("/{cacheName}/{cacheKey}")
+ def headEntry(@PathParam("cacheName") cacheName: String, @PathParam("cacheKey") key: String) = {
ManagerInstance.getEntry(cacheName, key) match {
- case b: MIMECacheEntry => {
- val lastMod = new Date(b.lastModified)
- request.evaluatePreconditions(lastMod, calcETAG(b)) match {
- case bldr: ResponseBuilder => bldr.build
- case null => Response.ok.`type`(b.contentType).lastModified(lastMod).tag(calcETAG(b)).build
- }
- }
- case x: Any => Response.ok.build
- case null => Response status(Status.NOT_FOUND) build
+ case b: MIMECacheEntry => {
+ val lastMod = new Date(b.lastModified)
+ request.evaluatePreconditions(lastMod, calcETAG(b)) match {
+ case bldr: ResponseBuilder => bldr.build
+ case null => Response.ok.`type`(b.contentType).lastModified(lastMod).tag(calcETAG(b)).build
+ }
+ }
+ case x: Any => Response.ok.build
+ case null => Response status (Status.NOT_FOUND) build
}
- }
+ }
- @PUT
- @POST
- @Path("/{cacheName}/{cacheKey}")
- def putEntry(@PathParam("cacheName") cacheName: String, @PathParam("cacheKey") key: String,
- @HeaderParam("Content-Type") mediaType: String, data: Array[Byte],
- @HeaderParam("timeToLiveSeconds") ttl: Long,
- @HeaderParam("maxIdleTimeSeconds") idleTime: Long) = {
- val cache = ManagerInstance.getCache(cacheName)
- if (request.getMethod == "POST" && cache.containsKey(key)) {
- Response.status(Status.CONFLICT).build()
- } else {
- val obj = if (mediaType == "application/x-java-serialized-object")
- new ObjectInputStream(new ByteArrayInputStream(data)).readObject
- else new MIMECacheEntry(mediaType, data)
- (ttl, idleTime, useAsync) match {
- case (0, 0, false) => cache.put(key, obj)
- case (x, 0, false) => cache.put(key, obj, ttl, TimeUnit.SECONDS)
- case (x, y, false) => cache.put(key, obj, ttl, TimeUnit.SECONDS, idleTime, TimeUnit.SECONDS)
- case (0, 0, true) => cache.putAsync(key, obj)
- case (x, 0, true) => cache.putAsync(key, obj, ttl, TimeUnit.SECONDS)
- case (x, y, true) => cache.putAsync(key, obj, ttl, TimeUnit.SECONDS, idleTime, TimeUnit.SECONDS)
- }
- Response.ok.build
- }
- }
+ @PUT
+ @POST
+ @Path("/{cacheName}/{cacheKey}")
+ def putEntry(@PathParam("cacheName") cacheName: String, @PathParam("cacheKey") key: String,
+ @HeaderParam("Content-Type") mediaType: String, data: Array[Byte],
+ @HeaderParam("timeToLiveSeconds") ttl: Long,
+ @HeaderParam("maxIdleTimeSeconds") idleTime: Long) = {
+ val cache = ManagerInstance.getCache(cacheName)
+ if (request.getMethod == "POST" && cache.containsKey(key)) {
+ Response.status(Status.CONFLICT).build()
+ } else {
+ val obj = if (mediaType == "application/x-java-serialized-object")
+ new ObjectInputStream(new ByteArrayInputStream(data)).readObject
+ else new MIMECacheEntry(mediaType, data)
+ (ttl, idleTime, useAsync) match {
+ case (0, 0, false) => cache.put(key, obj)
+ case (x, 0, false) => cache.put(key, obj, ttl, TimeUnit.SECONDS)
+ case (x, y, false) => cache.put(key, obj, ttl, TimeUnit.SECONDS, idleTime, TimeUnit.SECONDS)
+ case (0, 0, true) => cache.putAsync(key, obj)
+ case (x, 0, true) => cache.putAsync(key, obj, ttl, TimeUnit.SECONDS)
+ case (x, y, true) => cache.putAsync(key, obj, ttl, TimeUnit.SECONDS, idleTime, TimeUnit.SECONDS)
+ }
+ Response.ok.build
+ }
+ }
- @DELETE
- @Path("/{cacheName}/{cacheKey}")
- def removeEntry(@PathParam("cacheName") cacheName: String, @PathParam("cacheKey") key: String) = {
- if (useAsync) {
- ManagerInstance.getCache(cacheName).removeAsync(key)
- } else {
- ManagerInstance.getCache(cacheName).remove(key)
- }
- }
+ @DELETE
+ @Path("/{cacheName}/{cacheKey}")
+ def removeEntry(@PathParam("cacheName") cacheName: String, @PathParam("cacheKey") key: String) = {
+ if (useAsync) {
+ ManagerInstance.getCache(cacheName).removeAsync(key)
+ } else {
+ ManagerInstance.getCache(cacheName).remove(key)
+ }
+ }
- @DELETE
- @Path("/{cacheName}")
- def killCache(@PathParam("cacheName") cacheName: String) = {
- ManagerInstance.getCache(cacheName).stop
- }
+ @DELETE
+ @Path("/{cacheName}")
+ def killCache(@PathParam("cacheName") cacheName: String) = {
+ ManagerInstance.getCache(cacheName).clear
+ }
- def calcETAG(entry: MIMECacheEntry) = {
- new EntityTag(entry.contentType + entry.lastModified + entry.data.length)
+ def calcETAG(entry: MIMECacheEntry) = {
+ new EntityTag(entry.contentType + entry.lastModified + entry.data.length)
- }
+ }
- /** For dealing with binary entries in the cache */
- lazy val variantList = Variant.VariantListBuilder.newInstance.mediaTypes(MediaType.APPLICATION_XML_TYPE, MediaType.APPLICATION_JSON_TYPE).build
- lazy val jsonMapper = new ObjectMapper
- lazy val xstream = new XStream
+ /**For dealing with binary entries in the cache */
+ lazy val variantList = Variant.VariantListBuilder.newInstance.mediaTypes(MediaType.APPLICATION_XML_TYPE, MediaType.APPLICATION_JSON_TYPE).build
+ lazy val jsonMapper = new ObjectMapper
+ lazy val xstream = new XStream
}
@@ -129,11 +135,19 @@
*/
object ManagerInstance {
var instance: EmbeddedCacheManager = null
- def getCache(name: String) = {
- instance.getCache(name).asInstanceOf[Cache[String, Any]]
+
+ def getCache(name: String): Cache[String, Any] = {
+ if (name != DefaultCacheManager.DEFAULT_CACHE_NAME && !instance.getCacheNames.contains(name))
+ throw new CacheNotFoundException("Cache with name '" + name + "' not found amongst the configured caches")
+
+ if (name == DefaultCacheManager.DEFAULT_CACHE_NAME) instance.getCache[String, Any]
+ else instance.getCache(name)
}
- def getEntry(cacheName: String, key: String) : Any = {
- getCache(cacheName).get(key)
+
+ def getEntry(cacheName: String, key: String): Any = {
+ getCache(cacheName).get(key)
}
}
+class CacheNotFoundException(msg: String) extends CacheException(msg)
+
Modified: trunk/server/rest/src/main/scala/org/infinispan/rest/StartupListener.scala
===================================================================
--- trunk/server/rest/src/main/scala/org/infinispan/rest/StartupListener.scala 2010-05-26 17:18:47 UTC (rev 1862)
+++ trunk/server/rest/src/main/scala/org/infinispan/rest/StartupListener.scala 2010-05-27 06:55:29 UTC (rev 1863)
@@ -1,23 +1,33 @@
package org.infinispan.rest
-
import javax.servlet.{ServletContextListener, ServletContextEvent}
import org.infinispan.manager.DefaultCacheManager
+import scala.collection.JavaConversions._
-
/**
* To init the cache manager. Nice to do this on startup as any config problems will be picked up before any
* requests are attempted to be serviced. Less kitten carnage.
+ *
+ * @author Michael Neale
+ * @author Galder Zamarreno
+ * @since 4.0
*/
class StartupListener extends ServletContextListener {
val INFINISPAN_CONF = "infinispan.server.rest.cfg"
- def contextInitialized(ev: ServletContextEvent) = {
- ManagerInstance.instance = makeCacheManager(ev)
- ManagerInstance.instance.start
+ override def contextInitialized(ev: ServletContextEvent) {
+ ManagerInstance.instance = makeCacheManager(ev)
+ ManagerInstance.instance.start
+
+ // Start defined caches to avoid issues with lazily started caches
+ for (cacheName <- asIterator(ManagerInstance.instance.getCacheNames.iterator))
+ ManagerInstance.instance.getCache(cacheName)
+ // Finally, start default cache as well
+ ManagerInstance.instance.getCache[String, Any]
}
- def contextDestroyed(ev: ServletContextEvent) = ManagerInstance.instance.stop
+ override def contextDestroyed(ev: ServletContextEvent) = ManagerInstance.instance.stop
+
/** Prefer the system property, but also allow the servlet context to set the path to the config */
def makeCacheManager(ev: ServletContextEvent) =
(System.getProperty(INFINISPAN_CONF), ev.getServletContext.getAttribute(INFINISPAN_CONF)) match {
Modified: trunk/server/rest/src/test/scala/org/infinispan/rest/IntegrationTest.scala
===================================================================
--- trunk/server/rest/src/test/scala/org/infinispan/rest/IntegrationTest.scala 2010-05-26 17:18:47 UTC (rev 1862)
+++ trunk/server/rest/src/test/scala/org/infinispan/rest/IntegrationTest.scala 2010-05-27 06:55:29 UTC (rev 1863)
@@ -9,280 +9,286 @@
import java.io._
import org.testng.annotations.Test
import org.testng.Assert._
+import java.lang.reflect.Method
+import org.infinispan.manager.DefaultCacheManager
/**
* This tests using the Apache HTTP commons client library - but you could use anything
* Decided to do this instead of testing the Server implementation itself, as testing the impl directly was kind of too easy.
* (Given that RESTEasy does most of the heavy lifting !).
+ *
* @author Michael Neale
+ * @author Galder Zamarreno
+ * @since 4.0
*/
- at Test
+ at Test(groups = Array("functional"), testName = "rest.IntegrationTest")
class IntegrationTest {
+ val HOST = "http://localhost:8888/"
+ val cacheName = DefaultCacheManager.DEFAULT_CACHE_NAME
+ val fullPath = HOST + "rest/" + cacheName
+ //val HOST = "http://localhost:8080/infinispan/"
- val HOST = "http://localhost:8888/"
- //val HOST = "http://localhost:8080/infinispan/"
-
- def testBasicOperation = {
+ def testBasicOperation(m: Method) = {
+ // Now invoke...via HTTP
+ val client = new HttpClient
- //now invoke...via HTTP
- val client = new HttpClient
+ val fullPathKey = fullPath + "/" + m.getName
- val insert = new PutMethod(HOST + "rest/mycache/mydata")
- val initialXML = <hey>ho</hey>
+ val insert = new PutMethod(fullPathKey)
+ val initialXML = <hey>ho</hey>
- insert.setRequestBody(new ByteArrayInputStream(initialXML.toString.getBytes))
- insert.setRequestHeader("Content-Type", "application/octet-stream")
+ insert.setRequestBody(new ByteArrayInputStream(initialXML.toString.getBytes))
+ insert.setRequestHeader("Content-Type", "application/octet-stream")
- Client.call(insert)
+ Client.call(insert)
- assertEquals("", insert.getResponseBodyAsString.trim)
- assertEquals(HttpServletResponse.SC_OK, insert.getStatusCode)
+ assertEquals("", insert.getResponseBodyAsString.trim)
+ assertEquals(HttpServletResponse.SC_OK, insert.getStatusCode)
- val get = new GetMethod(HOST + "rest/mycache/mydata")
- Client.call(get)
- val bytes = get.getResponseBody
- assertEquals(bytes.size, initialXML.toString.getBytes.size)
- assertEquals(<hey>ho</hey>.toString, get.getResponseBodyAsString)
- val hdr: Header = get.getResponseHeader("Content-Type")
- assertEquals("application/octet-stream", hdr.getValue)
+ val get = new GetMethod(fullPathKey)
+ Client.call(get)
+ val bytes = get.getResponseBody
+ assertEquals(bytes.size, initialXML.toString.getBytes.size)
+ assertEquals(<hey>ho</hey>.toString, get.getResponseBodyAsString)
+ val hdr: Header = get.getResponseHeader("Content-Type")
+ assertEquals("application/octet-stream", hdr.getValue)
- val remove = new DeleteMethod(HOST + "rest/mycache/mydata");
- Client.call(remove)
- Client.call(get)
+ val remove = new DeleteMethod(fullPathKey);
+ Client.call(remove)
+ Client.call(get)
- assertEquals(HttpServletResponse.SC_NOT_FOUND, get.getStatusCode)
+ assertEquals(HttpServletResponse.SC_NOT_FOUND, get.getStatusCode)
- Client.call(insert)
- Client.call(get)
- assertEquals(<hey>ho</hey>.toString, get.getResponseBodyAsString)
+ Client.call(insert)
+ Client.call(get)
+ assertEquals(<hey>ho</hey>.toString, get.getResponseBodyAsString)
- val removeAll = new DeleteMethod(HOST + "rest/mycache");
- Client.call(removeAll)
+ val removeAll = new DeleteMethod(fullPath);
+ Client.call(removeAll)
- Client.call(get)
- assertEquals(HttpServletResponse.SC_NOT_FOUND, get.getStatusCode)
+ Client.call(get)
+ assertEquals(HttpServletResponse.SC_NOT_FOUND, get.getStatusCode)
- val bout = new ByteArrayOutputStream
- val oo = new ObjectOutputStream(bout)
- oo.writeObject(new MIMECacheEntry("foo", "hey".getBytes))
- oo.flush
- val byteData = bout.toByteArray
+ val bout = new ByteArrayOutputStream
+ val oo = new ObjectOutputStream(bout)
+ oo.writeObject(new MIMECacheEntry("foo", "hey".getBytes))
+ oo.flush
+ val byteData = bout.toByteArray
- val insertMore = new PutMethod(HOST + "rest/mycache/mydata")
+ val insertMore = new PutMethod(fullPathKey)
- insertMore.setRequestBody(new ByteArrayInputStream(byteData))
- insertMore.setRequestHeader("Content-Type", "application/octet-stream")
+ insertMore.setRequestBody(new ByteArrayInputStream(byteData))
+ insertMore.setRequestHeader("Content-Type", "application/octet-stream")
- Client.call(insertMore)
+ Client.call(insertMore)
- val getMore = new GetMethod(HOST + "rest/mycache/mydata")
- Client.call(getMore)
+ val getMore = new GetMethod(fullPathKey)
+ Client.call(getMore)
- val bytesBack = getMore.getResponseBody
- assertEquals(byteData.length, bytesBack.length)
+ val bytesBack = getMore.getResponseBody
+ assertEquals(byteData.length, bytesBack.length)
- val oin = new ObjectInputStream(new ByteArrayInputStream(bytesBack))
- val ce = oin.readObject.asInstanceOf[MIMECacheEntry]
- assertEquals("foo", ce.contentType)
+ val oin = new ObjectInputStream(new ByteArrayInputStream(bytesBack))
+ val ce = oin.readObject.asInstanceOf[MIMECacheEntry]
+ assertEquals("foo", ce.contentType)
+ }
- }
+ def testEmptyGet = {
+ assertEquals(
+ HttpServletResponse.SC_NOT_FOUND,
+ Client.call(new GetMethod(HOST + "rest/" + cacheName + "/nodata")).getStatusCode
+ )
+ }
- def testEmptyGet = {
- assertEquals(
- HttpServletResponse.SC_NOT_FOUND,
- Client.call(new GetMethod(HOST + "rest/emptycache/nodata")).getStatusCode
- )
- }
+ def testGet(m: Method) = {
+ val fullPathKey = fullPath + "/" + m.getName
+ val post = new PostMethod(fullPathKey)
+ post.setRequestHeader("Content-Type", "application/text")
+ post.setRequestBody("data")
+ Client.call(post)
- def testGet = {
- val post = new PostMethod(HOST + "rest/more2/data")
- post.setRequestHeader("Content-Type", "application/text")
- post.setRequestBody("data")
- Client.call(post)
+ val get = Client.call(new GetMethod(fullPathKey))
+ assertEquals(HttpServletResponse.SC_OK, get.getStatusCode)
+ assertNotNull(get.getResponseHeader("ETag").getValue)
+ assertNotNull(get.getResponseHeader("Last-Modified").getValue)
+ assertEquals("application/text", get.getResponseHeader("Content-Type").getValue)
+ assertEquals("data", get.getResponseBodyAsString)
+ }
- val get = Client.call(new GetMethod(HOST + "rest/more2/data"))
- assertEquals(HttpServletResponse.SC_OK, get.getStatusCode)
- assertNotNull(get.getResponseHeader("ETag").getValue)
- assertNotNull(get.getResponseHeader("Last-Modified").getValue)
- assertEquals("application/text", get.getResponseHeader("Content-Type").getValue)
- assertEquals("data", get.getResponseBodyAsString)
+ def testHead(m: Method) = {
+ val fullPathKey = fullPath + "/" + m.getName
+ val post = new PostMethod(fullPathKey)
+ post.setRequestHeader("Content-Type", "application/text")
+ post.setRequestBody("data")
+ Client.call(post)
+ val get = Client.call(new HeadMethod(fullPathKey))
+ assertEquals(HttpServletResponse.SC_OK, get.getStatusCode)
+ assertNotNull(get.getResponseHeader("ETag").getValue)
+ assertNotNull(get.getResponseHeader("Last-Modified").getValue)
+ assertEquals("application/text", get.getResponseHeader("Content-Type").getValue)
- }
+ assertNull(get.getResponseBodyAsString)
+ }
+ def testPostDuplicate(m: Method) = {
+ val fullPathKey = fullPath + "/" + m.getName
+ val post = new PostMethod(fullPathKey)
+ post.setRequestHeader("Content-Type", "application/text")
+ post.setRequestBody("data")
+ Client.call(post)
- def testHead = {
- val post = new PostMethod(HOST + "rest/more/data")
- post.setRequestHeader("Content-Type", "application/text")
- post.setRequestBody("data")
- Client.call(post)
+ //Should get a conflict as its a DUPE post
+ assertEquals(HttpServletResponse.SC_CONFLICT, Client.call(post).getStatusCode)
- val get = Client.call(new HeadMethod(HOST + "rest/more/data"))
- assertEquals(HttpServletResponse.SC_OK, get.getStatusCode)
- assertNotNull(get.getResponseHeader("ETag").getValue)
- assertNotNull(get.getResponseHeader("Last-Modified").getValue)
- assertEquals("application/text", get.getResponseHeader("Content-Type").getValue)
+ val put = new PutMethod(fullPathKey)
+ put.setRequestHeader("Content-Type", "application/text")
+ put.setRequestBody("data")
- assertNull(get.getResponseBodyAsString)
- }
+ //Should be all ok as its a put
+ assertEquals(HttpServletResponse.SC_OK, Client.call(put).getStatusCode)
+ }
- def testPostDuplicate() = {
- val post = new PostMethod(HOST + "rest/posteee/data")
- post.setRequestHeader("Content-Type", "application/text")
- post.setRequestBody("data")
- Client.call(post)
+ def testPutDataWithTimeToLive(m: Method) = {
+ val fullPathKey = fullPath + "/" + m.getName
+ val post = new PostMethod(fullPathKey)
+ post.setRequestHeader("Content-Type", "application/text")
+ post.setRequestHeader("timeToLiveSeconds", "2")
+ post.setRequestHeader("maxIdleTimeSeconds", "3")
+ post.setRequestBody("data")
+ Client.call(post)
- //Should get a conflict as its a DUPE post
- assertEquals(HttpServletResponse.SC_CONFLICT, Client.call(post).getStatusCode)
+ val get = Client.call(new GetMethod(fullPathKey))
+ assertEquals("data", get.getResponseBodyAsString)
- val put = new PutMethod(HOST + "rest/posteee/data")
- put.setRequestHeader("Content-Type", "application/text")
- put.setRequestBody("data")
+ Thread.sleep(3000)
+ Client.call(get)
+ assertEquals(HttpServletResponse.SC_NOT_FOUND, get.getStatusCode)
+ }
- //Should be all ok as its a put
- assertEquals(HttpServletResponse.SC_OK, Client.call(put).getStatusCode)
+ def testRemoveEntry(m: Method) = {
+ val fullPathKey = fullPath + "/" + m.getName
+ val put = new PostMethod(fullPathKey)
+ put.setRequestHeader("Content-Type", "application/text")
+ put.setRequestBody("data")
+ Client.call(put)
- }
+ assertEquals(HttpServletResponse.SC_OK, Client.call(new HeadMethod(fullPathKey)).getStatusCode)
- def testPutDataWithTimeToLive = {
- val post = new PostMethod(HOST + "rest/putttl/data")
- post.setRequestHeader("Content-Type", "application/text")
- post.setRequestHeader("timeToLiveSeconds", "2")
- post.setRequestHeader("maxIdleTimeSeconds", "3")
- post.setRequestBody("data")
- Client.call(post)
+ Client.call(new DeleteMethod(fullPathKey))
+ assertEquals(HttpServletResponse.SC_NOT_FOUND, Client.call(new HeadMethod(fullPathKey)).getStatusCode)
+ }
- val get = Client.call(new GetMethod(HOST + "rest/putttl/data"))
- assertEquals("data", get.getResponseBodyAsString)
+ def testWipeCacheBucket(m: Method) = {
+ val fullPathKey = fullPath + "/" + m.getName
+ val put = new PostMethod(fullPathKey)
+ put.setRequestHeader("Content-Type", "application/text")
+ put.setRequestBody("data")
+ Client.call(put)
- Thread.sleep(3000)
- Client.call(get)
- assertEquals(HttpServletResponse.SC_NOT_FOUND, get.getStatusCode)
- }
+ val put_ = new PostMethod(fullPathKey + "2")
+ put_.setRequestHeader("Content-Type", "application/text")
+ put_.setRequestBody("data")
+ Client.call(put_)
- def testRemoveEntry = {
- val put = new PostMethod(HOST + "rest/posteee/toremove")
- put.setRequestHeader("Content-Type", "application/text")
- put.setRequestBody("data")
- Client.call(put)
+ assertEquals(HttpServletResponse.SC_OK, Client.call(new HeadMethod(fullPathKey)).getStatusCode)
+ Client.call(new DeleteMethod(fullPath))
+ assertEquals(HttpServletResponse.SC_NOT_FOUND, Client.call(new HeadMethod(fullPathKey)).getStatusCode)
+ }
- assertEquals(HttpServletResponse.SC_OK, Client.call(new HeadMethod(HOST + "rest/posteee/toremove")).getStatusCode)
+ def testAsyncAddRemove(m: Method) = {
+ val fullPathKey = fullPath + "/" + m.getName
+ val put = new PostMethod(fullPathKey)
+ put.setRequestHeader("Content-Type", "application/text")
+ put.setRequestHeader("performAsync", "true")
+ put.setRequestBody("data")
+ Client.call(put)
- Client.call(new DeleteMethod(HOST + "rest/posteee/toremove"))
- assertEquals(HttpServletResponse.SC_NOT_FOUND, Client.call(new HeadMethod(HOST + "rest/posteee/toremove")).getStatusCode)
+ Thread.sleep(50)
+ assertEquals(HttpServletResponse.SC_OK, Client.call(new HeadMethod(fullPathKey)).getStatusCode)
- }
+ val del = new DeleteMethod(fullPathKey);
+ del.setRequestHeader("performAsync", "true")
+ Client.call(del)
+ Thread.sleep(50)
+ assertEquals(HttpServletResponse.SC_NOT_FOUND, Client.call(new HeadMethod(fullPathKey)).getStatusCode)
+ }
- def testWipeCacheBucket = {
- val put = new PostMethod(HOST + "rest/posteee/toremove")
- put.setRequestHeader("Content-Type", "application/text")
- put.setRequestBody("data")
- Client.call(put)
+ def testShouldCopeWithSerializable(m: Method) = {
+ val fullPathKey = fullPath + "/" + m.getName
+ Client.call(new GetMethod(fullPathKey))
- val put_ = new PostMethod(HOST + "rest/posteee/toremove2")
- put_.setRequestHeader("Content-Type", "application/text")
- put_.setRequestBody("data")
- Client.call(put_)
+ val obj = new MySer
+ obj.name = "mic"
+ ManagerInstance getCache(DefaultCacheManager.DEFAULT_CACHE_NAME) put (m.getName, obj)
+ ManagerInstance getCache(DefaultCacheManager.DEFAULT_CACHE_NAME) put (m.getName + "2", "hola")
+ ManagerInstance getCache(DefaultCacheManager.DEFAULT_CACHE_NAME) put (m.getName + "3", new MyNonSer)
- assertEquals(HttpServletResponse.SC_OK, Client.call(new HeadMethod(HOST + "rest/posteee/toremove")).getStatusCode)
- Client.call(new DeleteMethod(HOST + "rest/posteee"))
- assertEquals(HttpServletResponse.SC_NOT_FOUND, Client.call(new HeadMethod(HOST + "rest/posteee/toremove")).getStatusCode)
- }
+ //check we can get it back as an object...
+ val get = new GetMethod(fullPathKey);
+ get.setRequestHeader("Accept", "application/x-java-serialized-object")
+ Client.call(get)
+ assertEquals(HttpServletResponse.SC_OK, get.getStatusCode)
+ val in = new ObjectInputStream(get.getResponseBodyAsStream)
+ val res = in.readObject.asInstanceOf[MySer]
+ assertNotNull(res)
+ assertEquals("mic", res.name)
+ assertEquals("application/x-java-serialized-object", get.getResponseHeader("Content-Type").getValue)
- def testAsyncAddRemove = {
- val put = new PostMethod(HOST + "rest/posteee/async")
- put.setRequestHeader("Content-Type", "application/text")
- put.setRequestHeader("performAsync", "true")
- put.setRequestBody("data")
- Client.call(put)
+ val getStr = Client.call(new GetMethod(fullPathKey + "2"))
+ assertEquals("hola", getStr.getResponseBodyAsString)
+ assertEquals("text/plain", getStr.getResponseHeader("Content-Type").getValue)
- Thread.sleep(50)
- assertEquals(HttpServletResponse.SC_OK, Client.call(new HeadMethod(HOST + "rest/posteee/async")).getStatusCode)
+ //now check we can get it back as JSON if we want...
+ get.setRequestHeader("Accept", "application/json")
+ Client.call(get)
+ assertEquals("""{"name":"mic"}""", get.getResponseBodyAsString)
+ assertEquals("application/json", get.getResponseHeader("Content-Type").getValue)
- val del = new DeleteMethod(HOST + "rest/posteee/async");
- del.setRequestHeader("performAsync", "true")
- Client.call(del)
- Thread.sleep(50)
- assertEquals(HttpServletResponse.SC_NOT_FOUND, Client.call(new HeadMethod(HOST + "rest/posteee/async")).getStatusCode)
- }
+ //and why not XML
+ get.setRequestHeader("Accept", "application/xml")
+ Client.call(get)
+ assertEquals("application/xml", get.getResponseHeader("Content-Type").getValue)
+ assertTrue(get.getResponseBodyAsString.indexOf("<org.infinispan.rest.MySer>") > -1)
- @Test def shouldCopeWithSerializable = {
- Client.call(new GetMethod(HOST + "rest/wang/wangKey"))
+ //now check we can get it back as JSON if we want...
+ val get3 = new GetMethod(fullPathKey + "3")
+ get3.setRequestHeader("Accept", "application/json")
+ Client.call(get3)
+ assertEquals("""{"name":"mic"}""", get3.getResponseBodyAsString)
+ assertEquals("application/json", get3.getResponseHeader("Content-Type").getValue)
+ }
- val obj = new MySer
- obj.name = "mic"
- ManagerInstance getCache("wang") put("wangKey", obj)
- ManagerInstance getCache("wang") put("wangKey2", "hola")
- ManagerInstance getCache("wang") put("wangKey3", new MyNonSer)
+ def testInsertSerializableObjects(m: Method) = {
+ val fullPathKey = fullPath + "/" + m.getName
+ val put = new PutMethod(fullPathKey)
+ put.setRequestHeader("Content-Type", "application/x-java-serialized-object")
+ val bout = new ByteArrayOutputStream
+ new ObjectOutputStream(bout).writeObject(new MySer)
+ put.setRequestBody(new ByteArrayInputStream(bout.toByteArray))
+ Client.call(put)
+ val x = ManagerInstance.getCache(DefaultCacheManager.DEFAULT_CACHE_NAME).get(m.getName).asInstanceOf[MySer]
+ assertTrue(x.name == "mic")
+ }
- //check we can get it back as an object...
- val get = new GetMethod(HOST + "rest/wang/wangKey");
- get.setRequestHeader("Accept", "application/x-java-serialized-object")
- Client.call(get)
- assertEquals(HttpServletResponse.SC_OK, get.getStatusCode)
- val in = new ObjectInputStream(get.getResponseBodyAsStream)
- val res = in.readObject.asInstanceOf[MySer]
- assertNotNull(res)
- assertEquals("mic", res.name)
- assertEquals("application/x-java-serialized-object", get.getResponseHeader("Content-Type").getValue)
+}
- val getStr = Client.call(new GetMethod(HOST + "rest/wang/wangKey2"))
- assertEquals("hola", getStr.getResponseBodyAsString)
- assertEquals("text/plain", getStr.getResponseHeader("Content-Type").getValue)
+class MyNonSer {
+ var name: String = "mic"
- //now check we can get it back as JSON if we want...
- get.setRequestHeader("Accept", "application/json")
- Client.call(get)
- assertEquals("""{"name":"mic"}""", get.getResponseBodyAsString)
- assertEquals("application/json", get.getResponseHeader("Content-Type").getValue)
+ def getName = name
-
- //and why not XML
- get.setRequestHeader("Accept", "application/xml")
- Client.call(get)
- assertEquals("application/xml", get.getResponseHeader("Content-Type").getValue)
- assertTrue(get.getResponseBodyAsString.indexOf("<org.infinispan.rest.MySer>") > -1)
-
- //now check we can get it back as JSON if we want...
- val get3 = new GetMethod(HOST + "rest/wang/wangKey3")
- get3.setRequestHeader("Accept", "application/json")
- Client.call(get3)
- assertEquals("""{"name":"mic"}""", get3.getResponseBodyAsString)
- assertEquals("application/json", get3.getResponseHeader("Content-Type").getValue)
-
-
- }
-
-
- @Test def insertSerializableObjects = {
- val put = new PutMethod(HOST + "rest/posteee/something")
- put.setRequestHeader("Content-Type", "application/x-java-serialized-object")
- val bout = new ByteArrayOutputStream
- new ObjectOutputStream(bout).writeObject(new MySer)
- put.setRequestBody(new ByteArrayInputStream(bout.toByteArray))
- Client.call(put)
-
- val x = ManagerInstance.getCache("posteee").get("something").asInstanceOf[MySer]
- assertTrue(x.name == "mic")
-
- }
-
-
+ def setName(s: String) = {name = s}
}
+class MySer extends Serializable {
+ var name: String = "mic"
-class MyNonSer {
- var name: String = "mic"
- def getName = name
- def setName(s: String) = {name = s}
-}
+ /**These are needed for Jackson to Do Its Thing */
+ def getName = name
-class MySer extends Serializable {
- var name: String = "mic"
- /** These are needed for Jackson to Do Its Thing */
- def getName = name
- def setName(s: String) = {name = s}
+ def setName(s: String) = {name = s}
}
\ No newline at end of file
More information about the infinispan-commits
mailing list