1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.kuali.rice.kew.edl;
18
19
20 import junit.framework.AssertionFailedError;
21 import org.junit.Test;
22 import org.kuali.rice.core.config.Config;
23 import org.kuali.rice.core.config.ConfigContext;
24 import org.kuali.rice.kew.edl.bo.EDocLiteAssociation;
25 import org.kuali.rice.kew.edl.bo.EDocLiteDefinition;
26 import org.kuali.rice.kew.edl.bo.EDocLiteStyle;
27 import org.kuali.rice.kew.edl.service.EDocLiteService;
28 import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
29 import org.kuali.rice.kew.service.KEWServiceLocator;
30 import org.kuali.rice.kew.test.KEWTestCase;
31 import org.kuali.rice.kew.test.TestUtilities;
32 import org.kuali.rice.kew.util.XmlHelper;
33 import org.w3c.dom.Element;
34
35 import javax.xml.transform.Templates;
36 import java.io.ByteArrayInputStream;
37 import java.io.FileNotFoundException;
38 import java.util.List;
39 import java.util.Map;
40
41
42
43
44
45
46 public class EDocLiteServiceImplTest extends KEWTestCase {
47
48 @Test public void testLoadXML() throws FileNotFoundException {
49 loadXmlFile("EDocLiteContent.xml");
50 loadXmlFile("edlstyle.xml");
51
52 EDocLiteService edls = KEWServiceLocator.getEDocLiteService();
53
54 assertTrue("Definition not found", edls.getEDocLiteDefinitions().contains("profile"));
55 assertTrue("Style not found", edls.getEDocLiteStyles().contains("Default"));
56 assertEquals(1, edls.getEDocLiteAssociations().size());
57 EDocLiteDefinition def = edls.getEDocLiteDefinition("profile");
58 assertNotNull("'profile' definition not found", def);
59 assertEquals("profile", def.getName());
60 assertNotNull(def.getActiveInd());
61 assertTrue(def.getActiveInd().booleanValue());
62 EDocLiteStyle style = edls.getEDocLiteStyle("Default");
63 assertNotNull("'Default' style not found", style);
64 assertEquals("Default", style.getName());
65 assertNotNull(style.getActiveInd());
66 assertTrue(style.getActiveInd().booleanValue());
67 assertNotNull(style.getXmlContent());
68 }
69
70 @Test public void testLoadBadDefinition() throws FileNotFoundException {
71 EDocLiteService edls = KEWServiceLocator.getEDocLiteService();
72 try {
73 edls.loadXml(TestUtilities.loadResource(getClass(), "BadDefinition.xml"), null);
74 fail("BadDefinition was successfully parsed.");
75 } catch (RuntimeException re) {
76
77
78 assertTrue("Wrong exception occurred", re.getMessage().contains("EDocLite definition contains references to non-existent attributes"));
79 }
80 }
81
82 @Test public void testStoreDefinition() {
83 EDocLiteService edls = KEWServiceLocator.getEDocLiteService();
84 String defXml = "<edl></edl>";
85 try {
86 edls.saveEDocLiteDefinition(new ByteArrayInputStream(defXml.getBytes()));
87 throw new AssertionFailedError("Storing edl with no name succeeded");
88 } catch (WorkflowServiceErrorException wsee) {
89
90 }
91 defXml = "<edl name=\"test\"></edl>";
92 edls.saveEDocLiteDefinition(new ByteArrayInputStream(defXml.getBytes()));
93 EDocLiteDefinition def = edls.getEDocLiteDefinition("test");
94 assertNotNull(def);
95 assertEquals("test", def.getName());
96 }
97
98 @Test public void testStoreStyle() {
99 EDocLiteService edls = KEWServiceLocator.getEDocLiteService();
100 String styleXml = "<style></style>";
101 try {
102 edls.saveEDocLiteStyle(new ByteArrayInputStream(styleXml.getBytes()));
103 throw new AssertionFailedError("Storing style with no name succeeded");
104 } catch (WorkflowServiceErrorException wsee) {
105
106 }
107 styleXml = "<style name=\"test\"></style>";
108 try {
109 edls.saveEDocLiteStyle(new ByteArrayInputStream(styleXml.getBytes()));
110 throw new AssertionFailedError("Storing style with no xsl:stylesheet element succeeded");
111 } catch (WorkflowServiceErrorException wsee) {
112
113 }
114 styleXml = "<style name=\"test\"><xsl:stylesheet></xsl:stylesheet></style>";
115 edls.saveEDocLiteStyle(new ByteArrayInputStream(styleXml.getBytes()));
116 EDocLiteStyle style = edls.getEDocLiteStyle("test");
117 assertNotNull(style);
118 assertEquals("test", style.getName());
119 assertNotNull(style);
120 assertNotNull(style.getXmlContent());
121 }
122
123 @Test public void testStoreAssociation() {
124 EDocLiteService edls = KEWServiceLocator.getEDocLiteService();
125 String assocXml = "<association></association>";
126 try {
127 edls.saveEDocLiteAssociation(new ByteArrayInputStream(assocXml.getBytes()));
128 throw new AssertionFailedError("Storing association with no docType succeeded");
129 } catch (WorkflowServiceErrorException wsee) {
130
131 }
132 assocXml = "<association><docType></docType></association>";
133 try {
134 edls.saveEDocLiteAssociation(new ByteArrayInputStream(assocXml.getBytes()));
135 throw new AssertionFailedError("Storing association with empty docType succeeded");
136 } catch (WorkflowServiceErrorException wsee) {
137
138 }
139 assocXml = "<association><docType>foobar</docType></association>";
140 edls.saveEDocLiteAssociation(new ByteArrayInputStream(assocXml.getBytes()));
141 EDocLiteAssociation assoc = edls.getEDocLiteAssociation("foobar");
142 assertNull("Inactive Association was found", assoc);
143
144 assocXml = "<association><docType>foobar</docType><active>true</active></association>";
145 edls.saveEDocLiteAssociation(new ByteArrayInputStream(assocXml.getBytes()));
146 assoc = edls.getEDocLiteAssociation("foobar");
147 assertNotNull("Association was not found", assoc);
148 assertEquals("foobar", assoc.getEdlName());
149 assertNull(assoc.getDefinition());
150 assertNull(assoc.getStyle());
151
152 List<EDocLiteAssociation> assocs = edls.getEDocLiteAssociations();
153 assertEquals(1, assocs.size());
154 assoc = assocs.get(0);
155 assertEquals("foobar", assoc.getEdlName());
156 assertNull(assoc.getDefinition());
157 assertNull(assoc.getStyle());
158 assertNotNull(assoc.getActiveInd());
159 assertTrue(assoc.getActiveInd().booleanValue());
160
161 assocXml = "<association><style>style name</style><definition>definition name</definition><docType>foobar</docType><active>true</active></association>";
162 edls.saveEDocLiteAssociation(new ByteArrayInputStream(assocXml.getBytes()));
163 assoc = edls.getEDocLiteAssociation("foobar");
164 assertNotNull("Association was not found", assoc);
165 assertEquals("foobar", assoc.getEdlName());
166 assertEquals("definition name", assoc.getDefinition());
167 assertEquals("style name", assoc.getStyle());
168
169 assocs = edls.getEDocLiteAssociations();
170 assertEquals(1, assocs.size());
171 assoc = (EDocLiteAssociation) assocs.get(0);
172 assertNotNull("Association was not found", assoc);
173 assertEquals("foobar", assoc.getEdlName());
174 assertEquals("definition name", assoc.getDefinition());
175 assertEquals("style name", assoc.getStyle());
176 assertNotNull(assoc.getActiveInd());
177 assertTrue(assoc.getActiveInd().booleanValue());
178 }
179
180
181
182
183
184
185 @Test public void testConfigCaching() throws Exception {
186 ConfigContext.getCurrentContextConfig().putProperty(Config.EDL_CONFIG_LOCATION, "classpath:org/kuali/rice/kew/edl/TestEDLConfig.xml");
187
188 loadXmlFile("EDocLiteContent.xml");
189 loadXmlFile("edlstyle.xml");
190 loadXmlFile("widgets.xml");
191
192 Map config = EDLControllerFactory.fetchConfigFromCache("profile");
193 assertNull("Config should not be cached initially.", config);
194
195
196 EDLController edlController = KEWServiceLocator.getEDocLiteService().getEDLController("EDocLiteDocType");
197 assertNotNull(edlController);
198
199 config = EDLControllerFactory.fetchConfigFromCache("profile");
200 assertNotNull("Config should now be cached.", config);
201
202
203 assertEquals("Config processors should be the same.", edlController.getConfigProcessors().size(), config.size());
204 assertEquals(1, config.size());
205 Element key1 = (Element)edlController.getConfigProcessors().keySet().iterator().next();
206 Element key2 = (Element)config.keySet().iterator().next();
207 assertEquals("Key values should be the same", XmlHelper.getTextContent(key1), XmlHelper.getTextContent(key2));
208 assertEquals("Values should be the same", edlController.getConfigProcessors().get(key1), config.get(key2));
209
210
211 loadXmlFile("EDocLiteContent.xml");
212 config = EDLControllerFactory.fetchConfigFromCache("profile");
213 assertNull("Config should no longer be cached.", config);
214
215
216 edlController = KEWServiceLocator.getEDocLiteService().getEDLController("EDocLiteDocType");
217 assertNotNull(edlController);
218 config = EDLControllerFactory.fetchConfigFromCache("profile");
219 assertNotNull("Config should now be cached.", config);
220 }
221
222
223
224
225
226
227
228 @Test public void testStyleCaching() throws Exception {
229 ConfigContext.getCurrentContextConfig().putProperty(Config.EDL_CONFIG_LOCATION, "classpath:org/kuali/rice/kew/edl/TestEDLConfig.xml");
230
231 loadXmlFile("EDocLiteContent.xml");
232 loadXmlFile("edlstyle.xml");
233 loadXmlFile("widgets.xml");
234
235
236
237
238
239
240 EDocLiteAssociation association = KEWServiceLocator.getEDocLiteService().getEDocLiteAssociation("EDocLiteDocType");
241 assertNull("We should be using the Default style.", association.getStyle());
242 Templates templates = KEWServiceLocator.getEDocLiteService().getStyleAsTranslet(association.getStyle());
243 assertNotNull("Templates should not be null.", templates);
244
245
246
247
248
249
250
251
252
253 loadXmlFile("edlstyle.xml");
254
255
256
257
258 Templates newTemplates = KEWServiceLocator.getEDocLiteService().getStyleAsTranslet(association.getStyle());
259 assertNotNull("Templates should not be null.", templates);
260
261
262
263
264 assertFalse("Old Templates should be different from new Templates.", templates.equals(newTemplates));
265
266 }
267 }