AbstractUnmarshallXmlTest.java

  1. /*
  2.  * Copyright 2010 James Pether Sörling
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *   http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  *
  16.  *  $Id$
  17.  *  $HeadURL$
  18. */
  19. package com.hack23.cia.testfoundation;

  20. import java.io.BufferedReader;
  21. import java.io.InputStreamReader;
  22. import java.nio.charset.StandardCharsets;
  23. import java.nio.file.Paths;

  24. import javax.xml.transform.stream.StreamSource;

  25. import org.apache.commons.io.FilenameUtils;
  26. import org.springframework.oxm.Unmarshaller;

  27. /**
  28.  * The Class AbstractUnmarshallXmlTest.
  29.  *
  30.  * @param <T>
  31.  *            the generic type
  32.  */
  33. public abstract class AbstractUnmarshallXmlTest<T> extends AbstractFunctionalIntegrationTest {

  34.     /**
  35.      * Instantiates a new abstract unmarshall xml test.
  36.      */
  37.     public AbstractUnmarshallXmlTest() {
  38.         super();
  39.     }

  40.     /**
  41.      * Unmarshall xml.
  42.      *
  43.      * @param unmarshaller
  44.      *            the unmarshaller
  45.      * @param filename
  46.      *            the filename
  47.      * @return the t
  48.      * @throws Exception
  49.      *             the exception
  50.      */
  51.     protected final T unmarshallXml(final Unmarshaller unmarshaller, final String filename) throws Exception {
  52.         final BufferedReader inputStream = new BufferedReader(new InputStreamReader(
  53.                 java.nio.file.Files.newInputStream(
  54.                         Paths.get(FilenameUtils.getFullPath(filename), FilenameUtils.getName(filename))),
  55.                 StandardCharsets.UTF_8));

  56.         return (T) unmarshaller.unmarshal(new StreamSource(inputStream));
  57.     }
  58. }