1 /***
2 *
3 * Copyright 2004 Protique Ltd
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.codehaus.activespace;
19
20 import junit.framework.TestCase;
21
22 /***
23 * @version $Revision: 1.1.1.1 $
24 */
25 public abstract class SpaceTestSupport extends TestCase {
26 protected static long MAX_WAIT = 10000L;
27 protected static final long SLEEP_TIME = 2000L;
28
29 protected Space space;
30 protected SpaceFactory spaceFactory;
31 protected SpaceEvent spaceEvent;
32 protected String destination = "TEST." + getClass() + "." + getName();
33 protected int dispatchMode = SpaceFactory.DISPATCH_ONE_CONSUMER;
34 protected String filter = null;
35 protected final Object eventSemaphore = new Object();
36
37 public void testTake() throws Exception {
38 Object value = "some value to be taken";
39
40 space.put(value);
41
42 Object actual = space.take();
43 assertEquals("taken the right value", value, actual);
44 }
45
46 public void testTakeTimeout() throws Exception {
47 Object actual = space.take(1000L);
48 assertEquals("Should not have received anything yet!", null, actual);
49
50 Object value = new Double(32342);
51 space.put(value);
52
53 actual = space.take(10000L);
54 assertEquals("taken the right value", value, actual);
55 }
56
57 public void testTakeNoWait() throws Exception {
58
59
60
61 Object actual = space.takeNoWait();
62 assertEquals("Should not have received anything yet!", null, actual);
63
64 Object value = new Double(1234152);
65 space.put(value);
66
67 waitToLetAsyncDispatchCatchUp();
68
69 actual = space.takeNoWait();
70 assertEquals("taken the right value", value, actual);
71 }
72
73 public void testSpaceEvents() throws Exception {
74 spaceEvent = null;
75
76 SpaceListener listener = new SpaceListener() {
77 public void onEvent(SpaceEvent event) {
78 if (spaceEvent != null) {
79 throw new RuntimeException("Already received an event! Already recieved: "
80 + spaceEvent
81 + " and did not expect: "
82 + event);
83 }
84 spaceEvent = event;
85
86 synchronized (eventSemaphore) {
87 eventSemaphore.notifyAll();
88 }
89 }
90 };
91 space.addSpaceListener(listener);
92
93 Object value = "some value";
94
95 space.put(value);
96
97 while (spaceEvent == null) {
98 synchronized (eventSemaphore) {
99 try {
100 eventSemaphore.wait(MAX_WAIT);
101 break;
102 }
103 catch (InterruptedException e) {
104 onInteruptedException(e);
105 }
106 }
107 }
108
109 assertTrue("Received an event", spaceEvent != null);
110 assertSame("Event has correct space", space, spaceEvent.getSpace());
111 assertEquals("Event has correct value", value, spaceEvent.getEntry());
112
113 spaceEvent = null;
114 space.removeSpaceListener(listener);
115 }
116
117
118
119 public SpaceFactory getSpaceFactory() {
120 if (spaceFactory == null) {
121 spaceFactory = createSpaceFactory();
122 }
123 return spaceFactory;
124 }
125
126 protected abstract SpaceFactory createSpaceFactory();
127
128
129 /***
130 * Factory method to create a new space instance for testing
131 *
132 * @return
133 */
134 protected Space createSpace() throws SpaceException {
135 return getSpaceFactory().createSpace(destination, dispatchMode, filter);
136 }
137
138 protected void setUp() throws Exception {
139 super.setUp();
140 space = createSpace();
141 }
142
143 protected void tearDown() throws Exception {
144 super.tearDown();
145 space.close();
146 }
147
148
149 /***
150 * dispatching from the space to a space consumer will be asynchronous
151 * so we need to wait a little while
152 */
153 protected void waitToLetAsyncDispatchCatchUp() {
154 try {
155 Thread.sleep(SLEEP_TIME);
156 }
157 catch (InterruptedException e) {
158 onInteruptedException(e);
159 }
160 }
161
162 protected void onInteruptedException(InterruptedException e) {
163 System.out.println("Caught: " + e);
164 }
165
166 }