1 /* ====================================================================
2 * License:
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * 3. The end-user documentation included with the redistribution,
17 * if any, must include the following acknowledgment:
18 * "This product includes software developed by
19 * Robert Half International (http://www.rhi.com/)."
20 * Alternately, this acknowledgment may appear in the software itself,
21 * if and wherever such third-party acknowledgments normally appear.
22 *
23 * 4. The names "Parc", "RHI", and "Robert Half International" must
24 * not be used to endorse or promote products derived from this
25 * software without prior written permission. For written
26 * permission, please contact pete.mckinstry@rhi.com.
27 *
28 * 5. Products derived from this software may not be called "PARC",
29 * nor may "PARC" appear in their name, without prior written
30 * permission of Robert Half International.
31 *
32 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
33 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
36 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
39 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
40 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
41 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
42 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * SUCH DAMAGE.
44 * ====================================================================
45 *
46 */
47 package com.rhi.architecture.parc.filter;
48
49 import com.rhi.architecture.parc.*;
50
51 import java.util.ArrayList;
52 import java.util.Collection;
53 import java.util.Iterator;
54
55 import junit.framework.Test;
56 import junit.framework.TestSuite;
57
58 /***
59 * TestFilterTest.java
60 *
61 * @author <a href="mailto:pete.mckinstry@rhi.com">Pete McKinstry</a>
62 * @copyright 2002, Robert Half International. All rights reserved.
63 *
64 * @version 1.0
65 */
66 public class TestFilterTest extends AbstractFilterTest {
67
68 private Filter filter = null;
69
70 /***
71 * Constructor for TestFilterTest.
72 * @param name
73 */
74 public TestFilterTest(String name) {
75 super(name);
76 }
77
78 /***
79 * @see com.rhi.architecture.parc.filter.AbstractFilterTest#getFilter()
80 * @return Filter
81 */
82 protected synchronized Filter getFilter() {
83 if (filter==null) {
84 filter = new TestFilter();
85 }
86 return filter;
87 }
88
89 /***
90 * test pass thru records. (no processing.
91 * @throws Exception
92 */
93 public void testPassThruRecords() throws Exception {
94 log("TestFilterTest.testPassThruRecords");
95 getFilter().markForDeath();
96
97 Record r = new TestRecord();
98 ArrayList records = new ArrayList();
99 records.add(r);
100 this.getInputChannel().push(records);
101
102 // start the filter right away.
103 Thread filterThread = new Thread(getFilter());
104 filterThread.start();
105
106 // wait for filter to finish & shutdown.
107 while (filterThread.isAlive()) {
108 try {
109 Thread.sleep(100);
110 }
111 catch (InterruptedException e) {
112 // ignore
113 }
114 }
115
116 Collection output = this.getOutputChannel().pull(100);
117 assertTrue(
118 "filter failed to pass through records.",
119 output.size() == 1);
120 Iterator iter = output.iterator();
121 while (iter.hasNext()) {
122 Object o = iter.next();
123 assertTrue("record wrong type.", o instanceof TestRecord);
124 TestRecord record = (TestRecord) o;
125 assertTrue(
126 "output record doesn't match input record.",
127 record.equals(r));
128 }
129 }
130
131 /***
132 * test notification
133 * @throws Exception
134 */
135 public void testNotification() throws Exception {
136 log("TestFilterTest.testNotification");
137
138 // start the filter right away.
139 Thread filterThread = new Thread(getFilter());
140 filterThread.start();
141
142 log("waiting");
143 Thread.sleep(1000);
144
145 ArrayList records = new ArrayList();
146 for (int i=0; i< 10; ++i) {
147 Record r = new TestRecord();
148 records.add(r);
149 }
150 this.getInputChannel().push(records);
151
152 filter.markForDeath();
153 while (filterThread.isAlive()) {
154 try {
155 Thread.sleep(100);
156 }
157 catch (InterruptedException e) {
158 // ignore
159 }
160 }
161
162 Collection output = this.getOutputChannel().pull(100);
163 assertTrue("filter failed to pass through records.",
164 output.size() == 10);
165 Iterator iter = output.iterator();
166 while (iter.hasNext()) {
167 Object o = iter.next();
168 assertTrue("record wrong type.", o instanceof TestRecord);
169 }
170 }
171
172
173 /***
174 * test multi-threads
175 * @throws Exception
176 */
177 public void testMultipleThreads() throws Exception {
178 log("TestFilterTest.testMultipleThreads");
179
180 int numRecords = 1000;
181 int numThreads = 5;
182
183 // start the filter right away.
184 log("starting " + numThreads + " threads.");
185 ThreadGroup threadGroup = new ThreadGroup("testMultipleThreads");
186 for (int i=0; i< numThreads; ++i) {
187 new Thread(threadGroup, getFilter()).start();
188 }
189
190 ArrayList records = new ArrayList();
191 for (int i=0; i< numRecords; ++i) {
192 Record r = new TestRecord();
193 records.add(r);
194 }
195 this.getInputChannel().push(records);
196
197 filter.markForDeath();
198 while (threadGroup.activeCount()>0) {
199 try {
200 Thread.sleep(250);
201 }
202 catch (InterruptedException e) {
203 // ignore
204 }
205 }
206
207 Collection output = this.getOutputChannel().pull(5000);
208 assertTrue("filter failed to pass through records.",
209 output.size() == numRecords);
210 Iterator iter = output.iterator();
211 while (iter.hasNext()) {
212 Object o = iter.next();
213 assertTrue("record wrong type.", o instanceof TestRecord);
214 }
215 }
216
217 /***
218 * suite of all tests
219 * @return Test
220 */
221 public static Test suite() {
222 return new TestSuite(TestFilterTest.class);
223 }
224
225 /***
226 * main()
227 * @param args
228 */
229 public static void main(String[] args) {
230 junit.textui.TestRunner.run(suite());
231 }
232
233 /***
234 * generic logging method.
235 * @param msg
236 */
237 public static void log( String msg ) {
238 System.out.println(msg);
239 }
240
241 }
This page was automatically generated by Maven