+ refactor the cache namespace by introducing a small test utils class

SGF-11
SGF-10
This commit is contained in:
costin
2010-08-26 21:31:35 +03:00
parent 661c9e0031
commit ce094a862c
2 changed files with 40 additions and 15 deletions

View File

@@ -0,0 +1,32 @@
/*
* 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 java.lang.reflect.Field;
/**
* @author Costin Leau
*/
public abstract class TestUtils {
@SuppressWarnings("unchecked")
public static <T> T readField(String name, Object target) throws Exception {
Field field = target.getClass().getDeclaredField(name);
field.setAccessible(true);
return (T) field.get(target);
}
}

View File

@@ -20,14 +20,13 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.lang.reflect.Field;
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.TestUtils;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -47,30 +46,24 @@ public class CacheNamespaceTest {
public void testBasicCache() throws Exception {
assertTrue(context.containsBean("cache"));
CacheFactoryBean cfb = (CacheFactoryBean) context.getBean("&cache");
assertNull(readField("cacheXml", cfb));
assertNull(readField("properties", cfb));
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");
assertNull(readField("cacheXml", cfb));
assertNull(readField("properties", cfb));
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");
Resource res = (Resource) readField("cacheXml", cfb);
Resource res = TestUtils.readField("cacheXml", cfb);
assertEquals("cache.xml", res.getFilename());
assertEquals(context.getBean("props"), readField("properties", cfb));
assertEquals(context.getBean("props"), TestUtils.readField("properties", cfb));
}
private Object readField(String name, Object target) throws Exception {
Field field = target.getClass().getDeclaredField(name);
field.setAccessible(true);
return field.get(target);
}
}
}