/* * Copyright 2005-2011 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 org.wamblee.xmlrouter.impl; import static junit.framework.Assert.*; import static org.mockito.Matchers.*; import static org.mockito.Mockito.*; import javax.xml.transform.dom.DOMSource; import org.junit.Before; import org.junit.Test; import org.wamblee.xmlrouter.common.Id; import org.wamblee.xmlrouter.config.Transformation; public class RobustTransformationTest { private Transformation transformation; private Transformation robust; private DOMSource source; private DOMSource resSource; @Before public void setUp() { transformation = mock(Transformation.class); when(transformation.getId()).thenReturn(new Id("t1")); robust = new RobustTransformation("transformation", transformation); source = mock(DOMSource.class); resSource = mock(DOMSource.class); } @Test public void testNormalFlow() { when(transformation.getFromType()).thenReturn("from"); when(transformation.getToType()).thenReturn("to"); when(transformation.transform(same(source))).thenReturn(resSource); assertEquals("from", robust.getFromType()); assertEquals("to", robust.getToType()); assertSame(resSource, robust.transform(source)); } @Test public void testFromReturnsNull() { when(transformation.getFromType()).thenReturn(null); assertEquals(Constants.UNKNOWN_DOCUMENT_TYPE.toString(), robust.getFromType()); } @Test public void testToReturnsNull() { when(transformation.getToType()).thenReturn(null); assertEquals(Constants.UNKNOWN_DOCUMENT_TYPE.toString(), robust.getToType()); } @Test public void testFromThrowsException() { doThrow(new RuntimeException("x")).when(transformation).getFromType(); assertEquals(Constants.UNKNOWN_DOCUMENT_TYPE.toString(), robust.getFromType()); } @Test public void testToThrowsException() { doThrow(new RuntimeException("x")).when(transformation).getFromType(); assertEquals(Constants.UNKNOWN_DOCUMENT_TYPE.toString(), robust.getFromType()); } @Test public void testTransformReturnsNull() { when(transformation.transform(any(DOMSource.class))).thenReturn(null); assertEquals(null, robust.transform(source)); } @Test public void testTransformationThrowsException() { doThrow(new RuntimeException("x")).when(transformation).transform( any(DOMSource.class)); assertEquals(null, robust.transform(source)); } }