Package test.app

Source Code of test.app.FeedTest

/*
* FeedTest.java
* JUnit based test
*
* Created on April 16, 2007, 9:56 AM
*/

package test.app;

import java.io.File;
import org.restlet.Client;
import org.restlet.Context;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.data.ChallengeResponse;
import org.restlet.data.ChallengeScheme;
import org.restlet.data.Form;
import org.restlet.data.MediaType;
import org.restlet.data.Reference;
import org.restlet.data.Status;
import org.restlet.representation.StringRepresentation;
import test.util.APPTestCase;
import test.util.TestConfig;

/**
*
* @author alex
*/
public class FeedTest extends APPTestCase
{
  
   TestConfig config;
  
   public FeedTest(String testName)
   {
      super(testName);
   }

   protected void setUp() throws Exception
   {
   }

   protected void tearDown() throws Exception
   {
   }
  
   public void testBasics()
      throws Exception
   {
      File dir = TestConfig.getTestDir("test.app.feed.basics");
      dir.delete();
     
      TestConfig config = new TestConfig("test.app.feed.basics","localhost","localhost",8080);
      // Make sure we start clean
      System.out.println("Using database directory: "+config.getDatabaseDirectory());
     
      config.startServer();
     
      Client client = new Client(new Context(),config.getServerLocation().getSchemeProtocol()) {
         public void handle(Request request,Response response) {
            request.setChallengeResponse(new ChallengeResponse(ChallengeScheme.HTTP_BASIC,"admin","admin"));
            super.handle(request,response);
         }
      };
     
      Reference rootFeed = new Reference(config.getServerLocation()+"R/");
     
      Response response;
      Form headers;
      String entryLocation;
      Reference entryRef;

      response = client.handle(makeHead(rootFeed));
      System.out.println("Status: "+response.getStatus());
      switch (response.getStatus().getCode()) {
         case 404:
         {
            System.out.println("Creating feed.");
            response = client.handle(makePost(rootFeed,new StringRepresentation("<feed xmlns='http://www.w3.org/2005/Atom'><title>Root</title></feed>",MediaType.APPLICATION_ATOM)));
            System.out.println("Status: "+response.getStatus());
            response.getEntity().release();
            assertTrue(response.getStatus().isSuccess());
            break;
         }
         default:
            assertTrue(response.getStatus().isSuccess());
      }
     
      response = client.handle(makePost(rootFeed,new StringRepresentation("<entry xmlns='http://www.w3.org/2005/Atom'><title>Entry 1</title></entry>",MediaType.APPLICATION_ATOM)));
      System.out.println("Status: "+response.getStatus());
      assertTrue(response.getStatus().isSuccess());
      headers = (Form)response.getAttributes().get("org.restlet.http.headers");
      entryLocation = headers.getValues("Location");
      System.out.println("Entry: "+entryLocation);
      //System.out.println("Entry: "+response.getEntity().getText());
      response.getEntity().release();
     
      entryRef = new Reference(entryLocation);
      response = client.handle(makeGet(entryRef));
      System.out.println("Status: "+response.getStatus());
      response.getEntity().release();
      assertTrue(response.getStatus().isSuccess());
     
      response = client.handle(makePost(rootFeed,new StringRepresentation("<entry xmlns='http://www.w3.org/2005/Atom'><title>Entry 2</title></entry>",MediaType.APPLICATION_ATOM)));
      System.out.println("Status: "+response.getStatus());
      response.getEntity().release();
      assertTrue(response.getStatus().isSuccess());
      entryLocation = headers.getValues("Location");
      System.out.println("Entry: "+entryLocation);
      response.getEntity().release();
     
      entryRef = new Reference(entryLocation);
      response = client.handle(makeGet(entryRef));
      System.out.println("Status: "+response.getStatus());
      assertTrue(response.getStatus().isSuccess());
      response.getEntity().release();
     
      response = client.handle(makePut(rootFeed,new StringRepresentation("<feed xmlns='http://www.w3.org/2005/Atom'><title>Root</title><category term='test'/><category term='bingo'/></feed>",MediaType.APPLICATION_ATOM)));
      System.out.println("Status: "+response.getStatus());
      assertTrue(response.getStatus().isSuccess());
      response.getEntity().release();
     
      config.stopServer();
   }

}
TOP

Related Classes of test.app.FeedTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.