001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019: package de.schlund.pfixcore.oxm;
020:
021: import java.io.File;
022: import java.io.FileInputStream;
023: import java.io.FileNotFoundException;
024: import java.io.InputStream;
025: import java.net.URI;
026: import java.util.ArrayList;
027: import java.util.HashMap;
028: import java.util.List;
029: import java.util.Map;
030: import java.util.Properties;
031:
032: import javax.xml.transform.Result;
033: import javax.xml.transform.dom.DOMResult;
034:
035: import junit.framework.TestCase;
036:
037: import org.apache.log4j.ConsoleAppender;
038: import org.apache.log4j.Level;
039: import org.apache.log4j.Logger;
040: import org.apache.log4j.PatternLayout;
041: import org.w3c.dom.Document;
042:
043: import de.schlund.pfixcore.beans.BeanDescriptorFactory;
044: import de.schlund.pfixcore.oxm.helper.OxmTestHelper;
045: import de.schlund.pfixcore.oxm.impl.MarshallerImpl;
046: import de.schlund.pfixcore.oxm.impl.SerializerRegistry;
047: import de.schlund.pfixxml.util.XMLUtils;
048:
049: /**
050: * @author mleidig@schlund.de
051: * @author Stephan Schmidt <schst@stubbles.net>
052: */
053: public class Test extends TestCase {
054:
055: @Override
056: protected void setUp() throws Exception {
057: ConsoleAppender appender = new ConsoleAppender(
058: new PatternLayout("%p: %m\n"));
059: Logger logger = Logger.getRootLogger();
060: logger.setLevel((Level) Level.WARN);
061: logger.removeAllAppenders();
062: logger.addAppender(appender);
063: }
064:
065: public void testEnums() {
066:
067: BeanDescriptorFactory bdf = new BeanDescriptorFactory();
068: SerializerRegistry reg = new SerializerRegistry(bdf);
069: Marshaller m = new MarshallerImpl(reg);
070:
071: // Bean with enums
072:
073: EnumTestBean bean = new EnumTestBean();
074: bean.setText("abc");
075: bean.setDay(WeekDay.FRIDAY);
076: bean.setPrio(EnumTestBean.Priority.HIGH);
077:
078: Document doc = OxmTestHelper.createResultDocument();
079: Result res = new DOMResult(doc);
080: m.marshal(bean, res);
081: String expected = "<result prio=\"HIGH\" text=\"abc\"><day name=\"FRIDAY\" workingHours=\"6\"/></result>";
082: Document expDoc = OxmTestHelper.createDocument(expected);
083: XMLUtils.assertEquals(expDoc, doc);
084:
085: // List with enums
086:
087: List<Enum<?>> elist = new ArrayList<Enum<?>>();
088: elist.add(WeekDay.MONDAY);
089: elist.add(EnumTestBean.Priority.LOW);
090:
091: doc = OxmTestHelper.createResultDocument();
092: res = new DOMResult(doc);
093: m.marshal(elist, res);
094: expected = "<result><weekDay workingHours=\"8\" name=\"MONDAY\"/><priority>LOW</priority></result>";
095: expDoc = OxmTestHelper.createDocument(expected);
096: XMLUtils.assertEquals(expDoc, doc);
097: }
098:
099: public void testComplex() throws Exception {
100:
101: BeanDescriptorFactory bdf = new BeanDescriptorFactory();
102: SerializerRegistry reg = new SerializerRegistry(bdf);
103: Marshaller m = new MarshallerImpl(reg);
104:
105: ComplexTestBean bean = new ComplexTestBean();
106: bean.setBoolVal(true);
107: bean.setIntVal(1);
108: bean.setFloatVal(new Float(2.2));
109: bean.setStrVal("aaa");
110: bean.setBooleanArray(new Boolean[] { Boolean.TRUE,
111: Boolean.FALSE });
112: bean.type = ComplexTestBean.class;
113: bean.file = new File("/tmp");
114: Properties props = new Properties();
115: props.setProperty("key1", "val1");
116: props.setProperty("key2", "val2");
117: bean.props = props;
118: bean.uri = new URI("http://pustefix-framework.org");
119:
120: ComplexTestBean bean1 = new ComplexTestBean();
121: bean1.setBoolVal(false);
122: bean1.setIntVal(2);
123: bean1.setFloatVal(new Float(3.3));
124: bean1.setStrVal("bbb");
125: bean1.setBooleanArray(new Boolean[] { Boolean.TRUE,
126: Boolean.TRUE });
127:
128: ComplexTestBean bean2 = new ComplexTestBean();
129: bean2.setBoolVal(true);
130: bean2.setIntVal(3);
131: bean2.setFloatVal(new Float(4.4));
132: bean2.setStrVal("ccc");
133: bean2.setBooleanArray(new Boolean[] { Boolean.FALSE,
134: Boolean.FALSE });
135:
136: List<ComplexTestBean> beanList = new ArrayList<ComplexTestBean>();
137: beanList.add(bean1);
138: beanList.add(bean2);
139: bean.setChildList(beanList);
140:
141: Map<String, ComplexTestBean> beanMap = new HashMap<String, ComplexTestBean>();
142: beanMap.put(bean2.getStrVal(), bean2);
143: beanMap.put(bean1.getStrVal(), bean1);
144: bean.setChildMap(beanMap);
145:
146: Document expDoc = OxmTestHelper
147: .createDocument(getInputStream("testcomplex.xml"));
148: XMLUtils.stripWhitespace(expDoc);
149: Document doc = OxmTestHelper.createResultDocument();
150: Result res = new DOMResult(doc);
151: m.marshal(bean, res);
152: XMLUtils.assertEquals(expDoc, doc);
153: }
154:
155: public void testFragment() throws Exception {
156: BeanDescriptorFactory bdf = new BeanDescriptorFactory();
157: SerializerRegistry reg = new SerializerRegistry(bdf);
158: Marshaller m = new MarshallerImpl(reg);
159:
160: FragmentBean fBean = new FragmentBean();
161:
162: Document expDoc = OxmTestHelper
163: .createDocument(getInputStream("testfragment.xml"));
164: XMLUtils.stripWhitespace(expDoc);
165: Document doc = OxmTestHelper.createResultDocument();
166: Result res = new DOMResult(doc);
167: m.marshal(fBean, res);
168: XMLUtils.assertEquals(expDoc, doc);
169: }
170:
171: public void testForceElement() throws Exception {
172: BeanDescriptorFactory bdf = new BeanDescriptorFactory();
173: SerializerRegistry reg = new SerializerRegistry(bdf);
174: Marshaller m = new MarshallerImpl(reg);
175:
176: ForceElementTestBean bean = new ForceElementTestBean();
177:
178: Document doc = OxmTestHelper.createResultDocument();
179: Result res = new DOMResult(doc);
180: m.marshal(bean, res);
181:
182: String expected = "<result><foo>foo</foo><baz>bar</baz><openingDate>2008-03-02 18:43:12</openingDate></result>";
183: Document expDoc = OxmTestHelper.createDocument(expected);
184:
185: XMLUtils.assertEquals(expDoc, doc);
186: }
187:
188: private InputStream getInputStream(String fileName) {
189: InputStream in = getClass().getClassLoader()
190: .getResourceAsStream(
191: "de/schlund/pfixcore/oxm/" + fileName);
192: if (in == null) {
193: try {
194: in = new FileInputStream(
195: "tests/junit/de/schlund/pfixcore/oxm/"
196: + fileName);
197: } catch (FileNotFoundException x) {
198: throw new RuntimeException(x);
199: }
200: }
201: return in;
202: }
203: }
|