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