1 22 package org.objectweb.petals.jbi.component.lifecycle; 23 24 import javax.jbi.JBIException; 25 import javax.management.MalformedObjectNameException ; 26 import javax.management.ObjectName ; 27 28 import junit.framework.TestCase; 29 30 import org.easymock.classextension.EasyMock; 31 import org.objectweb.petals.jbi.component.context.ComponentContextImpl; 32 import org.objectweb.petals.jbi.component.lifecycle.mock.MockComponentDescriptionBuilder; 33 import org.objectweb.petals.jbi.component.thread.ComponentLifeCycleThread; 34 import org.objectweb.petals.jbi.management.systemstate.SystemStateImpl; 35 import org.objectweb.petals.jbi.messaging.mock.MockLogger; 36 import org.objectweb.petals.jbi.routing.mock.ComponentMock; 37 import org.objectweb.petals.tools.jbicommon.descriptor.ComponentDescription; 38 import org.objectweb.petals.util.LoggingUtil; 39 import org.objectweb.util.monolog.api.Logger; 40 41 import static org.easymock.classextension.EasyMock.createMock; 42 43 49 public class ComponentLifeCycleTest extends TestCase { 50 51 private ComponentLifeCycle componentLifeCycle; 52 53 private ComponentLifeCycleThread lifeCycleThread; 54 55 private ComponentMock componentMock; 56 57 private ComponentDescription componentDescription; 58 59 public void testDoInit() throws JBIException { 60 componentLifeCycle.doInit(); 61 assertNotNull(componentMock.getContext()); 62 } 63 64 public void testDoShutdown() throws JBIException { 65 componentLifeCycle.doShutdown(); 66 assertEquals(componentMock.getState(), "Shutdown"); 67 } 68 69 public void testDoStart() throws JBIException { 70 componentLifeCycle.doStart(); 71 assertEquals(componentMock.getState(), "Started"); 72 } 73 74 public void testDoStop() throws JBIException { 75 componentLifeCycle.doStop(); 76 assertEquals(componentMock.getState(), "Stopped"); 77 } 78 79 public void testGetComponent() { 80 assertEquals(componentLifeCycle.getComponent(), componentMock); 81 } 82 83 public void testGetComponentDescription() { 84 assertEquals(componentLifeCycle.getComponentDescription(), 85 componentDescription); 86 } 87 88 public void testGetExtensionMBeanName() 89 throws MalformedObjectNameException , NullPointerException , JBIException { 90 assertEquals(componentLifeCycle.getExtensionMBeanName(), 91 new ObjectName ("test@test:name=test")); 92 } 93 94 public void testGetName() { 95 assertEquals(componentLifeCycle.getName(), "testComponentDescription"); 96 } 97 98 public void testGetComponentContext() { 99 102 ComponentContextImpl context = new ComponentContextImpl(); 103 Logger logger = new MockLogger(); 104 ObjectName name = null; 105 try { 106 name = new ObjectName ("test@test:name=test"); 107 } catch (Exception e) { 108 fail("Could not create Test parameters"); 109 } 110 111 lifeCycleThread = new ComponentLifeCycleThread(componentMock); 112 lifeCycleThread.start(); 113 114 componentLifeCycle = new ComponentLifeCycle(null, null, context, null, 115 name, lifeCycleThread, new LoggingUtil(logger)); 116 119 ComponentContextImpl result = componentLifeCycle.getComponentContext(); 120 assertEquals("Wrong invokation result", result, context); 121 } 122 123 @Override 124 protected void setUp() throws MalformedObjectNameException , 125 NullPointerException { 126 componentDescription = MockComponentDescriptionBuilder 127 .buildComponentDescription(); 128 componentMock = new ComponentMock(); 129 ComponentContextImpl componentContextImpl = new ComponentContextImpl(); 130 SystemStateImpl systemStateImpl = createMock(SystemStateImpl.class); 131 Logger logger = new MockLogger(); 132 lifeCycleThread = new ComponentLifeCycleThread(componentMock); 133 lifeCycleThread.start(); 134 componentLifeCycle = new ComponentLifeCycle(componentDescription, 135 componentMock, componentContextImpl, systemStateImpl, 136 new ObjectName ("test@test:name=test"), lifeCycleThread, new LoggingUtil(logger)); 137 } 138 139 public void testSetState() throws Exception { 140 143 146 String state = LifeCycleMBean.STARTED; 147 150 try { 151 componentLifeCycle.setState(state); 152 } catch (Exception e) { 153 e.printStackTrace(); 154 fail("Error during method invokation"); 155 } 156 assertEquals("State was not properly set", componentLifeCycle 157 .getCurrentState(), state); 158 159 162 componentDescription = MockComponentDescriptionBuilder 163 .buildComponentDescription(); 164 componentMock = new ComponentMock(); 165 ComponentContextImpl componentContextImpl = new ComponentContextImpl(); 166 SystemStateImpl systemStateImpl = new SystemStateImpl() { 167 public void updateComponentLifeCycleState(String componentName, 168 String lifecycleState) throws Exception { 169 throw new Exception ( 170 "updateComponentLifeCycleState testing exception"); 171 } 172 }; 173 componentLifeCycle = new ComponentLifeCycle(componentDescription, 174 componentMock, componentContextImpl, systemStateImpl, 175 new ObjectName ("test@test:name=test"), null, EasyMock 176 .createMock(LoggingUtil.class)); 177 180 try { 181 componentLifeCycle.setState(state); 182 fail("No exception was raised"); 183 } catch (Exception e) { 184 } 186 } 187 } 188 | Popular Tags |