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       assertEquals("org.melati.template.NotFoundException: Could not find template user.wm",
153           e.subException.getMessage());
154       e = null;
155     }
156     File fileIn = new File(fileName);
157     assertTrue(fileIn.delete());
158     
159     fileName = "t2a.tmp";
160     args = new String[] { "appjunit", "org/melati/app/TemplateApp", "-o", fileName };
161     TemplateApp.main(args);
162     String output = "";
163     fileIn = new File(fileName);
164     BufferedReader in = new BufferedReader( new InputStreamReader(new FileInputStream(fileIn)));
165     while (in.ready()) {
166       output += in.readLine();
167     }
168     in.close();
169     fileIn.delete();      
170     assertEquals("Hello _guest_" + 
171             "You have expanded template org/melati/app/TemplateApp" + 
172             "Your melati contains:" + 
173             "Database : jdbc:hsqldb:mem:appjunit " + 
174             "Table    : null "  +
175             "Object   : null " + 
176             "Troid    : null " + 
177             "Method   : org/melati/app/TemplateApp " + 
178             "System Users" + 
179             "============" +
180             "  Melati guest user" + 
181             "  Melati database administrator", output);
182   }
183   
184   /**
185    * @see org.melati.app.TemplateApp#main(String[])
186    */
187   public void testMainThreeArgs() throws Exception {
188     String fileName = "t3.tmp";
189     String[] args = { "appjunit", "user", "0",
190          "-o", fileName };
191     TemplateApp it = new TemplateApp();
192     it.run(args);
193     String output = "";
194     File fileIn = new File(fileName);
195     BufferedReader in = new BufferedReader( new InputStreamReader(new FileInputStream(fileIn)));
196     while (in.ready()) {
197       output += in.readLine();
198     }
199     in.close();
200     fileIn.delete();      
201     assertEquals("Hello _guest_" + 
202             "You have expanded template org/melati/app/TemplateApp" + 
203             "Your melati contains:" + 
204             "Database : jdbc:hsqldb:mem:appjunit " + 
205             "Table    : user (from the data structure definition) "  +
206             "Object   : _guest_ " + 
207             "Troid    : 0 " + 
208             "Method   : null " + 
209             "System Users" + 
210             "============" +
211             "  Melati guest user" + 
212             "  Melati database administrator" , output);
213   }
214 
215   
216   /**
217    * Also covers .wm extension.
218    * @see org.melati.app.TemplateApp#main(String[])
219    */
220   public void testMainFourArgs() throws Exception {
221     String fileName = "t4.tmp";
222     String[] args = { "appjunit", "user", "0",
223         "org/melati/app/TemplateApp",  "-o", fileName };
224     TemplateApp it = new TemplateApp();
225     it.run(args);
226     String output = "";
227     File fileIn = new File(fileName);
228     BufferedReader in = new BufferedReader( new InputStreamReader(new FileInputStream(fileIn)));
229     while (in.ready()) {
230       output += in.readLine();
231     }
232     in.close();
233     fileIn.delete();      
234     assertEquals("Hello _guest_" + 
235             "You have expanded template org/melati/app/TemplateApp" + 
236             "Your melati contains:" + 
237             "Database : jdbc:hsqldb:mem:appjunit " + 
238             "Table    : user (from the data structure definition) "  +
239             "Object   : _guest_ " + 
240             "Troid    : 0 " + 
241             "Method   : org/melati/app/TemplateApp " + 
242             "System Users" + 
243             "============" +
244             "  Melati guest user" + 
245             "  Melati database administrator" , output);
246   }
247 
248   
249   /**
250    * @see org.melati.app.TemplateApp#main(String[])
251    */
252   public void testMainZeroArgs() throws Exception {
253     String fileName = "t0.tmp";
254     String[] args = { "-o", fileName };
255     TemplateApp it = new TemplateApp();
256     try { 
257       it.run(args);
258       fail("Should have bombed");
259     } catch (ConfigException e) {
260       e = null;
261     }
262     File fileIn = new File(fileName);
263     assertTrue(fileIn.delete());      
264   }
265 
266   /**
267    * @throws Exception
268    */
269   public void testLogin() throws Exception { 
270     String fileName = "t.tmp";
271     String[] args = { "appjunit", "user", "0",
272         "org/melati/app/TemplateApp",  "-u", "_administrator_","-p", "FIXME","-o", fileName};
273     TemplateApp it = new ConfiguredTemplateApp();
274     it.run(args);
275     String output = "";
276     File fileIn = new File(fileName);
277     BufferedReader in = new BufferedReader( new InputStreamReader(new FileInputStream(fileIn)));
278     while (in.ready()) {
279       output += in.readLine();
280     }
281     in.close();
282     fileIn.delete();      
283     assertEquals("Hello _administrator_" + 
284             "You have expanded template org/melati/app/TemplateApp" + 
285             "Your melati contains:" + 
286             "Database : jdbc:hsqldb:mem:appjunit " + 
287             "Table    : user (from the data structure definition) "  +
288             "Object   : _guest_ " + 
289             "Troid    : 0 " + 
290             "Method   : org/melati/app/TemplateApp " + 
291             "System Users" + 
292             "============" +
293             "  Melati guest user" + 
294             "  Melati database administrator" +
295             "Form settings=============  -u _administrator_  -p FIXME", output);
296     
297   }
298   /**
299    * Test no configured template engine.
300    */
301   public void testNoTemplateEngineConfigured() throws Exception { 
302     String fileName = "junitTest99.tmp";
303     String[] args = { "appjunit", "user", "0",
304         "org/melati/app/TemplateApp",  "-u", "_administrator_","-p", "FIXME","-o", fileName};
305     TemplateApp it = new MisConfiguredTemplateApp();
306     try { 
307       it.run(args);
308       fail("Should have blown up");
309     } catch (MelatiConfigurationException e) {
310       System.err.println(e.getMessage());
311       assertEquals("org.melati.util.MelatiConfigurationException: " +
312           "Have you configured a template engine? " +
313           "org.melati.MelatiConfig.templateEngine currently set to " + 
314           "org.melati.template.NoTemplateEngine",
315                    e.getMessage());
316       e = null;
317     }
318     File fileIn = new File(fileName);
319     assertTrue(fileIn.delete());      
320   }
321 }