diff --git a/src/test/java/org/springframework/data/gemfire/TestUtils.java b/src/test/java/org/springframework/data/gemfire/TestUtils.java new file mode 100644 index 00000000..7304e4a3 --- /dev/null +++ b/src/test/java/org/springframework/data/gemfire/TestUtils.java @@ -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 readField(String name, Object target) throws Exception { + Field field = target.getClass().getDeclaredField(name); + field.setAccessible(true); + return (T) field.get(target); + } +} 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 323e5e2b..cf84d99a 100644 --- a/src/test/java/org/springframework/data/gemfire/config/CacheNamespaceTest.java +++ b/src/test/java/org/springframework/data/gemfire/config/CacheNamespaceTest.java @@ -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); - } -} +} \ No newline at end of file