KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > cache > TestJMSCache


1 package org.jahia.services.cache;
2
3 import java.io.File JavaDoc;
4
5 import org.jahia.bin.JahiaInterface;
6 import org.jahia.settings.SettingsBean;
7 import org.jahia.utils.FilePathResolver;
8 import org.jahia.utils.PathResolver;
9 import junit.framework.TestCase;
10 import org.apache.log4j.xml.DOMConfigurator;
11
12 /**
13  * <p>Title: </p>
14  * <p>Description: </p>
15  * <p>Copyright: Copyright (c) 2002</p>
16  * <p>Company: Jahia Ltd</p>
17  * @author not attributable
18  * @version 1.0
19  */

20
21 public class TestJMSCache extends TestCase {
22
23     protected void setUp() throws Exception JavaDoc {
24         super.setUp();
25         DOMConfigurator.configureAndWatch("log4j.xml");
26     }
27
28     protected void tearDown() throws Exception JavaDoc {
29         super.tearDown();
30     }
31
32     public void testPut() {
33         CacheTest cacheTest1 = new CacheTest1();
34         CacheTest cacheTest2 = new CacheTest2();
35         CacheThread cacheThread1 = new CacheThread(cacheTest1);
36         CacheThread cacheThread2 = new CacheThread(cacheTest2);
37         Thread JavaDoc thread1 = new Thread JavaDoc(cacheThread1);
38         Thread JavaDoc thread2 = new Thread JavaDoc(cacheThread2);
39         thread1.start();
40         thread2.start();
41         try {
42             thread1.join();
43             thread2.join();
44         } catch (InterruptedException JavaDoc e) {
45             e.printStackTrace();
46         }
47     }
48
49     protected interface CacheTest {
50         public void runCacheTest(Cache cache);
51     }
52
53     private class CacheTest1 implements CacheTest {
54         public synchronized void runCacheTest(Cache cache) {
55             String JavaDoc entryKey = "prout";
56             String JavaDoc readEntryObj;
57             System.out.println("Thread 1, inserting values...");
58             for (int i=0; i < 100; i++) {
59                 cache.put(entryKey, Integer.toString(i));
60                 try {
61                     wait(2);
62                 } catch (InterruptedException JavaDoc ie) {
63                     ie.printStackTrace();
64                 }
65             }
66             System.out.println("Thread 1, finished inserting values.");
67             String JavaDoc value = (String JavaDoc) cache.get("prout");
68             System.out.println("Thread 1 : cache value=[" + value + "]");
69
70         }
71     }
72
73     private class CacheTest2 implements CacheTest {
74         public synchronized void runCacheTest(Cache cache) {
75             String JavaDoc entryKey = "prout";
76             cache.put("prout", Integer.toString(-1));
77             System.out.println("Thread 2, inserting values...");
78             for (int i=0; i < 100; i++) {
79                 cache.put(entryKey, Integer.toString(i));
80                 try {
81                     wait(2);
82                 } catch (InterruptedException JavaDoc ie) {
83                     ie.printStackTrace();
84                 }
85             }
86             System.out.println("Thread 2, finished inserting values.");
87             String JavaDoc value = (String JavaDoc) cache.get("prout");
88             System.out.println("Thread 2 : cache value=[" + value + "]");
89         }
90     }
91
92     private class CacheThread implements Runnable JavaDoc {
93
94         private Cache cache = null;
95         private CacheFactory cacheFactory = null;
96         private SettingsBean settings = null;
97         private CacheTest cacheTest;
98
99         public CacheThread(CacheTest cacheTest) {
100             this.cacheTest = cacheTest;
101         }
102
103         public void setUp() throws Exception JavaDoc {
104             PathResolver pathResolver = new FilePathResolver();
105             File JavaDoc jahiaJMSProperties = new File JavaDoc("jahia_jmstest.properties");
106             System.out.println("Loading Jahia configuration from " + jahiaJMSProperties.getAbsoluteFile().toString());
107             settings = new SettingsBean(pathResolver, "jahia_jmstest.properties", "", JahiaInterface.BUILD_NUMBER);
108             settings.load();
109             cacheFactory = new CacheFactory();
110             cacheFactory.init(settings);
111             cacheFactory.setJMXEnabled(false);
112             cacheFactory.enableJMSSynchronization();
113             cache = cacheFactory.createCacheInstance("testCache");
114         }
115
116         public void run() {
117             try {
118                 setUp();
119                 cacheTest.runCacheTest(cache);
120                 tearDown();
121             } catch (Exception JavaDoc e) {
122                 e.printStackTrace();
123             }
124         }
125
126         public void tearDown() throws Exception JavaDoc {
127             cache = null;
128             cacheFactory.shutdown();
129         }
130     }
131
132 }
133
Popular Tags