net.javacrumbs.springws.test.template.FreeMarkerTemplateProcessorTest.java Source code

Java tutorial

Introduction

Here is the source code for net.javacrumbs.springws.test.template.FreeMarkerTemplateProcessorTest.java

Source

/**
 * Copyright 2009-2010 the original author or authors.
 *
 * 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 net.javacrumbs.springws.test.template;

import static org.junit.Assert.assertTrue;

import java.io.IOException;

import net.javacrumbs.springws.test.AbstractMessageTest;
import net.javacrumbs.springws.test.context.WsTestContextHolder;

import org.custommonkey.xmlunit.Diff;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.ws.WebServiceMessage;
import org.w3c.dom.Document;

public class FreeMarkerTemplateProcessorTest extends AbstractMessageTest {
    private FreeMarkerTemplateProcessor processor;

    @Before
    public void setUp() throws Exception {
        processor = new FreeMarkerTemplateProcessor();
        processor.setResourceLoader(new DefaultResourceLoader());
        processor.afterPropertiesSet();
    }

    @Test
    public void testTemplate() throws IOException {
        WsTestContextHolder.getTestContext().setAttribute("number", 2);
        WebServiceMessage request = createMessage("xml/valid-message2.xml");
        ClassPathResource template = new ClassPathResource("mock-responses/test/freemarker-response.xml");
        Resource resource = processor.processTemplate(template, request);

        Document controlDocument = getXmlUtil()
                .loadDocument(new ClassPathResource("xml/resolved-different-response.xml"));
        Document responseDocument = getXmlUtil().loadDocument(resource);
        Diff diff = new Diff(controlDocument, responseDocument);
        assertTrue(diff.toString(), diff.similar());

        WsTestContextHolder.getTestContext().clear();
    }

    @Test
    public void testIgnore() throws IOException {
        WebServiceMessage request = createMessage("xml/valid-message2.xml");
        ClassPathResource template = new ClassPathResource("xml/control-message-payload-test.xml");
        Resource resource = processor.processTemplate(template, request);

        Document controlDocument = getXmlUtil()
                .loadDocument(new ClassPathResource("xml/control-message-payload-test.xml"));
        Document responseDocument = getXmlUtil().loadDocument(resource);
        Diff diff = new Diff(controlDocument, responseDocument);
        assertTrue(diff.toString(), diff.similar());

        WsTestContextHolder.getTestContext().clear();
    }
}