001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.spring;
017:
018: import java.io.IOException;
019: import java.util.Collections;
020:
021: import javax.servlet.ServletConfig;
022:
023: import junit.framework.TestCase;
024:
025: import org.directwebremoting.extend.Converter;
026: import org.directwebremoting.extend.ConverterManager;
027: import org.directwebremoting.extend.InboundContext;
028: import org.directwebremoting.extend.InboundVariable;
029: import org.directwebremoting.extend.MarshallException;
030: import org.directwebremoting.impl.ContainerUtil;
031: import org.directwebremoting.impl.StartupUtil;
032: import org.directwebremoting.util.FakeServletConfig;
033: import org.directwebremoting.util.FakeServletContext;
034: import org.springframework.beans.factory.xml.DefaultNamespaceHandlerResolver;
035: import org.springframework.beans.factory.xml.NamespaceHandlerResolver;
036: import org.springframework.beans.factory.xml.PluggableSchemaResolver;
037: import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
038: import org.springframework.core.io.ClassPathResource;
039: import org.springframework.core.io.Resource;
040: import org.springframework.web.context.support.StaticWebApplicationContext;
041: import org.xml.sax.InputSource;
042:
043: /**
044: * @author Brendan Grainger
045: */
046: public class DwrNamesapceHandlerTests extends TestCase {
047:
048: private SpringContainer container;
049: private StaticWebApplicationContext webappContext;
050:
051: protected void setUp() throws Exception {
052:
053: ServletConfig servletConfig = new FakeServletConfig("test",
054: new FakeServletContext());
055:
056: webappContext = new StaticWebApplicationContext();
057: webappContext.setServletConfig(servletConfig);
058:
059: NamespaceHandlerResolver resolver = new DefaultNamespaceHandlerResolver(
060: getClass().getClassLoader());
061: XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(
062: webappContext);
063: reader.setNamespaceHandlerResolver(resolver);
064: reader
065: .setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);
066: reader.setEntityResolver(new DummySchemaResolver());
067: reader.loadBeanDefinitions(getResource());
068:
069: container = new SpringContainer();
070: container.setBeanFactory(webappContext);
071:
072: container.addParameter("debug", "true");
073: ContainerUtil.setupDefaultContainer(container, servletConfig);
074:
075: StartupUtil.initWebContext(servletConfig, servletConfig
076: .getServletContext(), null, container);
077: StartupUtil.initServerContext(servletConfig, servletConfig
078: .getServletContext(), container);
079:
080: // Configure default converters etc
081: ContainerUtil.configureFromSystemDwrXml(container);
082:
083: ContainerUtil
084: .configure(
085: container,
086: Collections
087: .singletonList(webappContext
088: .getBean(DwrNamespaceHandler.DEFAULT_SPRING_CONFIGURATOR_ID)));
089: ContainerUtil.publishContainer(container, servletConfig);
090:
091: ContainerUtil.debugConfig(container);
092: }
093:
094: /**
095: * Test that includes are processed.
096: *
097: * Syntax is:
098: * <pre>
099: * <dwr:convert type="bean" class="org.directwebremoting.spring.TestBeanIncludeMethods" >
100: * <dwr:include method="includeMethod1" />
101: * <dwr:include method="includeMethod2" />
102: * </dwr:convert>
103: * </pre>
104: */
105: public void testIncludesWithIncludedMethod() {
106: ConverterManager converterManager = (ConverterManager) container
107: .getBean(ConverterManager.class.getName());
108: assertNotNull(converterManager);
109:
110: Converter converter = converterManager
111: .getConverterByMatchString("org.directwebremoting.spring.TestIncludesBean");
112: assertNotNull(converter);
113:
114: // Check that the included method
115: InboundContext inctx = new InboundContext();
116: inctx.createInboundVariable(0, "c0-e1", "string", "included");
117: inctx.createInboundVariable(0, "c0-e2", "string", "excluded");
118:
119: InboundVariable iv = new InboundVariable(inctx, "c0-param0",
120: "Object_Object",
121: "{includedProperty:reference:c0-e1,notIncludedProperty:reference:c0-e2}");
122: try {
123: TestIncludesBean tbi = (TestIncludesBean) converter
124: .convertInbound(TestIncludesBean.class, iv, inctx);
125: assertNotNull(tbi.getIncludedProperty());
126: assertNull(tbi.getNotIncludedProperty());
127: } catch (MarshallException ex) {
128: fail("Included method failed to be marshalled correctly");
129: }
130: }
131:
132: /**
133: * Test that excludes are processed.
134: *
135: * Syntax is:
136: * <pre>
137: * <dwr:convert type="bean" class="org.directwebremoting.spring.TestBeanIncludeMethods" >
138: * <dwr:exclude method="excludeMethod1" />
139: * <dwr:exclude method="excludeMethod2" />
140: * </dwr:convert>
141: * </pre>
142: */
143: public void testExcludes() {
144: ConverterManager converterManager = (ConverterManager) container
145: .getBean(ConverterManager.class.getName());
146: assertNotNull(converterManager);
147:
148: Converter converter = converterManager
149: .getConverterByMatchString("org.directwebremoting.spring.TestExcludesBean");
150: assertNotNull(converter);
151:
152: // Check that the included method
153: InboundContext inctx = new InboundContext();
154: inctx.createInboundVariable(0, "c0-e1", "string", "excluded");
155: inctx.createInboundVariable(0, "c0-e2", "string", "included");
156: InboundVariable iv = new InboundVariable(inctx, "c0-param0",
157: "Object_Object",
158: "{excludedProperty:reference:c0-e1, notExcludedProperty:reference:c0-e2}");
159:
160: try {
161: TestExcludesBean tbi = (TestExcludesBean) converter
162: .convertInbound(TestExcludesBean.class, iv, inctx);
163: assertNull(tbi.getExcludedProperty());
164: assertNotNull(tbi.getNotExcludedProperty());
165: } catch (MarshallException ex) {
166: fail("Included method failed to be marshalled correctly");
167: }
168: }
169:
170: /**
171: * Test that:
172: * <pre>
173: * <dwr:signature class="com.example.Check" >
174: * <dwr:data>
175: * <![CDATA[
176: * import java.util.List;
177: * import org.directwebremoting.spring.Check;
178: * Check.setLotteryResults(List<Integer> nos);
179: * ]]>
180: * </dwr:data>
181: *
182: * </dwr:signature>
183: * </pre>
184: * is parsed correctly
185: *
186: * @throws Exception
187: */
188: public void testParseSignatures() throws Exception {
189: SpringConfigurator config = (SpringConfigurator) webappContext
190: .getBean(DwrNamespaceHandler.DEFAULT_SPRING_CONFIGURATOR_ID);
191: assertNotNull(config.getSignatures());
192:
193: InboundContext inctx = new InboundContext();
194:
195: inctx.createInboundVariable(0, "c0-e1", "Array",
196: "[reference:c0-e2,reference:c0-e3]");
197: inctx.createInboundVariable(0, "c0-e2", "number", "1");
198: inctx.createInboundVariable(0, "c0-e3", "number", "2");
199: InboundVariable iv = new InboundVariable(inctx, "c0-param0",
200: "Object_Object", "{lotteryResults:reference:c0-e1}");
201:
202: try {
203: ConverterManager converterManager = (ConverterManager) container
204: .getBean(ConverterManager.class.getName());
205: Converter converter = converterManager
206: .getConverterByMatchString("org.directwebremoting.spring.Check");
207: assertNotNull(converter);
208: Check tbi = (Check) converter.convertInbound(Check.class,
209: iv, inctx);
210: assertEquals(Integer.class, tbi.getLotteryResults().get(0)
211: .getClass());
212: } catch (MarshallException ex) {
213: fail("Included method failed to be marshalled correctly");
214: }
215:
216: }
217:
218: private Resource getResource() {
219: return new ClassPathResource("dwr-beans.xml", getClass());
220: }
221:
222: private class DummySchemaResolver extends PluggableSchemaResolver {
223:
224: /**
225: *
226: */
227: public DummySchemaResolver() {
228: super (DwrNamesapceHandlerTests.this .getClass()
229: .getClassLoader());
230: }
231:
232: public InputSource resolveEntity(String publicId,
233: String systemId) throws IOException {
234: InputSource source = super
235: .resolveEntity(publicId, systemId);
236: if (source == null) {
237: Resource resource = new ClassPathResource(
238: "org/directwebremoting/spring/spring-dwr-2.0.xsd");
239: source = new InputSource(resource.getInputStream());
240: source.setPublicId(publicId);
241: source.setSystemId(systemId);
242: }
243: return source;
244: }
245: }
246: }
|