Package org.switchyard.transform

Examples of org.switchyard.transform.Transformer


     * the java package containing the test class using this {@link org.switchyard.test.TestMixIn}.
     * @param xmlResPath The XML classpath resource used to bind to the {@code javaType}.  This path can be relative to
     * the java package containing the test class using this {@link org.switchyard.test.TestMixIn}.
     */
    public void testJavaXMLReadWrite(Class<?> javaType, String bindingConfigResPath, String xmlResPath) {
        Transformer xml2JavaTransformer = newTransformer(bindingConfigResPath, SmooksTransformType.XML2JAVA);
        Transformer java2xmlTransformer = newTransformer(bindingConfigResPath, SmooksTransformType.JAVA2XML);
        String xml = getTestKit().readResourceString(xmlResPath);

        // XML to Java...
        Object javaObject = xml2JavaTransformer.transform(xml);
        Assert.assertNotNull("XML to Java transformation failed.  null object returned.", javaObject);
        Assert.assertTrue("XML to Java transformation failed.  Wrong type returned.  Expected '" + javaType.getName() + "' but got '" + javaObject.getClass().getName() + "'.", javaType.isInstance(javaObject));
        if (_dumpResultsToSysout) {
            System.out.println("------------XML to Java Result:\n" + javaObject);
            System.out.println("-------------------------------");
        }

        // Java to XML...
        String xmlResult = (String) java2xmlTransformer.transform(javaObject);
        Assert.assertNotNull("Java to XML transformation failed.  null object returned.", javaObject);
        if (_dumpResultsToSysout) {
            System.out.println("------------Java to XML Result:\n" + xmlResult);
            System.out.println("-------------------------------");
        }
View Full Code Here


            smooks.createExecutionContext();
        } catch (Exception e) {
            throw TransformMessages.MESSAGES.failedToCreateSmooksInstance(config, e);
        }

        Transformer transformer;

        if (transformationType == SmooksTransformType.JAVA2XML) {
            transformer = newXMLBindingTransformer(from, to, smooks, XMLBindingTransformer.BindingDirection.JAVA2XML);
        } else if (transformationType == SmooksTransformType.XML2JAVA) {
            transformer = newXMLBindingTransformer(from, to, smooks, XMLBindingTransformer.BindingDirection.XML2JAVA);
        } else if (transformationType == SmooksTransformType.SMOOKS) {
            transformer = new SmooksTransformer(from, to, smooks, model);
        } else {
            throw TransformMessages.MESSAGES.unhandledSmooksTransformationType(transformationType.toString());
        }

        transformer.setFrom(model.getFrom());
        transformer.setTo(model.getTo());

        return transformer;
    }
View Full Code Here

                }
            }
        }

        if (transformerObject instanceof Transformer) {
            Transformer transformer = (Transformer) transformerObject;
            QName transFrom = transformer.getFrom();
            QName transTo = transformer.getTo();

            if (transFrom.equals(OBJECT_TYPE) && transTo.equals(OBJECT_TYPE)) {
                // Type info not specified on transformer, so assuming it's a generic/multi-type transformer...
                transformers.add(transformer);
            } else if ((fromIsWild || transFrom.equals(from)) && (toIsWild || transTo.equals(to))) {
                // Matching (specific) or wildcard type info specified...
                transformers.add(transformer);
            } else if (isAssignableFrom(transFrom, from) && isAssignableFrom(transTo, to)) {
                // Compatible Java types...
                transformers.add(transformer);
            }

            if (!fromIsWild) {
                transformer.setFrom(from);
            }
            if (!toIsWild) {
                transformer.setTo(to);
            }
        }

        if (transformers.isEmpty()) {
            throw TransformMessages.MESSAGES.classDoesNotSupportTransformation(transformerObject.getClass().getName(), from.toString(), to.toString());
View Full Code Here

        return false;
    }

    private static Transformer newTransformer(final Object transformerObject, final Method publicMethod, QName from, QName to) {
        Transformer transformer = new BaseTransformer(from, to) {
            @Override
            public Object transform(Object from) {
                try {
                    return publicMethod.invoke(transformerObject, from);
                } catch (InvocationTargetException e) {
View Full Code Here

        }
    }

    @Test
    public void test_StringResult() throws IOException, SAXException {
        Transformer transformer = getTransformer("sw-config-03.xml");

        XMLUnit.setIgnoreWhitespace(true);
        XMLAssert.assertXMLEqual("<order type='B' />", transformer.transform("<order type='A' />").toString());
    }
View Full Code Here

        XMLAssert.assertXMLEqual("<order type='B' />", transformer.transform("<order type='A' />").toString());
    }

    @Test
    public void test_JavaResult() throws IOException, SAXException {
        Transformer transformer = getTransformer("sw-config-04.xml");

        Person person = (Person) transformer.transform("<person name='Max' age='50' />");
        Assert.assertEquals("Max", person.getName());
        Assert.assertEquals(50, person.getAge());
    }
View Full Code Here

        Assert.assertEquals("Max", person.getName());
        Assert.assertEquals(50, person.getAge());
    }

    protected Transformer getTransformer(String config) throws IOException {
        Transformer transformer = super.getTransformer(config);

        if(!(transformer instanceof SmooksTransformer)) {
            Assert.fail("Not an instance of SmooksTransformer.");
        }
View Full Code Here

*/
public class XMLBindingTransformerTest extends AbstractTransformerTestCase {

    @Test
    public void test_xml2java() throws IOException, SAXException {
        Transformer transformer = getTransformer("sw-config-xmlb-01.xml");

        // XML to Java
        Person person = (Person) transformer.transform("<person name='Max' age='50' />");
        Assert.assertEquals("Max", person.getName());
        Assert.assertEquals(50, person.getAge());
    }
View Full Code Here

        Assert.assertEquals(50, person.getAge());
    }

    @Test
    public void test_java2xml() throws IOException, SAXException {
        Transformer transformer = getTransformer("sw-config-xmlb-02.xml");

        // Java to XML
        Person person = new Person().setName("George").setAge(80);
        String xml = (String) transformer.transform(person);
        XMLUnit.setIgnoreWhitespace(true);
        XMLAssert.assertXMLEqual("<person name='George' age='80' />", xml);
    }
View Full Code Here

        }
    }

    @Test
    public void test_java2xml_invalid_type() throws IOException, SAXException {
        Transformer transformer = getTransformer("sw-config-xmlb-02.xml");

        try {
            transformer.transform("wrong type");
        } catch (RuntimeException e) {
          boolean exceptionMatch = e.getMessage().contains("SWITCHYARD016806");
          Assert.assertTrue(exceptionMatch);
        }
    }
View Full Code Here

TOP

Related Classes of org.switchyard.transform.Transformer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.