PageRenderTime 60ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/sling475/JSPSlingIncludeTest.java

http://sling-scala.googlecode.com/
Java | 343 lines | 172 code | 122 blank | 49 comment | 2 complexity | d6a1061bf9863da7929523c1ea9a6f9d MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.sling.launchpad.webapp.integrationtest;
  18. import java.util.Map;
  19. import java.util.HashMap;
  20. import java.io.File;
  21. import java.io.FileReader;
  22. import java.io.FileWriter;
  23. import java.io.InputStream;
  24. import java.io.IOException;
  25. import java.io.BufferedReader;
  26. import java.io.BufferedWriter;
  27. import java.io.FileInputStream;
  28. import org.apache.sling.commons.testing.integration.HttpTestBase;
  29. public class JSPSlingIncludeTest extends HttpTestBase {
  30. // Holds the URL's for top level child content mock nodes after created by the test frame work ex: contentUrlX means path is HTTP_BASE_URL + /root after created by testClient
  31. private String contentUrlX;
  32. private String contentUrlA;
  33. private String contentUrlB;
  34. private String contentUrlC;
  35. private String contentUrlD;
  36. private String contentUrlE;
  37. private String contentUrlF;
  38. private String contentUrlG;
  39. private String contentUrlDocument;
  40. // Text values of content types ex: /root means resource type /root if attached to resource type property of a content node.
  41. private String resourceTypeX;
  42. private String resourceTypeXA;
  43. private String resourceTypeXB;
  44. private String resourceTypeXC;
  45. private String resourceTypeXCA;
  46. private String resourceTypeXD;
  47. private String resourceTypeXDA;
  48. private String resourceTypeXE;
  49. private String resourceTypeXEA;
  50. private String resourceTypeXF;
  51. private String resourceTypeXG;
  52. private String resourceTypeDocument;
  53. // Preparing the environment for tests. i.e setting up nodes and assignig scripts.
  54. protected void setUp() throws Exception {
  55. super.setUp();
  56. Map<String,String> nodeProperties = new HashMap<String,String>();
  57. //first level resource /root
  58. resourceTypeX = "/root";
  59. nodeProperties.put("sling:resourceType", resourceTypeX);
  60. nodeProperties.put("message", "This is Resource /root");
  61. contentUrlX = testClient.createNode(HTTP_BASE_URL +resourceTypeX , nodeProperties);
  62. //second level resource /root/a for testAddSelector
  63. resourceTypeXA = resourceTypeX + "/a";
  64. nodeProperties.clear();
  65. nodeProperties.put("sling:resourceType", resourceTypeXA);
  66. nodeProperties.put("message", "This is Resource /root/a from add selector");
  67. contentUrlA = testClient.createNode(HTTP_BASE_URL +resourceTypeXA, nodeProperties);
  68. //second level resource /root/b for testReplaceSelector
  69. resourceTypeXB = resourceTypeX + "/b";
  70. nodeProperties.clear();
  71. nodeProperties.put("sling:resourceType", resourceTypeXB);
  72. nodeProperties.put("message", "This is Resource /root/b from replace selector");
  73. contentUrlB = testClient.createNode(HTTP_BASE_URL +resourceTypeXB, nodeProperties);
  74. //second level resource /root/c for testIncludeWithResource
  75. resourceTypeXC = resourceTypeX + "/c";
  76. nodeProperties.clear();
  77. nodeProperties.put("sling:resourceType", resourceTypeXC);
  78. nodeProperties.put("message", "This is Resource root/c");
  79. contentUrlC = testClient.createNode(HTTP_BASE_URL +resourceTypeXC, nodeProperties);
  80. //third level resource /root/c/a for testIncludeWithResource
  81. resourceTypeXCA = resourceTypeXC + "/a";
  82. nodeProperties.clear();
  83. nodeProperties.put("sling:resourceType", resourceTypeXCA);
  84. nodeProperties.put("message", "This is Resource root/c/a");
  85. testClient.createNode(HTTP_BASE_URL +resourceTypeXCA, nodeProperties);
  86. //second level resource /root/d for testIncludeWithPath
  87. resourceTypeXD = resourceTypeX + "/d";
  88. nodeProperties.clear();
  89. nodeProperties.put("sling:resourceType", resourceTypeXD);
  90. nodeProperties.put("message", "This is Resource root/d");
  91. contentUrlD = testClient.createNode(HTTP_BASE_URL +resourceTypeXD, nodeProperties);
  92. //third level resource /root/d/a for testIncludeWithPath
  93. resourceTypeXDA = resourceTypeXD + "/a";
  94. nodeProperties.clear();
  95. nodeProperties.put("sling:resourceType", resourceTypeXDA);
  96. nodeProperties.put("message", "This is Resource root/d/a");
  97. testClient.createNode(HTTP_BASE_URL +resourceTypeXDA, nodeProperties);
  98. //second level resource /root/e for testIncludeWithSelectorInPath
  99. resourceTypeXE = resourceTypeX + "/e";
  100. nodeProperties.clear();
  101. nodeProperties.put("sling:resourceType", resourceTypeXE);
  102. nodeProperties.put("message", "This is Resource root/e");
  103. contentUrlE = testClient.createNode(HTTP_BASE_URL +resourceTypeXE, nodeProperties);
  104. //third level resource /root/e/a for testIncludeWithSelectorInPath
  105. resourceTypeXEA = resourceTypeXE + "/a";
  106. nodeProperties.clear();
  107. nodeProperties.put("sling:resourceType", resourceTypeXEA);
  108. nodeProperties.put("message", "This is Resource root/e/a");
  109. testClient.createNode(HTTP_BASE_URL +resourceTypeXEA, nodeProperties);
  110. //second level resource /root/e for testFlush
  111. resourceTypeXF = resourceTypeX + "/f";
  112. nodeProperties.clear();
  113. nodeProperties.put("sling:resourceType", resourceTypeXF);
  114. nodeProperties.put("message", "This is Resource root/f");
  115. contentUrlF = testClient.createNode(HTTP_BASE_URL +resourceTypeXF, nodeProperties);
  116. //second level resource /root/e for testReplaceSuffix
  117. resourceTypeXG = resourceTypeX + "/g";
  118. nodeProperties.clear();
  119. nodeProperties.put("sling:resourceType", resourceTypeXG);
  120. nodeProperties.put("message", "This is Resource root/g");
  121. contentUrlG = testClient.createNode(HTTP_BASE_URL +resourceTypeXG, nodeProperties);
  122. //Code for creating the doc node for custom script test
  123. resourceTypeDocument = "/doc";
  124. nodeProperties.clear();
  125. nodeProperties.put("sling:resourceType", resourceTypeDocument);
  126. nodeProperties.put("message", "This is Resource /doc");
  127. contentUrlDocument = testClient.createNode(HTTP_BASE_URL + resourceTypeDocument, nodeProperties);
  128. uploadTestScript(resourceTypeX,"include/html.jsp","html.jsp");
  129. uploadTestScript(resourceTypeXA,"include/addSelectorTest.jsp","html.jsp");
  130. uploadTestScript(resourceTypeXA,"include/selectorTest.jsp","selector.jsp");
  131. uploadTestScript(resourceTypeXB,"include/replaceSelectorTest.jsp","html.jsp");
  132. uploadTestScript(resourceTypeXB,"include/selectorTest.jsp","replace.jsp");
  133. uploadTestScript(resourceTypeXC,"include/resourceTest.jsp","html.jsp");
  134. uploadTestScript(resourceTypeXCA,"include/selectorTest.jsp", "html.jsp");
  135. uploadTestScript(resourceTypeXD,"include/pathTest.jsp","html.jsp");
  136. uploadTestScript(resourceTypeXDA,"include/selectorTest.jsp", "html.jsp");
  137. uploadTestScript(resourceTypeXE,"include/pathSelectorTest.jsp","html.jsp");
  138. uploadTestScript(resourceTypeXEA,"include/selectorTest.jsp", "selector.jsp");
  139. uploadTestScript(resourceTypeXF,"include/flushTest.jsp","html.jsp");
  140. uploadTestScript(resourceTypeXF,"include/selectorTest.jsp", "selector.jsp");
  141. uploadTestScript(resourceTypeXF,"include/replaceTest.jsp", "replace.jsp");
  142. uploadTestScript(resourceTypeXG,"include/suffix.jsp","html.jsp");
  143. uploadTestScript(resourceTypeXG,"include/suffixTest.jsp", "suffixtest.jsp");
  144. }
  145. // Cleaning up initialized content after completing tests
  146. protected void tearDown() throws Exception {
  147. testClient.delete(contentUrlX);
  148. super.tearDown();
  149. }
  150. //Thjs test simply tests the message property of the current node without <sling:include />.
  151. public void testWithoutInclude() throws Exception{
  152. final String content = getContent(contentUrlX + ".html", CONTENT_TYPE_HTML);
  153. assertTrue("Message from included node",content.contains("This is Resource /root"));
  154. }
  155. // This test tests the addSelector parameter in <sling:include/> tag
  156. public void testAddSelector() throws Exception{
  157. final String content = getContent(contentUrlA + ".html", CONTENT_TYPE_HTML);
  158. assertTrue("Message from included node",content.contains("This is Resource /root/a from add selector"));
  159. }
  160. // This test tests the replaceSelector parameter in <sling:include/> tag.
  161. public void testReplaceSelector() throws Exception{
  162. final String content = getContent(contentUrlB + ".html", CONTENT_TYPE_HTML);
  163. assertTrue("Message from included node",content.contains("This is Resource /root/b from replace selector"));
  164. }
  165. //This test tests path parameter in <sling:include /> tag
  166. public void testIncludeWithPath() throws Exception{
  167. final String content = getContent(contentUrlD + ".html", CONTENT_TYPE_HTML);
  168. assertTrue("Message from included node",content.contains("This is Resource root/d/a"));
  169. }
  170. //This test tests resource parameter in <sling:include /> tag
  171. public void testIncludeWithResource() throws Exception{
  172. final String content = getContent(contentUrlC + ".html", CONTENT_TYPE_HTML);
  173. assertTrue("Message from included node",content.contains("This is Resource root/c/a"));
  174. }
  175. //This test tests path parameter in <sling:include /> tag providing a selector in the path.
  176. public void testIncludeWithSelectorInPath() throws Exception{
  177. final String content = getContent(contentUrlE + ".html", CONTENT_TYPE_HTML);
  178. assertTrue("Message from included node",content.contains("This is Resource root/e/a"));
  179. }
  180. //This test is intended to test flush parameter in <sling:include \>. The logic here is if response.isCommitted()
  181. // returns true that means the output buffer is flushed and hence response is committed.
  182. public void testFlush() throws Exception{
  183. final String content = getContent(contentUrlF + ".html", CONTENT_TYPE_HTML);
  184. assertTrue("result of flush",content.contains("flush successful becasue commit is true"));
  185. }
  186. //This test tests the replaceSuffix parameter in <sling:include /> tag
  187. public void testReplaceSuffix() throws Exception{
  188. final String content = getContent(contentUrlG + ".html/a.html" , CONTENT_TYPE_HTML);
  189. assertTrue("Test text from suffixTest",content.contains("The suffix in suffixTest.jsp is"));
  190. assertTrue("Replaced suffix from suffixTest",content.contains("a.b"));
  191. }
  192. //This is a custom test for documentation purposes
  193. // public void testCustomScriptInput() throws Exception{
  194. // uploadSlingIncludeScript("<sling:include replaceSelectors='suffixtest' replaceSuffix='/a.b' />" , "src/test/resources/integration-test/include/include.jsp");
  195. // String content = getContent(contentUrlDocument);
  196. // assertTrue("Replaced suffix from suffixTest",content.contains("a.b"));
  197. // System.out.println(content);
  198. // }
  199. public String getContent(String nodeURL) throws Exception{
  200. return getContent(nodeURL + ".html" , CONTENT_TYPE_HTML);
  201. }
  202. public void uploadSlingIncludeScript(String script , String mainScript){
  203. String initialScript = "";
  204. try {
  205. BufferedReader input = new BufferedReader(new FileReader(mainScript));
  206. String str;
  207. while ((str = input.readLine()) != null) {
  208. initialScript = initialScript + str;
  209. }
  210. input.close();
  211. } catch (IOException e) {}
  212. initialScript = initialScript + script;
  213. try {
  214. BufferedWriter out = new BufferedWriter(new FileWriter("src/test/resources/integration-test/include/document.jsp"));
  215. out.write(initialScript);
  216. out.close();
  217. uploadTestScript(resourceTypeXG,"include/document.jsp" , "html.jsp");
  218. //uploadTestScript(resourceTypeDocument,"include/suffixTest.jsp", "suffixtest.jsp");
  219. } catch (IOException e) {}
  220. }
  221. }