View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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.kuali.rice.core.impl.style;
17  
18  import org.apache.log4j.Logger;
19  import org.junit.Test;
20  import org.kuali.rice.core.api.CoreApiServiceLocator;
21  import org.kuali.rice.core.api.exception.RiceRuntimeException;
22  import org.kuali.rice.core.api.impex.xml.XmlIngestionException;
23  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
24  import org.kuali.rice.core.api.style.Style;
25  import org.kuali.rice.core.api.style.StyleService;
26  import org.kuali.rice.core.framework.impex.xml.XmlLoader;
27  import org.kuali.rice.core.impl.services.CoreImplServiceLocator;
28  import org.kuali.rice.kew.test.KEWTestCase;
29  import org.kuali.rice.kew.test.TestUtilities;
30  
31  import javax.xml.transform.Templates;
32  import javax.xml.transform.TransformerConfigurationException;
33  import javax.xml.transform.TransformerException;
34  import javax.xml.transform.stream.StreamResult;
35  import javax.xml.transform.stream.StreamSource;
36  import java.io.ByteArrayInputStream;
37  import java.io.FileNotFoundException;
38  import java.io.StringReader;
39  import java.io.StringWriter;
40  import java.io.Writer;
41  
42  import static org.junit.Assert.*;
43  
44  
45  /**
46   * Tests StyleServiceImpl
47   * @author Kuali Rice Team (rice.collab@kuali.org)
48   */
49  public class StyleServiceImplTest extends KEWTestCase {
50      private static final Logger LOG = Logger.getLogger(StyleServiceImplTest.class);
51  
52  	@Test public void testLoadXML() throws FileNotFoundException {
53          loadXmlFile("style.xml");
54  
55          StyleService styleService = CoreApiServiceLocator.getStyleService();
56          assertNotNull("Style 'an_arbitrary_style' not found", styleService.getStyle("an_arbitrary_style"));
57  
58          Style style = styleService.getStyle("an_arbitrary_style");
59          assertNotNull("'an_arbitrary_style' style not found", style);
60          assertEquals("an_arbitrary_style", style.getName());
61          assertTrue(style.isActive());
62          assertNotNull(style.getXmlContent());
63      }
64  
65  
66  	/**
67  	 * Tests automatic import of styles from files based on configuration properties.
68  	 * See edl.style.widgets in common-config-defualts.xml, edl.style.gidgets in kew-test-config.xml
69  	 */
70      @Test public void testLoadingFromConfiguredFile() {
71          StyleService styleService = CoreApiServiceLocator.getStyleService();
72          StyleDao dao = (StyleDao)GlobalResourceLoader.getService("styleDao");
73  
74          String notThereStyle = "gidgets";
75          String isThereStyle = "widgets";
76  
77          // first verify that the database doesn't contain these styles already
78          assertNull(dao.getStyle(notThereStyle));
79          assertNull(dao.getStyle(isThereStyle));
80  
81          // test loading an incorrectly configured style
82          try {
83              // the configured location for the gidgets style doesn't contain a file
84              styleService.getStyle(notThereStyle);
85              fail("should have thrown " + RiceRuntimeException.class.getSimpleName());
86          } catch (RiceRuntimeException e) {
87              LOG.info("^^^ CAUGHT EXPECTED EXCEPTION ^^^");
88          } catch (Exception e) {
89              fail("Wrong exception type '" + e.getClass() + "', should have been '" + RiceRuntimeException.class.getCanonicalName() + "'");
90          }
91  
92          styleService.getStyle("widgets");
93          // should succeed in loading it's style into the database
94      }
95  
96      @Test public void testInclusions() throws FileNotFoundException, TransformerConfigurationException, TransformerException {
97          loadXmlFile("style.xml");
98  
99          StyleService styleService = CoreApiServiceLocator.getStyleService();
100 
101         // ignoring the duplicate definition via inclusion test as the behavior seems
102         // unspecified
103         // XML.com claims it is an "error": http://www.xml.com/pub/a/2000/11/01/xslt/index.html
104         // XLST 1.0 spec doesn't seem to specify anything regarding this: http://www.w3.org/TR/xslt
105         // Michael Kay's XSLT Programmer's Reference states "...it is implementation-defined
106         // whether an XSLT processor will report duplicate declarations as an error , so
107         // the behavior may vary from on product to another
108         // (although it is not clear to me whether he is speaking specifically of identical
109         // literal definitions introduced by re-inclusion of the same exact stylesheet twice, or
110         // "logical" duplication of template match criteria)
111         /*Templates t = styleService.getStyleAsTranslet("test_includer");
112         StringWriter w = new StringWriter();
113         StreamResult result = new StreamResult(w);
114         try {
115             t.newTransformer().transform(new StreamSource(new StringReader("<a/>")), result);
116             System.err.println(w.toString());
117             fail("Exception not thrown on ambiguous template defs");
118         } catch (Exception e) {
119             // expected
120         }*/
121 
122         Writer w = new StringWriter();
123         StreamResult result = new StreamResult(w);
124         Templates t = styleService.getStyleAsTranslet("test_includer2");
125         t.newTransformer().transform(new StreamSource(new StringReader("<a/>")), result);
126         assertEquals("oneoneoneoneone", w.toString());
127 
128         w = new StringWriter();
129         result = new StreamResult(w);
130         t.newTransformer().transform(new StreamSource(new StringReader("<b/>")), result);
131         assertEquals("22222", w.toString());
132 
133         w = new StringWriter();
134         result = new StreamResult(w);
135         t = styleService.getStyleAsTranslet("test_importer");
136         t.newTransformer().transform(new StreamSource(new StringReader("<a/>")), result);
137         assertEquals("aaaaa", w.toString());
138 
139         w = new StringWriter();
140         result = new StreamResult(w);
141         t.newTransformer().transform(new StreamSource(new StringReader("<b/>")), result);
142         assertEquals("BBBBB", w.toString());
143 
144         w = new StringWriter();
145         result = new StreamResult(w);
146         t.newTransformer().transform(new StreamSource(new StringReader("<c/>")), result);
147         assertEquals("CCCCC", w.toString());
148     }
149 
150     @Test public void testLoadBadDefinition() throws FileNotFoundException {
151         XmlLoader xmlLoader = CoreImplServiceLocator.getStyleXmlLoader();
152         try {
153             xmlLoader.loadXml(TestUtilities.loadResource(getClass(), "badstyle.xml"), null);
154             fail("BadDefinition was successfully parsed.");
155         } catch (XmlIngestionException re) {
156             // should probably use type system to detect type of error, not just message string...
157             // maybe we need general parsing or "semantic" validation exception
158             assertTrue("Wrong exception occurred: " + re, re.getMessage().contains("Style 'style' element must contain a 'xsl:stylesheet' child element"));
159         }
160     }
161 
162     @Test public void testStoreStyle() {
163     	StyleService styleService = CoreApiServiceLocator.getStyleService();
164     	XmlLoader xmlLoader = CoreImplServiceLocator.getStyleXmlLoader();
165         String styleXml = "<data xmlns=\"ns:workflow\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"ns:workflow resource:WorkflowData\"><styles xmlns=\"ns:workflow/Style\" xsi:schemaLocation=\"ns:workflow/Style resource:Style\"><style></style></styles></data>";
166         try {
167             xmlLoader.loadXml(new ByteArrayInputStream(styleXml.getBytes()), null);
168             fail("Storing style with no name succeeded");
169         } catch (XmlIngestionException e) {
170             // expected due to lack of name
171         }
172         styleXml = "<data xmlns=\"ns:workflow\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"ns:workflow resource:WorkflowData\"><styles xmlns=\"ns:workflow/Style\" xsi:schemaLocation=\"ns:workflow/Style resource:Style\"><style name=\"test\"></style></styles></data>";
173         try {
174         	xmlLoader.loadXml(new ByteArrayInputStream(styleXml.getBytes()), null);
175             fail("Storing style with no xsl:stylesheet element succeeded");
176         } catch (XmlIngestionException e) {
177             // expected due to lack of stylesheet content
178         }
179         styleXml = "<data xmlns=\"ns:workflow\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"ns:workflow resource:WorkflowData\"><styles xmlns=\"ns:workflow/Style\" xsi:schemaLocation=\"ns:workflow/Style resource:Style\"><style name=\"test\"><xsl:stylesheet></xsl:stylesheet></style></styles></data>";
180         xmlLoader.loadXml(new ByteArrayInputStream(styleXml.getBytes()), null);
181         Style style = styleService.getStyle("test");
182         assertNotNull(style);
183         assertEquals("test", style.getName());
184         assertNotNull(style);
185         assertNotNull(style.getXmlContent());
186     }
187 }