KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jacorb.test.notification;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1999-2003 Gerald Brose
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */

23
24 import junit.framework.Test;
25
26 import org.jacorb.notification.filter.AbstractFilter;
27 import org.jacorb.notification.filter.MappingFilterImpl;
28 import org.jacorb.notification.filter.etcl.ETCLFilter;
29 import org.jacorb.notification.impl.DefaultEvaluationContextFactory;
30 import org.jacorb.notification.impl.DefaultMessageFactory;
31 import org.omg.CORBA.Any JavaDoc;
32 import org.omg.CORBA.AnyHolder JavaDoc;
33 import org.omg.CosNotification.EventType;
34 import org.omg.CosNotifyFilter.ConstraintExp;
35 import org.omg.CosNotifyFilter.MappingConstraintInfo;
36 import org.omg.CosNotifyFilter.MappingConstraintPair;
37 import org.omg.CosNotifyFilter.MappingFilterOperations;
38
39 /**
40  * @author Alphonse Bendt
41  */

42
43 public class MappingFilterTest extends NotificationTestCase
44 {
45     Any JavaDoc testPerson_;
46
47     AbstractFilter filter_;
48
49     // //////////////////////////////////////
50

51     public MappingFilterTest(String JavaDoc name, NotificationTestCaseSetup setup)
52     {
53         super(name, setup);
54     }
55
56     // //////////////////////////////////////
57

58     public void setUpTest() throws Exception JavaDoc
59     {
60         filter_ = new ETCLFilter(getConfiguration(), new DefaultEvaluationContextFactory(
61                 getEvaluator()), new DefaultMessageFactory(getConfiguration()), getORB(), getPOA());
62
63         testPerson_ = getTestUtils().getTestPersonAny();
64     }
65
66     public void testMatch() throws Exception JavaDoc
67     {
68         Any JavaDoc defaultValue = getORB().create_any();
69
70         MappingFilterOperations _mappingFilter = new MappingFilterImpl(getORB(),
71                 getConfiguration(), filter_, defaultValue);
72
73         AnyHolder JavaDoc anyHolder = new AnyHolder JavaDoc();
74
75         // filter is empty. should not match
76
assertTrue(!_mappingFilter.match(testPerson_, anyHolder));
77
78         // add some filter data
79
Any JavaDoc resultToSet = getORB().create_any();
80
81         resultToSet.insert_string("this indicates success");
82
83         EventType[] _eventType = new EventType[1];
84         _eventType[0] = new EventType("*", "*");
85         ConstraintExp constraintExp = new ConstraintExp(_eventType, "$.first_name == 'firstname'");
86
87         MappingConstraintPair[] mappingConstraintPair = new MappingConstraintPair[1];
88         mappingConstraintPair[0] = new MappingConstraintPair(constraintExp, resultToSet);
89
90         MappingConstraintInfo[] _info = _mappingFilter
91                 .add_mapping_constraints(mappingConstraintPair);
92
93         assertTrue(_info.length == 1);
94
95         assertEquals("$.first_name == 'firstname'", _info[0].constraint_expression.constraint_expr);
96         assertEquals(resultToSet, _info[0].value);
97
98         // this should match
99
assertTrue(_mappingFilter.match(testPerson_, anyHolder));
100         assertEquals(resultToSet, anyHolder.value);
101     }
102
103     public void testMatch2() throws Exception JavaDoc
104     {
105         Any JavaDoc defaultValue = getORB().create_any();
106
107         MappingFilterOperations _mappingFilter = new MappingFilterImpl(getORB(),
108                 getConfiguration(), filter_, defaultValue);
109
110         AnyHolder JavaDoc anyHolder = new AnyHolder JavaDoc();
111
112         // filter is empty. should not match
113
assertTrue(!_mappingFilter.match(testPerson_, anyHolder));
114
115         // add some filter data
116
Any JavaDoc resultToSet = getORB().create_any();
117
118         resultToSet.insert_string("this is 10");
119
120         EventType[] _eventType = new EventType[1];
121         _eventType[0] = new EventType("*", "*");
122         ConstraintExp constraintExp = new ConstraintExp(_eventType, "$ == 10");
123
124         MappingConstraintPair[] mappingConstraintPair = new MappingConstraintPair[2];
125         mappingConstraintPair[0] = new MappingConstraintPair(constraintExp, resultToSet);
126
127         constraintExp = new ConstraintExp(_eventType, "$ == 20");
128         resultToSet = getORB().create_any();
129         resultToSet.insert_string("this is 20");
130         mappingConstraintPair[1] = new MappingConstraintPair(constraintExp, resultToSet);
131
132         MappingConstraintInfo[] _info = _mappingFilter
133                 .add_mapping_constraints(mappingConstraintPair);
134
135         assertTrue(_info.length == 2);
136
137         Any JavaDoc testMessage = getORB().create_any();
138         testMessage.insert_long(10);
139
140         // this should match
141
assertTrue(_mappingFilter.match(testMessage, anyHolder));
142         assertEquals("this is 10", anyHolder.value.extract_string());
143
144         testMessage = getORB().create_any();
145         testMessage.insert_long(20);
146
147         assertTrue(_mappingFilter.match(testMessage, anyHolder));
148         assertEquals("this is 20", anyHolder.value.extract_string());
149     }
150
151     public static Test suite() throws Exception JavaDoc
152     {
153         return NotificationTestCase.suite(MappingFilterTest.class);
154     }
155 }
156
Popular Tags