001: // $Id: DataConverter.java 1546 2007-07-23 06:07:56Z grro $
002:
003: /*
004: * Copyright (c) xcache.org, 2007. All rights reserved.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
021: * The latest copy of this software may be found on http://www.xcache.org/
022: */
023: package org.xcache;
024:
025: import java.util.concurrent.atomic.AtomicInteger;
026: import java.util.logging.Level;
027:
028: import net.sf.ehcache.CacheManager;
029: import net.sf.ehcache.jcache.JCache;
030: import net.sf.jsr107cache.Cache;
031:
032: import org.junit.Assert;
033: import org.junit.Test;
034: import org.xsocket.JmxServer;
035: import org.xsocket.QAUtil;
036: import org.xsocket.stream.StreamUtils;
037:
038: /**
039: *
040: * @author grro@xcache.org
041: */
042: public final class StoreServerTest {
043:
044: private static final AtomicInteger num = new AtomicInteger();
045:
046: @Test
047: public void testStaticSingleServer() throws Exception {
048:
049: QAUtil.setLogLevel("org.xcache", Level.FINE);
050:
051: JmxServer jmxServer = new JmxServer();
052: jmxServer.start("test5");
053:
054: CacheServer storeServer = new CacheServer(getCacheFactory());
055: StreamUtils.start(storeServer);
056: CacheServerMBeanProxyFactory.createAndRegister(storeServer);
057:
058: StaticCacheClientManager cacheMgr = new StaticCacheClientManager();
059: cacheMgr.addCacheServer(storeServer.getLocalAddress(),
060: storeServer.getLocalPort());
061:
062: Cache cache = cacheMgr.getCache();
063:
064: Assert.assertTrue(storeServer.getCacheSize() == 0);
065: Object old = cache.put("1", "test1");
066: Assert.assertNull(old);
067: QAUtil.sleep(200);
068:
069: Assert.assertTrue(storeServer.getCacheSize() == 1);
070: old = cache.put("2", "test2test3test4test5test6test7");
071: Assert.assertNull(old);
072: QAUtil.sleep(200);
073: Assert.assertTrue(storeServer.getCacheSize() == 2);
074:
075: Object value = cache.get("2");
076: Assert.assertEquals("test2test3test4test5test6test7", value);
077:
078: old = cache.put("1", "test");
079: Assert.assertEquals("test1", old);
080: QAUtil.sleep(200);
081: Assert.assertTrue(storeServer.getCacheSize() == 2);
082:
083: old = cache.put("3", new Integer(6));
084: QAUtil.sleep(200);
085: Assert.assertTrue(storeServer.getCacheSize() == 3);
086:
087: value = cache.get("3");
088: Assert.assertEquals(new Integer(6), value);
089:
090: value = cache.get("3");
091: Assert.assertEquals(new Integer(6), value);
092:
093: value = cache.get("1");
094: Assert.assertEquals("test", value);
095:
096: value = cache.get("9");
097: Assert.assertNull(value);
098:
099: jmxServer.stop();
100: storeServer.close();
101: }
102:
103: @Test
104: public void testStatic() throws Exception {
105:
106: QAUtil.setLogLevel("org.xcache", Level.FINE);
107:
108: JmxServer jmxServer = new JmxServer();
109: jmxServer.start("test");
110:
111: CacheServer storeServerA = new CacheServer(getCacheFactory());
112: StreamUtils.start(storeServerA);
113: CacheServerMBeanProxyFactory.createAndRegister(storeServerA);
114:
115: CacheServer storeServerB = new CacheServer(getCacheFactory());
116: StreamUtils.start(storeServerB);
117: CacheServerMBeanProxyFactory.createAndRegister(storeServerB);
118:
119: StaticCacheClientManager cacheMgr = new StaticCacheClientManager();
120: cacheMgr.addCacheServer(storeServerA.getLocalAddress(),
121: storeServerA.getLocalPort());
122: cacheMgr.addCacheServer(storeServerB.getLocalAddress(),
123: storeServerB.getLocalPort());
124:
125: Cache cache = cacheMgr.getCache();
126:
127: Object old = cache.put("1", "test1");
128: Assert.assertNull(old);
129: QAUtil.sleep(200);
130:
131: old = cache.put("2", "test2test3test4test5test6test7");
132: Assert.assertNull(old);
133: QAUtil.sleep(200);
134:
135: Object value = cache.get("2");
136: Assert.assertEquals("test2test3test4test5test6test7", value);
137:
138: old = cache.put("1", "test");
139: Assert.assertEquals("test1", old);
140: QAUtil.sleep(200);
141:
142: old = cache.put("3", new Integer(6));
143: QAUtil.sleep(200);
144:
145: value = cache.get("3");
146: Assert.assertEquals(new Integer(6), value);
147:
148: value = cache.get("3");
149: Assert.assertEquals(new Integer(6), value);
150:
151: value = cache.get("1");
152: Assert.assertEquals("test", value);
153:
154: value = cache.get("9");
155: Assert.assertNull(value);
156:
157: jmxServer.stop();
158: storeServerA.close();
159: storeServerB.close();
160: }
161:
162: @Test
163: public void testDynamicGroup() throws Exception {
164:
165: QAUtil.setLogLevel("org.xcache", Level.FINE);
166:
167: JmxServer jmxServer = new JmxServer();
168: jmxServer.start("test");
169:
170: int segments = 5;
171: DynamicCacheClientManager locator = new DynamicCacheClientManager(
172: segments);
173:
174: CacheServer storeServerA = new CacheServer(getCacheFactory());
175: StreamUtils.start(storeServerA);
176: CacheServerMBeanProxyFactory.createAndRegister(storeServerA);
177:
178: locator.addCacheServer(storeServerA.getLocalAddress(),
179: storeServerA.getLocalPort());
180:
181: Cache cache = locator.getCache();
182:
183: for (int i = 0; i < segments * 21; i++) {
184: cache
185: .put(
186: i,
187: i
188: + "sdgfsfsfsfsfsfasdfasfasdfasdfsadfsafasdfasdfasdvsasdfsafsafsdfasfsadfsadfsaffd");
189: String data = (String) cache.get(i);
190: Assert.assertTrue(data.startsWith(Integer.toString(i)));
191: }
192:
193: CacheServer storeServerB = new CacheServer(getCacheFactory());
194: StreamUtils.start(storeServerB);
195: CacheServerMBeanProxyFactory.createAndRegister(storeServerB);
196:
197: locator.addCacheServer(storeServerB.getLocalAddress(),
198: storeServerB.getLocalPort());
199: for (int i = 0; i < segments * 21; i++) {
200: String data = (String) cache.get(i);
201: Assert.assertTrue(data.startsWith(Integer.toString(i)));
202: }
203:
204: jmxServer.stop();
205: storeServerA.close();
206: }
207:
208: private ICacheFactory getCacheFactory() {
209:
210: return new ICacheFactory() {
211: public Cache newCache() {
212: net.sf.ehcache.Cache ehcache = new net.sf.ehcache.Cache(
213: Integer.toString(num.incrementAndGet()), 100,
214: true, true, 0, 0);
215: CacheManager.getInstance().addCache(ehcache);
216: JCache jcache = new JCache(ehcache, null);
217: return jcache;
218: }
219: };
220: }
221: }
|