1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.framework.impex.xml.XmlLoader;
23 import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator;
24 import org.kuali.rice.coreservice.api.style.Style;
25 import org.kuali.rice.coreservice.api.style.StyleService;
26 import org.kuali.rice.coreservice.impl.CoreServiceImplServiceLocator;
27 import org.kuali.rice.kew.test.KEWTestCase;
28 import org.kuali.rice.kew.test.TestUtilities;
29 import org.kuali.rice.test.BaselineTestCase;
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 import java.util.List;
42
43 import static org.junit.Assert.*;
44
45
46
47
48
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
70
71
72 @Test public void testLoadingFromConfiguredFile() {
73 StyleService styleService = CoreServiceApiServiceLocator.getStyleService();
74
75 String notThereStyle = "gidgets";
76 String isThereStyle = "widgets";
77
78
79 List<String> styleNames = styleService.getAllStyleNames();
80 assertFalse("Style should not exist in database: " + notThereStyle, styleNames.contains(notThereStyle));
81 assertFalse("Style should not exist in database: " + isThereStyle, styleNames.contains(isThereStyle));
82
83
84 try {
85
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
96 }
97
98 @Test public void testInclusions() throws FileNotFoundException, TransformerConfigurationException, TransformerException {
99 loadXmlFile("style.xml");
100
101 StyleService styleService = CoreServiceApiServiceLocator.getStyleService();
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
159
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
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
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 }