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