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: package org.apache.commons.betwixt.io.read;
018:
019: import java.io.StringReader;
020: import java.io.StringWriter;
021:
022: import org.apache.commons.betwixt.AbstractTestCase;
023: import org.apache.commons.betwixt.io.BeanReader;
024: import org.apache.commons.betwixt.io.BeanWriter;
025:
026: /**
027: * @author <a href='http://jakarta.apache.org/commons'>Jakarta Commons Team</a>, <a href='http://www.apache.org'>Apache Software Foundation</a>
028: */
029: public class TestReadData extends AbstractTestCase {
030:
031: public TestReadData(String testName) {
032: super (testName);
033: }
034:
035: public void testReadInvalidDate() throws Exception {
036:
037: String xmlWithInvalidDate = "<?xml version='1.0'?>"
038: + "<AlertBean>" + " <message>Whatever</message>"
039: + " <summary>Sometime</summary>"
040: + " <timestamp>2004-13-32 00:00:00.0</timestamp>"
041: + "</AlertBean>";
042: StringReader invalidIn = new StringReader(xmlWithInvalidDate);
043:
044: String xmlWithValidDate = "<?xml version='1.0'?>"
045: + "<AlertBean>" + " <message>Whatever</message>"
046: + " <summary>Sometime</summary>"
047: + " <timestamp>1999-12-31 00:00:00.0</timestamp>"
048: + "</AlertBean>";
049: StringReader validIn = new StringReader(xmlWithValidDate);
050:
051: BeanReader reader = new BeanReader();
052: reader.registerBeanClass(AlertBean.class);
053: try {
054: AlertBean alterBean = (AlertBean) reader.parse(invalidIn);
055: fail("Invalid date so expected exception");
056: } catch (Exception e) {
057: // expected
058: }
059:
060: AlertBean alterBean = (AlertBean) reader.parse(validIn);
061: }
062:
063: public void testWritePrivateStaticClasses() throws Exception {
064: Nested nested = new Nested();
065: nested.setName("Timothy Taylor");
066: StringWriter out = new StringWriter();
067: out.write("<?xml version='1.0'?>");
068: BeanWriter writer = new BeanWriter(out);
069: writer.getXMLIntrospector().getConfiguration()
070: .setAttributesForPrimitives(false);
071: writer.getBindingConfiguration().setMapIDs(false);
072: writer.write("ale", nested);
073:
074: String expected = "<?xml version='1.0'?>"
075: + "<ale><name>Timothy Taylor</name></ale>";
076:
077: xmlAssertIsomorphic(parseString(out), parseString(expected),
078: true);
079: }
080:
081: // This test runs in Eclipse but not in maven :/
082: public void _testReadPrivateStaticClasses() throws Exception {
083:
084: StringReader in = new StringReader("<?xml version='1.0'?>"
085: + "<ale><name>Timothy Taylor</name></ale>");
086: BeanReader reader = new BeanReader();
087: reader.registerBeanClass("ale", Nested.class);
088: reader.getXMLIntrospector().getConfiguration()
089: .setAttributesForPrimitives(true);
090: Object out = reader.parse(in);
091: assertNotNull("Expected bean to be output", out);
092: assertEquals(
093: "Expected bean to be of type Nested",
094: "org.apache.commons.betwixt.io.read.TestReadData$Nested",
095: out.getClass().getName());
096: Nested bean = (Nested) out;
097: assertEquals("Expected name to be set", "Timothy Taylor", bean
098: .getName());
099: }
100:
101: private static class Nested {
102: private String name;
103:
104: public String getName() {
105: return name;
106: }
107:
108: public void setName(String name) {
109: this.name = name;
110: }
111:
112: }
113: }
|