1   package org.melati.test;
2   
3   import java.io.IOException;
4   import java.util.Vector;
5   
6   import javax.servlet.http.HttpServletRequest;
7   import javax.servlet.http.HttpServletResponse;
8   
9   import org.melati.Melati;
10  import org.melati.MelatiConfig;
11  import org.melati.PoemContext;
12  import org.melati.poem.Field;
13  import org.melati.template.webmacro.WebmacroTemplateEngine;
14  import org.melati.util.CharsetException;
15  import org.melati.util.MelatiBugMelatiException;
16  import org.melati.util.MelatiStringWriter;
17  import org.melati.util.MelatiWriter;
18  import org.melati.servlet.test.MockServletRequest;
19  import org.melati.servlet.test.MockServletResponse;
20  
21  import junit.framework.TestCase;
22  
23  /**
24   * Test Melati.
25   * 
26   * @author timp
27   * @since 30/05/2006
28   */
29  public class MelatiTest extends TestCase {
30  
31    /**
32     * Constructor for MelatiTest.
33     * @param name
34     */
35    public MelatiTest(String name) {
36      super(name);
37    }
38  
39    /**
40     * @see TestCase#setUp()
41     */
42    protected void setUp()
43        throws Exception {
44      super.setUp();
45    }
46  
47    /**
48     * @see TestCase#tearDown()
49     */
50    protected void tearDown()
51        throws Exception {
52      super.tearDown();
53    }
54  
55    protected PoemContext poemContext()  { 
56      PoemContext it = new PoemContext();
57     return it;
58   }
59    
60    
61    /**
62     * @see org.melati.Melati#Melati(MelatiConfig, HttpServletRequest, HttpServletResponse)
63     */
64    public void testMelatiMelatiConfigHttpServletRequestHttpServletResponse() {
65  
66    }
67  
68    /**
69     * @see org.melati.Melati#Melati(MelatiConfig, MelatiWriter)
70     */
71    public void testMelatiMelatiConfigMelatiWriter() {
72  
73    }
74  
75    /**
76     * @see org.melati.Melati#getRequest()
77     */
78    public void testGetRequest() {
79  
80    }
81  
82    /**
83     * @see org.melati.Melati#setRequest(HttpServletRequest)
84     */
85    public void testSetRequest() {
86  
87    }
88  
89    /**
90     * @see org.melati.Melati#getResponse()
91     */
92    public void testGetResponse() {
93  
94    }
95  
96    /**
97     * @see org.melati.Melati#setPoemContext(PoemContext)
98     */
99    public void testSetPoemContext() {
100 
101   }
102 
103   /**
104    * @see org.melati.Melati#loadTableAndObject()
105    */
106   public void testLoadTableAndObject() {
107 
108   }
109 
110   /**
111    * @see org.melati.Melati#getPoemContext()
112    */
113   public void testGetPoemContext() {
114 
115   }
116 
117   /**
118    * @see org.melati.Melati#getDatabase()
119    */
120   public void testGetDatabase() {
121 
122   }
123 
124   /**
125    * @see org.melati.Melati#getKnownDatabaseNames()
126    */
127   public void testGetKnownDatabaseNames() throws Exception {
128     MelatiConfig mc = null;
129     PoemContext pc = null;
130     Melati m = null;
131     mc = new MelatiConfig();
132     m = new Melati(mc, new MelatiStringWriter());
133     pc = poemContext();
134     m.setPoemContext(pc);
135     Vector known = m.getKnownDatabaseNames();
136     // Fails when run in single thread 
137     //assertEquals(0, known.size());
138     pc.setLogicalDatabase("melatijunit");
139     m.setPoemContext(pc);
140     m.loadTableAndObject();
141     known = m.getKnownDatabaseNames();
142     // Fails when run in single thread 
143     //assertEquals(1, known.size());
144     assertTrue(known.size() > 0);
145   }
146 
147   /**
148    * @see org.melati.Melati#getTable()
149    */
150   public void testGetTable() {
151 
152   }
153 
154   /**
155    * @see org.melati.Melati#getObject()
156    */
157   public void testGetObject() {
158 
159   }
160 
161   /**
162    * @see org.melati.Melati#getMethod()
163    */
164   public void testGetMethod() {
165 
166   }
167 
168   /**
169    * @see org.melati.Melati#setTemplateEngine(TemplateEngine)
170    */
171   public void testSetTemplateEngine() {
172 
173   }
174 
175   /**
176    * @see org.melati.Melati#getTemplateEngine()
177    */
178   public void testGetTemplateEngine() {
179 
180   }
181 
182   /**
183    * @see org.melati.Melati#setTemplateContext(TemplateContext)
184    */
185   public void testSetTemplateContext() {
186 
187   }
188 
189   /**
190    * @see org.melati.Melati#getTemplateContext()
191    */
192   public void testGetTemplateContext() {
193 
194   }
195 
196   /**
197    * @see org.melati.Melati#getServletTemplateContext()
198    */
199   public void testGetServletTemplateContext() {
200 
201   }
202 
203   /**
204    * @see org.melati.Melati#getConfig()
205    */
206   public void testGetConfig() {
207 
208   }
209 
210   /**
211    * @see org.melati.Melati#getPathInfoParts()
212    */
213   public void testGetPathInfoParts() {
214 
215   }
216 
217   /**
218    * @see org.melati.Melati#setArguments(String[])
219    */
220   public void testSetArguments() {
221 
222   }
223 
224   /**
225    * @see org.melati.Melati#getArguments()
226    */
227   public void testGetArguments() throws Exception {
228     MelatiConfig mc = null;
229     Melati m = null;
230     mc = new MelatiConfig();
231     m = new Melati(mc, new MelatiStringWriter());
232     m.setPoemContext(poemContext());
233     assertNull(m.getArguments());
234     m.setArguments(new String[] {"hello", "world"});
235     assertEquals(2, m.getArguments().length);
236 
237   }
238 
239   /**
240    * @see org.melati.Melati#getSession()
241    */
242   public void testGetSession() {
243 
244   }
245 
246   /**
247    * @see org.melati.Melati#getContextUtil(String)
248    */
249   public void testGetContextUtil() throws Exception {
250     MelatiConfig mc = null;
251     Melati m = null;
252     mc = new MelatiConfig();
253     m = new Melati(mc, new MelatiStringWriter());
254     m.setPoemContext(poemContext());
255     Object adminUtil = m.getContextUtil("org.melati.admin.AdminUtils");
256     assertTrue(adminUtil instanceof org.melati.admin.AdminUtils);
257     try { 
258       m.getContextUtil("unknownClass");
259       fail("Should have bombed");
260     } catch (MelatiBugMelatiException e) { 
261       e = null;
262     }
263   }
264 
265   /**
266    * @see org.melati.Melati#getLogoutURL()
267    * @throws Exception 
268    */
269   public void testGetLogoutURL() throws Exception {
270     MelatiConfig mc = new MelatiConfig();
271     Melati m = new Melati(mc, new MelatiStringWriter());
272     m.setPoemContext(poemContext());
273     MockServletRequest req = new MockServletRequest();
274     req.setRequestURI("");
275     m.setRequest(req);
276     assertEquals("/servletContext/mockServletPath/org.melati.login.Logout/null",m.getLogoutURL());
277   }
278 
279   /**
280    * @see org.melati.Melati#getLoginURL()
281    * @throws Exception 
282    */
283   public void testGetLoginURL() throws Exception {
284     MelatiConfig mc = new MelatiConfig();
285     Melati m = new Melati(mc, new MelatiStringWriter());
286     m.setPoemContext(poemContext());
287     MockServletRequest req = new MockServletRequest();
288     req.setRequestURI("");
289     m.setRequest(req);
290     assertEquals("/servletContext/mockServletPath/org.melati.login.Login/null",m.getLoginURL());
291 
292   }
293 
294   /**
295    * @see org.melati.Melati#getZoneURL()
296    */
297   public void testGetZoneURL() {
298 
299   }
300 
301   /**
302    * Not used in Melati.
303    * @throws Exception 
304    * @see org.melati.Melati#getServletURL()
305    */
306   public void testGetServletURL() throws Exception {
307     MelatiConfig mc = new MelatiConfig();
308     Melati m = new Melati(mc, new MelatiStringWriter());
309     m.setPoemContext(poemContext());
310     MockServletRequest req = new MockServletRequest();
311     req.setRequestURI("/");
312     m.setRequest(req);
313     assertEquals("http://localhost/servletContext/mockServletPath/",m.getServletURL());
314   }
315 
316   /**
317    * @see org.melati.Melati#getJavascriptLibraryURL()
318    */
319   public void testGetJavascriptLibraryURL() {
320 
321   }
322 
323   /**
324    * @throws Exception 
325    * @see org.melati.Melati#getPoemLocale()
326    */
327   public void testGetPoemLocale() throws Exception {
328     MelatiConfig mc = null;
329     Melati m = null;
330     mc = new MelatiConfig();
331     m = new Melati(mc, new MelatiStringWriter());
332     m.setPoemContext(poemContext());
333     assertEquals("en_GB", m.getPoemLocale().toString());
334     m.setRequest(new MockServletRequest());
335     assertEquals("en_GB", m.getPoemLocale().toString());
336     MockServletRequest msr = new MockServletRequest();
337     msr.setHeader("Accept-Language","en-gb");
338     m.setRequest(msr);
339     assertEquals("en_GB", m.getPoemLocale().toString());
340     msr.setHeader("Accept-Language","not");
341     assertEquals("en_GB", m.getPoemLocale().toString());
342     msr.setHeader("Accept-Language","en-us");
343     assertEquals("en_US", m.getPoemLocale().toString());
344     
345 
346   }
347 
348   /**
349    * @see org.melati.Melati#getPoemLocale(String)
350    */
351   public void testGetPoemLocaleString() {
352     assertNull(Melati.getPoemLocale(""));    
353     assertNull(Melati.getPoemLocale(";"));    
354     assertNull(Melati.getPoemLocale(";9"));    
355     assertNull(Melati.getPoemLocale(";nine"));    
356     assertNull(Melati.getPoemLocale("rubbish")); 
357     assertNull(Melati.getPoemLocale("rubbish;9")); 
358     assertNull(Melati.getPoemLocale("rubbish;nine")); 
359     assertEquals("en_GB", Melati.getPoemLocale("EN-GB").toString());    
360     assertEquals("en_GB", Melati.getPoemLocale("en-GB").toString());    
361     assertEquals("en_GB", Melati.getPoemLocale("en-gb").toString());    
362     assertEquals("en_GB", Melati.getPoemLocale("en-gb;").toString());    
363     assertEquals("en_GB", Melati.getPoemLocale("en-gb;9").toString());    
364     assertEquals("en_GB", Melati.getPoemLocale("en-gb;nine").toString());    
365     
366   }
367   /**
368    * @see org.melati.Melati#establishCharsets()
369    */
370   public void testEstablishCharsets() throws Exception {
371     MelatiConfig mc = new MelatiConfig();
372     Melati m = new Melati(mc, new MelatiStringWriter());
373     m.setPoemContext(poemContext());
374     MockServletRequest mock = new MockServletRequest();
375     mock.setHeader("Accept-Charset", "");
376     m.setRequest(mock);
377     m.establishCharsets();
378 
379     mock.setHeader("Accept-Charset", "rubbish");
380     m.setRequest(mock);
381     try { 
382       m.establishCharsets();
383       fail("Should have blown up");
384     } catch (CharsetException e) { 
385       e = null;
386     }
387 
388     mock.setHeader("Accept-Charset", "");
389     mock.setCharacterEncoding(null);
390     m.setRequest(mock);
391     m.establishCharsets();
392     
393   }
394 
395   /**
396    * @see org.melati.Melati#setResponseContentType(String)
397    */
398   public void testSetResponseContentType() {
399 
400   }
401 
402   /**
403    * @see org.melati.Melati#setMarkupLanguage(MarkupLanguage)
404    */
405   public void testSetMarkupLanguage() {
406 
407   }
408 
409   /**
410    * @see org.melati.Melati#getMarkupLanguage()
411    */
412   public void testGetMarkupLanguage() throws Exception {
413     MelatiConfig mc = null;
414     Melati m = null;
415     mc = new MelatiConfig();
416     m = new Melati(mc, new MelatiStringWriter());
417     m.setPoemContext(poemContext());
418     assertEquals("html/en_GB", m.getMarkupLanguage().toString());
419     assertEquals("html_attr/en_GB", m.getMarkupLanguage().getAttr().toString());
420   }
421 
422   /**
423    * @see org.melati.Melati#getHTMLMarkupLanguage()
424    */
425   public void testGetHTMLMarkupLanguage() throws Exception {
426     MelatiConfig mc = null;
427     Melati m = null;
428     mc = new MelatiConfig();
429     m = new Melati(mc, new MelatiStringWriter());
430     m.setPoemContext(poemContext());
431     assertEquals("html/en_GB", m.getHTMLMarkupLanguage().toString());
432     assertEquals("html_attr/en_GB", m.getHTMLMarkupLanguage().getAttr().toString());
433 
434   }
435 
436   /**
437    * @see org.melati.Melati#sameURLWith(String, String)
438    */
439   public void testSameURLWithStringString() throws Exception {
440     MelatiConfig mc = new MelatiConfig();
441     Melati m = new Melati(mc, new MelatiStringWriter());
442     m.setPoemContext(poemContext());
443     MockServletRequest mock = new MockServletRequest();
444     mock.setRequestURI("page");
445     m.setRequest(mock);
446 
447     assertEquals("page?noodles=1", m.sameURLWith("noodles","1"));
448     assertEquals("page?noodles=1", m.sameURLWith("noodles"));
449   }
450 
451   /**
452    * @see org.melati.Melati#sameURLWith(String)
453    */
454   public void testSameURLWithString() {
455 
456   }
457 
458   /**
459    * @see org.melati.Melati#getSameURL()
460    */
461   public void testGetSameURL() {
462 
463   }
464 
465   /**
466    * @see org.melati.Melati#setBufferingOff()
467    */
468   public void testSetBufferingOff() throws Exception {
469     MelatiConfig mc = new MelatiConfig();
470     HttpServletRequest mockRequest = new MockServletRequest();
471     HttpServletResponse mockResponse = new MockServletResponse();
472     Melati m = new Melati(mc, mockRequest, mockResponse);
473     m.setPoemContext(poemContext());
474     m.setBufferingOff();
475     m.setFlushingOn();
476     MelatiWriter mw = m.getWriter();
477     m.setWriter(new MelatiStringWriter());
478     mw = m.getWriter();
479     try {
480       m.setBufferingOff();
481       fail("Should have blown up");
482     } catch (IOException e) {
483       e = null;
484     }
485     try {
486       m.setFlushingOn();
487       fail("Should have blown up");
488     } catch (IOException e) {
489       e = null;
490     }
491     mw.flush();
492     mw.close();
493   }
494 
495   /**
496    * @see org.melati.Melati#setFlushingOn()
497    */
498   public void testSetFlushingOn() {
499 
500   }
501 
502   /**
503    * @see org.melati.Melati#getURLQueryEncoding()
504    */
505   public void testGetURLQueryEncoding() throws Exception {
506     MelatiConfig mc = new MelatiConfig();
507     Melati m = new Melati(mc, new MelatiStringWriter());
508     m.setPoemContext(poemContext());
509     MockServletRequest mock = new MockServletRequest();
510     m.setRequest(mock);
511     assertEquals("ISO-8859-1", m.getURLQueryEncoding());
512   }
513 
514   /**
515    * @see org.melati.Melati#urlEncode(String)
516    */
517   public void testUrlEncode() throws Exception {
518     MelatiConfig mc = new MelatiConfig();
519     Melati m = new Melati(mc, new MelatiStringWriter());
520     m.setPoemContext(poemContext());
521     MockServletRequest mock = new MockServletRequest();
522     m.setRequest(mock);
523     assertEquals("", m.urlEncode(""));
524     assertEquals("A+space+seperated+string", m.urlEncode("A space seperated string"));
525     mock.setCharacterEncoding("Unsupported Encoding");
526     m.setRequest(mock);
527     assertEquals("A space seperated string", m.urlEncode("A space seperated string"));
528   }
529 
530   /**
531    * @see org.melati.Melati#getEncoding()
532    */
533   public void testGetEncoding() {
534 
535   }
536 
537   /**
538    * @see org.melati.Melati#getWriter()
539    */
540   public void testGetWriter() {
541 
542   }
543 
544   /**
545    * @see org.melati.Melati#getStringWriter()
546    */
547   public void testGetStringWriter() throws Exception {
548     MelatiConfig mc = new MelatiConfig();
549     Melati m = new Melati(mc, new MelatiStringWriter());
550     m.setPoemContext(poemContext());
551     MockServletRequest mock = new MockServletRequest();
552     m.setRequest(mock);
553     assertNull(m.getTemplateEngine());
554     assertTrue(m.getStringWriter() instanceof MelatiStringWriter);
555   }
556 
557   /**
558    * @see org.melati.Melati#write()
559    */
560   public void testWrite() {
561 
562   }
563 
564 
565   /**
566    * @see org.melati.Melati#setVariableExceptionHandler(Object)
567    */
568   public void testSetVariableExceptionHandler() throws Exception {
569   }
570 
571   /**
572    * @see org.melati.Melati#getUser()
573    */
574   public void testGetUser() throws Exception {
575     MelatiConfig mc = null;
576     Melati m = null;
577     mc = new MelatiConfig();
578     m = new Melati(mc, new MelatiStringWriter());
579     m.setPoemContext(poemContext());
580     assertNull(m.getUser());
581   }
582 
583   /**
584    * @see org.melati.Melati#isReferencePoemType(Field)
585    */
586   public void testIsReferencePoemType() throws Exception {
587     MelatiConfig mc = null;
588     Melati m = null;
589     mc = new MelatiConfig();
590     m = new Melati(mc, new MelatiStringWriter());
591     m.setPoemContext(poemContext());
592     try { 
593       m.isReferencePoemType(null);
594       fail("Should have blown up");
595     } catch (NullPointerException e) { 
596       e = null;
597     }
598   }
599 
600   /**
601    * @see org.melati.Melati#templateName(String)
602    */
603   public void testTemplateName() throws Exception {
604     MelatiConfig mc = null;
605     Melati m = null;
606     mc = new MelatiConfig();
607     m = new Melati(mc, new MelatiStringWriter());
608     m.setPoemContext(poemContext());
609     try {
610       m.templateName("");
611       fail("Should have blown up");
612     } catch (MelatiBugMelatiException e) { 
613       e = null;
614     }
615     
616     m.setTemplateEngine(new WebmacroTemplateEngine());
617       
618     assertEquals("", m.templateName(""));
619     assertEquals("nonexistent", m.templateName("nonexistent"));
620     assertEquals("/org/melati/template/test/Templated.wm", m.templateName("org/melati/template/test/Templated"));
621     
622     m.getTemplateEngine().addRoot("org/melati/template/test/");
623     assertEquals("/org/melati/template/test/Templated.wm", m.templateName("Templated"));
624     PoemContext pc = m.getPoemContext();
625     pc.setLogicalDatabase("melatijunit");
626     m.setPoemContext(pc);
627     assertEquals("/org/melati/template/test/melatijunit/Specialised.wm", m.templateName("Specialised"));
628     
629    // Without a slash on the end
630     m.getTemplateEngine().addRoot("org/melati/template/test");
631     assertEquals("/org/melati/template/test/Templated.wm", m.templateName("Templated"));
632     assertEquals("/org/melati/template/test/melatijunit/Specialised.wm", m.templateName("Specialised"));
633     
634   }
635 
636   
637 }