View Javadoc
1   package org.melati.template.test;
2   
3   import org.melati.MelatiConfig;
4   import org.melati.poem.JdbcPersistent;
5   import org.melati.template.ClassNameTempletLoader;
6   import org.melati.template.JavaMarkupLanguage;
7   import org.melati.template.Template;
8   import org.melati.template.TemplateContext;
9   import org.melati.template.webmacro.WebmacroTemplateEngine;
10  import org.melati.util.MelatiException;
11  import org.melati.util.test.Node;
12  
13  import java.util.ArrayList;
14  import java.util.Properties;
15  import java.util.Vector;
16  
17  /**
18   * Test the JavaMarkupLanguage and its AttributeMarkupLanguage using WebMacro.
19   *
20   * @author timp
21   * @since 2017-01-21
22   */
23  public class JavaMarkupLanguageWebmacroTest extends JavaMarkupLanguageSpec {
24  
25    protected void melatiConfig() throws MelatiException {
26      mc = new MelatiConfig();
27      mc.setTemplateEngine(new WebmacroTemplateEngine());
28    }
29  
30  
31    public void testExpandedTemplate() {
32      MelatiConfig mc = new MelatiConfig();
33      templateEngine.init(mc);
34      TemplateContext templateContext = templateEngine.getTemplateContext();
35  
36  
37      Node persistent = (Node) getDb().getTable("node").newPersistent();
38      persistent.setName("Mum");
39      persistent.makePersistent();
40  
41      templateContext.put("object", persistent);
42  
43      Template template = ClassNameTempletLoader.getInstance().templet(
44          templateEngine, new JavaMarkupLanguage(), null, JdbcPersistent.class);
45      String renderedPersistent = templateEngine.expandedTemplate(
46          template,
47          templateContext);
48      assertTrue(renderedPersistent.startsWith("// Delete this line to prevent overwriting of this file\n" +
49          "package org.melati.poem")
50      );
51  
52    }
53  
54    public void testExpandedTemplatePurpose() {
55      MelatiConfig mc = new MelatiConfig();
56      templateEngine.init(mc);
57      TemplateContext templateContext = templateEngine.getTemplateContext();
58  
59  
60      Node persistent = (Node) getDb().getTable("node").newPersistent();
61      persistent.setName("Mum");
62      persistent.makePersistent();
63  
64      templateContext.put("object", persistent);
65  
66      Template template = ClassNameTempletLoader.getInstance().templet(
67          templateEngine, new JavaMarkupLanguage(), "DAO", JdbcPersistent.class);
68      String renderedPersistent = templateEngine.expandedTemplate(
69          template,
70          templateContext);
71      assertTrue(renderedPersistent.startsWith("// Delete this line to prevent overwriting of this file\n" +
72          "package org.melati.poem")
73      );
74      System.err.println(renderedPersistent);
75    }
76    public void testGetAttr() {
77      // we don't have one
78    }
79  
80    public void testEntitySubstitution() throws Exception {
81      char pound[] = {163};
82      assertEquals(new String(pound), ml.rendered(new String(pound)));
83    }
84  
85    public void testEscapedPersistent() {
86      // not implemented yet
87      try {
88        ml.escaped(getDb().getUserTable().getUserObject(0));
89      } catch (RuntimeException e) {
90        // expected
91      }
92    }
93  
94    public void testEncoded() {
95      try {
96        ml.encoded(" ");
97      } catch (RuntimeException e) {
98        e = null;
99      }
100   }
101 
102   public void testRenderedObject() throws Exception {
103     assertEquals("Fredd$", ml.rendered("Fredd$"));
104     // Note velocity seems to leave the line end on
105     // assertEquals("[1]", ml.rendered(new Integer("1")).trim());
106 
107     // assertEquals("1", ml.getAttr().rendered(new Integer("1")));
108     try {
109       ml.getAttr().rendered(new Bomber());
110       fail("Should have bombed");
111     } catch (RuntimeException e) {
112       assertEquals("Not expected to be called in Java", e.getMessage());
113       e = null;
114     }
115 
116     try {
117       ml.rendered(new Bomber());
118       fail("Should have bombed");
119     } catch (RuntimeException e) {
120       assertTrue(e.getMessage().indexOf("Bomber bombed") > -1);
121       e = null;
122     }
123 
124   }
125 
126   public void testRenderedAccessPoemException() throws Exception {
127     // not applicable
128   }
129 
130   /**
131    * @see org.melati.template.MarkupLanguage#renderedMarkup
132    */
133   public void testRenderedMarkupString() throws Exception {
134     try {
135       ml.renderedMarkup("</a>");
136       fail("Should have bombed");
137     } catch (RuntimeException e) {
138       assertEquals("Not expected to be called in Java", e.getMessage());
139       e = null;
140     }
141     assertEquals("<\\/a>", ml.rendered("</a>"));
142   }
143 
144   /**
145    * Test that toString is used if no template found.
146    * FIXME a template has to be found as there is always an Object template
147    * What is this test meant to be about?
148    */
149   public void testUntemplatedObjectUsesToString() throws Exception {
150     assertEquals("\npublic class java.util.Properties {\n" +
151         "  String value = \"{}\";\n}\n", ml.rendered(new Properties()));
152   }
153 
154   public void testSpecialTemplateFound() throws Exception {
155     // not applicable
156   }
157 
158   public void testInputField() throws Exception {
159     // not applicable
160   }
161 
162   public void testInputFieldSelection() throws Exception {
163     // not applicable
164   }
165 
166   public void testSelectionWindowField() throws Exception {
167     // not applicable
168   }
169 
170   public void testInputFieldForRestrictedField() throws Exception {
171     // not applicable
172   }
173 
174   public void testInputAs() throws Exception {
175   }
176 
177   public void testSearchInput() throws Exception {
178   }
179 
180   public void testRenderedTreeable() throws Exception {
181   }
182 
183   /**
184    * Test NPE not thrown.
185    */
186   public void testNull() throws Exception {
187     assertEquals("null", ml.rendered(null));
188   }
189 
190   public void testRenderedList() {
191     ArrayList<Object> l = new ArrayList<Object>();
192     assertEquals("[]\n", ml.rendered(l));
193     l.add(null);
194     assertEquals("[null]\n", ml.rendered(l));
195     l.add(null);
196     assertEquals("[null,null]\n", ml.rendered(l));
197     l.add(Boolean.TRUE);
198     assertEquals("[null,null,true]\n", ml.rendered(l));
199     l.add(Boolean.FALSE);
200     assertEquals("[null,null,true,false]\n", ml.rendered(l));
201     l.add(null);
202     assertEquals("[null,null,true,false,null]\n", ml.rendered(l));
203     l.add("test");
204     assertEquals("[null,null,true,false,null,\"test\"]\n", ml.rendered(l));
205 
206     Vector<Object> v = new Vector<Object>();
207     assertEquals("[]\n", ml.rendered(v));
208     v.add("one");
209     assertEquals("[\"one\"]\n", ml.rendered(v));
210     v.add("one");
211     assertEquals("[\"one\",\"one\"]\n", ml.rendered(v));
212     assertEquals("[\"one\",\"one\"]\n", ml.rendered(v.elements()));
213   }
214 
215   public void testRenderedSelection() {
216     // not applicable
217   }
218 }