1 package org.melati.app.test;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.InputStreamReader;
7 import java.util.Hashtable;
8
9 import org.melati.Melati;
10 import org.melati.app.InvalidArgumentsException;
11 import org.melati.app.TemplateApp;
12 import org.melati.app.UnhandledExceptionException;
13 import org.melati.util.ConfigException;
14 import org.melati.util.MelatiConfigurationException;
15
16 import junit.framework.TestCase;
17
18
19
20
21
22
23 public class TemplateAppTest extends TestCase {
24
25
26
27
28
29 public TemplateAppTest(String name) {
30 super(name);
31 }
32
33
34
35
36 protected void setUp() throws Exception {
37 }
38
39
40
41
42 protected void tearDown() throws Exception {
43
44 }
45
46
47
48
49 @SuppressWarnings("unchecked")
50 public void testInit() throws Exception {
51 TemplateApp ta = new TemplateApp();
52 String[] args = { "appjunit", "user", "0", "method", "field", "value" };
53 Melati m = ta.init(args);
54
55 assertEquals("appjunit", m.getDatabase().getName());
56 Hashtable<String,String> f = (Hashtable<String,String>)m.getTemplateContext().get("Form");
57 assertEquals("value", f.get("field"));
58 }
59
60
61
62
63 public void testInitWithUnmatcheArgs0() throws Exception {
64 TemplateApp ta = new TemplateApp();
65 String[] args = { "appjunit", "user", "0", "method", "field", "value",
66 "unmatched" };
67 try {
68 ta.init(args);
69 fail("Should have bombed");
70 } catch (InvalidArgumentsException e) {
71 e = null;
72 }
73 }
74
75
76
77
78 public void testMain() throws Exception {
79 String fileName = "t.tmp";
80 String[] args = { "appjunit", "user", "0",
81 "org/melati/app/TemplateApp", "field", "value", "-o", fileName };
82 TemplateApp.main(args);
83 String output = "";
84 File fileIn = new File(fileName);
85 BufferedReader in = new BufferedReader( new InputStreamReader(new FileInputStream(fileIn)));
86 while (in.ready()) {
87 output += in.readLine();
88 }
89 in.close();
90 fileIn.delete();
91 assertEquals("Hello _guest_" +
92 "You have expanded template org/melati/app/TemplateApp" +
93 "Your melati contains:" +
94 "Database : jdbc:hsqldb:mem:appjunit" +
95 "Table : user (from the data structure definition)" +
96 "Object : _guest_" +
97 "Troid : 0" +
98 "Method : org/melati/app/TemplateApp" +
99 "System Users" +
100 "============" +
101 " Melati guest user" +
102 " Melati database administrator" +
103 "Form settings" +
104 "=============" +
105 " field value", output);
106 }
107
108
109
110
111 public void testMainOneArg() throws Exception {
112 String fileName = "tttt.tmp";
113 String[] args = { "appjunit", "-o", fileName };
114 TemplateApp it = new TemplateApp();
115 it.run(args);
116 String output = "";
117 File fileIn = new File(fileName);
118 BufferedReader in = new BufferedReader(
119 new InputStreamReader(
120 new FileInputStream(fileIn)));
121 while (in.ready()) {
122 output += in.readLine();
123 }
124 in.close();
125 fileIn.delete();
126 System.err.println(output);
127 assertEquals("Hello _guest_" +
128 "You have expanded template org/melati/app/TemplateApp" +
129 "Your melati contains:" +
130 "Database : jdbc:hsqldb:mem:appjunit" +
131 "Table : null" +
132 "Object : null" +
133 "Troid : null" +
134 "Method : null" +
135 "System Users" +
136 "============" +
137 " Melati guest user" +
138 " Melati database administrator",output);
139 }
140
141
142
143
144 public void testMainTwoArgs() throws Exception {
145 String fileName = "t25555.tmp";
146 String[] args = { "appjunit", "user", "-o", fileName };
147 TemplateApp it = new TemplateApp();
148 try {
149 it.run(args);
150 fail("Should have blown up");
151 } catch (UnhandledExceptionException e) {
152 e.printStackTrace();
153
154 assertTrue(e.subException.getMessage().
155 startsWith("org.melati.template.NotFoundException: Could not find template user"));
156 e = null;
157 }
158 File fileIn = new File(fileName);
159 assertTrue(fileIn.delete());
160
161 fileName = "t2a.tmp";
162 args = new String[] { "appjunit", "org/melati/app/TemplateApp", "-o", fileName };
163 TemplateApp.main(args);
164 String output = "";
165 fileIn = new File(fileName);
166 BufferedReader in = new BufferedReader( new InputStreamReader(new FileInputStream(fileIn)));
167 while (in.ready()) {
168 output += in.readLine();
169 }
170 in.close();
171 fileIn.delete();
172 assertEquals("Hello _guest_" +
173 "You have expanded template org/melati/app/TemplateApp" +
174 "Your melati contains:" +
175 "Database : jdbc:hsqldb:mem:appjunit" +
176 "Table : null" +
177 "Object : null" +
178 "Troid : null" +
179 "Method : org/melati/app/TemplateApp" +
180 "System Users" +
181 "============" +
182 " Melati guest user" +
183 " Melati database administrator", output);
184 }
185
186
187
188
189 public void testMainThreeArgs() throws Exception {
190 String fileName = "t3.tmp";
191 String[] args = { "appjunit", "user", "0",
192 "-o", fileName };
193 TemplateApp it = new TemplateApp();
194 it.run(args);
195 String output = "";
196 File fileIn = new File(fileName);
197 BufferedReader in = new BufferedReader( new InputStreamReader(new FileInputStream(fileIn)));
198 while (in.ready()) {
199 output += in.readLine();
200 }
201 in.close();
202 fileIn.delete();
203 assertEquals("Hello _guest_" +
204 "You have expanded template org/melati/app/TemplateApp" +
205 "Your melati contains:" +
206 "Database : jdbc:hsqldb:mem:appjunit" +
207 "Table : user (from the data structure definition)" +
208 "Object : _guest_" +
209 "Troid : 0" +
210 "Method : null" +
211 "System Users" +
212 "============" +
213 " Melati guest user" +
214 " Melati database administrator" , output);
215 }
216
217
218
219
220
221
222 public void testMainFourArgs() throws Exception {
223 String fileName = "t4.tmp";
224 String[] args = { "appjunit", "user", "0",
225 "org/melati/app/TemplateApp", "-o", fileName };
226 TemplateApp it = new TemplateApp();
227 it.run(args);
228 String output = "";
229 File fileIn = new File(fileName);
230 BufferedReader in = new BufferedReader( new InputStreamReader(new FileInputStream(fileIn)));
231 while (in.ready()) {
232 output += in.readLine();
233 }
234 in.close();
235 fileIn.delete();
236 assertEquals("Hello _guest_" +
237 "You have expanded template org/melati/app/TemplateApp" +
238 "Your melati contains:" +
239 "Database : jdbc:hsqldb:mem:appjunit" +
240 "Table : user (from the data structure definition)" +
241 "Object : _guest_" +
242 "Troid : 0" +
243 "Method : org/melati/app/TemplateApp" +
244 "System Users" +
245 "============" +
246 " Melati guest user" +
247 " Melati database administrator" , output);
248 }
249
250
251
252
253
254 public void testMainZeroArgs() throws Exception {
255 String fileName = "t0.tmp";
256 String[] args = { "-o", fileName };
257 TemplateApp it = new TemplateApp();
258 try {
259 it.run(args);
260 fail("Should have bombed");
261 } catch (ConfigException e) {
262 e = null;
263 }
264 File fileIn = new File(fileName);
265 assertTrue(fileIn.delete());
266 }
267
268
269
270
271 public void testLogin() throws Exception {
272 String fileName = "t.tmp";
273 String[] args = { "appjunit", "user", "0",
274 "org/melati/app/TemplateApp", "-u", "_administrator_","-p", "FIXME","-o", fileName};
275 TemplateApp it = new ConfiguredTemplateApp();
276 it.run(args);
277 String output = "";
278 File fileIn = new File(fileName);
279 BufferedReader in = new BufferedReader( new InputStreamReader(new FileInputStream(fileIn)));
280 while (in.ready()) {
281 output += in.readLine();
282 }
283 in.close();
284 fileIn.delete();
285 assertEquals("Hello _administrator_" +
286 "You have expanded template org/melati/app/TemplateApp" +
287 "Your melati contains:" +
288 "Database : jdbc:hsqldb:mem:appjunit" +
289 "Table : user (from the data structure definition)" +
290 "Object : _guest_" +
291 "Troid : 0" +
292 "Method : org/melati/app/TemplateApp" +
293 "System Users" +
294 "============" +
295 " Melati guest user" +
296 " Melati database administrator" +
297 "Form settings============= -u _administrator_ -p FIXME", output);
298
299 }
300
301
302
303 public void testNoTemplateEngineConfigured() throws Exception {
304 String fileName = "junitTest99.tmp";
305 String[] args = { "appjunit", "user", "0",
306 "org/melati/app/TemplateApp", "-u", "_administrator_","-p", "FIXME","-o", fileName};
307 TemplateApp it = new MisConfiguredTemplateApp();
308 try {
309 it.run(args);
310 fail("Should have blown up");
311 } catch (MelatiConfigurationException e) {
312 System.err.println(e.getMessage());
313 assertEquals("org.melati.util.MelatiConfigurationException: " +
314 "Have you configured a template engine? " +
315 "org.melati.MelatiConfig.templateEngine currently set to " +
316 "org.melati.template.NoTemplateEngine",
317 e.getMessage());
318 e = null;
319 }
320 File fileIn = new File(fileName);
321 assertTrue(fileIn.delete());
322 }
323 }