FileDocCategorySizeDatePackage
TestAbstractFeedHandler.javaAPI DocApache Lucene 2.2.08643Sat Jun 16 22:20:52 BST 2007org.apache.lucene.gdata.servlet.handler

TestAbstractFeedHandler

public class TestAbstractFeedHandler extends TestCase
author
Simon Willnauer

Fields Summary
private org.easymock.MockControl
requestMockControl
private HttpServletRequest
mockRequest
private String
accountName
private org.easymock.MockControl
adminServiceMockControl
private org.apache.lucene.gdata.server.administration.AdminService
adminService
private org.apache.lucene.gdata.utils.ServiceFactoryStub
stub
private String
serviceName
private static String
fileDir
private static File
incomingFeed
BufferedReader
reader
Constructors Summary
Methods Summary
protected voidsetUp()

    
        
        try {
            
            GDataServerRegistry.getRegistry().registerComponent(StorageStub.class,null);
            GDataServerRegistry.getRegistry().registerComponent(ServiceFactoryStub.class,null);
        } catch (RegistryException e) {
            
            e.printStackTrace();
        }
    
        super.setUp();
        
        GDataServerRegistry.getRegistry().registerService(new ProvidedServiceStub());
       this.requestMockControl = MockControl.createControl(HttpServletRequest.class);
       this.adminServiceMockControl = MockControl.createControl(AdminService.class);
       this.adminService = (AdminService)this.adminServiceMockControl.getMock();
       this.mockRequest = (HttpServletRequest)this.requestMockControl.getMock();
       this.stub = (ServiceFactoryStub)GDataServerRegistry.getRegistry().lookup(ServiceFactory.class,ComponentType.SERVICEFACTORY);
       this.stub.setAdminService(this.adminService);
       this.reader =  new BufferedReader(new FileReader(incomingFeed));
    
protected voidtearDown()

        super.tearDown();
    
public voidtestCreateFeedFromRequest()

        
        this.requestMockControl.expectAndDefaultReturn(this.mockRequest 
                .getParameter("service"), this.serviceName);
        this.requestMockControl.expectAndReturn(this.mockRequest.getReader(),this.reader);
        this.requestMockControl.replay();
        AbstractFeedHandler handler = new InsertFeedHandler();
        try{
        ServerBaseFeed feed = handler.createFeedFromRequest(this.mockRequest);
        assertNotNull(feed.getId());
        
        }catch (Exception e) {
            e.printStackTrace();
            fail("unexpected exception -- "+e.getMessage());
            
        }
        this.requestMockControl.verify();
        this.requestMockControl.reset();
        /*
         * Test for not registered service
         */
        this.requestMockControl.expectAndDefaultReturn(this.mockRequest 
                .getParameter("service"), "some other service");
        this.requestMockControl.replay();
         handler = new InsertFeedHandler();
        try{
        ServerBaseFeed feed = handler.createFeedFromRequest(this.mockRequest);
        
        fail(" exception expected");
        }catch (FeedHandlerException e) {
            e.printStackTrace();
            assertEquals(HttpServletResponse.SC_NOT_FOUND,handler.getErrorCode());
        }
        this.requestMockControl.verify();
        
        this.requestMockControl.reset();
        /*
         * Test for IOException
         */
        this.requestMockControl.expectAndDefaultReturn(this.mockRequest 
                .getParameter("service"), this.serviceName);
        this.reader.close();
        this.requestMockControl.expectAndReturn(this.mockRequest.getReader(),this.reader);
        this.requestMockControl.replay();
         handler = new InsertFeedHandler();
        try{
        ServerBaseFeed feed = handler.createFeedFromRequest(this.mockRequest);
        
        fail(" exception expected");
        }catch (IOException e) {
            e.printStackTrace();
            assertEquals(HttpServletResponse.SC_BAD_REQUEST,handler.getErrorCode());
        }
        this.requestMockControl.verify();
        
        
        
        
    
public voidtestCreateRequestedAccount()

        this.requestMockControl.expectAndDefaultReturn(this.mockRequest 
                .getParameter(AbstractFeedHandler.PARAMETER_ACCOUNT), this.accountName);
        GDataAccount a = new GDataAccount();
        a.setName("helloworld");
        this.adminServiceMockControl.expectAndReturn(this.adminService.getAccount(this.accountName),a );
        this.requestMockControl.replay();
        this.adminServiceMockControl.replay();
        AbstractFeedHandler handler = new InsertFeedHandler();
        try{
            
            GDataAccount account = handler.createRequestedAccount(this.mockRequest);
       
        assertEquals(a,account);
        
        }catch (Exception e) {
            e.printStackTrace();
            fail("unexpected exception -- "+e.getMessage());
            
        }
        this.requestMockControl.verify();
        this.requestMockControl.reset();
        this.adminServiceMockControl.verify();
        this.adminServiceMockControl.reset();
        
        /*
         *Test for service exception 
         */
        
        this.requestMockControl.expectAndDefaultReturn(this.mockRequest 
                .getParameter(AbstractFeedHandler.PARAMETER_ACCOUNT), this.accountName);
        
        a.setName("helloworld");
        this.adminServiceMockControl.expectAndDefaultThrow(this.adminService.getAccount(this.accountName),new ServiceException(GDataResponse.BAD_REQUEST) );
        this.requestMockControl.replay();
        this.adminServiceMockControl.replay();
         handler = new InsertFeedHandler();
        try{
            
            GDataAccount account = handler.createRequestedAccount(this.mockRequest);
       
            fail(" exception expected ");
        
        }catch (Exception e) {
            e.printStackTrace();
            assertEquals(HttpServletResponse.SC_BAD_REQUEST,handler.getErrorCode());
            
        }
        this.requestMockControl.verify();
        this.requestMockControl.reset();
        this.adminServiceMockControl.verify();
        this.adminServiceMockControl.reset();