+ refactor the tests for better app context cleanup

+ additionally add forking to not have overlapping contexts
This commit is contained in:
costin
2010-08-27 19:29:49 +03:00
parent 54dd44dfe6
commit d3af92fe91
7 changed files with 83 additions and 40 deletions

View File

@@ -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 {

View File

@@ -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

View File

@@ -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();
}
}

View File

@@ -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<K, V> extends CacheListenerAdapter<K, V> {

View File

@@ -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) {
}
}
}

View File

@@ -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));
}
}