001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.betwixt.io.read;
019:
020: import java.io.StringReader;
021: import java.util.List;
022:
023: import junit.framework.Test;
024: import junit.framework.TestSuite;
025:
026: import org.apache.commons.betwixt.AbstractTestCase;
027: import org.apache.commons.betwixt.BindingConfiguration;
028: import org.apache.commons.betwixt.ElementDescriptor;
029: import org.apache.commons.betwixt.XMLIntrospector;
030: import org.apache.commons.betwixt.io.BeanReader;
031:
032: /**
033: * Test harness for Mapping Actions.
034: *
035: * @author Robert Burrell Donkin
036: * @version $Id: TestMappingActions.java 438373 2006-08-30 05:17:21Z bayard $
037: */
038: public class TestMappingActions extends AbstractTestCase {
039:
040: public TestMappingActions(String name) {
041: super (name);
042: }
043:
044: public static Test suite() {
045: return new TestSuite(TestMappingActions.class);
046: }
047:
048: public void testSimpleRead() throws Exception {
049:
050: String xml = "<?xml version='1.0'?><AddressBean><street>1 Main Street</street><city>New Town</city>"
051: + "<code>NT1 1AA</code><country>UK</country></AddressBean>";
052:
053: //SimpleLog log = new SimpleLog("[test]");
054: //log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
055: //BeanRuleSet.setLog(log);
056: BeanReader reader = new BeanReader();
057: reader.registerBeanClass(AddressBean.class);
058: AddressBean address = (AddressBean) reader
059: .parse(new StringReader(xml));
060:
061: assertFalse("Address is mapped", address == null);
062: assertEquals("Street", "1 Main Street", address.getStreet());
063: assertEquals("City", "New Town", address.getCity());
064: assertEquals("Code", "NT1 1AA", address.getCode());
065: assertEquals("Country", "UK", address.getCountry());
066: }
067:
068: public void testPrimitiveCollective() throws Exception {
069:
070: String xml = "<?xml version='1.0'?><SimpleStringCollective><strings>"
071: + "<string>one</string><string>two</string><string>three</string>"
072: + "</strings></SimpleStringCollective>";
073:
074: //SimpleLog log = new SimpleLog("[test]");
075: //log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
076: // BeanRuleSet.setLog(log);
077: BeanReader reader = new BeanReader();
078: reader.registerBeanClass(SimpleStringCollective.class);
079: SimpleStringCollective collective = (SimpleStringCollective) reader
080: .parse(new StringReader(xml));
081:
082: assertFalse("SimpleStringCollective mapped", collective == null);
083: List strings = collective.getStrings();
084: assertEquals("String count", 3, strings.size());
085: assertEquals("First string", "one", strings.get(0));
086: assertEquals("Second string", "two", strings.get(1));
087: assertEquals("Third string", "three", strings.get(2));
088: }
089:
090: public void testBodyUpdateActionNoMatch() throws Exception {
091: AddressBean bean = new AddressBean();
092: bean.setStreet("DEFAULT");
093: bean.setCode("DEFAULT");
094: bean.setCountry("DEFAULT");
095:
096: XMLIntrospector introspector = new XMLIntrospector();
097: ElementDescriptor elementDescriptor = introspector.introspect(
098: AddressBean.class).getElementDescriptor();
099:
100: ReadContext context = new ReadContext(
101: new BindingConfiguration(), new ReadConfiguration());
102: context.setBean(bean);
103: context.markClassMap(AddressBean.class);
104: context.pushElement("NoMatch");
105: context.setXMLIntrospector(introspector);
106: SimpleTypeBindAction action = new SimpleTypeBindAction();
107: action.body("Street value", context);
108: assertEquals("Street is unset", "DEFAULT", bean.getStreet());
109: assertEquals("Country is unset", "DEFAULT", bean.getCountry());
110: assertEquals("Code is unset", "DEFAULT", bean.getCode());
111: }
112:
113: public void testBodyUpdateActionMatch() throws Exception {
114: AddressBean bean = new AddressBean();
115: bean.setStreet("DEFAULT");
116: bean.setCode("DEFAULT");
117: bean.setCountry("DEFAULT");
118:
119: XMLIntrospector introspector = new XMLIntrospector();
120: ReadContext context = new ReadContext(
121: new BindingConfiguration(), new ReadConfiguration());
122: context.pushBean(bean);
123: context.markClassMap(AddressBean.class);
124: context.pushElement("street");
125: context.setXMLIntrospector(introspector);
126: SimpleTypeBindAction action = new SimpleTypeBindAction();
127: action.body("Street value", context);
128: assertEquals("Street is set", "Street value", bean.getStreet());
129: assertEquals("Country is unset", "DEFAULT", bean.getCountry());
130: assertEquals("Code is unset", "DEFAULT", bean.getCode());
131: }
132:
133: public void testCollection() throws Exception {
134: String xml = "<?xml version='1.0'?>"
135: + "<elements><element><value>alpha</value></element></elements>";
136: StringReader in = new StringReader(xml);
137: BeanReader reader = new BeanReader();
138: reader.registerBeanClass(Elements.class);
139: Elements result = (Elements) reader.parse(in);
140: assertNotNull("Element alpha exists", result
141: .getElement("alpha"));
142: }
143: }
|