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); robust = new RobustTransformation(new Id(100), 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)); } }