    001package org.apache.turbine.pipeline;
    002
    003/*
    004 * Licensed to the Apache Software Foundation (ASF) under one
    005 * or more contributor license agreements.  See the NOTICE file
    006 * distributed with this work for additional information
    007 * regarding copyright ownership.  The ASF licenses this file
    008 * to you under the Apache License, Version 2.0 (the
    009 * "License"); you may not use this file except in compliance
    010 * with the License.  You may obtain a copy of the License at
    011 *
    012 *   http://www.apache.org/licenses/LICENSE-2.0
    013 *
    014 * Unless required by applicable law or agreed to in writing,
    015 * software distributed under the License is distributed on an
    016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    017 * KIND, either express or implied.  See the License for the
    018 * specific language governing permissions and limitations
    019 * under the License.
    020 */
    021
    022import static org.junit.Assert.assertEquals;
    023import static org.junit.Assert.assertTrue;
    024
    025import java.io.StringReader;
    026import java.io.StringWriter;
    027
    028import javax.xml.bind.JAXBContext;
    029import javax.xml.bind.Marshaller;
    030import javax.xml.bind.Unmarshaller;
    031
    032import org.junit.Before;
    033import org.junit.Test;
    034
    035/**
    036 * Tests TurbinePipeline.
    037 *
    038 * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
    039 * @version $Id: PipelineCreationTest.java 1725002 2016-01-16 16:31:45Z tv $
    040 */
    041public class PipelineCreationTest
    042{
    043    private Pipeline pipeline;
    044
    045    @Before
    046    public void setUp()
    047    {
    048        pipeline = new TurbinePipeline();
    049        pipeline.addValve(new SimpleValve());
    050        pipeline.addValve(new DetermineActionValve());
    051    }
    052
    053    @Test
    054    public void testSavingPipeline() throws Exception
    055    {
    056        JAXBContext context = JAXBContext.newInstance(TurbinePipeline.class);
    057        Marshaller marshaller = context.createMarshaller();
    058        StringWriter writer = new StringWriter();
    059        marshaller.marshal(pipeline, writer);
    060        assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
    061                + "<pipeline><valves>"
    062                + "<valve>org.apache.turbine.pipeline.SimpleValve</valve>"
    063                + "<valve>org.apache.turbine.pipeline.DetermineActionValve</valve>"
    064                + "</valves></pipeline>", writer.toString());
    065    }
    066
    067    @Test
    068    public void testReadingPipeline() throws Exception
    069    {
    070        String xml = "<pipeline name=\"default\"><valves>"
    071                + "<valve>org.apache.turbine.pipeline.SimpleValve</valve>"
    072                + "<valve>org.apache.turbine.pipeline.DetermineActionValve</valve>"
    073                + "</valves></pipeline>";
    074        JAXBContext context = JAXBContext.newInstance(TurbinePipeline.class);
    075        Unmarshaller unmarshaller = context.createUnmarshaller();
    076        StringReader reader = new StringReader(xml);
    077        Pipeline pipeline = (Pipeline) unmarshaller.unmarshal(reader);
    078        assertEquals(2, pipeline.getValves().length);
    079        assertTrue(pipeline.getValves()[0] instanceof SimpleValve);
    080        assertTrue(pipeline.getValves()[1] instanceof DetermineActionValve);
    081    }
    082
    083}