Tips and tricks for using JiBX
start > usage-questions > junit-test-bindings

junit-test-bindings

Created by dsosnoski. Last edited by dsosnoski, one year and 293 days ago. Viewed 2,049 times. #2
First thing I can't seem to find is how to unit test my bindings. No solutions in this list or out on the net that I can find.

This is a tricky issue, assuming you want to compile the bindings as part of your JUnit test (rather than running the binding compiler ahead of time). I'm doing this in the tests of new code going into the JiBX framework, including the schema model. Here's the base test class code to run the binding compiler as part of the class initialization, and to access the binding for use by tests:

public class SchemaTestBase extends TestCase
{
    private static final String SCHEMA_CLASS =
        "org.jibx.schema.elements.SchemaElement";
    private static final IBindingFactory m_bindingFactory;
    static {
        try {

// set paths to be used for loading referenced classes URL[] urls = Loader.getClassPaths(); String[] paths = new String[urls.length]; for (int i = 0; i < urls.length; i++) { paths[i] = urls[i].getFile(); } ClassCache.setPaths(paths); ClassFile.setPaths(paths);

// find the binding definition ClassLoader loader = SchemaTestBase.class.getClassLoader(); InputStream is = loader.getResourceAsStream("org/jibx/schema/binding.xml"); if (is == null) { throw new RuntimeException("Schema binding definition not found"); }

// process the binding BoundClass.reset(); MungedClass.reset(); BindingDefinition.reset(); BindingDefinition def = Utility.loadBinding("binding.xml", "binding", is, null, true); def.generateCode(false);

// output the modified class files MungedClass.fixChanges(true);

// look up the mapped class and associated binding factory Class mclas = Class.forName(SCHEMA_CLASS); m_bindingFactory = BindingDirectory.getFactory(mclas);

} catch (JiBXException e) { throw new RuntimeException("JiBXException: " + e.getMessage()); } catch (IOException e) { throw new RuntimeException("IOException: " + e.getMessage()); } catch (ClassNotFoundException e) { throw new RuntimeException("ClassNotFoundException: " + e.getMessage()); } } ...

/** * Read a schema definition into model from stream. * * @param is schema input stream * @return schema element * @throws Exception */ protected SchemaElement readSchema(InputStream is) throws Exception { IUnmarshallingContext ictx = m_bindingFactory.createUnmarshallingContext(); return (SchemaElement)ictx.unmarshalDocument(is, null); }

/** * Read a schema definition into model from string. * * @param text customization document text * @return root element * @throws Exception */ protected SchemaElement readSchema(String text) throws Exception { return readSchema(new ByteArrayInputStream(text.getBytes("utf-8"))); }

I have my sample documents as strings within the actual test case classes:

public static final String COMPLEX_RESTRICTION_SCHEMA1 =
        "<schema targetNamespace='urn:anything'" +
        "    xmlns='http://www.w3.org/2001/XMLSchema'" +
        "    elementFormDefault='qualified'>n" +
        "  <simpleType name='simple'>n" +
        "    <restriction base='string'>n" +
        "      <length value='5'/>n" +
        "      <whitespace value='replace'/>n" +
        "    </restriction>n" +
        "  </simpleType>n" +
        "</schema>";

This isn't bulletproof, but works for my testing in Eclipse (at least most of the time - I've got some other new code where I'm doing what I think is the same thing, and that fails to find the binding about every other time I run it, for no reason I've been able to find so far). Hopefully it'll get you started.

no comments
snipsnap.org | Copyright 2000-2002 Matthias L. Jugel and Stephan J. Schmidt