KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > util > TestBodyBuilder


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.util;
16
17 import org.apache.hivemind.service.BodyBuilder;
18
19 import hivemind.test.FrameworkTestCase;
20
21 /**
22  * Tests the utility class {@link org.apache.hivemind.service.BodyBuilder},
23  * used with dynamic code generation.
24  *
25  * @author Howard Lewis Ship
26  */

27 public class TestBodyBuilder extends FrameworkTestCase
28 {
29     private BodyBuilder _b = new BodyBuilder();
30
31     public void testBasic()
32     {
33         _b.begin();
34         _b.addln("invoke();");
35         _b.end();
36
37         assertEquals("{\n invoke();\n}\n", _b.toString());
38     }
39
40     public void testQuoted()
41     {
42         _b.add("invoke(");
43         _b.addQuoted("fred");
44         _b.add(");");
45
46         assertEquals("invoke(\"fred\");", _b.toString());
47     }
48
49     public void testNested()
50     {
51         _b.begin();
52
53         _b.add("while(true)");
54         _b.begin();
55         _b.add("_i += 1;");
56         _b.end();
57
58         _b.end();
59
60         assertEquals("{\n while(true)\n {\n _i += 1;\n }\n}\n", _b.toString());
61     }
62
63     public void testAddln()
64     {
65         _b.begin();
66         _b.addln("invoke(fred);");
67         _b.addln("invoke(barney);");
68         _b.end();
69
70         assertEquals("{\n invoke(fred);\n invoke(barney);\n}\n", _b.toString());
71     }
72
73     public void testClear()
74     {
75         _b.add("fred");
76
77         assertEquals("fred", _b.toString());
78
79         _b.clear();
80
81         _b.add("barney");
82
83         assertEquals("barney", _b.toString());
84     }
85
86     public void testAddPattern1()
87     {
88         _b.add("today is {0}.", "tuesday");
89
90         assertEquals("today is tuesday.", _b.toString());
91     }
92
93     public void testAddlnPattern1()
94     {
95         _b.addln("The capital of France is {0}.", "Paris");
96
97         assertEquals("The capital of France is Paris.\n", _b.toString());
98     }
99
100     public void testAddPattern2()
101     {
102         _b.add("Current suspects are: {0} and {1}.", "Tony", "Junior");
103
104         assertEquals("Current suspects are: Tony and Junior.", _b.toString());
105     }
106
107     public void testAddlnPattern2()
108     {
109         _b.addln("The capital of {0} is {1}.", "Germany", "Berlin");
110
111         assertEquals("The capital of Germany is Berlin.\n", _b.toString());
112     }
113
114     public void testAddPattern3()
115     {
116         _b.add("{0} + {1} = {2}", new Integer JavaDoc(5), new Integer JavaDoc(7), new Integer JavaDoc(13));
117
118         assertEquals("5 + 7 = 13", _b.toString());
119     }
120
121     public void testAddlnPattern3()
122     {
123         _b.addln("The Holy Trinity: {0}, {1} and {2}.", "Tapestry", "HiveMind", "Hibernate");
124
125         assertEquals("The Holy Trinity: Tapestry, HiveMind and Hibernate.\n", _b.toString());
126     }
127
128 }
129
Popular Tags