| 1 | /* |
|---|
| 2 | * Copyright 2008 Outerthought bvba and Schaubroeck nv |
|---|
| 3 | * |
|---|
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|---|
| 5 | * you may not use this file except in compliance with the License. |
|---|
| 6 | * You may obtain a copy of the License at |
|---|
| 7 | * |
|---|
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 9 | * |
|---|
| 10 | * Unless required by applicable law or agreed to in writing, software |
|---|
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|---|
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|---|
| 13 | * See the License for the specific language governing permissions and |
|---|
| 14 | * limitations under the License. |
|---|
| 15 | */ |
|---|
| 16 | package org.kauriproject.template; |
|---|
| 17 | |
|---|
| 18 | import java.util.ArrayList; |
|---|
| 19 | import java.util.Calendar; |
|---|
| 20 | import java.util.HashMap; |
|---|
| 21 | import java.util.List; |
|---|
| 22 | import java.util.Map; |
|---|
| 23 | |
|---|
| 24 | import org.kauriproject.template.el.GroovyExpression; |
|---|
| 25 | import org.kauriproject.util.xml.LocalDocumentBuilderFactory; |
|---|
| 26 | import org.kauriproject.xml.sax.Saxable; |
|---|
| 27 | import org.w3c.dom.Document; |
|---|
| 28 | import org.xml.sax.helpers.AttributesImpl; |
|---|
| 29 | import org.xml.sax.SAXException; |
|---|
| 30 | import org.xml.sax.ContentHandler; |
|---|
| 31 | |
|---|
| 32 | public class TemplateExecutionTest extends TemplateTestBase { |
|---|
| 33 | |
|---|
| 34 | public void testPlain() throws Exception { |
|---|
| 35 | testFlow("/org/kauriproject/template/plain.xml", true); |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | public void testForeach() throws Exception { |
|---|
| 39 | Map<String, Object> variables = new HashMap<String, Object>(); |
|---|
| 40 | String[] myarray = new String[] { "one", "two", "three" }; |
|---|
| 41 | List<String> mylist = new ArrayList<String>(); |
|---|
| 42 | mylist.add("item1"); |
|---|
| 43 | mylist.add("item2"); |
|---|
| 44 | mylist.add("item3"); |
|---|
| 45 | variables.put("mylist", mylist); |
|---|
| 46 | variables.put("myarray", myarray); |
|---|
| 47 | testFlow("/org/kauriproject/template/foreach.xml", variables, true); |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | public void testIf() throws Exception { |
|---|
| 51 | testFlow("/org/kauriproject/template/if.xml", true); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | public void testChoose() throws Exception { |
|---|
| 55 | testFlow("/org/kauriproject/template/choose.xml", true); |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | public void testEL() throws Exception { |
|---|
| 59 | // preload groovy |
|---|
| 60 | assertNotNull("error loading groovy", new GroovyExpression("\"loaded groovy\"") |
|---|
| 61 | .evaluate(new DefaultTemplateContext())); |
|---|
| 62 | testFlow("/org/kauriproject/template/el.xml", true); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | public void testELFunctions() throws Exception { |
|---|
| 66 | testFlow("/org/kauriproject/template/elfunc.xml", true); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | public void testMix() throws Exception { |
|---|
| 70 | testFlow("/org/kauriproject/template/mix.xml", true); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | public void testAttribute() throws Exception { |
|---|
| 74 | testFlow("/org/kauriproject/template/attribute.xml", true); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | public void testAttribute2() throws Exception { |
|---|
| 78 | testFlow("/org/kauriproject/template/attribute2.xml", true); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | public void testElement() throws Exception { |
|---|
| 82 | testFlow("/org/kauriproject/template/element.xml", true); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | public void testBig() throws Exception { |
|---|
| 86 | testFlow("/org/kauriproject/template/big.xml", false); |
|---|
| 87 | // test it two times in a row |
|---|
| 88 | testFlow("/org/kauriproject/template/big.xml", true); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | public void testTaglib() throws Exception { |
|---|
| 92 | testFlow("/org/kauriproject/template/taglib/taglib.xml", true); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | public void testInsert() throws Exception { |
|---|
| 96 | testFlow("/org/kauriproject/template/insert.xml", true); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | public void testVariable() throws Exception { |
|---|
| 100 | Map<String, Object> variables = new HashMap<String, Object>(); |
|---|
| 101 | Calendar cal = Calendar.getInstance(); |
|---|
| 102 | cal.set(2008, 1, 8, 11, 55, 0); |
|---|
| 103 | variables.put("mydate", cal.getTime()); |
|---|
| 104 | testFlow("/org/kauriproject/template/variable.xml", variables, true); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | public void testComment() throws Exception { |
|---|
| 108 | testFlow("/org/kauriproject/template/comment.xml", true); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | public void testMacro() throws Exception { |
|---|
| 112 | testFlow("/org/kauriproject/template/macro.xml", true); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | public void testInclude() throws Exception { |
|---|
| 116 | testFlow("/org/kauriproject/template/include.xml", true); |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | public void testImport() throws Exception { |
|---|
| 120 | testFlow("/org/kauriproject/template/import.xml", true); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | public void testInheritance() throws Exception { |
|---|
| 124 | testFlow("/org/kauriproject/template/inherit.xml", true); |
|---|
| 125 | testFlow("/org/kauriproject/template/inherit_base.xml", true); |
|---|
| 126 | testFlow("/org/kauriproject/template/inherit_multiple.xml", true); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | public void testInit() throws Exception { |
|---|
| 130 | testFlow("/org/kauriproject/template/init_flat.xml", true); |
|---|
| 131 | testFlow("/org/kauriproject/template/init_special.xml", true); |
|---|
| 132 | // special should yield the same result as flat |
|---|
| 133 | String result1 = loadXML("/org/kauriproject/template/init_flat_result.xml"); |
|---|
| 134 | String result2 = loadXML("/org/kauriproject/template/init_special_result.xml"); |
|---|
| 135 | assertEquals("Both results should be equal to get a useful test.", result1, result2); |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | public void testDropPrefixMappings() throws Exception { |
|---|
| 139 | // test drop mapping |
|---|
| 140 | testFlow("/org/kauriproject/template/dropprefix1.xml", true); |
|---|
| 141 | |
|---|
| 142 | // test drop mapping leaves non-dropped namespaces in place |
|---|
| 143 | testFlow("/org/kauriproject/template/dropprefix2.xml", true); |
|---|
| 144 | |
|---|
| 145 | // test drop all namespaces |
|---|
| 146 | testFlow("/org/kauriproject/template/dropprefix3.xml", true); |
|---|
| 147 | |
|---|
| 148 | // test drop mapping which is still used in the document |
|---|
| 149 | testFlow("/org/kauriproject/template/dropprefix4.xml", true); |
|---|
| 150 | |
|---|
| 151 | // test drop mapping which is still used in the document, attrs with same local name |
|---|
| 152 | testFlow("/org/kauriproject/template/dropprefix4a.xml", true); |
|---|
| 153 | |
|---|
| 154 | // test drop default namespace |
|---|
| 155 | testFlow("/org/kauriproject/template/dropprefix5.xml", true); |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | public void testVariableFromSrc() throws Exception { |
|---|
| 159 | Map<String, Object> parameters = new HashMap<String, Object>(); |
|---|
| 160 | parameters.put("overwriteTest1", "overwriteTest1 - external value"); |
|---|
| 161 | parameters.put("overwriteTest2", "overwriteTest2 - external value"); |
|---|
| 162 | testFlow("/org/kauriproject/template/variable_from_src.xml", parameters, true); |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | public void testXmlVariables() throws Exception { |
|---|
| 166 | Map<String, Object> variables = new HashMap<String, Object>(); |
|---|
| 167 | |
|---|
| 168 | Document document = LocalDocumentBuilderFactory.newDocument(); |
|---|
| 169 | document.appendChild(document.createElement("domElement")); |
|---|
| 170 | variables.put("document", document); |
|---|
| 171 | |
|---|
| 172 | Saxable saxable = new Saxable() { |
|---|
| 173 | public void toSAX(ContentHandler contentHandler) throws SAXException { |
|---|
| 174 | contentHandler.startElement("", "saxable", "saxable", new AttributesImpl()); |
|---|
| 175 | String message = "hi"; |
|---|
| 176 | contentHandler.characters(message.toCharArray(), 0, message.length()); |
|---|
| 177 | contentHandler.endElement("", "saxable", "saxable"); |
|---|
| 178 | } |
|---|
| 179 | }; |
|---|
| 180 | variables.put("saxable", saxable); |
|---|
| 181 | |
|---|
| 182 | testFlow("/org/kauriproject/template/xmlvariables.xml", variables, true); |
|---|
| 183 | } |
|---|
| 184 | } |
|---|