alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Axis 2 example source code file (AbstractDerivedPopulater.java)

This example Axis 2 source code file (AbstractDerivedPopulater.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Axis 2 tags/keywords

bean, bytearrayoutputstream, class, class, compareomelemntserializations, exception, exception, io, javabean, method, object, object, omelement, propertydescriptor, reflection, string, string, xmltestcase

The Axis 2 AbstractDerivedPopulater.java source code

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.axis2.schema.populate.derived;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.lang.reflect.Method;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.util.StAXUtils;
import org.custommonkey.xmlunit.XMLTestCase;

public abstract class AbstractDerivedPopulater extends XMLTestCase {

    // force others to implement this method
    public abstract void testPopulate() throws Exception;

    /**
     * Simple reusable method to make object instances via reflection
     */
    protected Object process(String testString,String className) throws Exception{
        XMLStreamReader reader = StAXUtils.createXMLStreamReader(new ByteArrayInputStream(testString.getBytes()));
        Class clazz = Class.forName(className);
        Class[] declaredClasse = clazz.getDeclaredClasses();
        //ideally this should be 1
        Class innerClazz = declaredClasse[0];
        Method parseMethod = innerClazz.getMethod("parse",new Class[]{XMLStreamReader.class});
        Object obj = parseMethod.invoke(null,new Object[]{reader});
        assertNotNull(obj);

        return obj;

    }

    protected  String className= null;
    protected Class propertyClass = null;

    /**
     * check value function - may be overridden
     * @param xmlToSet
     * @param value
     * @throws Exception
     */
    protected void checkValue(String xmlToSet, String value) throws Exception {
        Object o = process(xmlToSet, className);
        Class beanClass = Class.forName(className);

        BeanInfo info = Introspector.getBeanInfo(beanClass);
        PropertyDescriptor[] propDescs = info.getPropertyDescriptors();
        for (int i = 0; i < propDescs.length; i++) {
            PropertyDescriptor propDesc = propDescs[i];

            if  (propDesc.getPropertyType().equals(propertyClass)){
                String s = convertToString(propDesc.getReadMethod().invoke(o,
                        (Object[]) null));
                compare(value,s);
            }
        }


        CompareOMElemntSerializations(o, xmlToSet);
    }

    /**
     * Compares serializations - may be overridden
     * @param o
     * @param xmlToSet
     * @throws Exception
     */
    protected void CompareOMElemntSerializations(Object o, String xmlToSet) throws Exception {
        OMElement element = getOMElement(o);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(baos);
        element.serialize(writer);
        writer.flush();

        assertXMLEqual(baos.toString(),xmlToSet);
    }

    /**
     * Get OM element via reflection
     * @param bean
     * @return
     * @throws Exception
     */
    protected OMElement getOMElement(Object bean) throws Exception {
        Method method = bean.getClass().getMethod("getOMElement", new Class[]{
                javax.xml.namespace.QName.class,
                org.apache.axiom.om.OMFactory.class});

        return (OMElement) method.invoke(bean, new Object[]{null, OMAbstractFactory.getOMFactory()});

    }

    /**
     * comapare method - may be overridden
     * @param val1
     * @param val2
     */
    protected void compare(String val1,String val2){
        assertEquals(val1,val2);
    }

    /**
     *
     * @param o
     * @return
     */
    protected String convertToString(Object o){
        return o.toString();
    }
}

Other Axis 2 examples (source code examples)

Here is a short list of links related to this Axis 2 AbstractDerivedPopulater.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.