abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dand...@apache.org
Subject svn commit: r614618 [2/2] - in /incubator/abdera/java/branches/server_refactor/src: main/java/org/apache/abdera/protocol/server/ main/java/org/apache/abdera/protocol/server/context/ main/java/org/apache/abdera/protocol/server/impl/ main/java/org/apache...
Date Wed, 23 Jan 2008 18:49:32 GMT
Added: incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/customer/CustomerAdapter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/customer/CustomerAdapter.java?rev=614618&view=auto
==============================================================================
--- incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/customer/CustomerAdapter.java
(added)
+++ incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/customer/CustomerAdapter.java
Wed Jan 23 10:49:24 2008
@@ -0,0 +1,137 @@
+package org.apache.abdera.protocol.server.customer;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import javax.xml.namespace.QName;
+
+import org.apache.abdera.Abdera;
+import org.apache.abdera.factory.Factory;
+import org.apache.abdera.i18n.iri.IRI;
+import org.apache.abdera.model.Content;
+import org.apache.abdera.model.Element;
+import org.apache.abdera.model.Person;
+import org.apache.abdera.protocol.server.RequestContext;
+import org.apache.abdera.protocol.server.ResponseContext;
+import org.apache.abdera.protocol.server.context.ResponseContextException;
+import org.apache.abdera.protocol.server.impl.AbstractEntityCollectionAdapter;
+
+public class CustomerAdapter extends AbstractEntityCollectionAdapter<Customer> {
+  private static final String ID_PREFIX = "urn:acme:customer:";
+  
+  private final static AtomicInteger nextId = new AtomicInteger(1000);
+  private Map<Integer, Customer> customers = new HashMap<Integer, Customer>();
+  private Factory factory = new Abdera().getFactory();
+  
+  public String getId(RequestContext request) {
+    return "tag:example.org,2007:feed";
+  }
+
+  
+  public ResponseContext getCategories(RequestContext request) {
+    return null;
+  }
+  
+  @Override
+  public Customer postEntry(String title, IRI id, String summary, Date updated, List<Person>
authors,
+                            Content content, RequestContext request) throws ResponseContextException
{   
+    Customer customer = contentToCustomer(content);
+    customers.put(customer.getId(), customer);
+    
+    return customer;
+  }
+
+  private Customer contentToCustomer(Content content) {
+    Customer customer = new Customer();
+    
+    return contentToCustomer(content, customer);
+  }
+
+  private Customer contentToCustomer(Content content, Customer customer) {
+    Element firstChild = content.getFirstChild();
+    customer.setName(firstChild.getAttributeValue("name"));
+    customer.setId(nextId.incrementAndGet());
+    return customer;
+  }
+
+  public void deleteEntry(String resourceName, RequestContext request) throws ResponseContextException
{
+    Integer id = getIdFromResourceName(resourceName);
+    customers.remove(id);
+  }
+
+  public String getAuthor() {
+    return "Acme Industries";
+  }
+
+  @Override
+  public List<Person> getAuthors(Customer entry, RequestContext request) throws ResponseContextException
{
+    Person author = request.getAbdera().getFactory().newAuthor();
+    author.setName("Acme Industries");
+    return Arrays.asList(author);
+  }
+
+  public Object getContent(Customer entry, RequestContext request) {
+    Content content = factory.newContent();
+    Element customerEl = factory.newElement(new QName("customer"));
+    customerEl.setAttributeValue(new QName("name"), entry.getName());
+   
+    content.setValueElement(customerEl);
+    return content;
+  }
+
+  public Iterable<Customer> getEntries(RequestContext request) {
+    return customers.values();
+  }
+
+  public Customer getEntry(String resourceName, RequestContext request) throws ResponseContextException
{
+    Integer id = getIdFromResourceName(resourceName);
+    return customers.get(id);
+  }
+
+  private Integer getIdFromResourceName(String resourceName) throws ResponseContextException
{
+    int idx = resourceName.indexOf("-");
+    if (idx == -1) {
+      throw new ResponseContextException(404);
+    }
+    Integer id = new Integer(resourceName.substring(0, idx));
+    return id;
+  }
+
+  public Customer getEntryFromId(String id, RequestContext request) {
+    return customers.get(new Integer(id));
+  }
+
+  public String getId(Customer entry) {
+    // TODO: is this valid?
+    return ID_PREFIX + entry.getId();
+  }
+
+  public String getName(Customer entry) {
+    return entry.getId() + "-" + entry.getName().replaceAll(" ", "_");
+  }
+
+  public String getTitle(RequestContext request) {
+    return "Acme Customer Database";
+  }
+
+  public String getTitle(Customer entry) {
+    return entry.getName();
+  }
+
+  public Date getUpdated(Customer entry) {
+    return new Date();
+  }
+
+  @Override
+  public void putEntry(Customer entry, String title, Date updated, 
+                       List<Person> authors, String summary,
+                       Content content, RequestContext request) throws ResponseContextException
{
+    contentToCustomer(content, entry);
+  }
+
+}

Propchange: incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/customer/CustomerAdapter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/customer/CustomerAdapter.java
------------------------------------------------------------------------------
    svn:executable = *

Propchange: incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/customer/CustomerAdapter.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/customer/CustomerAdapterTest.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/customer/CustomerAdapterTest.java?rev=614618&view=auto
==============================================================================
--- incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/customer/CustomerAdapterTest.java
(added)
+++ incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/customer/CustomerAdapterTest.java
Wed Jan 23 10:49:24 2008
@@ -0,0 +1,125 @@
+package org.apache.abdera.protocol.server.customer;
+
+import java.io.IOException;
+import java.util.Date;
+
+import javax.xml.namespace.QName;
+
+import org.apache.abdera.Abdera;
+import org.apache.abdera.factory.Factory;
+import org.apache.abdera.i18n.iri.IRI;
+import org.apache.abdera.model.Base;
+import org.apache.abdera.model.Element;
+import org.apache.abdera.model.Entry;
+import org.apache.abdera.protocol.client.AbderaClient;
+import org.apache.abdera.protocol.client.ClientResponse;
+import org.apache.abdera.protocol.client.RequestOptions;
+import org.apache.abdera.protocol.server.Provider;
+import org.apache.abdera.protocol.server.impl.DefaultProvider;
+import org.apache.abdera.protocol.server.impl.SimpleWorkspaceInfo;
+import org.apache.abdera.protocol.server.servlet.AbderaServlet;
+import org.apache.abdera.writer.Writer;
+import org.apache.abdera.writer.WriterFactory;
+import org.mortbay.jetty.Server;
+import org.mortbay.jetty.servlet.Context;
+import org.mortbay.jetty.servlet.ServletHolder;
+
+import junit.framework.TestCase;
+
+public class CustomerAdapterTest extends TestCase {
+
+  private Server server;
+  private DefaultProvider customerProvider;
+
+  @Override
+  protected void setUp() throws Exception {
+    super.setUp();
+    
+    customerProvider = new DefaultProvider("^/([^\\/])+/");
+    
+    CustomerAdapter ca = new CustomerAdapter();
+    ca.setHref("foo/acme/customers");
+    
+    SimpleWorkspaceInfo wi = new SimpleWorkspaceInfo();
+    wi.setTitle("Customer Workspace");
+    wi.addCollection(ca);
+    
+    customerProvider.addWorkspace(wi);
+    
+    initializeJetty();
+  }
+
+  public void testCustomerProvider() throws Exception {
+
+    Abdera abdera = new Abdera();
+    Factory factory = abdera.getFactory();
+
+    AbderaClient client = new AbderaClient(abdera);
+
+    String base = "http://localhost:9002/";
+
+    // Testing of entry creation
+    IRI colUri = new IRI(base).resolve("foo/acme/customers"); // base +
+                                                          // docCollection.getHref().toString();
+    Entry entry = factory.newEntry();
+    entry.setTitle("This is ignored right now");
+    entry.setUpdated(new Date());
+    entry.addAuthor("Acme Industries");
+    entry.setId(factory.newUuidUri());
+    entry.setSummary("Customer document");
+    
+    Element customerEl = factory.newElement(new QName("customer"));
+    customerEl.setAttributeValue(new QName("name"), "Dan Diephouse");
+    entry.setContent(customerEl);
+
+    RequestOptions opts = new RequestOptions();
+    opts.setContentType("application/atom+xml;type=entry");
+    ClientResponse res = client.post(colUri.toString(), entry, opts);
+    assertEquals(201, res.getStatus());
+
+    // prettyPrint(abdera, res.getDocument());
+
+    IRI location = res.getLocation();
+    assertEquals(colUri + "/1001-Dan_Diephouse", 
+                 location.toString());
+
+    // GET the entry
+    res = client.get(location.toString());
+    assertEquals(200, res.getStatus());
+
+    // prettyPrint(abdera, res.getDocument());
+    org.apache.abdera.model.Document<Entry> entry_doc = res.getDocument();
+    entry = entry_doc.getRoot();
+
+    res = client.get(colUri + "/foobar");
+    assertEquals(404, res.getStatus());
+  }
+
+  protected void prettyPrint(Abdera abdera, Base doc) throws IOException {
+    WriterFactory factory = abdera.getWriterFactory();
+    Writer writer = factory.getWriter("prettyxml");
+    writer.writeTo(doc, System.out);
+    System.out.println();
+  }
+
+  private void initializeJetty() throws Exception {
+
+    server = new Server(9002);
+    Context root = new Context(server, "/", Context.NO_SESSIONS);
+    root.addServlet(new ServletHolder(new AbderaServlet() {
+
+      @Override
+      protected Provider createProvider() {
+        customerProvider.init(getAbdera(), null);
+        return customerProvider;
+      }
+    }), "/*");
+    server.start();
+  }
+
+  @Override
+  protected void tearDown() throws Exception {
+    if (server != null) server.stop();
+  }
+
+}

Propchange: incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/customer/CustomerAdapterTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/customer/CustomerAdapterTest.java
------------------------------------------------------------------------------
    svn:executable = *

Propchange: incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/customer/CustomerAdapterTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/JettyServer.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/JettyServer.java?rev=614618&r1=614617&r2=614618&view=diff
==============================================================================
--- incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/JettyServer.java
(original)
+++ incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/JettyServer.java
Wed Jan 23 10:49:24 2008
@@ -26,7 +26,7 @@
 
 public class JettyServer {
   
-  public static final int DEFAULT_PORT = 8080;
+  public static final int DEFAULT_PORT = 9002;
   
   private final int port;
   private Server server;

Modified: incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/basic/BasicTest.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/basic/BasicTest.java?rev=614618&r1=614617&r2=614618&view=diff
==============================================================================
--- incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/basic/BasicTest.java
(original)
+++ incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/basic/BasicTest.java
Wed Jan 23 10:49:24 2008
@@ -1,25 +1,26 @@
 /*
-* Licensed to the Apache Software Foundation (ASF) under one or more
-* contributor license agreements.  The ASF licenses this file to You
-* under the Apache License, Version 2.0 (the "License"); you may not
-* use this file except in compliance with the License.
-* You may obtain a copy of the License at
-*
-*     http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.  For additional information regarding
-* copyright in this work, please see the NOTICE file in the top level
-* directory of this distribution.
-*/
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  The ASF licenses this file to You
+ * under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.  For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
 package org.apache.abdera.protocol.server.test.basic;
 
 import java.io.ByteArrayInputStream;
 import java.util.Date;
 
+import junit.framework.Assert;
 import junit.framework.TestCase;
 
 import org.apache.abdera.Abdera;
@@ -37,119 +38,120 @@
 import org.apache.abdera.protocol.server.test.JettyServer;
 import org.apache.abdera.util.Constants;
 import org.apache.abdera.util.MimeTypeHelper;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
 
-public class BasicTest 
-  extends TestCase {
+public class BasicTest extends Assert {
 
   private static JettyServer server;
   private static Abdera abdera = Abdera.getInstance();
   private static AbderaClient client = new AbderaClient();
 
-  public BasicTest() {
-    try {
-      if (server == null) {
-        server = new JettyServer();
-        server.start(BasicProvider.class);
-      }
-    } catch (Exception e) {}
+  @BeforeClass
+  public static void setUp() throws Exception {
+    if (server == null) {
+      server = new JettyServer();
+      server.start(BasicProvider.class);
+    }
   }
   
-  private int count = 6;
-  
-  @Override protected void tearDown() throws Exception {
-    if (--count == 0) server.stop();
+  @AfterClass
+  public static void tearDown() throws Exception {
+    server.stop();
   }
-  
+  @Test
   public void testGetService() {
-    ClientResponse resp = client.get("http://localhost:8080/");
+    ClientResponse resp = client.get("http://localhost:9002/");
     assertNotNull(resp);
     assertEquals(resp.getType(), ResponseType.SUCCESS);
     assertTrue(MimeTypeHelper.isMatch(resp.getContentType().toString(), Constants.APP_MEDIA_TYPE));
     Document<Service> doc = resp.getDocument();
     Service service = doc.getRoot();
-    assertEquals(service.getWorkspaces().size(),1);
+    assertEquals(service.getWorkspaces().size(), 1);
     Workspace workspace = service.getWorkspace("Abdera");
-    assertEquals(workspace.getCollections().size(),1);
+    assertEquals(workspace.getCollections().size(), 1);
     Collection collection = workspace.getCollection("title for any sample feed");
     assertNotNull(collection);
     assertTrue(collection.acceptsEntry());
-    assertEquals(collection.getResolvedHref().toString(), "http://localhost:8080/sample");
+    assertEquals(collection.getResolvedHref().toString(), "http://localhost:9002/sample");
   }
-  
+  @Test
   public void testGetFeed() {
-    ClientResponse resp = client.get("http://localhost:8080/sample");
+    ClientResponse resp = client.get("http://localhost:9002/sample");
     assertNotNull(resp);
-    assertEquals(resp.getType(),ResponseType.SUCCESS);
+    assertEquals(resp.getType(), ResponseType.SUCCESS);
     assertTrue(MimeTypeHelper.isMatch(resp.getContentType().toString(), Constants.ATOM_MEDIA_TYPE));
     Document<Feed> doc = resp.getDocument();
     Feed feed = doc.getRoot();
-    assertEquals(feed.getId().toString(), "http://localhost:8080/sample");
+    assertEquals(feed.getId().toString(), "http://localhost:9002/sample");
     assertEquals(feed.getTitle(), "title for any sample feed");
     assertEquals(feed.getAuthor().getName(), "rayc");
     assertEquals(feed.getEntries().size(), 0);
     resp.release();
   }
-  
+  @Test
   public void testPostEntry() {
     Entry entry = abdera.newEntry();
-    entry.setId("http://localhost:8080/sample/foo");
+    entry.setId("http://localhost:9002/sample/foo");
     entry.setTitle("test entry");
     entry.setContent("Test Content");
     entry.addLink("http://example.org");
     entry.setUpdated(new Date());
     entry.addAuthor("James");
-    ClientResponse resp = client.post("http://localhost:8080/sample", entry);
+    ClientResponse resp = client.post("http://localhost:9002/sample", entry);
     assertNotNull(resp);
-    assertEquals(resp.getType(),ResponseType.SUCCESS);
+    assertEquals(resp.getType(), ResponseType.SUCCESS);
     assertEquals(resp.getStatus(), 201);
-    assertEquals(resp.getLocation().toString(), "http://localhost:8080/sample/foo");
-    resp = client.get("http://localhost:8080/sample");
+    assertEquals(resp.getLocation().toString(), "http://localhost:9002/sample/foo");
+    resp = client.get("http://localhost:9002/sample");
     Document<Feed> feed_doc = resp.getDocument();
     Feed feed = feed_doc.getRoot();
-    assertEquals(feed.getEntries().size(),1);
+    assertEquals(feed.getEntries().size(), 1);
     resp.release();
   }
-  
+  @Test
   public void testPostMedia() {
-    ByteArrayInputStream in = new ByteArrayInputStream(new byte[] {0x01,0x02,0x03,0x04});
+    ByteArrayInputStream in = new ByteArrayInputStream(new byte[] {0x01, 0x02, 0x03, 0x04});
     RequestOptions options = client.getDefaultRequestOptions();
     options.setContentType("application/octet-stream");
-    ClientResponse resp = client.post("http://localhost:8080/sample", in, options);
-    assertEquals(resp.getType(),ResponseType.CLIENT_ERROR);
+    ClientResponse resp = client.post("http://localhost:9002/sample", in, options);
+    assertEquals(resp.getType(), ResponseType.CLIENT_ERROR);
     assertEquals(resp.getStatus(), 415);
     resp.release();
   }
-  
+  @Test
   public void testPutEntry() {
-    ClientResponse resp = client.get("http://localhost:8080/sample/foo");
+    ClientResponse resp = client.get("http://localhost:9002/sample/foo");
     Document<Entry> doc = resp.getDocument();
     Entry entry = doc.getRoot();
     entry.setTitle("This is the modified title");
     resp.release();
-    resp = client.put("http://localhost:8080/sample/foo", entry);
+    resp = client.put("http://localhost:9002/sample/foo", entry);
     assertEquals(resp.getType(), ResponseType.SUCCESS);
     assertEquals(resp.getStatus(), 200);
     resp.release();
-    resp = client.get("http://localhost:8080/sample/foo");
+    resp = client.get("http://localhost:9002/sample/foo");
     doc = resp.getDocument();
     entry = doc.getRoot();
     assertEquals(entry.getTitle(), "This is the modified title");
     resp.release();
-    resp = client.get("http://localhost:8080/sample");
+    resp = client.get("http://localhost:9002/sample");
     Document<Feed> feed_doc = resp.getDocument();
     Feed feed = feed_doc.getRoot();
-    assertEquals(feed.getEntries().size(),1);
+    assertEquals(feed.getEntries().size(), 1);
     resp.release();
   }
-  
+
+  @Test
   public void testDeleteEntry() {
-    ClientResponse resp = client.delete("http://localhost:8080/sample/foo");    
-    assertEquals(resp.getType(),ResponseType.SUCCESS);
+    ClientResponse resp = client.delete("http://localhost:9002/sample/foo");
+    assertEquals(resp.getType(), ResponseType.SUCCESS);
     resp.release();
-    resp = client.get("http://localhost:8080/sample");
+    resp = client.get("http://localhost:9002/sample");
     Document<Feed> feed_doc = resp.getDocument();
     Feed feed = feed_doc.getRoot();
-    assertEquals(feed.getEntries().size(),0);
+    assertEquals(feed.getEntries().size(), 0);
     resp.release();
   }
 }

Modified: incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/impl/TestProvider.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/impl/TestProvider.java?rev=614618&r1=614617&r2=614618&view=diff
==============================================================================
--- incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/impl/TestProvider.java
(original)
+++ incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/impl/TestProvider.java
Wed Jan 23 10:49:24 2008
@@ -21,7 +21,7 @@
 import org.apache.abdera.protocol.server.impl.DefaultProvider;
 import org.apache.abdera.protocol.server.impl.RegexTargetResolver;
 import org.apache.abdera.protocol.server.impl.SimpleCollection;
-import org.apache.abdera.protocol.server.impl.SimpleWorkspace;
+import org.apache.abdera.protocol.server.impl.SimpleWorkspaceInfo;
 import org.apache.abdera.protocol.server.test.simple.SimpleAdapter;
 
 public class TestProvider 
@@ -35,7 +35,7 @@
         .setPattern("/atom/([^/#?;]+)(\\?[^#]*)?", TargetType.TYPE_COLLECTION, "collection")
         .setPattern("/atom/([^/#?]+)/([^/#?]+)(\\?[^#]*)?", TargetType.TYPE_ENTRY, "collection",
"entry")
     );        
-    SimpleWorkspace workspace = new SimpleWorkspace();
+    SimpleWorkspaceInfo workspace = new SimpleWorkspaceInfo();
     workspace.setTitle("My Blog");
     workspace.addCollection(
       new SimpleCollection(

Modified: incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/simple/SimpleAdapter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/simple/SimpleAdapter.java?rev=614618&r1=614617&r2=614618&view=diff
==============================================================================
--- incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/simple/SimpleAdapter.java
(original)
+++ incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/simple/SimpleAdapter.java
Wed Jan 23 10:49:24 2008
@@ -36,17 +36,34 @@
 import org.apache.abdera.protocol.server.TargetType;
 import org.apache.abdera.protocol.server.RequestContext.Scope;
 import org.apache.abdera.protocol.server.context.BaseResponseContext;
+import org.apache.abdera.protocol.server.context.ResponseContextException;
 import org.apache.abdera.protocol.server.context.StreamWriterResponseContext;
+import org.apache.abdera.protocol.server.impl.AbstractCollectionAdapter;
 import org.apache.abdera.util.Constants;
 import org.apache.abdera.util.MimeTypeHelper;
 import org.apache.abdera.writer.StreamWriter;
 
 @SuppressWarnings("unchecked")
-public class SimpleAdapter  
-  implements CollectionAdapter {
-    
-  private Document<Feed> feed_doc;
+public class SimpleAdapter extends AbstractCollectionAdapter {
   
+  @Override
+  public String getAuthor() throws ResponseContextException {
+    return "Simple McGee";
+  }
+
+  @Override
+  public String getId(RequestContext request) {
+    return "tag:example.org,2008:feed";
+  }
+
+  public String getHref(RequestContext request) {
+    return "atom/feed";
+  }
+
+  public String getTitle(RequestContext request) {
+    return "A simple feed";
+  }
+
   public ResponseContext extensionRequest(RequestContext request) {
     return ProviderHelper.notallowed(
       request, 
@@ -54,46 +71,24 @@
       ProviderHelper.getDefaultMethods(request));
   }
   
-  private Document<Feed> init_feed_doc(
-    Abdera abdera, 
-    RequestContext context) {
-      Feed feed = 
-        (Feed) context.getAttribute(
-          Scope.SESSION, 
-          "feed");
-      if (feed == null) {
-        Factory factory = abdera.getFactory();
-        feed = factory.newFeed();
-        try {
-          feed.setId("tag:example.org,2006:feed");
-          feed.setTitle("Simple");
-          feed.setUpdated(new Date());
-          feed.addLink("");
-          feed.addLink("","self");
-          feed.addAuthor("Simple");
-          feed.setBaseUri(context.getResolvedUri());
-        } catch (Exception e) {}
-        context.setAttribute(
-          Scope.SESSION, 
-          "feed", 
-          feed);
-      } 
-      return feed.getDocument();
-  }
-  
-  private synchronized Document<Feed> get_feed_doc(
-    RequestContext context) {
-      if (feed_doc == null)
-        feed_doc = 
-          init_feed_doc(
-            context.getAbdera(), 
-            context);
-    return feed_doc;
+  private Document<Feed> getFeedDocument(RequestContext context) throws ResponseContextException
{
+    Feed feed = (Feed)context.getAttribute(Scope.SESSION, "feed");
+    if (feed == null) {
+      feed = createFeedBase(context);
+      context.setAttribute(Scope.SESSION, "feed", feed);
+    }
+    return feed.getDocument();
   }
 
   public ResponseContext getFeed(
     RequestContext request) {
-      Document<Feed> feed = get_feed_doc(request); 
+      Document<Feed> feed;
+      try {
+        feed = getFeedDocument(request);
+      } catch (ResponseContextException e) {
+        return e.getResponseContext();
+      } 
+      
       return ProviderHelper.returnBase(
         feed, 
         200, 
@@ -137,8 +132,6 @@
     RequestContext request) {
       Abdera abdera = request.getAbdera();
       try {
-        if (!checkMediaType(request))
-          return ProviderHelper.notsupported(request);
         Document<Entry> entry_doc = 
           (Document<Entry>) request.getDocument(
             abdera.getParser()).clone();
@@ -148,9 +141,8 @@
             return ProviderHelper.badrequest(request);
           setEntryDetails(
             entry, 
-            abdera.getFactory().newUuidUri(), 
-            request.getTarget().getParameter("collection"));
-          Feed feed = get_feed_doc(request).getRoot();
+            abdera.getFactory().newUuidUri());
+          Feed feed = getFeedDocument(request).getRoot();
           feed.insertEntry(entry);
           feed.setUpdated(new Date());
           BaseResponseContext rc = 
@@ -173,25 +165,15 @@
         return ProviderHelper.badrequest(request);
     }
   }
-
-  private boolean checkMediaType(RequestContext request) {
-    MimeType contentType = request.getContentType();
-    String ctype = 
-      contentType != null ? 
-        contentType.toString() : 
-          null;
-    return ctype != null && MimeTypeHelper.isAtom(ctype);
-  }
   
   private void setEntryDetails(
     Entry entry, 
-    String id,
-    String collection) {
+    String id) {
       entry.setUpdated(new Date());
       entry.setEdited(entry.getUpdated());
       entry.getIdElement().setValue(id);
       entry.addLink(
-        "/atom/" + collection + "/" + entry.getId().toASCIIString(), 
+        "/atom/feed/" + entry.getId().toASCIIString(), 
         "edit");
   }
   
@@ -201,9 +183,6 @@
       Entry orig_entry = getAbderaEntry(request);
       if (orig_entry != null) {
         try {
-          if (!checkMediaType(request))
-            return ProviderHelper.notsupported(
-              request);
           Document<Entry> entry_doc = 
             (Document<Entry>) request.getDocument(
               abdera.getParser()).clone();
@@ -215,10 +194,9 @@
               return ProviderHelper.badrequest(request);
             setEntryDetails(
               entry, 
-              orig_entry.getId().toString(),
-              request.getTarget().getParameter("collection"));
+              orig_entry.getId().toString());
             orig_entry.discard();
-            Feed feed = get_feed_doc(request).getRoot();
+            Feed feed = getFeedDocument(request).getRoot();
             feed.insertEntry(entry);
             feed.setUpdated(new Date());
             return ProviderHelper.nocontent();
@@ -240,7 +218,7 @@
   private Entry getAbderaEntry(
     RequestContext request) {
       try { 
-        return get_feed_doc( 
+        return getFeedDocument( 
           request)
             .getRoot()
             .getEntry(
@@ -249,7 +227,7 @@
       return null;
   }
   
-  private String getEntryID(
+  public String getEntryID(
     RequestContext request) {
       if (request.getTarget().getType() != TargetType.TYPE_ENTRY) 
         return null;

Modified: incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/simple/SimpleProvider.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/simple/SimpleProvider.java?rev=614618&r1=614617&r2=614618&view=diff
==============================================================================
--- incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/simple/SimpleProvider.java
(original)
+++ incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/simple/SimpleProvider.java
Wed Jan 23 10:49:24 2008
@@ -1,20 +1,20 @@
 /*
-* Licensed to the Apache Software Foundation (ASF) under one or more
-* contributor license agreements.  The ASF licenses this file to You
-* under the Apache License, Version 2.0 (the "License"); you may not
-* use this file except in compliance with the License.
-* You may obtain a copy of the License at
-*
-*     http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.  For additional information regarding
-* copyright in this work, please see the NOTICE file in the top level
-* directory of this distribution.
-*/
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  The ASF licenses this file to You
+ * under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.  For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
 package org.apache.abdera.protocol.server.test.simple;
 
 import java.io.IOException;
@@ -26,60 +26,23 @@
 import org.apache.abdera.protocol.server.Target;
 import org.apache.abdera.protocol.server.TargetType;
 import org.apache.abdera.protocol.server.context.StreamWriterResponseContext;
-import org.apache.abdera.protocol.server.impl.AbstractWorkspaceProvider;
+import org.apache.abdera.protocol.server.impl.DefaultProvider;
+import org.apache.abdera.protocol.server.impl.DefaultWorkspaceManager;
 import org.apache.abdera.protocol.server.impl.RegexTargetResolver;
+import org.apache.abdera.protocol.server.impl.SimpleWorkspaceInfo;
 import org.apache.abdera.util.Constants;
 import org.apache.abdera.writer.StreamWriter;
 
-public class SimpleProvider 
-  extends AbstractWorkspaceProvider {
+public class SimpleProvider extends DefaultProvider {
 
-  private final CollectionAdapter adapter;
-  private final String name;
-  private final String href;
-  
   public SimpleProvider() {
-      this.adapter = new SimpleAdapter();
-      this.name = "Simple";
-      this.href = "/atom/feed";
-  }
-  
-  public CollectionAdapter getCollectionAdapter(
-    RequestContext request) {
-      return adapter;
-  }
-  
-  protected ResponseContext getServiceDocument(
-    RequestContext request) {
-      return 
-        new StreamWriterResponseContext(request.getAbdera()) {
-          protected void writeTo(
-            StreamWriter sw) 
-              throws IOException {
-            sw.startDocument()
-              .startService()
-              .startWorkspace()
-              .writeTitle(name)
-              .startCollection(href)
-              .writeTitle(name)
-              .writeAcceptsEntry()
-              .startCategories(false)
-              .endCategories()
-              .endCollection()
-              .endWorkspace()
-              .endService()
-              .endDocument();
-          }
-        }
-        .setStatus(200)
-        .setContentType(Constants.APP_MEDIA_TYPE);
-  }
- 
-  protected Resolver<Target> getTargetResolver(RequestContext request) {
-    return new RegexTargetResolver()
-      .setPattern("/atom(\\?[^#]*)?", TargetType.TYPE_SERVICE)
-      .setPattern("/atom/([^/#?]+);categories", TargetType.TYPE_CATEGORIES, "collection")
-      .setPattern("/atom/([^/#?;]+)(\\?[^#]*)?", TargetType.TYPE_COLLECTION, "collection")
-      .setPattern("/atom/([^/#?]+)/([^/#?]+)(\\?[^#]*)?", TargetType.TYPE_ENTRY, "collection","entry");
+    super("/atom");
+    
+    SimpleWorkspaceInfo wkspc = new SimpleWorkspaceInfo();
+    wkspc.setTitle("A Simple Workspace");
+    wkspc.addCollection(new SimpleAdapter());
+
+    addWorkspace(wkspc);
   }
+
 }

Modified: incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/simple/SimpleTest.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/simple/SimpleTest.java?rev=614618&r1=614617&r2=614618&view=diff
==============================================================================
--- incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/simple/SimpleTest.java
(original)
+++ incubator/abdera/java/branches/server_refactor/src/test/java/org/apache/abdera/protocol/server/test/simple/SimpleTest.java
Wed Jan 23 10:49:24 2008
@@ -1,28 +1,28 @@
 /*
-* Licensed to the Apache Software Foundation (ASF) under one or more
-* contributor license agreements.  The ASF licenses this file to You
-* under the Apache License, Version 2.0 (the "License"); you may not
-* use this file except in compliance with the License.
-* You may obtain a copy of the License at
-*
-*     http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.  For additional information regarding
-* copyright in this work, please see the NOTICE file in the top level
-* directory of this distribution.
-*/
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  The ASF licenses this file to You
+ * under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.  For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
 package org.apache.abdera.protocol.server.test.simple;
 
 import java.io.ByteArrayInputStream;
+import java.io.IOException;
 import java.util.Date;
 
-import junit.framework.TestCase;
-
 import org.apache.abdera.Abdera;
+import org.apache.abdera.model.Base;
 import org.apache.abdera.model.Categories;
 import org.apache.abdera.model.Collection;
 import org.apache.abdera.model.Document;
@@ -37,50 +37,60 @@
 import org.apache.abdera.protocol.server.test.JettyServer;
 import org.apache.abdera.util.Constants;
 import org.apache.abdera.util.MimeTypeHelper;
+import org.apache.abdera.writer.Writer;
+import org.apache.abdera.writer.WriterFactory;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
 
-public class SimpleTest 
-  extends TestCase {
+public class SimpleTest extends Assert {
 
   private static JettyServer server;
   private static Abdera abdera = Abdera.getInstance();
   private static AbderaClient client = new AbderaClient();
-
-  public SimpleTest() {
+  
+  @BeforeClass
+  public static void setUp() throws Exception {
     try {
-      if (server == null) {
-        server = new JettyServer();
-        server.start(SimpleProvider.class);
-      }
-    } catch (Exception e) {}
+      server = new JettyServer();
+      server.start(SimpleProvider.class);
+    } catch (Exception e) {
+      e.printStackTrace();
+    }
   }
-  
-  private int count = 7;
-  
-  @Override protected void tearDown() throws Exception {
-    if (--count == 0) 
-      server.stop();
+
+  @AfterClass
+  public static void tearDown() throws Exception {
+    server.stop();
   }
-  
+
+  @Test
   public void testGetService() {
-    ClientResponse resp = client.get("http://localhost:8080/atom");
+    ClientResponse resp = client.get("http://localhost:9002/atom");
     assertNotNull(resp);
-    assertEquals(resp.getType(),ResponseType.SUCCESS);
+    assertEquals(ResponseType.SUCCESS, resp.getType());
     assertTrue(MimeTypeHelper.isMatch(resp.getContentType().toString(), Constants.APP_MEDIA_TYPE));
     Document<Service> doc = resp.getDocument();
     Service service = doc.getRoot();
-    assertEquals(service.getWorkspaces().size(),1);
+    assertEquals(service.getWorkspaces().size(), 1);
     Workspace workspace = service.getWorkspaces().get(0);
-    assertEquals(workspace.getCollections().size(),1);
+    assertEquals(workspace.getCollections().size(), 1);
     Collection collection = workspace.getCollections().get(0);
-    assertEquals(collection.getResolvedHref().toString(), "http://localhost:8080/atom/feed");
-    assertEquals(collection.getTitle().toString(), "Simple");
+    assertEquals(collection.getResolvedHref().toString(), "http://localhost:9002/atom/feed");
+    assertEquals(collection.getTitle().toString(), "A simple feed");
     resp.release();
   }
-  
+
+  @Test
   public void testGetCategories() {
-    ClientResponse resp = client.get("http://localhost:8080/atom/feed;categories");
+    ClientResponse resp = client.get("http://localhost:9002/atom/feed;categories");
     assertNotNull(resp);
-    assertEquals(resp.getType(),ResponseType.SUCCESS);
+    assertEquals(ResponseType.SUCCESS, resp.getType());
     assertTrue(MimeTypeHelper.isMatch(resp.getContentType().toString(), Constants.CAT_MEDIA_TYPE));
     Document<Categories> doc = resp.getDocument();
     Categories cats = doc.getRoot();
@@ -90,65 +100,80 @@
     assertEquals(cats.getCategories().get(2).getTerm(), "baz");
     assertFalse(cats.isFixed());
   }
-  
-  public void testGetFeed() {
-    ClientResponse resp = client.get("http://localhost:8080/atom/feed");
+
+  @Test
+  public void testGetFeed() throws Exception {
+    ClientResponse resp = client.get("http://localhost:9002/atom/feed");
     assertNotNull(resp);
-    assertEquals(resp.getType(),ResponseType.SUCCESS);
+    assertEquals(ResponseType.SUCCESS, resp.getType());
     assertTrue(MimeTypeHelper.isMatch(resp.getContentType().toString(), Constants.ATOM_MEDIA_TYPE));
     Document<Feed> doc = resp.getDocument();
     Feed feed = doc.getRoot();
-    assertEquals(feed.getId().toString(), "tag:example.org,2006:feed");
-    assertEquals(feed.getTitle(), "Simple");
-    assertEquals(feed.getAuthor().getName(), "Simple");
+    assertEquals("tag:example.org,2008:feed", feed.getId().toString());
+    assertEquals(feed.getTitle(), "A simple feed");
+    assertEquals(feed.getAuthor().getName(), "Simple McGee");
     assertEquals(feed.getEntries().size(), 0);
     resp.release();
   }
-  
+
+  protected void prettyPrint(Base doc) throws IOException {
+      WriterFactory writerFactory = abdera.getWriterFactory();
+      Writer writer = writerFactory.getWriter("prettyxml");
+      writer.writeTo(doc, System.out);
+      System.out.println();
+  }
+
+
+  @Test
   public void testPostEntry() {
     Entry entry = abdera.newEntry();
-    entry.setId("http://localhost:8080/atom/feed/entries/1");
+    entry.setId("http://localhost:9002/atom/feed/entries/1");
     entry.setTitle("test entry");
     entry.setContent("Test Content");
     entry.addLink("http://example.org");
     entry.setUpdated(new Date());
     entry.addAuthor("James");
-    ClientResponse resp = client.post("http://localhost:8080/atom/feed", entry);
+    ClientResponse resp = client.post("http://localhost:9002/atom/feed", entry);
     assertNotNull(resp);
-    assertEquals(resp.getType(),ResponseType.SUCCESS);
+    assertEquals(ResponseType.SUCCESS, resp.getType());
     assertEquals(resp.getStatus(), 201);
     assertNotNull(resp.getLocation());
     resp.release();
-    resp = client.get("http://localhost:8080/atom/feed");
+    resp = client.get("http://localhost:9002/atom/feed");
     Document<Feed> feed_doc = resp.getDocument();
     Feed feed = feed_doc.getRoot();
-    assertEquals(feed.getEntries().size(),1);
+    assertEquals(feed.getEntries().size(), 1);
   }
-  
+
+  @Test
   public void testPostMedia() {
-    ByteArrayInputStream in = new ByteArrayInputStream(new byte[] {0x01,0x02,0x03,0x04});
+    ByteArrayInputStream in = new ByteArrayInputStream(new byte[] {0x01, 0x02, 0x03, 0x04});
     RequestOptions options = client.getDefaultRequestOptions();
     options.setContentType("application/octet-stream");
-    ClientResponse resp = client.post("http://localhost:8080/atom/feed", in, options);
-    assertEquals(resp.getType(),ResponseType.CLIENT_ERROR);
-    assertEquals(resp.getStatus(), 415);
+    ClientResponse resp = client.post("http://localhost:9002/atom/feed", in, options);
+    assertEquals(resp.getType(), ResponseType.CLIENT_ERROR);
+    assertEquals(resp.getStatus(), 405);
     resp.release();
   }
-  
-  public void testPutEntry() {
-    ClientResponse resp = client.get("http://localhost:8080/atom/feed");
+
+  @Test
+  public void testPutEntry() throws IOException {
+    ClientResponse resp = client.get("http://localhost:9002/atom/feed");
     Document<Feed> feed_doc = resp.getDocument();
     Feed feed = feed_doc.getRoot();
+    prettyPrint(feed);
     Entry entry = feed.getEntries().get(0);
     String edit = entry.getEditLinkResolvedHref().toString();
     resp.release();
     resp = client.get(edit);
     Document<Entry> doc = resp.getDocument();
+    doc.writeTo(System.out);
+    prettyPrint(doc.getRoot());
     entry = doc.getRoot();
     entry.setTitle("This is the modified title");
     resp.release();
     resp = client.put(edit, entry);
-    assertEquals(resp.getType(), ResponseType.SUCCESS);
+    assertEquals(ResponseType.SUCCESS, resp.getType());
     assertEquals(resp.getStatus(), 204);
     resp.release();
     resp = client.get(edit);
@@ -156,27 +181,28 @@
     entry = doc.getRoot();
     assertEquals(entry.getTitle(), "This is the modified title");
     resp.release();
-    resp = client.get("http://localhost:8080/atom/feed");
+    resp = client.get("http://localhost:9002/atom/feed");
     feed_doc = resp.getDocument();
     feed = feed_doc.getRoot();
-    assertEquals(feed.getEntries().size(),1);
+    assertEquals(feed.getEntries().size(), 1);
     resp.release();
   }
-  
+
+  @Test
   public void testDeleteEntry() {
-    ClientResponse resp = client.get("http://localhost:8080/atom/feed");
+    ClientResponse resp = client.get("http://localhost:9002/atom/feed");
     Document<Feed> feed_doc = resp.getDocument();
     Feed feed = feed_doc.getRoot();
     Entry entry = feed.getEntries().get(0);
     String edit = entry.getEditLinkResolvedHref().toString();
     resp.release();
-    resp = client.delete(edit);    
-    assertEquals(resp.getType(),ResponseType.SUCCESS);
+    resp = client.delete(edit);
+    assertEquals(ResponseType.SUCCESS, resp.getType());
     resp.release();
-    resp = client.get("http://localhost:8080/atom/feed");
+    resp = client.get("http://localhost:9002/atom/feed");
     feed_doc = resp.getDocument();
     feed = feed_doc.getRoot();
-    assertEquals(feed.getEntries().size(),0);
+    assertEquals(feed.getEntries().size(), 0);
     resp.release();
   }
 }



Mime
View raw message