KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > conditional > TestParser


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.conditional;
16
17 import org.apache.hivemind.test.HiveMindTestCase;
18
19 /**
20  * Tests for {@link org.apache.hivemind.conditional.Parser}.
21  *
22  * @author Howard M. Lewis Ship
23  * @since 1.1
24  */

25 public class TestParser extends HiveMindTestCase
26 {
27     public void testSingleTerm()
28     {
29         Parser p = new Parser();
30         Node n = p.parse("class foo");
31
32         Evaluator ev = ((NodeImpl) n).getEvaluator();
33         ClassNameEvaluator cne = (ClassNameEvaluator) ev;
34
35         assertEquals("foo", cne.getClassName());
36     }
37
38     public void testExtraToken()
39     {
40         Parser p = new Parser();
41
42         try
43         {
44             p.parse("class foo bar");
45             unreachable();
46         }
47         catch (RuntimeException JavaDoc ex)
48         {
49             assertEquals("Unexpected token <SYMBOL(bar)> in expression 'class foo bar'.", ex
50                     .getMessage());
51         }
52     }
53
54     public void testTermAsExpression()
55     {
56         Parser p = new Parser();
57
58         NodeImpl n = (NodeImpl) p.parse("(property foo.bar)");
59         PropertyEvaluator ev = (PropertyEvaluator) n.getEvaluator();
60
61         assertEquals("foo.bar", ev.getPropertyName());
62     }
63
64     public void testNot()
65     {
66         Parser p = new Parser();
67
68         NodeImpl n = (NodeImpl) p.parse("not (property foo)");
69
70         assertTrue(n.getEvaluator() instanceof NotEvaluator);
71
72         NodeImpl n2 = (NodeImpl) n.getLeft();
73
74         PropertyEvaluator ev = (PropertyEvaluator) n2.getEvaluator();
75
76         assertEquals("foo", ev.getPropertyName());
77     }
78
79     public void testMissingToken()
80     {
81         Parser p = new Parser();
82
83         try
84         {
85             p.parse("not (property foo");
86             unreachable();
87         }
88         catch (RuntimeException JavaDoc ex)
89         {
90             assertEquals(
91                     "End of input reached unexpectedly, parsing expression 'not (property foo'.",
92                     ex.getMessage());
93         }
94     }
95
96     public void testWrongToken()
97     {
98         Parser p = new Parser();
99
100         try
101         {
102             p.parse("not property foo");
103             unreachable();
104         }
105         catch (RuntimeException JavaDoc ex)
106         {
107             assertEquals(
108                     "Expected OPAREN (not PROPERTY) parsing expression 'not property foo'.",
109                     ex.getMessage());
110         }
111     }
112
113     public void testWrongTokenInTerm()
114     {
115         Parser p = new Parser();
116
117         try
118         {
119             p.parse("and property foo");
120             unreachable();
121         }
122         catch (RuntimeException JavaDoc ex)
123         {
124             assertEquals("Unexpected token <AND> in expression 'and property foo'.", ex
125                     .getMessage());
126         }
127     }
128
129     public void testAnd()
130     {
131         Parser p = new Parser();
132
133         NodeImpl n = (NodeImpl) p.parse("property foo and class bar");
134
135         assertTrue(n.getEvaluator() instanceof AndEvaluator);
136
137         NodeImpl n2 = (NodeImpl) n.getLeft();
138         PropertyEvaluator ev1 = (PropertyEvaluator) n2.getEvaluator();
139
140         assertEquals("foo", ev1.getPropertyName());
141
142         NodeImpl n3 = (NodeImpl) n.getRight();
143         ClassNameEvaluator ev2 = (ClassNameEvaluator) n3.getEvaluator();
144
145         assertEquals("bar", ev2.getClassName());
146     }
147
148     public void testOr()
149     {
150         Parser p = new Parser();
151
152         NodeImpl n = (NodeImpl) p.parse("property foo or class bar");
153
154         assertTrue(n.getEvaluator() instanceof OrEvaluator);
155
156         NodeImpl n2 = (NodeImpl) n.getLeft();
157         PropertyEvaluator ev1 = (PropertyEvaluator) n2.getEvaluator();
158
159         assertEquals("foo", ev1.getPropertyName());
160
161         NodeImpl n3 = (NodeImpl) n.getRight();
162         ClassNameEvaluator ev2 = (ClassNameEvaluator) n3.getEvaluator();
163
164         assertEquals("bar", ev2.getClassName());
165     }
166 }
Popular Tags