Hi all,
These days I am trying to use pax exam to make a integration test for osgi
and at the beginning everything goes well , however I encounter an
error:checkword3.DictionaryServiceImpl can not be cast to
checkword3.DictionaryServiceImpl. I can not figure it out why it happens.
Steps:
1 new a plugin project named checkword3 in eclipse and choose declarative
service example at the last step.
2 write a test for the DictionaryService like this:
package checkword3;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.ops4j.pax.exam.CoreOptions.bundle;
import static org.ops4j.pax.exam.CoreOptions.equinox;
import static org.ops4j.pax.exam.CoreOptions.frameworks;
import static org.ops4j.pax.exam.CoreOptions.options;
import static org.ops4j.pax.exam.CoreOptions.provision;
import java.lang.reflect.Method;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.Inject;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.junit.Configuration;
import org.ops4j.pax.exam.junit.JUnit4TestRunner;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
@RunWith( JUnit4TestRunner.class )
public class JUnitTest
{
@Inject
private BundleContext bundleContext;
private Bundle testBundle;
@Before
public void setUp()throws Exception{
System.out.println("setup ");
}
@Configuration
public static Option[] configuration()
{
return options(
frameworks(
equinox()
),
provision(
bundle("file:D:/eclipse3.5-G22/eclipse/plugins/org.eclipse.osgi.services_3.2.0.v20090520-1800.jar"),
bundle("file:C:/Documents and
Settings/Administrator/.m2/repository/org/eclipse/equinox/org.eclipse.equinox.ds/1.1.0.v20090520-1800/org.eclipse.equinox.ds-1.1.0.v20090520-1800.jar"),
bundle("file:C:/Documents and
Settings/Administrator/.m2/repository/org/eclipse/equinox/org.eclipse.equinox.util/1.0.100.v20090520-1800/org.eclipse.equinox.util-1.0.100.v20090520-1800.jar"),
bundle("file:D:/eclipse3.5-G22/eclipse/plugins/javax.servlet_2.5.0.v200806031605.jar"),
bundle("file:D:/eclipse3.5-G22/eclipse/plugins/javax.transaction_1.1.1.v200906161300.jar"),
bundle("file:D:/eclipse3.5-G22/eclipse/plugins/org.eclipse.persistence.jpa.equinox.weaving_1.1.2.v20090612-r4475.jar"),
bundle("file:D:/CheckWord3_1.0.0.200910271110.jar")
)
);
}
@Test
public void testMethod(final BundleContext bundleContext ) throws
Exception
{
System.out.println("test");
Bundle[] bundles=bundleContext.getBundles();
ServiceReference []srs=bundles[13].getRegisteredServices();
for(ServiceReference sr:srs){
System.out.println("SERVICES "+sr);
}
Object o=bundleContext.getService(srs[0]);
Class clazz=o.getClass();
Method check=clazz.getMethod("check", String.class);
System.out.println(bundleContext.getBundle().getBundleId());
srs=bundles[13].getRegisteredServices();
for(ServiceReference sr:srs){
System.out.println("SERVICES "+sr);
}
assertEquals(check.invoke(o, "osgi"),true);
assertEquals(check.invoke(o, "eclipse"),true);
assertEquals(check.invoke(o, "equinox"),true);
assertEquals(check.invoke(o, "aaa"),false);
System.out.println(o.getClass().getClassLoader());
System.out.println(DictionaryService.class.getClassLoader());
DictionaryService
ds=(DictionaryService)bundleContext.getService(srs[0]);
}
java.lang.ClassCastException: checkword3.DictionaryServiceImpl cannot be
cast to checkword3.DictionaryService
at checkword3.JUnitTest.testMethod(JUnitTest.java:102)
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:597)
at
org.ops4j.pax.exam.junit.extender.impl.internal.CallableTestMethodImpl.injectContextAndInvoke(CallableTestMethodImpl.java:130)
at
org.ops4j.pax.exam.junit.extender.impl.internal.CallableTestMethodImpl.call(CallableTestMethodImpl.java:101)
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:597)
at
org.ops4j.pax.exam.rbc.internal.RemoteBundleContextImpl.remoteCall(RemoteBundleContextImpl.java:80)
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:597)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:305)
at sun.rmi.transport.Transport$1.run(Transport.java:159)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
at
sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
at
sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
at
sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
at
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
then I will get the error , I found that the two classloaders are
different,but I can solve it.
pls help me thanks a lot.
--
Best Regards
Ben Liang
_______________________________________________
general mailing list
[email protected]
http://lists.ops4j.org/mailman/listinfo/general