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.derived;
020:
021: import java.beans.BeanInfo;
022: import java.beans.Introspector;
023: import java.beans.PropertyDescriptor;
024: import java.io.ByteArrayInputStream;
025: import java.io.ByteArrayOutputStream;
026: import java.lang.reflect.Method;
027:
028: import javax.xml.stream.XMLOutputFactory;
029: import javax.xml.stream.XMLStreamReader;
030: import javax.xml.stream.XMLStreamWriter;
031:
032: import org.apache.axiom.om.OMAbstractFactory;
033: import org.apache.axiom.om.OMElement;
034: import org.apache.axiom.om.util.StAXUtils;
035: import org.custommonkey.xmlunit.XMLTestCase;
036:
037: public abstract class AbstractDerivedPopulater extends XMLTestCase {
038:
039: // force others to implement this method
040: public abstract void testPopulate() throws Exception;
041:
042: /**
043: * Simple reusable method to make object instances via reflection
044: */
045: protected Object process(String testString, String className)
046: throws Exception {
047: XMLStreamReader reader = StAXUtils
048: .createXMLStreamReader(new ByteArrayInputStream(
049: testString.getBytes()));
050: Class clazz = Class.forName(className);
051: Class[] declaredClasse = clazz.getDeclaredClasses();
052: //ideally this should be 1
053: Class innerClazz = declaredClasse[0];
054: Method parseMethod = innerClazz.getMethod("parse",
055: new Class[] { XMLStreamReader.class });
056: Object obj = parseMethod.invoke(null, new Object[] { reader });
057: assertNotNull(obj);
058:
059: return obj;
060:
061: }
062:
063: protected String className = null;
064: protected Class propertyClass = null;
065:
066: /**
067: * check value function - may be overridden
068: * @param xmlToSet
069: * @param value
070: * @throws Exception
071: */
072: protected void checkValue(String xmlToSet, String value)
073: throws Exception {
074: Object o = process(xmlToSet, className);
075: Class beanClass = Class.forName(className);
076:
077: BeanInfo info = Introspector.getBeanInfo(beanClass);
078: PropertyDescriptor[] propDescs = info.getPropertyDescriptors();
079: for (int i = 0; i < propDescs.length; i++) {
080: PropertyDescriptor propDesc = propDescs[i];
081:
082: if (propDesc.getPropertyType().equals(propertyClass)) {
083: String s = convertToString(propDesc.getReadMethod()
084: .invoke(o, (Object[]) null));
085: compare(value, s);
086: }
087: }
088:
089: CompareOMElemntSerializations(o, xmlToSet);
090: }
091:
092: /**
093: * Compares serializations - may be overridden
094: * @param o
095: * @param xmlToSet
096: * @throws Exception
097: */
098: protected void CompareOMElemntSerializations(Object o,
099: String xmlToSet) throws Exception {
100: OMElement element = getOMElement(o);
101: ByteArrayOutputStream baos = new ByteArrayOutputStream();
102: XMLStreamWriter writer = XMLOutputFactory.newInstance()
103: .createXMLStreamWriter(baos);
104: element.serialize(writer);
105: writer.flush();
106:
107: assertXMLEqual(baos.toString(), xmlToSet);
108: }
109:
110: /**
111: * Get OM element via reflection
112: * @param bean
113: * @return
114: * @throws Exception
115: */
116: protected OMElement getOMElement(Object bean) throws Exception {
117: Method method = bean.getClass().getMethod(
118: "getOMElement",
119: new Class[] { javax.xml.namespace.QName.class,
120: org.apache.axiom.om.OMFactory.class });
121:
122: return (OMElement) method.invoke(bean, new Object[] { null,
123: OMAbstractFactory.getOMFactory() });
124:
125: }
126:
127: /**
128: * comapare method - may be overridden
129: * @param val1
130: * @param val2
131: */
132: protected void compare(String val1, String val2) {
133: assertEquals(val1, val2);
134: }
135:
136: /**
137: *
138: * @param o
139: * @return
140: */
141: protected String convertToString(Object o) {
142: return o.toString();
143: }
144: }
|