1 package org.sapia.util.text.test; 2 3 import java.util.Iterator ; 6 7 import junit.framework.TestCase; 10 import junit.textui.TestRunner; 11 12 import org.sapia.util.text.ConstantElement; 15 import org.sapia.util.text.CompositeElement; 16 import org.sapia.util.text.TemplateElementIF; 17 import org.sapia.util.text.TemplateFactory; 18 import org.sapia.util.text.VariableElement; 19 20 21 29 public class TemplateFactoryTest extends TestCase { 30 31 private TemplateFactory _theFactory; 32 33 public static void main(String [] args) { 34 TestRunner.run(TemplateFactoryTest.class); 35 } 36 37 public TemplateFactoryTest(String aName) { 38 super(aName); 39 } 40 41 public void setUp() { 42 _theFactory = new TemplateFactory( 43 TemplateFactory.DEFAULT_STARTING_DELIMITER, 44 TemplateFactory.DEFAULT_ENDING_DELIMITER); 45 } 46 47 public void testParseSimpleText() throws Exception { 48 String aContent = "This is a simple text"; 49 TemplateElementIF aTemplate = _theFactory.parse(aContent); 50 51 assertTrue("The template is not of a valid type", aTemplate instanceof CompositeElement); 52 CompositeElement aCompositeElement = (CompositeElement) aTemplate; 53 54 assertEquals("The composite element size is not valid ", 1, aCompositeElement.getElements().size()); 55 assertTrue("The template element is not of a valid type", aCompositeElement.getElements().get(0) instanceof ConstantElement); 56 57 ConstantElement aConstant = (ConstantElement) aCompositeElement.getElements().get(0); 58 assertEquals("The content of the constant element is invalid", aContent, aConstant.getContent()); 59 } 60 61 public void testParseEmptyText() throws Exception { 62 String aContent = ""; 63 TemplateElementIF aTemplate = _theFactory.parse(aContent); 64 65 assertTrue("The template is not of a valid type", aTemplate instanceof CompositeElement); 66 CompositeElement aCompositeElement = (CompositeElement) aTemplate; 67 68 assertEquals("The composite element size is not valid ", 0, aCompositeElement.getElements().size()); 69 } 70 71 public void testParseNullText() throws Exception { 72 try { 73 TemplateElementIF aTemplate = _theFactory.parse(null); 74 fail("Should not parse a null content. Result of parse(): " + aTemplate); 75 } catch (IllegalArgumentException iae) { 76 } 77 } 78 79 public void testParseSimpleVariable() throws Exception { 80 String aContent = "${simple.variable}"; 81 TemplateElementIF aTemplate = _theFactory.parse(aContent); 82 83 assertTrue("The template is not of a valid type", aTemplate instanceof CompositeElement); 84 CompositeElement aCompositeElement = (CompositeElement) aTemplate; 85 assertEquals("The composite element size is not valid ", 1, aCompositeElement.getElements().size()); 86 Iterator someElements = aCompositeElement.getElements().iterator(); 87 88 TemplateElementIF anElement = (TemplateElementIF) someElements.next(); 89 assertTrue("The template element is not of a valid type", anElement instanceof VariableElement); 90 assertEquals("The name of the variable element is invalid", "simple.variable", ((VariableElement) anElement).getName()); 91 } 92 93 public void testParseEmptyVariable() throws Exception { 94 String aContent = "${}"; 95 TemplateElementIF aTemplate = _theFactory.parse(aContent); 96 97 assertTrue("The template is not of a valid type", aTemplate instanceof CompositeElement); 98 CompositeElement aCompositeElement = (CompositeElement) aTemplate; 99 assertEquals("The composite element size is not valid ", 1, aCompositeElement.getElements().size()); 100 Iterator someElements = aCompositeElement.getElements().iterator(); 101 102 TemplateElementIF anElement = (TemplateElementIF) someElements.next(); 103 assertTrue("The template element is not of a valid type", anElement instanceof ConstantElement); 104 assertEquals("The content of the constant element is invalid", aContent, ((ConstantElement) anElement).getContent()); 105 } 106 107 public void testParseUnclosedVariable() throws Exception { 108 String aContent = "${ This is a test"; 109 TemplateElementIF aTemplate = _theFactory.parse(aContent); 110 111 assertTrue("The template is not of a valid type", aTemplate instanceof CompositeElement); 112 CompositeElement aCompositeElement = (CompositeElement) aTemplate; 113 assertEquals("The composite element size is not valid ", 1, aCompositeElement.getElements().size()); 114 Iterator someElements = aCompositeElement.getElements().iterator(); 115 116 TemplateElementIF anElement = (TemplateElementIF) someElements.next(); 117 assertTrue("The template element is not of a valid type", anElement instanceof ConstantElement); 118 assertEquals("The content of the constant element is invalid", aContent, ((ConstantElement) anElement).getContent()); 119 } 120 121 public void testParseUnclosedEmptyVariable() throws Exception { 122 String aContent = "${"; 123 TemplateElementIF aTemplate = _theFactory.parse(aContent); 124 125 assertTrue("The template is not of a valid type", aTemplate instanceof CompositeElement); 126 CompositeElement aCompositeElement = (CompositeElement) aTemplate; 127 assertEquals("The composite element size is not valid ", 1, aCompositeElement.getElements().size()); 128 Iterator someElements = aCompositeElement.getElements().iterator(); 129 130 TemplateElementIF anElement = (TemplateElementIF) someElements.next(); 131 assertTrue("The template element is not of a valid type", anElement instanceof ConstantElement); 132 assertEquals("The content of the constant element is invalid", aContent, ((ConstantElement) anElement).getContent()); 133 } 134 135 public void testParseUnclosedVariableWithVariable() throws Exception { 136 String aContent = "${ This is a text and ${this.a.variable}"; 137 TemplateElementIF aTemplate = _theFactory.parse(aContent); 138 139 assertTrue("The template is not of a valid type", aTemplate instanceof CompositeElement); 140 CompositeElement aCompositeElement = (CompositeElement) aTemplate; 141 assertEquals("The composite element size is not valid ", 2, aCompositeElement.getElements().size()); 142 Iterator someElements = aCompositeElement.getElements().iterator(); 143 144 TemplateElementIF anElement = (TemplateElementIF) someElements.next(); 145 assertTrue("The template element is not of a valid type", anElement instanceof ConstantElement); 146 assertEquals("The content of the constant element is invalid", "${ This is a text and ", ((ConstantElement) anElement).getContent()); 147 148 anElement = (TemplateElementIF) someElements.next(); 149 assertTrue("The template element is not of a valid type", anElement instanceof VariableElement); 150 assertEquals("The name of the variable element is invalid", "this.a.variable", ((VariableElement) anElement).getName()); 151 } 152 153 public void testParseSimpleComposite() throws Exception { 154 String aContent = "This is a simple ${variable} example"; 155 TemplateElementIF aTemplate = _theFactory.parse(aContent); 156 157 assertTrue("The template is not of a valid type", aTemplate instanceof CompositeElement); 158 CompositeElement aCompositeElement = (CompositeElement) aTemplate; 159 assertEquals("The composite element size is not valid ", 3, aCompositeElement.getElements().size()); 160 Iterator someElements = aCompositeElement.getElements().iterator(); 161 162 TemplateElementIF anElement = (TemplateElementIF) someElements.next(); 163 assertTrue("The template element is not of a valid type", anElement instanceof ConstantElement); 164 assertEquals("The content of the constant element 1 is invalid", "This is a simple ", ((ConstantElement) anElement).getContent()); 165 166 anElement = (TemplateElementIF) someElements.next(); 167 assertTrue("The template element is not of a valid type", anElement instanceof VariableElement); 168 assertEquals("The name of the variable element 2 is invalid", "variable", ((VariableElement) anElement).getName()); 169 170 anElement = (TemplateElementIF) someElements.next(); 171 assertTrue("The template element is not of a valid type", anElement instanceof ConstantElement); 172 assertEquals("The content of the element 3 is invalid", " example", ((ConstantElement) anElement).getContent()); 173 } 174 175 public void testParseCompositeWithEmptyVariable() throws Exception { 176 String aContent = "This is a simple ${} example"; 177 TemplateElementIF aTemplate = _theFactory.parse(aContent); 178 179 assertTrue("The template is not of a valid type", aTemplate instanceof CompositeElement); 180 CompositeElement aCompositeElement = (CompositeElement) aTemplate; 181 assertEquals("The composite element size is not valid ", 3, aCompositeElement.getElements().size()); 182 Iterator someElements = aCompositeElement.getElements().iterator(); 183 184 TemplateElementIF anElement = (TemplateElementIF) someElements.next(); 185 assertTrue("The template element is not of a valid type", anElement instanceof ConstantElement); 186 assertEquals("The content of the constant element 1 is invalid", "This is a simple ", ((ConstantElement) anElement).getContent()); 187 188 anElement = (TemplateElementIF) someElements.next(); 189 assertTrue("The template element is not of a valid type", anElement instanceof ConstantElement); 190 assertEquals("The content of the constant element 2 is invalid", "${}", ((ConstantElement) anElement).getContent()); 191 192 anElement = (TemplateElementIF) someElements.next(); 193 assertTrue("The template element is not of a valid type", anElement instanceof ConstantElement); 194 assertEquals("The content of the element 3 is invalid", " example", ((ConstantElement) anElement).getContent()); 195 } 196 197 public void testParseCompositeWithUnclosedVariable() throws Exception { 198 String aContent = "This is a complex ${ mixed up example ${for.the} fun of it}"; 199 TemplateElementIF aTemplate = _theFactory.parse(aContent); 200 201 assertTrue("The template is not of a valid type", aTemplate instanceof CompositeElement); 202 CompositeElement aCompositeElement = (CompositeElement) aTemplate; 203 assertEquals("The composite element size is not valid ", 4, aCompositeElement.getElements().size()); 204 Iterator someElements = aCompositeElement.getElements().iterator(); 205 206 TemplateElementIF anElement = (TemplateElementIF) someElements.next(); 207 assertTrue("The template element is not of a valid type", anElement instanceof ConstantElement); 208 assertEquals("The content of the constant element 1 is invalid", "This is a complex ", ((ConstantElement) anElement).getContent()); 209 210 anElement = (TemplateElementIF) someElements.next(); 211 assertTrue("The template element is not of a valid type", anElement instanceof ConstantElement); 212 assertEquals("The content of the constant element 2 is invalid", "${ mixed up example ", ((ConstantElement) anElement).getContent()); 213 214 anElement = (TemplateElementIF) someElements.next(); 215 assertTrue("The template element is not of a valid type", anElement instanceof VariableElement); 216 assertEquals("The name of the variable element 3 is invalid", "for.the", ((VariableElement) anElement).getName()); 217 218 anElement = (TemplateElementIF) someElements.next(); 219 assertTrue("The template element is not of a valid type", anElement instanceof ConstantElement); 220 assertEquals("The content of the element 4 is invalid", " fun of it}", ((ConstantElement) anElement).getContent()); 221 } 222 223 } | Popular Tags |