From d3af92fe91a94710155cf2da23c9ac5d7e9fbe57 Mon Sep 17 00:00:00 2001 From: costin Date: Fri, 27 Aug 2010 19:29:49 +0300 Subject: [PATCH] + refactor the tests for better app context cleanup + additionally add forking to not have overlapping contexts --- pom.xml | 2 +- .../data/gemfire/CacheIntegrationTest.java | 22 ++++----- .../data/gemfire/DeclarableSupportTest.java | 3 -- .../data/gemfire/RecreatingContextTest.java | 46 +++++++++++++++++++ .../data/gemfire/RegionIntegrationTest.java | 3 -- .../data/gemfire/TestUtils.java | 14 ++++++ .../gemfire/config/CacheNamespaceTest.java | 33 ++++++------- 7 files changed, 83 insertions(+), 40 deletions(-) create mode 100644 src/test/java/org/springframework/data/gemfire/RecreatingContextTest.java diff --git a/pom.xml b/pom.xml index c588a4f2..bf9eb3da 100644 --- a/pom.xml +++ b/pom.xml @@ -225,7 +225,7 @@ limitations under the License. org.apache.maven.plugins maven-surefire-plugin - none + always diff --git a/src/test/java/org/springframework/data/gemfire/CacheIntegrationTest.java b/src/test/java/org/springframework/data/gemfire/CacheIntegrationTest.java index db5c6e6a..14441d78 100644 --- a/src/test/java/org/springframework/data/gemfire/CacheIntegrationTest.java +++ b/src/test/java/org/springframework/data/gemfire/CacheIntegrationTest.java @@ -20,28 +20,22 @@ package org.springframework.data.gemfire; import junit.framework.Assert; import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.ApplicationContext; -import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.annotation.DirtiesContext.ClassMode; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.gemstone.gemfire.cache.Cache; /** - * Integration test trying various basic configurations of Gemfire through Spring. + * Integration test trying various basic configurations of GemFire through Spring. + * + * Made abstract to avoid multiple caches running at the same time. * * @author Costin Leau */ -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(locations = { "basic-cache.xml" }) -@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) -public class CacheIntegrationTest { +public class CacheIntegrationTest extends RecreatingContextTest{ - @Autowired - private ApplicationContext ctx; + @Override + protected String location() { + return "org/springframework/data/gemfire/basic-cache.xml"; + } @Test public void testBasicCache() throws Exception { diff --git a/src/test/java/org/springframework/data/gemfire/DeclarableSupportTest.java b/src/test/java/org/springframework/data/gemfire/DeclarableSupportTest.java index 3ded2b07..9eaba86d 100644 --- a/src/test/java/org/springframework/data/gemfire/DeclarableSupportTest.java +++ b/src/test/java/org/springframework/data/gemfire/DeclarableSupportTest.java @@ -24,8 +24,6 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.annotation.DirtiesContext.ClassMode; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -37,7 +35,6 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "cache-with-declarable-ctx.xml" }) -@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) public class DeclarableSupportTest { @Autowired diff --git a/src/test/java/org/springframework/data/gemfire/RecreatingContextTest.java b/src/test/java/org/springframework/data/gemfire/RecreatingContextTest.java new file mode 100644 index 00000000..625ee0b9 --- /dev/null +++ b/src/test/java/org/springframework/data/gemfire/RecreatingContextTest.java @@ -0,0 +1,46 @@ +/* + * Copyright 2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.data.gemfire; + +import org.junit.After; +import org.junit.Before; +import org.springframework.context.support.GenericApplicationContext; +import org.springframework.context.support.GenericXmlApplicationContext; + +/** + * Simple testing class that creates the app context after each method. + * Used to properly destroy the beans defined inside Spring. + * + * @author Costin Leau + */ +public abstract class RecreatingContextTest { + + protected GenericApplicationContext ctx; + + protected abstract String location(); + + @Before + public void createCtx() { + ctx = new GenericXmlApplicationContext(location()); + } + + @After + public void destroyCtx() { + if (ctx != null) + ctx.destroy(); + } +} diff --git a/src/test/java/org/springframework/data/gemfire/RegionIntegrationTest.java b/src/test/java/org/springframework/data/gemfire/RegionIntegrationTest.java index 11c9984e..81968c8f 100644 --- a/src/test/java/org/springframework/data/gemfire/RegionIntegrationTest.java +++ b/src/test/java/org/springframework/data/gemfire/RegionIntegrationTest.java @@ -25,8 +25,6 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; -import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.annotation.DirtiesContext.ClassMode; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -46,7 +44,6 @@ import com.gemstone.gemfire.cache.util.CacheWriterAdapter; */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "basic-region.xml" }) -@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) public class RegionIntegrationTest { private static class CacheList extends CacheListenerAdapter { diff --git a/src/test/java/org/springframework/data/gemfire/TestUtils.java b/src/test/java/org/springframework/data/gemfire/TestUtils.java index 7304e4a3..61048466 100644 --- a/src/test/java/org/springframework/data/gemfire/TestUtils.java +++ b/src/test/java/org/springframework/data/gemfire/TestUtils.java @@ -29,4 +29,18 @@ public abstract class TestUtils { field.setAccessible(true); return (T) field.get(target); } + + public static void cleanBeanFactoryStaticReference() { + try { + Field field = GemfireBeanFactoryLocator.class.getDeclaredField("canUseDefaultBeanFactory"); + field.setAccessible(true); + field.set(null, true); + + field = GemfireBeanFactoryLocator.class.getDeclaredField("defaultFactory"); + field.setAccessible(true); + field.set(null, null); + + } catch (Exception ex) { + } + } } diff --git a/src/test/java/org/springframework/data/gemfire/config/CacheNamespaceTest.java b/src/test/java/org/springframework/data/gemfire/config/CacheNamespaceTest.java index cf84d99a..249a7be3 100644 --- a/src/test/java/org/springframework/data/gemfire/config/CacheNamespaceTest.java +++ b/src/test/java/org/springframework/data/gemfire/config/CacheNamespaceTest.java @@ -21,49 +21,44 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.ApplicationContext; import org.springframework.core.io.Resource; import org.springframework.data.gemfire.CacheFactoryBean; +import org.springframework.data.gemfire.RecreatingContextTest; import org.springframework.data.gemfire.TestUtils; -import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * @author Costin Leau */ -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration("cache-ns.xml") -@DirtiesContext -public class CacheNamespaceTest { +public class CacheNamespaceTest extends RecreatingContextTest { - @Autowired - private ApplicationContext context; + @Override + protected String location() { + return "org/springframework/data/gemfire/config/cache-ns.xml"; + } @Test public void testBasicCache() throws Exception { - assertTrue(context.containsBean("cache")); - CacheFactoryBean cfb = (CacheFactoryBean) context.getBean("&cache"); + assertTrue(ctx.containsBean("cache")); + CacheFactoryBean cfb = (CacheFactoryBean) ctx.getBean("&cache"); assertNull(TestUtils.readField("cacheXml", cfb)); assertNull(TestUtils.readField("properties", cfb)); } @Test public void testNamedCache() throws Exception { - assertTrue(context.containsBean("cache-with-name")); - CacheFactoryBean cfb = (CacheFactoryBean) context.getBean("&cache-with-name"); + assertTrue(ctx.containsBean("cache-with-name")); + CacheFactoryBean cfb = (CacheFactoryBean) ctx.getBean("&cache-with-name"); assertNull(TestUtils.readField("cacheXml", cfb)); assertNull(TestUtils.readField("properties", cfb)); } @Test public void testCacheWithXml() throws Exception { - assertTrue(context.containsBean("cache-with-xml")); - CacheFactoryBean cfb = (CacheFactoryBean) context.getBean("&cache-with-xml"); + assertTrue(ctx.containsBean("cache-with-xml")); + CacheFactoryBean cfb = (CacheFactoryBean) ctx.getBean("&cache-with-xml"); Resource res = TestUtils.readField("cacheXml", cfb); assertEquals("cache.xml", res.getFilename()); - assertEquals(context.getBean("props"), TestUtils.readField("properties", cfb)); + assertEquals(ctx.getBean("props"), TestUtils.readField("properties", cfb)); } + } \ No newline at end of file