Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * $Id: MuleResourceAdapterTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
   * --------------------------------------------------------------------------------------
   * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
   *
   * The software in this package is published under the terms of the CPAL v1.0
   * license, a copy of which has been included with this distribution in the
   * LICENSE.txt file.
   */
 
 
 package org.mule.module.jca;
 
 
 
 import  javax.resource.ResourceException;
 import  javax.resource.spi.ActivationSpec;
 import  javax.resource.spi.BootstrapContext;
 import  javax.resource.spi.InvalidPropertyException;
 import  javax.resource.spi.ResourceAdapter;
 import  javax.resource.spi.UnavailableException;
 import  javax.resource.spi.XATerminator;
 import  javax.resource.spi.endpoint.MessageEndpoint;
 import  javax.resource.spi.endpoint.MessageEndpointFactory;
 import  javax.resource.spi.work.WorkManager;
 
 {
 
     @Override
     protected void doSetUp() throws Exception
     {
          = new MuleResourceAdapter();
     }
 
     @Override
     protected void doTearDown() throws Exception
     {
          = null;
     }
 
     public void testResolveModelName() throws ResourceException
     {
         MuleActivationSpec activationSpec = new MuleActivationSpec();
         activationSpec.setModelName("activationSpecModelName");
         .setModelName("resourceAdaptorModelName");
         String modelName = .resolveModelName(activationSpec);
         assertEquals("activationSpecModelName"modelName);
     }
 
     public void testResolveModelNameFromResourceAdaptor() throws ResourceException
     {
         MuleActivationSpec activationSpec = new MuleActivationSpec();
         .setModelName("resourceAdaptorModelName");
         String modelName = .resolveModelName(activationSpec);
         assertEquals("resourceAdaptorModelName"modelName);
     }
 
     public void testResolveModelNameFromActivationSpec() throws ResourceException
     {
         MuleActivationSpec activationSpec = new MuleActivationSpec();
         activationSpec.setModelName("activationSpecModelName");
         String modelName = .resolveModelName(activationSpec);
         assertEquals("activationSpecModelName"modelName);
     }
 
     public void testResolveModelModelNameMissing()
     {
         MuleActivationSpec activationSpec = new MuleActivationSpec();
         try
         {
             .resolveModelName(activationSpec);
             fail("Exception expected: No modelName set.");
         }
         catch (ResourceException e)
         {
             // expected
         }
     }
 
     public void testGetJcaModelCreateNew() throws MuleException, ResourceException
     {
         JcaModel jcaModel = .getJcaModel("jca");
         assertEquals("jca"jcaModel.getName());
    }
    public void testGetJcaModelUseExisting() throws MuleException, ResourceException
    {
        Model jcaModel = new JcaModel();
        jcaModel.setName("jca");
        .getRegistry().registerModel(jcaModel);
        JcaModel jcaModel2 = .getJcaModel("jca");
        assertEquals("jca"jcaModel2.getName());
        assertEquals(jcaModeljcaModel2);
    }
    public void testGetJcaModel3ExistingWrongType() throws MuleException
    {
        Model sedaModel = new SedaModel();
        sedaModel.setName("jca");
        .getRegistry().registerModel(sedaModel);
        try
        {
            .getJcaModel("jca");
            fail("Exception Expected: Model is not JcaModel");
        }
        catch (Exception e)
        {
            // expected
        }
    }
    public void testCreateMessageInflowEndpoint() throws MuleException
    {
        MuleActivationSpec activationSpec = new MuleActivationSpec();
        activationSpec.setEndpoint("test://testEndpoint");
        ImmutableEndpoint endpoint = .createMessageInflowEndpoint(activationSpec);
        assertEndpointAttributes(endpoint);
    }
    public void testCreateJcaComponent() throws Exception
    {
        MessageEndpointFactory endpointFactory = new MockEndpointFactory();
        JcaModel jcaModel = .getJcaModel("jca");
        MuleActivationSpec activationSpec = new MuleActivationSpec();
        activationSpec.setEndpoint("test://testEndpoint");
        InboundEndpoint endpoint = .createMessageInflowEndpoint(activationSpec);
        Service service = .createJcaService(endpointFactoryjcaModelendpoint);
        // Check service
        assertNotNull(service);
        assertEquals("JcaService#" + endpointFactory.hashCode(), service.getName());
        assertNotNull(service);
        assertTrue(service instanceof JcaService);
        assertNotNull(service.getComponent());
        assertTrue(service.getComponent() instanceof JcaComponent);
        assertNotNull(((JcaComponentservice.getComponent()).);
        testJcaService(service);
        testEndpoint(service);
        // Check endpoint
        ImmutableEndpoint endpoint2 = ((ServiceCompositeMessageSourceservice.getMessageSource()).getEndpoints().get(0);
        assertEquals(endpointendpoint2);
        // Check service implementation
        assertEquals(endpointFactory, ((JcaComponentservice.getComponent()).);
    }
    public void testendpointActivationOK() throws Exception
    {
        MuleActivationSpec activationSpec = new MuleActivationSpec();
        activationSpec.setResourceAdapter();
        activationSpec.setModelName("jcaModel");
        activationSpec.setEndpoint("test://testEndpoint");
        MessageEndpointFactory endpointFactory = new MockEndpointFactory();
        .endpointActivation(endpointFactoryactivationSpec);
        MuleEndpointKey key = new MuleEndpointKey(endpointFactoryactivationSpec);
        Service service = ..get(key);
        assertEquals("JcaService#" + endpointFactory.hashCode(), service.getName());
        testJcaService(service);
        testEndpoint(service);
        
        assertTrue(service.getComponent() instanceof JcaComponent);
        
        assertEquals(endpointFactory, ((JcaComponentservice.getComponent()).);
        // Additional activation with same endpointFactory does not increase size of
        // endpoint cache.
        .endpointActivation(endpointFactoryactivationSpec);
        // Additional activation with new DefaultEndpointFactory instance increments size of
        // endpoint cahce
        .endpointActivation(new MockEndpointFactory(), activationSpec);
    }
    {
        try
        {
            ActivationSpec activationSpec = new MockActivationSpec();
            activationSpec.setResourceAdapter();
            .endpointActivation(new MockEndpointFactory(), activationSpec);
            fail("Exception expected: Invalid ActivationSpec type");
        }
        catch (ResourceException e)
        {
            // expected
        }
    }
    public void testendpointDeactivationOK() throws ResourceException
    {
        MuleActivationSpec activationSpec = new MuleActivationSpec();
        activationSpec.setResourceAdapter();
        activationSpec.setModelName("jcaModel");
        activationSpec.setEndpoint("test://test");
        MessageEndpointFactory endpointFactory = new MockEndpointFactory();
        .endpointActivation(endpointFactoryactivationSpec);
        .endpointDeactivation(endpointFactoryactivationSpec);
    }
    protected void testEndpoint(Service service)
    {
        ServiceCompositeMessageSource inboundRouterCollection = (ServiceCompositeMessageSourceservice.getMessageSource();
        ImmutableEndpoint endpoint = inboundRouterCollection.getEndpoints().get(0);
        assertEndpointAttributes(endpoint);
    }
    protected void assertEndpointAttributes(ImmutableEndpoint endpoint)
    {
        assertNotNull(endpoint);
        assertNotNull(endpoint.getConnector());
        assertEquals("testEndpoint"endpoint.getEndpointURI().getAddress());
    }
    protected void testJcaService(Service service)
    {
        // Check for a single inbound router, no outbound routers and no nested
        // routers
        assertNotNull(service);
        assertNotNull(service.getMessageSource());
        // InboundPassThroughRouter is now set in runtime rather than in service creation as in 1.4.x
        // assertEquals(1, service.getInboundRouter().getRouters().size());
    }
class MockEndpointFactory implements MessageEndpointFactory
    public MessageEndpoint createEndpoint(XAResource arg0throws UnavailableException
    {
        return null;
    }
    public boolean isDeliveryTransacted(Method arg0throws NoSuchMethodException
    {
        return false;
    }
class MockActivationSpec implements ActivationSpec
    public void validate() throws InvalidPropertyException
    {
        // do nothing
    }
    public ResourceAdapter getResourceAdapter()
    {
        return null;
    }
    public void setResourceAdapter(ResourceAdapter arg0throws ResourceException
    {
        // do nothing
    }
class MockBoostrapContext implements BootstrapContext
    public Timer createTimer() throws UnavailableException
    {
        return null;
    }
    public WorkManager getWorkManager()
    {
        return new TestJCAWorkManager();
    }
    public XATerminator getXATerminator()
    {
        return null;
    }
New to GrepCode? Check out our FAQ X