View Javadoc

1   package org.apache.struts2.osgi.interceptor;
2   
3   import org.easymock.EasyMock;
4   import org.apache.struts2.osgi.OsgiHost;
5   import org.osgi.framework.BundleContext;
6   import org.osgi.framework.ServiceReference;
7   
8   import javax.servlet.ServletContext;
9   
10  import com.opensymphony.xwork2.ActionInvocation;
11  import junit.framework.TestCase;
12  
13  import java.util.List;
14  
15  public class OsgiInterceptorTest extends TestCase {
16      public void testBundleContextAware() throws Exception {
17          ServletContext servletContext = EasyMock.createStrictMock(ServletContext.class);
18          BundleContext bundleContext = EasyMock.createStrictMock(BundleContext.class);
19          ActionInvocation actionInvocation = EasyMock.createStrictMock(ActionInvocation.class);
20          BundleContextAware bundleContextAware = EasyMock.createStrictMock(BundleContextAware.class);
21  
22          EasyMock.expect(servletContext.getAttribute(OsgiHost.OSGI_BUNDLE_CONTEXT)).andReturn(bundleContext);
23          EasyMock.expect(actionInvocation.getAction()).andReturn(bundleContextAware);
24          bundleContextAware.setBundleContext(bundleContext);
25          EasyMock.expect(actionInvocation.invoke()).andReturn("");
26  
27          EasyMock.replay(bundleContextAware);
28          EasyMock.replay(servletContext);
29          EasyMock.replay(actionInvocation);
30  
31          OsgiInterceptor osgiInterceptor = new OsgiInterceptor();
32          osgiInterceptor.setServletContext(servletContext);
33          osgiInterceptor.intercept(actionInvocation);
34  
35          EasyMock.verify(bundleContextAware);
36      }
37  
38       public void testBundleContextAwareNegative() throws Exception {
39          ServletContext servletContext = EasyMock.createStrictMock(ServletContext.class);
40          ActionInvocation actionInvocation = EasyMock.createStrictMock(ActionInvocation.class);
41          BundleContextAware bundleContextAware = EasyMock.createStrictMock(BundleContextAware.class);
42  
43          EasyMock.expect(servletContext.getAttribute(OsgiHost.OSGI_BUNDLE_CONTEXT)).andReturn(null);
44          EasyMock.expect(actionInvocation.invoke()).andReturn("");
45  
46          EasyMock.replay(bundleContextAware);
47          EasyMock.replay(servletContext);
48          EasyMock.replay(actionInvocation);
49  
50          OsgiInterceptor osgiInterceptor = new OsgiInterceptor();
51          osgiInterceptor.setServletContext(servletContext);
52          osgiInterceptor.intercept(actionInvocation);
53  
54          EasyMock.verify(bundleContextAware);
55      }
56  
57      public void testServiceAware() throws Exception {
58          ServletContext servletContext = EasyMock.createStrictMock(ServletContext.class);
59          BundleContext bundleContext = EasyMock.createStrictMock(BundleContext.class);
60          ActionInvocation actionInvocation = EasyMock.createStrictMock(ActionInvocation.class);
61          SomeAction someAction = new SomeAction();
62  
63          //service refs
64          ServiceReference objectRef = EasyMock.createNiceMock(ServiceReference.class);
65          Object someObject = new Object();
66  
67          EasyMock.expect(servletContext.getAttribute(OsgiHost.OSGI_BUNDLE_CONTEXT)).andReturn(bundleContext);
68          EasyMock.expect(actionInvocation.getAction()).andReturn(someAction);
69          EasyMock.expect(actionInvocation.invoke()).andReturn("");
70          EasyMock.expect(bundleContext.getAllServiceReferences(Object.class.getName(), null)).andReturn(new ServiceReference[] {objectRef});
71          EasyMock.expect(bundleContext.getService(objectRef)).andReturn(someObject);
72  
73          EasyMock.replay(bundleContext);
74          EasyMock.replay(servletContext);
75          EasyMock.replay(actionInvocation);
76  
77          OsgiInterceptor osgiInterceptor = new OsgiInterceptor();
78          osgiInterceptor.setServletContext(servletContext);
79          osgiInterceptor.intercept(actionInvocation);
80  
81          List<Object> objects = someAction.getServices();
82          assertNotNull(objects);
83          assertSame(someObject, objects.get(0));
84      }
85  }