KickJava   Java API By Example, From Geeks To Geeks.

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


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

3 package test.jmock.examples.calculator;
4
5 import junit.framework.TestCase;
6 import org.jmock.examples.calculator.CalculatorException;
7 import org.jmock.examples.calculator.Expression;
8 import org.jmock.examples.calculator.SimpleEnvironment;
9 import org.jmock.util.Dummy;
10
11
12 public class SimpleEnvironmentTest extends TestCase
13 {
14     private final String JavaDoc variableName = "<VARIABLE NAME>";
15
16     private SimpleEnvironment environment;
17
18     public void setUp() {
19         environment = new SimpleEnvironment();
20     }
21
22     public void testThrowsExceptionIfVariableNotInEnvironment() {
23         try {
24             environment.getVariable(variableName);
25             fail("CalculatorException expected");
26         }
27         catch (CalculatorException ex) {
28             assertTrue("error message should contain name of variable",
29                        ex.getMessage().indexOf(variableName) >= 0);
30         }
31     }
32
33     public void testStoresVariableDefinitionByName() throws Exception JavaDoc {
34         String JavaDoc nameX = "<NAME X>";
35         Expression dummyExpressionX = dummyExpression("dummyExpressionX");
36         String JavaDoc nameY = "<NAME Y>";
37         Expression dummyExpressionY = dummyExpression("dummyExpressionY");
38
39         environment.setVariable(nameX, dummyExpressionX);
40         environment.setVariable(nameY, dummyExpressionY);
41
42         assertSame("should be variable definition X",
43                    dummyExpressionX, environment.getVariable(nameX));
44         assertSame("should be variable definition Y",
45                    dummyExpressionY, environment.getVariable(nameY));
46     }
47
48     private Expression dummyExpression( String JavaDoc name ) {
49         return (Expression)Dummy.newDummy(Expression.class, name);
50     }
51 }
52
Popular Tags