KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > jmock > examples > calculator > InfixParserTest


1 /* Copyright (c) 2000-2004 jMock.org
2  */

3 package test.jmock.examples.calculator;
4
5
6 import org.jmock.Mock;
7 import org.jmock.MockObjectTestCase;
8 import org.jmock.examples.calculator.Expression;
9 import org.jmock.examples.calculator.ExpressionFactory;
10 import org.jmock.examples.calculator.InfixParser;
11 import org.jmock.examples.calculator.ParseException;
12 import org.jmock.util.Dummy;
13
14
15 public class InfixParserTest
16         extends MockObjectTestCase
17 {
18     private Mock mockExpressionFactory;
19     private InfixParser parser;
20     private Expression mockLiteral1 = dummyExpression("mockLiteral1");
21     private Expression mockLiteral2 = dummyExpression("mockLiteral2");
22     private Expression mockAddition = dummyExpression("mockAddition");
23     private Expression mockSubtraction = dummyExpression("mockSubtraction");
24     private Expression mockMultiplication = dummyExpression("mockMultiplication");
25     private Expression mockDivision = dummyExpression("mockDivision");
26     private Expression mockPower = dummyExpression("mockPower");
27     private Expression mockVariableReference = dummyExpression("mockVariableReference");
28
29
30     public void setUp() {
31         mockExpressionFactory = mock(ExpressionFactory.class);
32         parser = new InfixParser((ExpressionFactory)mockExpressionFactory.proxy());
33     }
34
35     public void testParsesLiteral() throws Exception JavaDoc {
36         mockExpressionFactory.expects(once()).method("newLiteral").with(eq(4.0))
37                 .will(returnValue(mockLiteral1));
38
39         assertSame("should be literal", mockLiteral1, parser.parse("4.0"));
40     }
41
42     public void testParsesVariableReference() throws Exception JavaDoc {
43         mockExpressionFactory.expects(once()).method("newVariableReference").with(eq("varName"))
44                 .will(returnValue(mockVariableReference));
45
46         assertSame("should be variable reference",
47                    mockVariableReference, parser.parse("varName"));
48     }
49
50     public void testParsesAddition() throws Exception JavaDoc {
51         mockExpressionFactory.expects(once()).method("newLiteral").with(eq(1.0))
52                 .will(returnValue(mockLiteral1));
53         mockExpressionFactory.expects(once()).method("newLiteral").with(eq(2.0))
54                 .will(returnValue(mockLiteral2));
55         mockExpressionFactory.expects(once()).method("newAddition")
56                 .with(same(mockLiteral1), same(mockLiteral2))
57                 .will(returnValue(mockAddition));
58
59         assertSame("should be addition", mockAddition, parser.parse("1+2"));
60     }
61
62     public void testThrowsExceptionForInvalidAdditionSyntax() throws Exception JavaDoc {
63         mockExpressionFactory.expects(once()).method("newLiteral").with(eq(1.0))
64                 .will(returnValue(mockLiteral1));
65
66         try {
67             parser.parse("1+");
68             fail("ParseException expected when missing rhs");
69         }
70         catch (ParseException expected) { /* expected */
71         }
72
73         try {
74             parser.parse("+2");
75             fail("ParseException expected when missing lhs");
76         }
77         catch (ParseException expected) { /* expected */
78         }
79     }
80
81     public void testParsesSubtraction() throws Exception JavaDoc {
82         mockExpressionFactory.expects(once()).method("newLiteral").with(eq(1.0))
83                 .will(returnValue(mockLiteral1));
84         mockExpressionFactory.expects(once()).method("newLiteral").with(eq(2.0))
85                 .will(returnValue(mockLiteral2));
86         mockExpressionFactory.expects(once()).method("newSubtraction").with(same(mockLiteral1), same(mockLiteral2))
87                 .will(returnValue(mockSubtraction));
88
89         assertSame("should be addition", mockSubtraction, parser.parse("1-2"));
90     }
91
92     public void testThrowsExceptionForInvalidSubtractionSyntax() throws Exception JavaDoc {
93         mockExpressionFactory.expects(once()).method("newLiteral").with(eq(1.0))
94                 .will(returnValue(mockLiteral1));
95
96         try {
97             parser.parse("1-");
98             fail("ParseException expected when missing rhs");
99         }
100         catch (ParseException expected) { /* expected */
101         }
102
103         try {
104             parser.parse("-2");
105             fail("ParseException expected when missing lhs");
106         }
107         catch (ParseException expected) { /* expected */
108         }
109     }
110
111     public void testParsesMultiplication() throws Exception JavaDoc {
112         mockExpressionFactory.expects(once()).method("newLiteral").with(eq(1.0))
113                 .will(returnValue(mockLiteral1));
114         mockExpressionFactory.expects(once()).method("newLiteral").with(eq(2.0))
115                 .will(returnValue(mockLiteral2));
116         mockExpressionFactory.expects(once()).method("newMultiplication")
117                 .with(same(mockLiteral1), same(mockLiteral2))
118                 .will(returnValue(mockMultiplication));
119
120         assertSame("should be multiplication", mockMultiplication, parser.parse("1*2"));
121     }
122
123     public void testParsesDivision() throws Exception JavaDoc {
124         mockExpressionFactory.expects(once()).method("newLiteral").with(eq(1.0))
125                 .will(returnValue(mockLiteral1));
126         mockExpressionFactory.expects(once()).method("newLiteral").with(eq(2.0))
127                 .will(returnValue(mockLiteral2));
128         mockExpressionFactory.expects(once()).method("newDivision").with(same(mockLiteral1), same(mockLiteral2))
129                 .will(returnValue(mockDivision));
130
131         assertSame("should be division", mockDivision, parser.parse("1/2"));
132     }
133
134     public void testParsesPower() throws Exception JavaDoc {
135         mockExpressionFactory.expects(once()).method("newLiteral").with(eq(1.0))
136                 .will(returnValue(mockLiteral1));
137         mockExpressionFactory.expects(once()).method("newLiteral").with(eq(2.0))
138                 .will(returnValue(mockLiteral2));
139         mockExpressionFactory.expects(once()).method("newPower").with(same(mockLiteral1), same(mockLiteral2))
140                 .will(returnValue(mockPower));
141
142         assertSame("should be power", mockPower, parser.parse("1^2"));
143     }
144
145     public void testParseParenthesis() throws Exception JavaDoc {
146         Expression xReference = dummyExpression("xReference");
147         Expression yReference = dummyExpression("yReference");
148         Expression zReference = dummyExpression("zReference");
149         Expression addition = dummyExpression("addition");
150         Expression multiplication = dummyExpression("multiplication");
151
152         mockExpressionFactory.expects(once()).method("newVariableReference").with(eq("x"))
153                 .will(returnValue(xReference));
154         mockExpressionFactory.expects(once()).method("newVariableReference").with(eq("y"))
155                 .will(returnValue(yReference));
156         mockExpressionFactory.expects(once()).method("newVariableReference").with(eq("z"))
157                 .will(returnValue(zReference));
158         mockExpressionFactory.expects(once()).method("newAddition").with(same(xReference), same(yReference))
159                 .will(returnValue(addition));
160         mockExpressionFactory.expects(once()).method("newMultiplication").with(same(addition), same(zReference))
161                 .will(returnValue(multiplication));
162
163         assertSame("should be multiplication",
164                    multiplication, parser.parse("(x+y)*z"));
165     }
166
167     public void testReportsUnexpectedEndOfInput() {
168         try {
169             parser.parse("");
170             fail("ParseException expected");
171         }
172         catch (ParseException ex) {
173             assertTrue("error message should report unexpected end of input",
174                        ex.getMessage().indexOf("unexpected end of input") >= 0);
175         }
176     }
177
178     public void testReportsUnexpectedToken() {
179         String JavaDoc wrongOperator = "*";
180
181         try {
182             parser.parse(wrongOperator);
183             fail("ParseException expected");
184         }
185         catch (ParseException ex) {
186             assertTrue("error message should include unexpected token",
187                        ex.getMessage().indexOf(wrongOperator) >= 0);
188         }
189     }
190
191     public void testReportsMissingClosingParenthesis() throws Exception JavaDoc {
192         Expression xReference = dummyExpression("xReference");
193
194         mockExpressionFactory.expects(once()).method("newVariableReference").with(eq("x"))
195                 .will(returnValue(xReference));
196
197         try {
198             parser.parse("(x");
199             fail("ParseException expected");
200         }
201         catch (ParseException expected) { /* expected */
202         }
203     }
204
205     private Expression dummyExpression( String JavaDoc name ) {
206         return (Expression)Dummy.newDummy(Expression.class, name);
207     }
208 }
209
Popular Tags