KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > clustering > test > ClusterCacheTest


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.services.clustering.test;
19
20 import java.util.Random JavaDoc;
21
22 import org.sape.carbon.core.component.Lookup;
23 import org.sape.carbon.services.clustering.ClusterCache;
24
25 import junit.extensions.ActiveTestSuite;
26 import junit.framework.Test;
27 import junit.framework.TestCase;
28 import junit.framework.TestSuite;
29
30 /**
31  * Template for junit test harness. Change this description to reflect what
32  * this class is testing.
33  *
34  * @since carbon 1.0
35  * @author Greg Hinkle, January 2002
36  * @version $Revision: 1.4 $($Author: dvoet $ / $Date: 2003/05/05 21:21:09 $)
37  * @copyright 2002 Sapient
38  */

39 public class ClusterCacheTest extends TestCase {
40
41     public ClusterCacheTest(String JavaDoc name) {
42         super(name);
43     }
44
45     public void testCache() {
46
47         final int SET_COUNT = 5;
48         final int SET_SIZE = 10;
49
50         long[] crcs = new long[SET_SIZE];
51
52
53
54         try {
55             ClusterCache cache =
56                 (ClusterCache)
57                 Lookup.getInstance().fetchComponent(
58                     "/clustering/ClusterCache");
59
60             Random JavaDoc rand = new Random JavaDoc(System.currentTimeMillis());
61             int offset = rand.nextInt();
62             System.out.println("Testing with offset: " + offset);
63
64             for (int i = 0; i < SET_COUNT; i++) {
65
66                 for (int j = 0; j < SET_SIZE; j++) {
67                     TestObject data = new TestObject();
68                     crcs[j] = data.getCrc();
69                     cache.put(new Integer JavaDoc(j+offset),data);
70                     delay();
71                 }
72                 System.out.println("Finished phase: 1 round: " + i);
73                 System.out.println("Current Cluster Cache Size: " + cache.size());
74
75                 for (int j = 0; j < SET_SIZE; j++) {
76                     TestObject data = (TestObject) cache.get(new Integer JavaDoc(j+offset));
77                     if (data == null) {
78                         fail("Unable to retrieve keyed object at key: " +
79                             (j+offset));
80                     }
81                     long crc = data.getCrc();
82                     if (crcs[j] != crc) {
83                         fail("Data object retrieved had different value then the one put in. " +
84                             "In Crc: " + crcs[j] + " Out Crc: " + crc);
85                     }
86                     delay();
87                 }
88                 System.out.println("Finished phase: 2 round: " + i);
89                 System.out.println("Current Cluster Cache Size: " + cache.size());
90
91                 for (int j = 0; j < SET_SIZE; j++) {
92                     TestObject data = (TestObject)cache.remove(new Integer JavaDoc(j+offset));
93                     long crc = data.getCrc();
94                     if (crcs[j] != crc) {
95                         fail("Data object retrieved had different value then the one put in. " +
96                             "In Crc: " + crcs[j] + " Out Crc: " + crc);
97                     }
98                     delay();
99                 }
100                 System.out.println("Finished phase: 3 round: " + i);
101                 System.out.println("Current Cluster Cache Size: " + cache.size());
102
103             }
104         } catch (Exception JavaDoc e) {
105             e.printStackTrace();
106         }
107     }
108
109     private void delay() {
110         try {
111             Thread.yield();
112             Thread.sleep(10);
113         } catch (InterruptedException JavaDoc ie) { }
114     }
115
116     /**
117      * Method called by jUnit to get all the tests in this test case.
118      * @return Test the suite of tests in this test case
119      */

120     public static Test suite() {
121         TestSuite masterSuite = new TestSuite();
122
123         // add single threaded tests
124
Test singleThreadedTests = getSingleThreadedTests();
125         if(singleThreadedTests != null) {
126             masterSuite.addTest(singleThreadedTests);
127         }
128
129         // add multi threaded tests
130
Test multiThreadedTests = getMultiThreadedTests();
131         if(multiThreadedTests != null) {
132             masterSuite.addTest(multiThreadedTests);
133         }
134
135         return masterSuite;
136     }
137
138     /**
139      * This method is used within the suite method to get all of the single
140      * threaded tests.
141      *
142      * Add all your single threaded tests in this method with a line like:
143      * suite.addTest(new GroupsTest("testFunction1"));
144      *
145      * @return Test the suite of single threaded tests in this test case
146      */

147     private static Test getSingleThreadedTests() {
148         TestSuite suite = new TestSuite();
149         /*
150          * add your tests here following these examples:
151          *
152          * suite.addTest(new GroupsTest("testFunction1"));
153          * suite.addTest(new GroupsTest("testFunction2"));
154          */

155         suite.addTest(new ClusterCacheTest("testCache"));
156
157         return suite;
158     }
159
160     /**
161      * This method is used within the suite method to get all of the multi
162      * threaded tests.
163      *
164      * Add all your multi threaded tests in this method with a line like:
165      * addTest(suite, "testFunction1", 5);
166      *
167      * @return Test the suite of multi-threaded tests in this test case
168      */

169     private static Test getMultiThreadedTests() {
170         TestSuite suite = new ActiveTestSuite();
171         /*
172          * add your tests here following these examples:
173          *
174          * addTest(suite, "testFunction1", 5);
175          * addTest(suite, "testFunction2", 10);
176          */

177         return suite;
178     }
179
180     /**
181      * This method will add the give test to the give suite the specified
182      * number of times. This is best used for multi-threaded tests where
183      * suite is an instance of ActiveTestSuite and you want to run the same
184      * test in multiple threads.
185      *
186      * @param suite the suite to add the test to.
187      * @param testName the name of the test to add.
188      * @param number the number of times to add the test to the suite
189      */

190     private static void addTest(TestSuite suite, String JavaDoc testName, int number) {
191         for(int count=0; count<number; count++) {
192             suite.addTest(new ClusterCacheTest(testName));
193         }
194     }
195 }
196
Popular Tags