All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.ws.jaxme.junit.PrintParseTest Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2003, 2004  The Apache Software Foundation
 * 
 * Licensed 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.ws.jaxme.junit;

import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import org.apache.ws.jaxme.impl.JMMarshallerImpl;
import org.apache.ws.jaxme.tests.printparse.Test;
import org.apache.ws.jaxme.tests.printparse.impl.TestImpl;
import org.xml.sax.InputSource;

import junit.framework.TestCase;


/** Test case for the
 * printMethod and parseMethod
 * attributes in jaxb:javaType.
 */
public class PrintParseTest extends TestCase {
	private String getNamespace() {
		TestImpl test = new TestImpl();
        return test.getQName().getNamespaceURI();
    }

    private String getPackageName() {
		String testClassName = Test.class.getName();
        int offset = testClassName.lastIndexOf('.');
        return testClassName.substring(0, offset);
    }

    private JAXBContext getJAXBContext() throws JAXBException {
    	return JAXBContext.newInstance(getPackageName());
    }

    /** Tests the use of jaxb:javaType/@print.
     */
    public void testPrint() throws Exception {
    	boolean[] bools = new boolean[]{false, true};
        int[] ints = new int[]{0,1};
        for (int i = 0;  i < bools.length;  i++) {
            Test test = new TestImpl();
            test.setBool(bools[i]);
            StringWriter sw = new StringWriter();
            Marshaller m = getJAXBContext().createMarshaller();
            m.setProperty(JMMarshallerImpl.JAXME_XML_DECLARATION, Boolean.FALSE);
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
            m.marshal(test, sw);
            String expect =
              "" + ints[i] +
              "";
            String got = sw.toString();
            assertEquals(expect, got);
        }
    }

    /** Tests the use of jaxb:javaType/@parse.
     */
    public void testParse() throws Exception {
        boolean[] bools = new boolean[]{false, true};
        int[] ints = new int[]{0,1};
        for (int i = 0;  i < bools.length;  i++) {
            String input =
                "" + ints[i] +
                "";
            Unmarshaller u = getJAXBContext().createUnmarshaller();
            Test test = (Test) u.unmarshal(new InputSource(new StringReader(input)));
            assertEquals(bools[i], test.isBool());
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy