Hi Luciano,

You shouldn't need to use the registry or other loaders to test this. Just create the loader you want and mock out the XMLStreamReader and LoaderRegistry to return the correct values. The mocked loader should return component definitions that are newed up rather than created by a loader.One way to look at this is that you are verifying the behavior of the composite loader, not the registry and its interaction with other loaders.

Some reading I've found useful is:

http://www.martinfowler.com/articles/mocksArentStubs.html

http://www.artima.com/intv/testdrivenP.html

http://www.martinfowler.com/articles/continuousIntegration.html

http://www.easymock.org/EasyMock2_2_Documentation.html

Jim


On Jan 23, 2007, at 12:44 AM, Luciano Resende wrote:

I'm trying to use easyMock to create a unit testing to verify that exception
is being thrown for duplicated Components...
Looks like the reader that is simulating the scdl is returning the right
values, but then, the registry does not find a loader to handle
implementation.java

Portion given by the reader so far :

       <composite xmlns="http://www.osoa.org/xmlns/sca/1.0";
                  name="composite_component-include2">

           <component name="component">
               <implementation.java class="
org.apache.tuscany.core.mock.component.BasicInterfaceImpl"/>

It's failing as it's not finding the proper loader to handle
implementation.java... How do I tell get the registry properly loaded with
the proper loaders registred ? Or any other way I should handle this ?

Code:

public void testLoadDuplicatedComponentTypeFromMock() throws Exception{
       /*
       //main
       <composite xmlns="http://www.osoa.org/xmlns/sca/1.0";
name="CompositeComponent">
           <include name="composite_component-include1"
scdlLocation="composite_component-include1.scdl"/>
           <include name="composite_component-include2"
scdlLocation="composite_component-include2.scdl"/>
       </composite>
       //include 1
       <composite xmlns="http://www.osoa.org/xmlns/sca/1.0";
                  name="composite_component-include2">

           <component name="component">
               <implementation.java class="
org.apache.tuscany.core.mock.component.BasicInterfaceImpl"/>
           </component>

           <service name="service" >
               <interface.java interface="
org.apache.tuscany.core.mock.component.BasicInterface"/>
               <reference>component</reference>
           </service>
       </composite>
        */


       EasyMock.expect(mockReader.getName()).andReturn(COMPOSITE);
       EasyMock.expect(mockReader.getAttributeValue((String)
EasyMock.isNull(), EasyMock.isA(String.class))).andReturn (COMPOSITE_NAME);
       EasyMock.expect(mockReader.next()).andReturn(
XMLStreamConstants.START_ELEMENT);
       EasyMock.expect(mockReader.getAttributeValue((String)
EasyMock.isNull(), EasyMock.isA(String.class))).andReturn (COMPOSITE_NAME);

       //first component
       EasyMock.expect(mockReader.next()).andReturn(
XMLStreamConstants.START_ELEMENT);
       EasyMock.expect(mockReader.getName()).andReturn(COMPONENT);
       EasyMock.expect(mockReader.getAttributeValue((String)
EasyMock.isNull(), EasyMock.isA(String.class))).andReturn (COMPONENT_NAME);
       EasyMock.expect(mockReader.getAttributeValue((String)
EasyMock.isNull(), EasyMock.eq("initLevel"))).andReturn(null);
       EasyMock.expect(mockReader.getAttributeValue(EasyMock.isA(
String.class), EasyMock.isA(String.class))).andReturn(null);
       EasyMock.expect(mockReader.nextTag()).andReturn(0);
       EasyMock.expect(mockReader.getAttributeValue(EasyMock.isA(
String.class), EasyMock.isA(String.class))).andReturn(null);
       EasyMock.expect(mockReader.next()).andReturn(
XMLStreamConstants.START_ELEMENT);
       EasyMock.expect(mockReader.next()).andReturn(
XMLStreamConstants.END_ELEMENT);
       EasyMock.expect(mockReader.getName()).andReturn(null
/*COMPONENT_IMPLEMENTATION_JAVA*/);
       EasyMock.expect(mockReader.getAttributeValue((String)
EasyMock.isNull(), EasyMock.isA(String.class))).andReturn(null
/*COMPONENT_IMPLEMENTATION_JAVA_CLASS*/);
       EasyMock.expect(mockReader.getAttributeValue((String)
EasyMock.isNull(), EasyMock.eq("initLevel"))).andReturn(null);
       EasyMock.expect(mockReader.getAttributeValue(EasyMock.isA(
String.class), EasyMock.isA(String.class))).andReturn(null);
       EasyMock.expect(mockReader.nextTag()).andReturn(0);
       EasyMock.expect(mockReader.getAttributeValue(EasyMock.isA(
String.class), EasyMock.isA(String.class))).andReturn(null);
       EasyMock.expect(mockReader.next()).andReturn(
XMLStreamConstants.END_ELEMENT);
       //second and duplicated component
       EasyMock.expect(mockReader.next()).andReturn(
XMLStreamConstants.START_ELEMENT);
       EasyMock.expect(mockReader.getName()).andReturn(COMPONENT);
       EasyMock.expect(mockReader.getAttributeValue((String)
EasyMock.isNull(), EasyMock.isA(String.class))).andReturn (COMPONENT_NAME);
       EasyMock.expect(mockReader.getAttributeValue(EasyMock.isA(
String.class), EasyMock.isA(String.class))).andReturn(null);
       EasyMock.expect(mockReader.next()).andReturn(
XMLStreamConstants.START_ELEMENT);
       EasyMock.expect(mockReader.getName
()).andReturn(COMPONENT_IMPLEMENTATION_JAVA);
       EasyMock.expect(mockReader.getAttributeValue((String)
EasyMock.isNull(), EasyMock.isA(String.class
))).andReturn(COMPONENT_IMPLEMENTATION_JAVA_CLASS);
       EasyMock.expect(mockReader.getAttributeValue(EasyMock.isA(
String.class), EasyMock.isA(String.class))).andReturn(null);
       EasyMock.expect(mockReader.next()).andReturn(
XMLStreamConstants.END_ELEMENT);

       //replay reader
       EasyMock.replay(mockReader);

       ModelObject modelObject = new ModelObject() {};

       CompositeComponentType composite = loader.load(
EasyMock.createNiceMock(CompositeComponent.class),
           modelObject,
           mockReader,
           EasyMock.createNiceMock(DeploymentContext.class));

       assertEquals(COMPOSITE_NAME, composite.getName());

   }

Exception:

java.lang.NullPointerException
at org.apache.tuscany.spi.loader.UnrecognizedElementException.<init>(
UnrecognizedElementException.java:39)
   at org.apache.tuscany.core.loader.LoaderRegistryImpl.load(
LoaderRegistryImpl.java:82)
at org.apache.tuscany.core.loader.ComponentLoader.loadImplementation(
ComponentLoader.java:199)
   at org.apache.tuscany.core.loader.ComponentLoader.load(
ComponentLoader.java:110)
   at org.apache.tuscany.core.loader.ComponentLoader.load(
ComponentLoader.java:79)
   at org.apache.tuscany.core.loader.LoaderRegistryImpl.load(
LoaderRegistryImpl.java:84)
at org.apache.tuscany.core.implementation.composite.CompositeLoader.load
(CompositeLoader.java:96)
at org.apache.tuscany.core.implementation.composite.CompositeLoader.load
(CompositeLoader.java:68)
   at org.apache.tuscany.core.loader.LoaderRegistryImpl.load(
LoaderRegistryImpl.java:84)
at org.apache.tuscany.core.implementation.composite.CompositeLoader.load
(CompositeLoader.java:96)
   at
org.apache.tuscany.core.implementation.composite.CompositeLoaderTestCa se.testLoadDuplicatedComponentTypeFromMock
(CompositeLoaderTestCase.java:152)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(
NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(
DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:585)
   at junit.framework.TestCase.runTest(TestCase.java:154)
   at junit.framework.TestCase.runBare(TestCase.java:127)
   at junit.framework.TestResult$1.protect(TestResult.java:106)
   at junit.framework.TestResult.runProtected(TestResult.java:124)
   at junit.framework.TestResult.run(TestResult.java:109)
   at junit.framework.TestCase.run(TestCase.java:118)
   at junit.framework.TestSuite.runTest(TestSuite.java:208)
   at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(
JUnit3TestReference.java:128)
   at org.eclipse.jdt.internal.junit.runner.TestExecution.run(
TestExecution.java:38)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
RemoteTestRunner.java:460)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
RemoteTestRunner.java:673)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(
RemoteTestRunner.java:386)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(
RemoteTestRunner.java:196)




--
Luciano Resende
http://people.apache.org/~lresende


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to