/* * 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 java.util.Arrays; import java.util.Collection; import java.util.Collections; import javax.xml.transform.dom.DOMSource; import org.junit.Before; import org.junit.Test; import org.wamblee.xmlrouter.common.Id; import org.wamblee.xmlrouter.subscribe.Destination; public class RobustDestinationTest { private Destination destination; private Destination robust; private DOMSource source; @Before public void setUp() { destination = mock(Destination.class); robust = new RobustDestination(new Id("100"), destination); source = mock(DOMSource.class); } @Test public void testNormalFlow() { when(destination.getName()).thenReturn("name"); when( destination.chooseFromTargetTypes((Collection) anyObject())) .thenReturn(Arrays.asList("x")); assertEquals("name", robust.getName()); assertEquals(Arrays.asList("x"), robust.chooseFromTargetTypes(Arrays.asList("x", "y"))); when(destination.receive(same(source))).thenReturn(true); assertTrue(robust.receive(source)); verify(destination).receive(same(source)); when(destination.receive(same(source))).thenReturn(false); assertFalse(robust.receive(source)); } @Test public void testNameReturnsNull() { when(destination.getName()).thenReturn(null); assertEquals(Constants.UNKNOWN_DESTINATION_NAME.toString(), robust.getName()); } @Test public void testNameThrowsException() { doThrow(new RuntimeException("x")).when(destination).getName(); assertEquals(Constants.UNKNOWN_DESTINATION_NAME.toString(), robust.getName()); } @Test public void testChooseFromTargetTypesReturnsNull() { when( destination.chooseFromTargetTypes((Collection) anyObject())) .thenReturn(null); assertEquals(Collections.EMPTY_LIST, robust.chooseFromTargetTypes(Arrays.asList("x"))); } @Test public void testChooseFromTargetTypesThrowsException() { doThrow(new RuntimeException("x")).when(destination) .chooseFromTargetTypes(anyList()); assertEquals(Collections.EMPTY_LIST, robust.chooseFromTargetTypes(Arrays.asList("x"))); } @Test public void testChooseFromTargetTypesReturnsInvalidType() { when( destination.chooseFromTargetTypes((Collection) anyObject())) .thenReturn(Arrays.asList("x", "y")); assertEquals(Arrays.asList("y"), robust.chooseFromTargetTypes(Arrays.asList("y", "z"))); } @Test public void testReceiveThrowsException() { doThrow(new RuntimeException("x")).when(destination).receive( any(DOMSource.class)); assertEquals(false, robust.receive(source)); } }