1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
47
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
68
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
78 assertNull(dao.getStyle(notThereStyle));
79 assertNull(dao.getStyle(isThereStyle));
80
81
82 try {
83
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
94 }
95
96 @Test public void testInclusions() throws FileNotFoundException, TransformerConfigurationException, TransformerException {
97 loadXmlFile("style.xml");
98
99 StyleService styleService = CoreApiServiceLocator.getStyleService();
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
157
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
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
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 }