001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.schema.populate.simple;
020:
021: import junit.framework.TestCase;
022:
023: import javax.xml.stream.XMLInputFactory;
024: import javax.xml.stream.XMLStreamReader;
025: import javax.xml.stream.XMLStreamWriter;
026: import javax.xml.stream.XMLOutputFactory;
027: import java.beans.BeanInfo;
028: import java.beans.Introspector;
029: import java.beans.PropertyDescriptor;
030: import java.io.ByteArrayInputStream;
031: import java.io.ByteArrayOutputStream;
032: import java.lang.reflect.Method;
033:
034: import org.apache.axiom.om.util.StAXUtils;
035: import org.apache.axiom.om.OMElement;
036: import org.apache.axiom.om.OMAbstractFactory;
037: import org.custommonkey.xmlunit.XMLTestCase;
038:
039: public abstract class AbstractSimplePopulater extends XMLTestCase {
040:
041: // force others to implement this method
042: public abstract void testPopulate() throws Exception;
043:
044: protected String className = null;
045: protected Class propertyClass = null;
046:
047: /**
048: * Simple reusable method to make object instances via reflection
049: * @param testString
050: * @param className
051: * @return
052: * @throws Exception
053: */
054: protected Object process(String testString, String className)
055: throws Exception {
056: XMLStreamReader reader = StAXUtils
057: .createXMLStreamReader(new ByteArrayInputStream(
058: testString.getBytes()));
059: Class clazz = Class.forName(className);
060: Class[] declaredClasse = clazz.getDeclaredClasses();
061: //ideally this should be 1
062: Class innerClazz = declaredClasse[0];
063: Method parseMethod = innerClazz.getMethod("parse",
064: new Class[] { XMLStreamReader.class });
065: Object obj = parseMethod.invoke(null, new Object[] { reader });
066: assertNotNull(obj);
067:
068: return obj;
069:
070: }
071:
072: /**
073: * Simple method to compare. May be overridden
074: * @param xmlToSet
075: * @param value
076: * @throws Exception
077: */
078: protected void checkValue(String xmlToSet, String value)
079: throws Exception {
080: Object o = process(xmlToSet, className);
081: Class beanClass = Class.forName(className);
082: BeanInfo info = Introspector.getBeanInfo(beanClass);
083: PropertyDescriptor[] propDescs = info.getPropertyDescriptors();
084: for (int i = 0; i < propDescs.length; i++) {
085: PropertyDescriptor propDesc = propDescs[i];
086: if (propDesc.getPropertyType().equals(propertyClass)) {
087: String s = convertToString(propDesc.getReadMethod()
088: .invoke(o, (Object[]) null));
089: compare(value, s);
090: }
091:
092: }
093:
094: }
095:
096: /**
097: * Compares serializations - may be overridden
098: * @param o
099: * @param xmlToSet
100: * @throws Exception
101: */
102: protected void CompareOMElemntSerializations(Object o,
103: String xmlToSet) throws Exception {
104: OMElement element = getOMElement(o);
105: ByteArrayOutputStream baos = new ByteArrayOutputStream();
106: XMLStreamWriter writer = XMLOutputFactory.newInstance()
107: .createXMLStreamWriter(baos);
108: element.serialize(writer);
109: writer.flush();
110:
111: assertXMLEqual(baos.toString(), xmlToSet);
112: }
113:
114: /**
115: * Get OM element via reflection
116: * @param bean
117: * @return
118: * @throws Exception
119: */
120: protected OMElement getOMElement(Object bean) throws Exception {
121: Method method = bean.getClass().getMethod(
122: "getOMElement",
123: new Class[] { javax.xml.namespace.QName.class,
124: org.apache.axiom.om.OMFactory.class });
125:
126: return (OMElement) method.invoke(bean, new Object[] { null,
127: OMAbstractFactory.getOMFactory() });
128:
129: }
130:
131: /**
132: * value comparisom
133: * @param val1
134: * @param val2
135: */
136: protected void compare(String val1, String val2) {
137: assertEquals(val1, val2);
138: }
139:
140: /**
141: * conversion - may be overriddens
142: * @param o
143: * @return
144: */
145: protected String convertToString(Object o) {
146: return o.toString();
147: }
148: }
|