1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.kuali.rice.kew.edl.service.impl;
18
19
20 import junit.framework.AssertionFailedError;
21 import org.apache.log4j.Logger;
22 import org.junit.Test;
23 import org.kuali.rice.core.exception.RiceRuntimeException;
24 import org.kuali.rice.kew.edl.bo.EDocLiteStyle;
25 import org.kuali.rice.kew.edl.dao.EDocLiteDAO;
26 import org.kuali.rice.kew.edl.service.StyleService;
27 import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
28 import org.kuali.rice.kew.service.KEWServiceLocator;
29 import org.kuali.rice.kew.test.KEWTestCase;
30 import org.kuali.rice.kew.test.TestUtilities;
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.*;
38
39
40
41
42
43
44 public class StyleServiceImplTest extends KEWTestCase {
45 private static final Logger LOG = Logger.getLogger(StyleServiceImplTest.class);
46
47 @Test public void testLoadXML() throws FileNotFoundException {
48 loadXmlFile("style.xml");
49
50 StyleService styleService = KEWServiceLocator.getStyleService();
51 assertNotNull("Style 'an_arbitrary_style' not found", styleService.getStyle("an_arbitrary_style"));
52
53 assertTrue("Style not found among all styles", styleService.getStyleNames().contains("an_arbitrary_style"));
54
55 EDocLiteStyle style = styleService.getStyle("an_arbitrary_style");
56 assertNotNull("'an_arbitrary_style' style not found", style);
57 assertEquals("an_arbitrary_style", style.getName());
58 assertNotNull(style.getActiveInd());
59 assertTrue(style.getActiveInd().booleanValue());
60 assertNotNull(style.getXmlContent());
61 }
62
63
64
65
66
67
68 @Test public void testLoadingFromConfiguredFile() {
69 StyleService styleService = KEWServiceLocator.getStyleService();
70 EDocLiteDAO dao = (EDocLiteDAO)KEWServiceLocator.getService("enEDocLiteDAO");
71
72 String notThereStyle = "gidgets";
73 String isThereStyle = "widgets";
74
75
76 assertNull(dao.getEDocLiteStyle(notThereStyle));
77 assertNull(dao.getEDocLiteStyle(isThereStyle));
78
79
80 try {
81
82 styleService.getStyle(notThereStyle);
83 fail("should have thrown " + RiceRuntimeException.class.getSimpleName());
84 } catch (RiceRuntimeException e) {
85 LOG.info("^^^ CAUGHT EXPECTED EXCEPTION ^^^");
86 } catch (Exception e) {
87 fail("Wrong exception type '" + e.getClass() + "', should have been '" + RiceRuntimeException.class.getCanonicalName() + "'");
88 }
89
90 styleService.getStyle("widgets");
91
92 }
93
94 @Test public void testInclusions() throws FileNotFoundException, TransformerConfigurationException, TransformerException {
95 loadXmlFile("style.xml");
96
97 StyleService styleService = KEWServiceLocator.getStyleService();
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120 Writer w = new StringWriter();
121 StreamResult result = new StreamResult(w);
122 Templates t = styleService.getStyleAsTranslet("test_includer2");
123 t.newTransformer().transform(new StreamSource(new StringReader("<a/>")), result);
124 assertEquals("oneoneoneoneone", w.toString());
125
126 w = new StringWriter();
127 result = new StreamResult(w);
128 t.newTransformer().transform(new StreamSource(new StringReader("<b/>")), result);
129 assertEquals("22222", w.toString());
130
131 w = new StringWriter();
132 result = new StreamResult(w);
133 t = styleService.getStyleAsTranslet("test_importer");
134 t.newTransformer().transform(new StreamSource(new StringReader("<a/>")), result);
135 assertEquals("aaaaa", w.toString());
136
137 w = new StringWriter();
138 result = new StreamResult(w);
139 t.newTransformer().transform(new StreamSource(new StringReader("<b/>")), result);
140 assertEquals("BBBBB", w.toString());
141
142 w = new StringWriter();
143 result = new StreamResult(w);
144 t.newTransformer().transform(new StreamSource(new StringReader("<c/>")), result);
145 assertEquals("CCCCC", w.toString());
146 }
147
148 @Test public void testLoadBadDefinition() throws FileNotFoundException {
149 StyleService styleService = KEWServiceLocator.getStyleService();
150 try {
151 styleService.loadXml(TestUtilities.loadResource(getClass(), "badstyle.xml"), null);
152 fail("BadDefinition was successfully parsed.");
153 } catch (RuntimeException re) {
154
155
156 assertTrue("Wrong exception occurred: " + re, re.getMessage().contains("Style 'style' element must contain a 'xsl:stylesheet' child element"));
157 }
158 }
159
160 @Test public void testStoreStyle() {
161 StyleService styleService = KEWServiceLocator.getStyleService();
162 String styleXml = "<style></style>";
163 try {
164 styleService.saveStyle(new ByteArrayInputStream(styleXml.getBytes()));
165 throw new AssertionFailedError("Storing style with no name succeeded");
166 } catch (WorkflowServiceErrorException wsee) {
167
168 }
169 styleXml = "<style name=\"test\"></style>";
170 try {
171 styleService.saveStyle(new ByteArrayInputStream(styleXml.getBytes()));
172 throw new AssertionFailedError("Storing style with no xsl:stylesheet element succeeded");
173 } catch (WorkflowServiceErrorException wsee) {
174
175 }
176 styleXml = "<style name=\"test\"><xsl:stylesheet></xsl:stylesheet></style>";
177 styleService.saveStyle(new ByteArrayInputStream(styleXml.getBytes()));
178 EDocLiteStyle style = styleService.getStyle("test");
179 assertNotNull(style);
180 assertEquals("test", style.getName());
181 assertNotNull(style);
182 assertNotNull(style.getXmlContent());
183 }
184
185
186
187
188
189
190
191 @Test public void testStyleCaching() throws Exception {
192 loadXmlFile("style.xml");
193
194
195 Templates cachedTemplates = new StyleServiceImpl().fetchTemplatesFromCache("an_arbitrary_style");
196 assertNull("The default style template should not be cached yet.", cachedTemplates);
197
198
199 Templates templates = KEWServiceLocator.getStyleService().getStyleAsTranslet("an_arbitrary_style");
200 assertNotNull("Templates should not be null.", templates);
201 templates = KEWServiceLocator.getStyleService().getStyleAsTranslet("an_arbitrary_style");
202 assertNotNull("Templates should not be null.", templates);
203
204
205 cachedTemplates = new StyleServiceImpl().fetchTemplatesFromCache("an_arbitrary_style");
206 assertNotNull("Templates should now be cached.", cachedTemplates);
207
208
209 assertEquals("Templates should be the same.", templates, cachedTemplates);
210
211
212 loadXmlFile("style.xml");
213 cachedTemplates = new StyleServiceImpl().fetchTemplatesFromCache("an_arbitrary_style");
214 assertNull("After re-import, the Default style Templates should no longer be cached.", cachedTemplates);
215
216
217 Templates newTemplates = KEWServiceLocator.getStyleService().getStyleAsTranslet("an_arbitrary_style");
218 assertNotNull("Templates should not be null.", templates);
219 newTemplates = KEWServiceLocator.getStyleService().getStyleAsTranslet("an_arbitrary_style");
220 assertNotNull("Templates should not be null.", templates);
221
222 cachedTemplates = new StyleServiceImpl().fetchTemplatesFromCache("an_arbitrary_style");
223 assertNotNull("Templates should now be cached.", cachedTemplates);
224
225
226 assertFalse("Old Templates should be different from new Templates.", templates.equals(newTemplates));
227
228 }
229 }