View Javadoc
1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.edl.impl;
17  
18  import org.junit.Test;
19  import org.kuali.rice.core.api.config.property.Config;
20  import org.kuali.rice.core.api.config.property.ConfigContext;
21  import org.kuali.rice.core.api.impex.xml.XmlIngestionException;
22  import org.kuali.rice.coreservice.api.style.Style;
23  import org.kuali.rice.coreservice.api.style.StyleService;
24  import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator;
25  import org.kuali.rice.edl.impl.bo.EDocLiteAssociation;
26  import org.kuali.rice.edl.impl.bo.EDocLiteDefinition;
27  import org.kuali.rice.edl.impl.service.EDocLiteService;
28  import org.kuali.rice.edl.impl.service.EdlServiceLocator;
29  import org.kuali.rice.kew.test.KEWTestCase;
30  import org.kuali.rice.kew.test.TestUtilities;
31  import org.kuali.rice.test.BaselineTestCase;
32  
33  import javax.xml.transform.Templates;
34  import java.io.ByteArrayInputStream;
35  import java.io.FileNotFoundException;
36  import java.util.List;
37  
38  import static org.junit.Assert.*;
39  
40  /**
41   * Tests EDocLiteServiceImpl
42   * @author Kuali Rice Team (rice.collab@kuali.org)
43   */
44  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
45  public class EDocLiteServiceImplTest extends KEWTestCase {
46  
47  	@Test public void testLoadXML() throws FileNotFoundException {
48          loadXmlFile("EDocLiteContent.xml");
49          loadXmlFile("edlstyle.xml");
50  
51          EDocLiteService edls = EdlServiceLocator.getEDocLiteService();
52          StyleService styleService = CoreServiceApiServiceLocator.getStyleService();
53          //edls.loadXml(new FileInputStream("conf/examples/xml/EDocLiteContent.xml"));
54          assertTrue("Definition not found", edls.getEDocLiteDefinitions().contains("profile"));
55          Style defaultStyle = styleService.getStyle("Default");
56          assertNotNull("Style not found", defaultStyle);
57          assertEquals(1, edls.getEDocLiteAssociations().size());
58          EDocLiteDefinition def = edls.getEDocLiteDefinition("profile");
59          assertNotNull("'profile' definition not found", def);
60          assertEquals("profile", def.getName());
61          assertNotNull(def.getActiveInd());
62          assertTrue(def.getActiveInd().booleanValue());
63          Style style = styleService.getStyle("Default");
64          assertNotNull("'Default' style not found", style);
65          assertEquals("Default", style.getName());
66          assertTrue(style.isActive());
67          assertNotNull(style.getXmlContent());
68      }
69  
70      @Test public void testLoadBadDefinition() throws FileNotFoundException {
71          EDocLiteService edls = EdlServiceLocator.getEDocLiteService();
72          try {
73              edls.loadXml(TestUtilities.loadResource(getClass(), "BadDefinition.xml"), null);
74              fail("BadDefinition was successfully parsed.");
75          } catch (XmlIngestionException 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 = EdlServiceLocator.getEDocLiteService();
84          String defXml = "<edl></edl>";
85          try {
86              edls.saveEDocLiteDefinition(new ByteArrayInputStream(defXml.getBytes()));
87              fail("Storing edl with no name succeeded");
88          } catch (XmlIngestionException 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 testStoreAssociation() {
99          EDocLiteService edls = EdlServiceLocator.getEDocLiteService();
100         String assocXml = "<association></association>";
101         try {
102             edls.saveEDocLiteAssociation(new ByteArrayInputStream(assocXml.getBytes()));
103             fail("Storing association with no docType succeeded");
104         } catch (XmlIngestionException wsee) {
105             // expected due to lack of doctype
106         }
107         assocXml = "<association><docType></docType></association>";
108         try {
109             edls.saveEDocLiteAssociation(new ByteArrayInputStream(assocXml.getBytes()));
110             fail("Storing association with empty docType succeeded");
111         } catch (XmlIngestionException wsee) {
112             // expected due to emtpy doctype value
113         }
114         assocXml = "<association><docType>foobar</docType></association>";
115         edls.saveEDocLiteAssociation(new ByteArrayInputStream(assocXml.getBytes()));
116         EDocLiteAssociation assoc = edls.getEDocLiteAssociation("foobar");
117         assertNull("Inactive Association was found", assoc);
118 
119         assocXml = "<association><docType>foobar</docType><active>true</active></association>";
120         edls.saveEDocLiteAssociation(new ByteArrayInputStream(assocXml.getBytes()));
121         assoc = edls.getEDocLiteAssociation("foobar");
122         assertNotNull("Association was not found", assoc);
123         assertEquals("foobar", assoc.getEdlName());
124         assertNull(assoc.getDefinition());
125         assertNull(assoc.getStyle());
126 
127         List<EDocLiteAssociation> assocs = edls.getEDocLiteAssociations();
128         assertEquals(1, assocs.size());
129         assoc = assocs.get(0);
130         assertEquals("foobar", assoc.getEdlName());
131         assertNull(assoc.getDefinition());
132         assertNull(assoc.getStyle());
133         assertNotNull(assoc.getActiveInd());
134         assertTrue(assoc.getActiveInd().booleanValue());
135 
136         assocXml = "<association><style>style name</style><definition>definition name</definition><docType>foobar</docType><active>true</active></association>";
137         edls.saveEDocLiteAssociation(new ByteArrayInputStream(assocXml.getBytes()));
138         assoc = edls.getEDocLiteAssociation("foobar");
139         assertNotNull("Association was not found", assoc);
140         assertEquals("foobar", assoc.getEdlName());
141         assertEquals("definition name", assoc.getDefinition());
142         assertEquals("style name", assoc.getStyle());
143 
144         assocs = edls.getEDocLiteAssociations();
145         assertEquals(1, assocs.size());
146         assoc = (EDocLiteAssociation) assocs.get(0);
147         assertNotNull("Association was not found", assoc);
148         assertEquals("foobar", assoc.getEdlName());
149         assertEquals("definition name", assoc.getDefinition());
150         assertEquals("style name", assoc.getStyle());
151         assertNotNull(assoc.getActiveInd());
152         assertTrue(assoc.getActiveInd().booleanValue());
153     }
154 
155     /**
156      * Tests the caching of "styles" in EDocLiteServiceImpl.
157      *
158      * The style cache is really a cache of java.xml.transform.Templates objects which represent
159      * the "compiled" stylesheets.
160      */
161     @Test public void testStyleCaching() throws Exception {
162     	ConfigContext.getCurrentContextConfig().putProperty(Config.EDL_CONFIG_LOCATION, "classpath:org/kuali/rice/kew/edl/TestEDLConfig.xml");
163 
164     	loadXmlFile("EDocLiteContent.xml");
165         loadXmlFile("edlstyle.xml");
166         loadXmlFile("widgets.xml");
167 
168         // try to grab the templates out of the cache, it shouldn't be cached yet
169 //        Templates cachedTemplates = new EDocLiteServiceImpl().fetchTemplatesFromCache("Default");
170 //        assertNull("The default style template should not be cached yet.", cachedTemplates);
171 
172         // fetch the Templates object from the service
173         EDocLiteAssociation association = EdlServiceLocator.getEDocLiteService().getEDocLiteAssociation("EDocLiteDocType");
174         assertNull("We should be using the Default style.", association.getStyle());
175         Templates templates = EdlServiceLocator.getEDocLiteService().getStyleAsTranslet(association.getStyle());
176         assertNotNull("Templates should not be null.", templates);
177 
178         // the Templates should now be cached
179 //        cachedTemplates = new EDocLiteServiceImpl().fetchTemplatesFromCache("Default");
180 //        assertNotNull("Templates should now be cached.", cachedTemplates);
181 
182 //        // the cached Templates should be the same as the Templates we fetched from the service
183 //        assertEquals("Templates should be the same.", templates, cachedTemplates);
184 
185         // now re-import the style and the templates should no longer be cached
186         loadXmlFile("edlstyle.xml");
187 //        cachedTemplates = new EDocLiteServiceImpl().fetchTemplatesFromCache("Default");
188 //        assertNull("After re-import, the Default style Templates should no longer be cached.", cachedTemplates);
189 
190         // re-fetch the templates from the service and verify they are in the cache
191         Templates newTemplates = EdlServiceLocator.getEDocLiteService().getStyleAsTranslet(association.getStyle());
192         assertNotNull("Templates should not be null.", templates);
193 //        cachedTemplates = new EDocLiteServiceImpl().fetchTemplatesFromCache("Default");
194 //        assertNotNull("Templates should now be cached.", cachedTemplates);
195 
196         // lastly, check that the newly cached templates are not the same as the original templates
197         assertFalse("Old Templates should be different from new Templates.", templates.equals(newTemplates));
198 
199     }
200 }