View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    *
4    *
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.opensource.org/licenses/ecl2.php
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Tests EDocLiteServiceImpl
44   * @author Kuali Rice Team (rice.collab@kuali.org)
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          //edls.loadXml(new FileInputStream("conf/examples/xml/EDocLiteContent.xml"));
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              // should probably use type system to detect type of error, not just message string...
77              // maybe we need general parsing or "semantic" validation exception
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              // expected due to lack of name
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             // expected due to lack of name
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             // expected due to lack of stylesheet content
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             // expected due to lack of doctype
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             // expected due to emtpy doctype value
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      * Tests the caching behavior of configs in EDocLiteServiceImpl.  The config cache is a
182      * map of XML org.w3c.dom.Element to config classname mappings.  This cache is, in-reality, maintained
183      * by the EDLControllerFactory.
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     	// fetch the edl controller which should result in caching
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     	// compare the config in the cache with the config on the EDLController
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     	// now import the EDocLite again and it should be cleared from the cache
211     	loadXmlFile("EDocLiteContent.xml");
212     	config = EDLControllerFactory.fetchConfigFromCache("profile");
213     	assertNull("Config should no longer be cached.", config);
214 
215     	// fetch again and we should be back in action
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      * Tests the caching of "styles" in EDocLiteServiceImpl.
224      *
225      * The style cache is really a cache of java.xml.transform.Templates objects which represent
226      * the "compiled" stylesheets.
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         // try to grab the templates out of the cache, it shouldn't be cached yet
236 //        Templates cachedTemplates = new EDocLiteServiceImpl().fetchTemplatesFromCache("Default");
237 //        assertNull("The default style template should not be cached yet.", cachedTemplates);
238 
239         // fetch the Templates object from the service
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         // the Templates should now be cached
246 //        cachedTemplates = new EDocLiteServiceImpl().fetchTemplatesFromCache("Default");
247 //        assertNotNull("Templates should now be cached.", cachedTemplates);
248 
249 //        // the cached Templates should be the same as the Templates we fetched from the service
250 //        assertEquals("Templates should be the same.", templates, cachedTemplates);
251 
252         // now re-import the style and the templates should no longer be cached
253         loadXmlFile("edlstyle.xml");
254 //        cachedTemplates = new EDocLiteServiceImpl().fetchTemplatesFromCache("Default");
255 //        assertNull("After re-import, the Default style Templates should no longer be cached.", cachedTemplates);
256 
257         // re-fetch the templates from the service and verify they are in the cache
258         Templates newTemplates = KEWServiceLocator.getEDocLiteService().getStyleAsTranslet(association.getStyle());
259         assertNotNull("Templates should not be null.", templates);
260 //        cachedTemplates = new EDocLiteServiceImpl().fetchTemplatesFromCache("Default");
261 //        assertNotNull("Templates should now be cached.", cachedTemplates);
262 
263         // lastly, check that the newly cached templates are not the same as the original templates
264         assertFalse("Old Templates should be different from new Templates.", templates.equals(newTemplates));
265 
266     }
267 }