KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > test > notification > FilterTest


1 package org.jacorb.test.notification;
2
3 import java.util.Hashtable JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.Map JavaDoc;
6 import java.util.Random JavaDoc;
7
8 import junit.framework.Assert;
9 import junit.framework.Test;
10 import junit.framework.TestCase;
11
12 import org.jacorb.notification.IContainer;
13 import org.jacorb.notification.TypedEventMessage;
14 import org.jacorb.notification.filter.FilterFactoryImpl;
15 import org.jacorb.notification.filter.DefaultFilterFactoryDelegate;
16 import org.omg.CORBA.Any JavaDoc;
17 import org.omg.CosNotification.EventType;
18 import org.omg.CosNotification.Property;
19 import org.omg.CosNotifyFilter.ConstraintExp;
20 import org.omg.CosNotifyFilter.ConstraintInfo;
21 import org.omg.CosNotifyFilter.Filter;
22 import org.omg.CosNotifyFilter.FilterFactory;
23 import org.omg.CosNotifyFilter.FilterFactoryHelper;
24 import org.picocontainer.MutablePicoContainer;
25
26 /**
27  * @author Alphonse Bendt
28  * @author John Farrell
29  */

30
31 public class FilterTest extends NotificationTestCase
32 {
33     static final Random JavaDoc random_ = new Random JavaDoc(System.currentTimeMillis());
34
35     ////////////////////////////////////////
36

37     FilterFactory factory_;
38
39     Filter filter_;
40
41     Any JavaDoc testPerson_;
42
43     NotificationTestUtils testUtils_;
44
45     FilterFactoryImpl factoryServant_;
46
47     ////////////////////////////////////////
48

49     public FilterTest(String JavaDoc name, NotificationTestCaseSetup setup)
50     {
51         super(name, setup);
52     }
53
54     ////////////////////////////////////////
55

56     public void setUpTest() throws Exception JavaDoc
57     {
58         IContainer container = new IContainer()
59         {
60             public MutablePicoContainer getContainer()
61             {
62                 return getPicoContainer();
63             }
64             
65             public void destroy()
66             {
67                 // no operation
68
}
69         };
70
71         factoryServant_ = new FilterFactoryImpl(getORB(), getPOA(), getConfiguration(), new DefaultFilterFactoryDelegate(container, getConfiguration()));
72
73         factoryServant_.activate();
74
75         factory_ = FilterFactoryHelper.narrow(factoryServant_.activate());
76
77         testUtils_ = new NotificationTestUtils(getORB());
78
79         testPerson_ = testUtils_.getTestPersonAny();
80
81         filter_ = factory_.create_filter("EXTENDED_TCL");
82     }
83
84     public void tearDownTest() throws Exception JavaDoc
85     {
86         factoryServant_.dispose();
87     }
88
89     public void testFalseMatch() throws Exception JavaDoc
90     {
91         ConstraintExp[] _constraintExp = new ConstraintExp[1];
92         EventType[] _eventType = new EventType[1];
93         _eventType[0] = new EventType("*", "*");
94         String JavaDoc _expression = "FALSE";
95         _constraintExp[0] = new ConstraintExp(_eventType, _expression);
96         filter_.add_constraints(_constraintExp);
97
98         assertFalse(filter_.match(testPerson_));
99     }
100
101     /**
102      * create remote filter object and invoke match operation on it
103      */

104     public void testMatch() throws Exception JavaDoc
105     {
106         ConstraintExp[] _constraintExp = new ConstraintExp[1];
107         EventType[] _eventType = new EventType[1];
108         _eventType[0] = new EventType("*", "*");
109
110         _constraintExp[0] = new ConstraintExp(_eventType, "$.first_name == 'firstname'");
111         filter_.add_constraints(_constraintExp);
112
113         // this should match
114
assertTrue(filter_.match(testPerson_));
115     }
116
117     public void testAccessNonExistingMember() throws Exception JavaDoc
118     {
119         EventType[] _eventType = new EventType[] { new EventType("*", "*") };
120
121         ConstraintExp[] _constraintExp = new ConstraintExp[] {
122                 new ConstraintExp(_eventType, "$not_exist == 3"),
123                 new ConstraintExp(_eventType, "TRUE"), };
124
125         filter_.add_constraints(_constraintExp);
126
127         assertTrue(filter_.match(testPerson_));
128     }
129
130     public void testMatchEmptyFilter() throws Exception JavaDoc
131     {
132         assertTrue(!filter_.match(testPerson_));
133     }
134
135     public void testMatch_EventTypes_IsEmpty() throws Exception JavaDoc
136     {
137         ConstraintExp[] _constraintExp = new ConstraintExp[1];
138         EventType[] _eventType = new EventType[0];
139
140         _constraintExp[0] = new ConstraintExp(_eventType, "$.first_name == 'firstname'");
141         filter_.add_constraints(_constraintExp);
142
143         // this should match
144
assertTrue(filter_.match(testPerson_));
145     }
146
147     public void testMatch_EventType_IsEmptyString() throws Exception JavaDoc
148     {
149         ConstraintExp[] _constraintExp = new ConstraintExp[1];
150         EventType[] _eventType = new EventType[] { new EventType("", "") };
151
152         _constraintExp[0] = new ConstraintExp(_eventType, "$.first_name == 'firstname'");
153         filter_.add_constraints(_constraintExp);
154
155         // this should match
156
assertTrue(filter_.match(testPerson_));
157     }
158
159     public void testMatch_FilterString_IsEmpty() throws Exception JavaDoc
160     {
161         ConstraintExp[] _constraintExp = new ConstraintExp[1];
162         EventType[] _eventType = new EventType[] { new EventType("*", "*") };
163
164         _constraintExp[0] = new ConstraintExp(_eventType, "");
165         filter_.add_constraints(_constraintExp);
166
167         // this should match
168
assertTrue(filter_.match(testPerson_));
169     }
170
171     public void testMatchModify() throws Exception JavaDoc
172     {
173         ConstraintExp[] _constraintExp = new ConstraintExp[1];
174         EventType[] _eventType = new EventType[1];
175         _eventType[0] = new EventType("*", "*");
176         _constraintExp[0] = new ConstraintExp(_eventType, "$.first_name == 'something'");
177         ConstraintInfo[] _info = filter_.add_constraints(_constraintExp);
178
179         // oops wrong
180
assertTrue(!filter_.match(testPerson_));
181
182         // modify the filter
183
_info[0].constraint_expression.constraint_expr = "$.first_name == 'firstname'";
184         filter_.modify_constraints(new int[0], _info);
185
186         // this one should match
187
assertTrue(filter_.match(testPerson_));
188     }
189
190     public void testConstraintGrammar() throws Exception JavaDoc
191     {
192         assertEquals("EXTENDED_TCL", filter_.constraint_grammar());
193     }
194
195     public void testAddConstraints() throws Exception JavaDoc
196     {
197         ConstraintExp[] _constraintExp = new ConstraintExp[1];
198
199         EventType[] _eventType = new EventType[1];
200         _eventType[0] = new EventType("domain", "name");
201         String JavaDoc _expression = "1 + 1";
202         _constraintExp[0] = new ConstraintExp(_eventType, _expression);
203
204         ConstraintInfo[] _info = filter_.add_constraints(_constraintExp);
205
206         assertTrue(_info.length == 1);
207         assertTrue(_info[0].constraint_expression.event_types.length == 1);
208         assertEquals(_expression, _info[0].constraint_expression.constraint_expr);
209         assertEquals(_eventType[0].domain_name,
210                 _info[0].constraint_expression.event_types[0].domain_name);
211         assertEquals(_eventType[0].type_name,
212                 _info[0].constraint_expression.event_types[0].type_name);
213     }
214
215     public void testDeleteConstraints() throws Exception JavaDoc
216     {
217         ConstraintExp[] _constraintExp = new ConstraintExp[2];
218
219         EventType[] _eventType = new EventType[1];
220         _eventType[0] = new EventType("domain", "name");
221         String JavaDoc _expression = "1 + 1";
222         String JavaDoc _expression2 = "2 + 2";
223         _constraintExp[0] = new ConstraintExp(_eventType, _expression);
224
225         _eventType[0] = new EventType("domain2", "name");
226         _constraintExp[1] = new ConstraintExp(_eventType, _expression2);
227
228         ConstraintInfo[] _info = filter_.add_constraints(_constraintExp);
229
230         assertTrue(_info.length == 2);
231         assertTrue(_info[0].constraint_expression.event_types.length == 1);
232
233         assertTrue(_info[1].constraint_expression.event_types.length == 1);
234
235         int[] _delete = { _info[0].constraint_id };
236
237         filter_.modify_constraints(_delete, new ConstraintInfo[0]);
238
239         ConstraintInfo[] _info2 = filter_.get_all_constraints();
240         assertTrue(_info2.length == 1);
241         assertEquals(_info[1].constraint_id, _info2[0].constraint_id);
242
243         assertEquals(_info[1].constraint_expression.constraint_expr,
244                 _info2[0].constraint_expression.constraint_expr);
245     }
246
247     /**
248      * multithreaded test. Some Writers modify the Constraints of a Filter. Some Readers constantly
249      * access the Filter. They should always get consistent data.
250      */

251     public void testModifyConcurrent() throws Exception JavaDoc
252     {
253         FilterRead _fr1 = new FilterRead(this, filter_, 100);
254         FilterRead _fr2 = new FilterRead(this, filter_, 100);
255         FilterRead _fr3 = new FilterRead(this, filter_, 100);
256         FilterRead _fr4 = new FilterRead(this, filter_, 100);
257
258         FilterModify _mod1 = new FilterModify(this, filter_, "true", 50);
259         FilterModify _mod2 = new FilterModify(this, filter_, "false", 50);
260
261         _fr1.start();
262         _fr2.start();
263         _fr3.start();
264         _fr4.start();
265
266         _mod1.start();
267         _mod2.start();
268
269         _fr1.join();
270         _fr2.join();
271         _fr3.join();
272         _fr4.join();
273
274         _mod1.join();
275         _mod2.join();
276     }
277
278     public void testMatchTyped() throws Exception JavaDoc
279     {
280         Property[] _props = new Property[] { new Property("operation", toAny("operationName")),
281                 new Property("value1", toAny(100)), new Property("value2", toAny(200)) };
282
283         ConstraintExp[] _constraintExp = new ConstraintExp[1];
284
285         EventType[] _eventType = new EventType[1];
286         _eventType[0] = new EventType("", "%TYPED");
287
288         String JavaDoc _expression = "$value1 > 50 and $value2 > 50";
289
290         _constraintExp[0] = new ConstraintExp(_eventType, _expression);
291
292         filter_.add_constraints(_constraintExp);
293
294         assertTrue(filter_.match_typed(_props));
295     }
296
297     public void testFilterTypedMessageEvent() throws Exception JavaDoc
298     {
299         TypedEventMessage _mesg = new TypedEventMessage();
300
301         String JavaDoc _domainName = "IDL:org.jacorb/org/jacorb/test/filter/Bla:1.0";
302         String JavaDoc _operationName = "blaOperation";
303
304         _mesg.setTypedEvent(_domainName, _operationName, new Property[] {
305                 new Property("param1", toAny("value1")), new Property("param2", toAny(100)) });
306
307         assertFalse(_mesg.match(filter_));
308
309         ConstraintExp[] _constraintExp = new ConstraintExp[1];
310
311         EventType[] _eventType = new EventType[1];
312         _eventType[0] = new EventType("", "%TYPED");
313
314         String JavaDoc _expression = "$event_type.domain_name == '" + _domainName
315                 + "' and $event_type.type_name == '" + _operationName + "' and $param2 > 50";
316
317         _constraintExp[0] = new ConstraintExp(_eventType, _expression);
318
319         filter_.add_constraints(_constraintExp);
320
321         assertTrue(_mesg.match(filter_));
322     }
323
324     public static Test suite() throws Exception JavaDoc
325     {
326         return NotificationTestCase.suite(FilterTest.class);
327     }
328 }
329
330 //////////////////////////////////////////////////
331

332 class FilterRead extends Thread JavaDoc
333 {
334     Filter filter_;
335
336     int iterations_;
337
338     boolean lengthOk_ = true;
339
340     boolean countOk_ = true;
341
342     TestCase testCase_;
343
344     static int sCounter = 0;
345
346     FilterRead()
347     {
348         super();
349         setDaemon(true);
350     }
351
352     FilterRead(TestCase testCase, Filter filter, int iterations)
353     {
354         super();
355         testCase_ = testCase;
356         filter_ = filter;
357         iterations_ = iterations;
358     }
359
360     public void run()
361     {
362         try
363         {
364             sleep(FilterTest.random_.nextInt(1000));
365         } catch (InterruptedException JavaDoc e)
366         {
367             // ignored
368
}
369
370         CounterMap _counter = new CounterMap();
371         for (int x = 0; x < iterations_; x++)
372         {
373             // constraint count should always be a multiple of 10
374
ConstraintInfo[] _info = filter_.get_all_constraints();
375             Assert.assertTrue(_info.length % 10 == 0);
376
377             for (int y = 0; y < _info.length; y++)
378             {
379                 _counter.incr(_info[y].constraint_expression.constraint_expr);
380             }
381
382             Iterator JavaDoc _i = _counter.allCounters();
383
384             // constraint type count should always be a multiple of 10
385
while (_i.hasNext())
386             {
387                 Counter _c = (Counter) _i.next();
388                 Assert.assertTrue(_c.value() % 10 == 0);
389             }
390
391             _counter.reset();
392
393             try
394             {
395                 Thread.sleep(FilterTest.random_.nextInt(110));
396             } catch (InterruptedException JavaDoc ie)
397             {
398                 // ignored
399
}
400         }
401     }
402 }
403
404 ////////////////////////////////////////
405

406 class CounterMap
407 {
408     Map JavaDoc counters_ = new Hashtable JavaDoc();
409
410     public void incr(Object JavaDoc t)
411     {
412         Counter _c = (Counter) counters_.get(t);
413         if (_c == null)
414         {
415             _c = new Counter();
416             counters_.put(t, _c);
417         }
418         _c.incr();
419     }
420
421     public int value(Object JavaDoc t)
422     {
423         Counter _c = (Counter) counters_.get(t);
424         if (_c == null)
425         {
426             return 0;
427         }
428         
429             return _c.value();
430         
431     }
432
433     public void reset()
434     {
435         counters_.clear();
436     }
437
438     Iterator JavaDoc allCounters()
439     {
440         return counters_.values().iterator();
441     }
442 }
443
444 ////////////////////////////////////////
445

446 class Counter
447 {
448     int counter_ = 0;
449
450     public void incr()
451     {
452         ++counter_;
453     }
454
455     public int value()
456     {
457         return counter_;
458     }
459 }
460
461 class FilterModify extends Thread JavaDoc
462 {
463     TestCase testCase_;
464
465     Filter filter_;
466
467     int iterations_ = 100;
468
469     ConstraintExp[] constraintExp_;
470
471     FilterModify(TestCase testCase, Filter filter, String JavaDoc expression, int iterations)
472     {
473         super();
474
475         setDaemon(true);
476
477         testCase_ = testCase;
478         filter_ = filter;
479         iterations_ = iterations;
480
481         constraintExp_ = new ConstraintExp[10];
482
483         EventType[] _eventType = new EventType[1];
484         _eventType[0] = new EventType("domain", expression);
485
486         for (int x = 0; x < constraintExp_.length; x++)
487         {
488             constraintExp_[x] = new ConstraintExp(_eventType, expression);
489         }
490     }
491
492     public void run()
493     {
494         try
495         {
496             sleep(FilterTest.random_.nextInt(1000));
497         } catch (InterruptedException JavaDoc e)
498         {
499             // ignored
500
}
501
502         ConstraintInfo[] _info = null;
503         for (int x = 0; x < iterations_; x++)
504         {
505             try
506             {
507                 if (_info != null)
508                 {
509                     int[] _toBeDeleted = new int[_info.length];
510                     for (int y = 0; y < _info.length; y++)
511                     {
512                         _toBeDeleted[y] = _info[y].constraint_id;
513                     }
514                     // delete the constraints this thread added earlier
515
filter_.modify_constraints(_toBeDeleted, new ConstraintInfo[0]);
516
517                     try
518                     {
519                         Thread.sleep(FilterTest.random_.nextInt(20));
520                     } catch (InterruptedException JavaDoc ie)
521                     {
522                         // ignore
523
}
524
525                 }
526                 // add some constraints
527
_info = filter_.add_constraints(constraintExp_);
528
529                 try
530                 {
531                     Thread.sleep(FilterTest.random_.nextInt(200));
532                 } catch (InterruptedException JavaDoc ie)
533                 {
534                     // ignore
535
}
536             } catch (Exception JavaDoc e)
537             {
538                 e.printStackTrace();
539                 Assert.fail();
540             }
541         }
542     }
543 }
Popular Tags