+ initial commit

This commit is contained in:
costin
2010-06-29 20:58:20 +03:00
commit 8ddeae76a1
41 changed files with 3241 additions and 0 deletions

View File

@@ -0,0 +1,66 @@
/*
* 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.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.
*
* @author Costin Leau
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "basic-cache.xml" })
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
public class CacheIntegrationTest {
@Autowired
private ApplicationContext ctx;
@Test
public void testBasicCache() throws Exception {
ctx.getBean("default-cache");
}
@Test
public void testCacheWithProps() throws Exception {
Cache cache = ctx.getBean("cache-with-props", Cache.class);
// the name property seems to be ignored
// Assert.assertEquals("cache-with-props", cache.getDistributedSystem().getName());
}
@Test
public void testNamedCache() throws Exception {
ctx.getBean("named-cache");
}
@Test
public void testCacheWithXml() throws Exception {
Cache cache = ctx.getBean("cache-with-xml", Cache.class);
//Assert.assertEquals("cache-with-props", cache.getDistributedSystem().getName());
}
}

View File

@@ -0,0 +1,54 @@
/*
* 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 static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
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;
/**
* Integration test for declarable support (and GEF bean factory locator).
*
* @author Costin Leau
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "cache-with-declarable-ctx.xml" })
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
public class DeclarableSupportTest {
@Autowired
private BeanFactory ctx;
@Test
public void testUserObject() throws Exception {
assertNotNull(UserObject.THIS);
UserObject obj = UserObject.THIS;
assertSame(ctx, obj.getBeanFactory());
assertSame(ctx.getBean("bean"), obj.getProp2());
assertEquals("Enescu", obj.getProp1());
}
}

View File

@@ -0,0 +1,137 @@
/*
* 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 static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.access.BeanFactoryReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.gemfire.GemfireBeanFactoryLocator;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Costin Leau
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "locatorContext.xml" })
public class GemfireBeanFactoryLocatorTest {
@Autowired
private ApplicationContext applicationContext;
private GemfireBeanFactoryLocator locator1, locator2;
private String INSTANCE_1 = "instance1";
private String INSTANCE_2 = "instance2";
@Before
public void init() {
locator1 = new GemfireBeanFactoryLocator();
locator1.setBeanName(INSTANCE_1);
locator1.setBeanFactory(applicationContext);
locator1.afterPropertiesSet();
locator2 = new GemfireBeanFactoryLocator();
locator2.setBeanName(INSTANCE_2);
locator2.setBeanFactory(applicationContext);
locator2.afterPropertiesSet();
}
@After
public void destroy() {
BeanFactoryReference ref1;
try {
ref1 = locator1.useBeanFactory(INSTANCE_1);
ref1.release();
BeanFactoryReference ref2 = locator2.useBeanFactory(INSTANCE_2);
ref2.release();
} catch (IllegalArgumentException e) {
// it's okay
}
locator1.destroy();
locator2.destroy();
locator1 = null;
locator2 = null;
}
@Test
public void testBeanFactoryRelease() throws Exception {
}
@Test
public void testFactoryLocator() throws Exception {
BeanFactoryReference reference1 = locator1.useBeanFactory(INSTANCE_1);
BeanFactoryReference reference2 = locator2.useBeanFactory(INSTANCE_2);
BeanFactoryReference aliasRef1 = locator1.useBeanFactory("alias1");
BeanFactoryReference aliasRef2 = locator1.useBeanFactory("alias2");
// verify the static map
BeanFactory factory1 = reference1.getFactory();
BeanFactory factory2 = reference2.getFactory();
BeanFactory factory3 = reference2.getFactory();
// get the alias from different factories
BeanFactory alias1 = aliasRef1.getFactory();
BeanFactory alias2 = aliasRef2.getFactory();
assertSame(factory1, factory2);
assertSame(factory1, factory3);
// verify it's the same bean factory as the application context
assertSame(factory1, applicationContext);
// verify aliases
assertSame(alias1, alias2);
assertSame(factory1, alias1);
aliasRef1.release();
aliasRef2.release();
reference1.release();
reference2.release();
}
@Test
public void testDefaultFactoryLocator() throws Exception {
try {
locator1.useBeanFactory(null);
fail("there are more then one bean factories registered - should have thrown exception");
} catch (IllegalArgumentException e) {
// it's okay
}
}
@Test
public void testFactoryLocatorContract() throws Exception {
BeanFactoryReference factory1 = locator1.useBeanFactory(INSTANCE_1);
assertNotNull(factory1.getFactory());
factory1.release();
try {
factory1.getFactory();
fail("should have received exception");
} catch (IllegalArgumentException e) {
// it's okay
}
}
}

View File

@@ -0,0 +1,102 @@
/*
* 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 static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import java.util.Arrays;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.gemfire.ClientRegionFactoryBean;
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.CacheListener;
import com.gemstone.gemfire.cache.CacheLoader;
import com.gemstone.gemfire.cache.CacheLoaderException;
import com.gemstone.gemfire.cache.LoaderHelper;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.util.CacheListenerAdapter;
import com.gemstone.gemfire.cache.util.CacheWriterAdapter;
/**
* @author Costin Leau
*/
@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> {
}
private static class CacheLoad<K, V> implements CacheLoader<K, V> {
public V load(LoaderHelper<K, V> arg0) throws CacheLoaderException {
return null;
}
public void close() {
}
}
private static class CacheWrite<K, V> extends CacheWriterAdapter<K, V> {
}
@Autowired
private ApplicationContext ctx;
@Test
public void testBasicRegion() throws Exception {
Region region = ctx.getBean("basic", Region.class);
assertEquals("basic", region.getName());
}
@Test
public void testExistingRegion() throws Exception {
Region region = ctx.getBean("root", Region.class);
// the name property seems to be ignored
assertEquals("root", region.getName());
}
@Test
public void testRegionWithListeners() throws Exception {
Region region = ctx.getBean("listeners", Region.class);
assertEquals("listeners", region.getName());
CacheListener[] listeners = region.getAttributes().getCacheListeners();
assertEquals(2, listeners.length);
assertSame(CacheList.class, listeners[0].getClass());
assertSame(CacheLoad.class, region.getAttributes().getCacheLoader().getClass());
assertSame(CacheWrite.class, region.getAttributes().getCacheWriter().getClass());
}
//@Test
//TODO: Disabled since for some reason in Spring, I get the bean rather then the client
public void testRegionInterest() throws Exception {
ClientRegionFactoryBean regionFB = (ClientRegionFactoryBean) ctx.getBean("&basic-client");
System.out.println("**** interests are " + Arrays.toString(regionFB.getInterests()));
//BeanDefinition bd = ((BeanDefinitionRegistry) ctx.getAutowireCapableBeanFactory()).getBeanDefinition("basic-client");
// System.out.println(bd.getPropertyValues().getPropertyValue("interests").getValue());
}
}

View File

@@ -0,0 +1,88 @@
/*
* 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 static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.Map;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.AfterTransaction;
import org.springframework.test.context.transaction.BeforeTransaction;
import org.springframework.transaction.annotation.Transactional;
/**
* Simple TX integration test.
*
* @author Costin Leau
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "basic-tx-config.xml" })
@Transactional
public class TxIntegrationTest {
@Resource(name = "rollback-region")
private Map<String, String> rollbackRegion;
@Resource(name = "commit-region")
private Map<String, String> commitRegion;
private boolean txCommit = false;
@BeforeTransaction
public void addItemsToTheCache() {
rollbackRegion.put("Vlaicu", "Aurel");
rollbackRegion.put("Coanda", "Henri");
commitRegion.put("Coanda", "Henri");
commitRegion.put("Vlaicu", "Aurel");
txCommit = false;
}
@Test
public void testTransactionRollback() throws Exception {
rollbackRegion.remove("Coanda");
rollbackRegion.put("Ciurcu", "Alexandru");
}
@Test
@Rollback(value = false)
public void testTransactionCommit() throws Exception {
commitRegion.put("Poenaru", "Petrache");
commitRegion.remove("Coanda");
commitRegion.put("Vlaicu", "A");
txCommit = true;
}
@AfterTransaction
public void testTxOutcome() {
if (txCommit) {
assertFalse(commitRegion.containsKey("Coanda"));
assertTrue(commitRegion.containsKey("Poenaru"));
assertTrue(commitRegion.containsValue("A"));
}
assertTrue(rollbackRegion.containsKey("Coanda"));
assertTrue(rollbackRegion.containsKey("Vlaicu"));
assertFalse(rollbackRegion.containsKey("Ciurcu"));
}
}

View File

@@ -0,0 +1,73 @@
/*
* 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.springframework.data.gemfire.WiringDeclarableSupport;
import com.gemstone.gemfire.cache.CacheLoader;
import com.gemstone.gemfire.cache.CacheLoaderException;
import com.gemstone.gemfire.cache.LoaderHelper;
/**
* User object used for testing Spring wiring.
*
* @author Costin Leau
*/
public class UserObject extends WiringDeclarableSupport implements CacheLoader {
public static UserObject THIS;
private String prop1;
private Object prop2;
public UserObject() {
System.out.println("Initialized");
THIS = this;
}
public Object load(LoaderHelper helper) throws CacheLoaderException {
return new Object();
}
/**
* @return the prop1
*/
public String getProp1() {
return prop1;
}
/**
* @param prop1 the prop1 to set
*/
public void setProp1(String prop1) {
this.prop1 = prop1;
}
/**
* @return the prop2
*/
public Object getProp2() {
return prop2;
}
/**
* @param prop2 the prop2 to set
*/
public void setProp2(Object prop2) {
this.prop2 = prop2;
}
}

View File

@@ -0,0 +1,90 @@
/*
* 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.serialization;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.io.Serializable;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.gemfire.serialization.AsmInstantiatorGenerator;
import com.gemstone.gemfire.DataSerializable;
import com.gemstone.gemfire.Instantiator;
/**
* @author Costin Leau
*/
public class AsmInstantiatorFactoryTest {
public static class SomeClass implements DataSerializable {
public static boolean instantiated = false;
public SomeClass() {
instantiated = true;
}
public void fromData(DataInput in) throws IOException, ClassNotFoundException {
}
public void toData(DataOutput out) throws IOException {
}
}
private AsmInstantiatorGenerator asmFactory = null;
@Before
public void setUp() {
SomeClass.instantiated = false;
asmFactory = new AsmInstantiatorGenerator();
}
@Test
public void testClassGeneration() throws Exception {
Instantiator instantiator = asmFactory.getInstantiator(SomeClass.class, 100);
assertEquals(100, instantiator.getId());
assertEquals(SomeClass.class, instantiator.getInstantiatedClass());
Object instance = instantiator.newInstance();
assertEquals(SomeClass.class, instance.getClass());
assertTrue(SomeClass.instantiated);
}
@Test
public void testGeneratedClassName() throws Exception {
Instantiator instantiator = asmFactory.getInstantiator(SomeClass.class, 100);
assertTrue(instantiator.getClass().getName().contains("$"));
}
@Test
public void testInterfaces() throws Exception {
Instantiator instantiator = asmFactory.getInstantiator(SomeClass.class, 100);
assertTrue(instantiator instanceof Serializable);
}
@Test
public void testCacheInPlace() throws Exception {
Instantiator instance1 = asmFactory.getInstantiator(SomeClass.class, 120);
Instantiator instance2 = asmFactory.getInstantiator(SomeClass.class, 125);
assertSame(instance1, instance2);
}
}

View File

@@ -0,0 +1,118 @@
/*
* 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.serialization;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.awt.Point;
import java.awt.Shape;
import java.beans.Beans;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.gemfire.serialization.AsmInstantiatorGenerator;
import org.springframework.data.gemfire.serialization.WiringInstantiator;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.gemstone.gemfire.DataSerializable;
/**
* @author Costin Leau
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("simple-config.xml")
public class WiringInstantiatorTest {
@Autowired
private ApplicationContext ctx;
@Autowired
private WiringInstantiator instantiator;
public static class AnnotatedBean implements DataSerializable {
@Autowired
Point point;
Shape shape;
@Autowired
void initShape(Shape shape) {
this.shape = shape;
}
public void fromData(DataInput in) throws IOException, ClassNotFoundException {
}
public void toData(DataOutput out) throws IOException {
}
}
public static class TemplateWiringBean implements DataSerializable {
Point point;
Beans beans;
public void setBeans(Beans bs) {
this.beans = bs;
}
public void fromData(DataInput in) throws IOException, ClassNotFoundException {
}
public void toData(DataOutput out) throws IOException {
}
}
@Test
public void testAutowiredBean() throws Exception {
Object instance = instantiator.newInstance();
assertNotNull(instance);
assertTrue(instance instanceof AnnotatedBean);
AnnotatedBean bean = (AnnotatedBean) instance;
assertNotNull(bean.point);
assertNotNull(bean.shape);
assertSame(bean.point, ctx.getBean("point"));
assertSame(bean.shape, ctx.getBean("area"));
}
@Test
public void testTemplateBean() throws Exception {
WiringInstantiator instantiator2 = new WiringInstantiator(
new AsmInstantiatorGenerator().getInstantiator(
TemplateWiringBean.class, 99));
instantiator2.setBeanFactory(ctx.getAutowireCapableBeanFactory());
instantiator2.afterPropertiesSet();
Object instance = instantiator2.newInstance();
assertTrue(instance instanceof TemplateWiringBean);
TemplateWiringBean bean = (TemplateWiringBean) instance;
assertTrue(bean.point == null);
assertNotNull(bean.beans);
assertSame(bean.beans, ctx.getBean("beans"));
}
}