@After public void tearDown() throws Exception { System.setProperty("TEST_PROPERTY", ""); new GeoServerExtensions().setApplicationContext(null); }
@SuppressWarnings({"unchecked", "rawtypes"}) @Test public void testExtensionProvider() { ApplicationContext appContext = createMock(ApplicationContext.class); GeoServerExtensions gse = new GeoServerExtensions(); gse.setApplicationContext(appContext); expect(appContext.getBeanNamesForType(ExtensionFilter.class)).andReturn(new String[0]); expect(appContext.getBeanNamesForType(GeoServerExtensionsTest.class)) .andReturn(new String[0]); expect(appContext.getBeanNamesForType(ExtensionProvider.class)) .andReturn(new String[] {"testKey2"}); ExtensionProvider xp = createMock(ExtensionProvider.class); expect(xp.getExtensionPoint()).andReturn(GeoServerExtensionsTest.class); expect(xp.getExtensions(GeoServerExtensionsTest.class)).andReturn(Arrays.asList(this)); expect(appContext.getBean("testKey2")).andReturn(xp); expect(appContext.isSingleton((String) anyObject())).andReturn(true).anyTimes(); replay(xp); replay(appContext); assertEquals(1, GeoServerExtensions.extensions(GeoServerExtensionsTest.class).size()); verify(xp); verify(appContext); }
GeoServerExtensions gsext = new GeoServerExtensions(); gsext.setApplicationContext(appContext);
@Test public void ModuleStatusTest() { GeoServerExtensions gse = new GeoServerExtensions(); List<ModuleStatus> statusBeans = gse.extensions(ModuleStatus.class); assertEquals("gs-main", statusBeans.get(0).getModule()); assertEquals("GeoServer Main", statusBeans.get(0).getName()); } }
@Test public void testBeanString() { ApplicationContext appContext = createMock(ApplicationContext.class); GeoServerExtensions gse = new GeoServerExtensions(); gse.setApplicationContext(null); Logger LOGGER = Logging.getLogger("org.geoserver.platform"); Level level = LOGGER.getLevel(); try { LOGGER.setLevel(Level.SEVERE); assertNull(GeoServerExtensions.bean("beanName")); } finally { LOGGER.setLevel(level); } gse.setApplicationContext(appContext); expect(appContext.isSingleton((String) anyObject())).andReturn(true).anyTimes(); expect(appContext.getBean("beanName")).andReturn(null); // call #1 expect(appContext.getBean("beanName")).andReturn(this); // call #2 replay(appContext); assertNull(GeoServerExtensions.bean("beanName")); // call #1 assertSame(this, GeoServerExtensions.bean("beanName")); // call #2 verify(appContext); }
ApplicationContext customAppContext = createMock(ApplicationContext.class); GeoServerExtensions gse = new GeoServerExtensions(); gse.setApplicationContext(appContext);
GeoServerExtensions gsext = new GeoServerExtensions(); gsext.setApplicationContext(appContext);
@Test public void testExtensionFilterByClass() { ApplicationContext appContext = createNiceMock(ApplicationContext.class); // setApplicationContext cleared the static cache // set the expectation over the app context used as argument ClassExclusionFilter filter = new ClassExclusionFilter(); filter.setBeanClass(GeoServerExtensionsTest.class); expect(appContext.getBeanNamesForType(ExtensionFilter.class)) .andReturn(new String[] {"filter"}) .anyTimes(); expect(appContext.getBean("filter")).andReturn(filter).anyTimes(); expect(appContext.getBeanNamesForType(GeoServerExtensionsTest.class)) .andReturn(new String[] {"testId"}) .anyTimes(); expect(appContext.getBean("testId")).andReturn(this).anyTimes(); replay(appContext); // build extensions GeoServerExtensions gse = new GeoServerExtensions(); gse.setApplicationContext(appContext); // check we get nothing List<GeoServerExtensionsTest> extensions = GeoServerExtensions.extensions(GeoServerExtensionsTest.class); assertEquals(0, extensions.size()); // change the bean id and we should get one result instead filter.setBeanClass(Integer.class); extensions = GeoServerExtensions.extensions(GeoServerExtensionsTest.class); assertEquals(1, extensions.size()); assertSame(this, extensions.get(0)); }
@Test public void testExtensionFilterByName() { ApplicationContext appContext = createNiceMock(ApplicationContext.class); // setApplicationContext cleared the static cache // set the expectation over the app context used as argument NameExclusionFilter filter = new NameExclusionFilter(); filter.setBeanId("testId"); expect(appContext.getBeanNamesForType(ExtensionFilter.class)) .andReturn(new String[] {"filter"}) .anyTimes(); expect(appContext.getBean("filter")).andReturn(filter).anyTimes(); expect(appContext.getBeanNamesForType(GeoServerExtensionsTest.class)) .andReturn(new String[] {"testId"}) .anyTimes(); expect(appContext.getBean("testId")).andReturn(this).anyTimes(); replay(appContext); // build extensions GeoServerExtensions gse = new GeoServerExtensions(); gse.setApplicationContext(appContext); // check we get nothing List<GeoServerExtensionsTest> extensions = GeoServerExtensions.extensions(GeoServerExtensionsTest.class); assertEquals(0, extensions.size()); // change the bean id and we should get one result instead filter.setBeanId("holabaloo"); extensions = GeoServerExtensions.extensions(GeoServerExtensionsTest.class); assertEquals(1, extensions.size()); assertSame(this, extensions.get(0)); }
@Test public void testExtensions() { ApplicationContext appContext = createMock(ApplicationContext.class); GeoServerExtensions gse = new GeoServerExtensions(); gse.setApplicationContext(appContext); assertEquals(0, GeoServerExtensions.extensionsCache.size()); expect(appContext.getBeanNamesForType(ExtensionFilter.class)).andReturn(new String[0]); expect(appContext.getBeanNamesForType(GeoServerExtensionsTest.class)) .andReturn(new String[] {"testKey", "fakeKey"}); expect(appContext.getBeanNamesForType(ExtensionProvider.class)).andReturn(new String[0]); expect(appContext.getBean("testKey")).andReturn(this); // note I'm testing null is a valid value. If that's not the case, it // should be reflected in the code, but I'm writing the test after the // code so that's what it does expect(appContext.isSingleton((String) anyObject())).andReturn(true).anyTimes(); expect(appContext.getBean("fakeKey")).andReturn(null); replay(appContext); List<GeoServerExtensionsTest> extensions = GeoServerExtensions.extensions(GeoServerExtensionsTest.class); assertNotNull(extensions); assertEquals(2, extensions.size()); assertTrue(extensions.contains(this)); assertTrue(extensions.contains(null)); assertEquals(3, GeoServerExtensions.extensionsCache.size()); assertTrue(GeoServerExtensions.extensionsCache.containsKey(GeoServerExtensionsTest.class)); assertNotNull(GeoServerExtensions.extensionsCache.get(GeoServerExtensionsTest.class)); assertEquals( 2, GeoServerExtensions.extensionsCache.get(GeoServerExtensionsTest.class).length); verify(appContext); }
expect(appContext.getBean("defaultStatus")).andStubReturn(impl); GeoServerExtensions gse = new GeoServerExtensions(); gse.setApplicationContext(appContext); expect(appContext.getBeanNamesForType(ExtensionFilter.class)).andReturn(new String[0]);
@Test public void testSetApplicationContext() { ApplicationContext appContext1 = createMock(ApplicationContext.class); ApplicationContext appContext2 = createMock(ApplicationContext.class); GeoServerExtensions gse = new GeoServerExtensions(); gse.setApplicationContext(appContext1); GeoServerExtensions.extensionsCache.put( GeoServerExtensionsTest.class, new String[] {"fake"}); assertSame(appContext1, GeoServerExtensions.context); gse.setApplicationContext(appContext2); assertSame(appContext2, GeoServerExtensions.context); assertEquals(0, GeoServerExtensions.extensionsCache.size()); }
@BeforeClass public static void initAppContext() { // setup extensions so that we can do extension point lookups ApplicationContext ac = new ClassPathXmlApplicationContext( new String[] {"classpath:/securedObjectsContext.xml"}); new GeoServerExtensions().setApplicationContext(ac); }
@After public void tearDown() throws Exception { System.setProperty("TEST_PROPERTY", ""); new GeoServerExtensions().setApplicationContext(null); }
@SuppressWarnings({"unchecked", "rawtypes"}) @Test public void testExtensionProvider() { ApplicationContext appContext = createMock(ApplicationContext.class); GeoServerExtensions gse = new GeoServerExtensions(); gse.setApplicationContext(appContext); expect(appContext.getBeanNamesForType(ExtensionFilter.class)).andReturn(new String[0]); expect(appContext.getBeanNamesForType(GeoServerExtensionsTest.class)) .andReturn(new String[0]); expect(appContext.getBeanNamesForType(ExtensionProvider.class)) .andReturn(new String[] {"testKey2"}); ExtensionProvider xp = createMock(ExtensionProvider.class); expect(xp.getExtensionPoint()).andReturn(GeoServerExtensionsTest.class); expect(xp.getExtensions(GeoServerExtensionsTest.class)).andReturn(Arrays.asList(this)); expect(appContext.getBean("testKey2")).andReturn(xp); expect(appContext.isSingleton((String) anyObject())).andReturn(true).anyTimes(); replay(xp); replay(appContext); assertEquals(1, GeoServerExtensions.extensions(GeoServerExtensionsTest.class).size()); verify(xp); verify(appContext); }
@Test public void testModuleStatus() { ModuleStatus status = createMock("testStatus", ModuleStatus.class); expect(status.getName()).andReturn("test").atLeastOnce(); ModuleStatusImpl impl = new ModuleStatusImpl(); impl.setName("default"); ApplicationContext appContext = createMock(ApplicationContext.class); expect(appContext.getBean("testStatus")).andStubReturn(status); expect(appContext.getBean("defaultStatus")).andStubReturn(impl); GeoServerExtensions gse = new GeoServerExtensions(); gse.setApplicationContext(appContext); expect(appContext.getBeanNamesForType(ExtensionFilter.class)).andReturn(new String[0]); expect(appContext.getBeanNamesForType(ModuleStatus.class)) .andStubReturn(new String[] {"testStatus", "defaultStatus"}); expect(appContext.isSingleton((String) anyObject())).andReturn(true).anyTimes(); expect(appContext.getBeanNamesForType(ExtensionProvider.class)).andReturn(new String[0]); EasyMock.replay(status, appContext); List<ModuleStatus> list = gse.extensions(ModuleStatus.class); assertEquals("test interfact mock", "test", status.getName()); assertEquals("found module status", 2, list.size()); for (ModuleStatus s : list) { System.out.println(s.getName()); } EasyMock.verify(status); }
@Test public void testBeanString() { ApplicationContext appContext = createMock(ApplicationContext.class); GeoServerExtensions gse = new GeoServerExtensions(); gse.setApplicationContext(null); Logger LOGGER = Logging.getLogger("org.geoserver.platform"); Level level = LOGGER.getLevel(); try { LOGGER.setLevel(Level.SEVERE); assertNull(GeoServerExtensions.bean("beanName")); } finally { LOGGER.setLevel(level); } gse.setApplicationContext(appContext); expect(appContext.isSingleton((String) anyObject())).andReturn(true).anyTimes(); expect(appContext.getBean("beanName")).andReturn(null); // call #1 expect(appContext.getBean("beanName")).andReturn(this); // call #2 replay(appContext); assertNull(GeoServerExtensions.bean("beanName")); // call #1 assertSame(this, GeoServerExtensions.bean("beanName")); // call #2 verify(appContext); }
@Test public void testExtensionFilterByClass() { ApplicationContext appContext = createNiceMock(ApplicationContext.class); // setApplicationContext cleared the static cache // set the expectation over the app context used as argument ClassExclusionFilter filter = new ClassExclusionFilter(); filter.setBeanClass(GeoServerExtensionsTest.class); expect(appContext.getBeanNamesForType(ExtensionFilter.class)) .andReturn(new String[] {"filter"}) .anyTimes(); expect(appContext.getBean("filter")).andReturn(filter).anyTimes(); expect(appContext.getBeanNamesForType(GeoServerExtensionsTest.class)) .andReturn(new String[] {"testId"}) .anyTimes(); expect(appContext.getBean("testId")).andReturn(this).anyTimes(); replay(appContext); // build extensions GeoServerExtensions gse = new GeoServerExtensions(); gse.setApplicationContext(appContext); // check we get nothing List<GeoServerExtensionsTest> extensions = GeoServerExtensions.extensions(GeoServerExtensionsTest.class); assertEquals(0, extensions.size()); // change the bean id and we should get one result instead filter.setBeanClass(Integer.class); extensions = GeoServerExtensions.extensions(GeoServerExtensionsTest.class); assertEquals(1, extensions.size()); assertSame(this, extensions.get(0)); }
@Test public void testExtensionFilterByName() { ApplicationContext appContext = createNiceMock(ApplicationContext.class); // setApplicationContext cleared the static cache // set the expectation over the app context used as argument NameExclusionFilter filter = new NameExclusionFilter(); filter.setBeanId("testId"); expect(appContext.getBeanNamesForType(ExtensionFilter.class)) .andReturn(new String[] {"filter"}) .anyTimes(); expect(appContext.getBean("filter")).andReturn(filter).anyTimes(); expect(appContext.getBeanNamesForType(GeoServerExtensionsTest.class)) .andReturn(new String[] {"testId"}) .anyTimes(); expect(appContext.getBean("testId")).andReturn(this).anyTimes(); replay(appContext); // build extensions GeoServerExtensions gse = new GeoServerExtensions(); gse.setApplicationContext(appContext); // check we get nothing List<GeoServerExtensionsTest> extensions = GeoServerExtensions.extensions(GeoServerExtensionsTest.class); assertEquals(0, extensions.size()); // change the bean id and we should get one result instead filter.setBeanId("holabaloo"); extensions = GeoServerExtensions.extensions(GeoServerExtensionsTest.class); assertEquals(1, extensions.size()); assertSame(this, extensions.get(0)); }
@Test public void testSetApplicationContext() { ApplicationContext appContext1 = createMock(ApplicationContext.class); ApplicationContext appContext2 = createMock(ApplicationContext.class); GeoServerExtensions gse = new GeoServerExtensions(); gse.setApplicationContext(appContext1); GeoServerExtensions.extensionsCache.put( GeoServerExtensionsTest.class, new String[] {"fake"}); assertSame(appContext1, GeoServerExtensions.context); gse.setApplicationContext(appContext2); assertSame(appContext2, GeoServerExtensions.context); assertEquals(0, GeoServerExtensions.extensionsCache.size()); }