1 /**
2 * Copyright 2005-2006 the original author or authors.
3 *
4 * Licensed under the Gnu General Pubic License, Version 2.0 (the
5 * "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/gpl-license.php
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the Gnu General Public License for more details.
14 */
15 package org.figure8.join.services.cache;
16
17 import org.figure8.join.businessobjects.commons.Release;
18
19 import junit.framework.Test;
20 import junit.framework.TestCase;
21 import junit.framework.TestSuite;
22
23 import java.util.Date;
24 import java.util.List;
25 /**
26 * JUnit test case for EhCache.
27 * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
28 * @version $Revision: 1.2 $
29 */
30 public class EhCacheTest extends TestCase{
31
32
33
34 /** EhCacheMnaager containing caches */
35 private EhCacheManager cacheManager = null;
36
37
38
39
40 /** Default constructor. Build a test case using a name. */
41 public EhCacheTest(String testName){
42 super(testName);
43 }
44
45
46
47
48 /**
49 * Make this class a TestSuite.
50 * @return A TestSuite containing this TestCase.
51 */
52 public static Test suite(){
53 TestSuite suite = new TestSuite(EhCacheTest.class);
54 return suite;
55 }
56
57 /** Create and initialize a EhCacheManager */
58 public void setUp(){
59
60 cacheManager = new EhCacheManager();
61 try{
62 cacheManager.createCaches();
63 }
64 catch (CacheException ce){
65 fail("CacheException while creating Eh caches: " + ce.getCause().getMessage());
66 }
67 }
68
69
70
71
72 /** Test the getKey() method on cache */
73 public void testGetKey(){
74
75 Cache cache = cacheManager.getCache(EternalCacheKeys.RELEASE_NAME_TO_RELEASE_KEY);
76
77 assertNotNull("Releases cache exists and is configured", cache);
78 assertEquals("Releases cache as the correct key", EternalCacheKeys.RELEASE_NAME_TO_RELEASE_KEY, cache.getKey());
79 }
80
81 /** Test successive put() and get() on cache */
82 public void testPutAntGet(){
83
84 Cache cache = cacheManager.getCache(EternalCacheKeys.RELEASE_NAME_TO_RELEASE_KEY);
85 Release release1 = new Release("1.0", 1, 0, new Date());
86 Release release2 = new Release("2.0", 2, 0, new Date());
87 try{
88 cache.put(release1.getName(), release1);
89 cache.put(release2.getName(), release2);
90
91 Release foundRelease = (Release)cache.get("1.0");
92 assertNotNull("Found a Release in cache", foundRelease);
93 assertEquals("Found the correct Release in cache", 1, foundRelease.getMajor());
94 assertEquals("Found the correct Release in cache", 0, foundRelease.getMinor());
95 assertEquals("Found the correct Release in cache", "1.0", foundRelease.getName());
96
97 List elements = cache.getElements();
98 assertNotNull("Retrieve cache elements", elements);
99 assertEquals("Retrieve correct cache elements count", 2, elements.size());
100 }
101 catch (CacheException ce){
102 fail("CacheException while putting in EhCache: " + ce.getCause().getMessage());
103 }
104 }
105
106 /** Test succesive put(), remove() and clear() on cache */
107 public void testRemoveAndClear(){
108
109 Cache cache = cacheManager.getCache(EternalCacheKeys.RELEASE_NAME_TO_RELEASE_KEY);
110 Release release = new Release("3.0", 3, 0, new Date());
111 try{
112 cache.put(release.getName(), release);
113
114 Release foundRelease = (Release)cache.get("3.0");
115 assertNotNull("Found a Release in cache", foundRelease);
116
117
118 cache.remove("3.0");
119 foundRelease = (Release)cache.get("3.0");
120 assertNull("Release cannot be found cause removed", foundRelease);
121
122
123 cache.put("3.0", release);
124 cache.put("4.0", release);
125 cache.clear();
126 assertEquals("Releases cache contains no element", 0, ((EhCache)cache).getWrappedCache().getSize());
127 }
128 catch (Exception e){
129 fail("CacheException while clearing in EhCache: " + e.getCause().getMessage());
130 }
131 }
132 }