|
| 1 | +// Copyright (c) 2018 Pivotal Software, Inc. All rights reserved. |
| 2 | +// |
| 3 | +// This software, the RabbitMQ Java client library, is triple-licensed under the |
| 4 | +// Mozilla Public License 1.1 ("MPL"), the GNU General Public License version 2 |
| 5 | +// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see |
| 6 | +// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL, |
| 7 | +// please see LICENSE-APACHE2. |
| 8 | +// |
| 9 | +// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, |
| 10 | +// either express or implied. See the LICENSE file for specific language governing |
| 11 | +// rights and limitations of this software. |
| 12 | +// |
| 13 | +// If you have any questions regarding licensing, please contact us at |
| 14 | + |
| 15 | + |
| 16 | +package com.rabbitmq.client.test; |
| 17 | + |
| 18 | +import com.rabbitmq.client.Channel; |
| 19 | +import com.rabbitmq.client.Connection; |
| 20 | +import org.junit.Before; |
| 21 | +import org.junit.Test; |
| 22 | +import org.junit.runner.RunWith; |
| 23 | +import org.junit.runners.Parameterized; |
| 24 | +import org.mockito.Mock; |
| 25 | +import org.mockito.stubbing.OngoingStubbing; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | +import java.util.NoSuchElementException; |
| 29 | +import java.util.Optional; |
| 30 | + |
| 31 | +import static org.junit.Assert.*; |
| 32 | +import static org.mockito.Mockito.mock; |
| 33 | +import static org.mockito.Mockito.when; |
| 34 | +import static org.mockito.MockitoAnnotations.initMocks; |
| 35 | + |
| 36 | +@RunWith(Parameterized.class) |
| 37 | +public class ConnectionTest { |
| 38 | + |
| 39 | + @Parameterized.Parameter |
| 40 | + public TestConfigurator configurator; |
| 41 | + @Mock |
| 42 | + Connection c = mock(Connection.class); |
| 43 | + @Mock |
| 44 | + Channel ch = mock(Channel.class); |
| 45 | + |
| 46 | + @Parameterized.Parameters |
| 47 | + public static Object[] configurators() { |
| 48 | + return new Object[]{new NotNumberedChannelCreationCallback(), new NumberedChannelCreationCallback()}; |
| 49 | + } |
| 50 | + |
| 51 | + @Before |
| 52 | + public void init() { |
| 53 | + initMocks(this); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void openChannelWithNonNullChannelShouldReturnNonEmptyOptional() throws Exception { |
| 58 | + configurator.mockAndWhenChannel(c).thenReturn(ch); |
| 59 | + configurator.mockAndWhenOptional(c).thenCallRealMethod(); |
| 60 | + Optional<Channel> optional = configurator.open(c); |
| 61 | + assertTrue(optional.isPresent()); |
| 62 | + assertSame(ch, optional.get()); |
| 63 | + } |
| 64 | + |
| 65 | + @Test(expected = NoSuchElementException.class) |
| 66 | + public void openChannelWithNullChannelShouldReturnEmptyOptional() throws Exception { |
| 67 | + configurator.mockAndWhenChannel(c).thenReturn(null); |
| 68 | + configurator.mockAndWhenOptional(c).thenCallRealMethod(); |
| 69 | + Optional<Channel> optional = configurator.open(c); |
| 70 | + assertFalse(optional.isPresent()); |
| 71 | + optional.get(); |
| 72 | + } |
| 73 | + |
| 74 | + @Test(expected = IOException.class) |
| 75 | + public void openChannelShouldPropagateIoException() throws Exception { |
| 76 | + configurator.mockAndWhenChannel(c).thenThrow(IOException.class); |
| 77 | + configurator.mockAndWhenOptional(c).thenCallRealMethod(); |
| 78 | + configurator.open(c); |
| 79 | + } |
| 80 | + |
| 81 | + interface TestConfigurator { |
| 82 | + |
| 83 | + OngoingStubbing<Channel> mockAndWhenChannel(Connection c) throws IOException; |
| 84 | + |
| 85 | + OngoingStubbing<Optional<Channel>> mockAndWhenOptional(Connection c) throws IOException; |
| 86 | + |
| 87 | + Optional<Channel> open(Connection c) throws IOException; |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + static class NotNumberedChannelCreationCallback implements TestConfigurator { |
| 92 | + |
| 93 | + @Override |
| 94 | + public OngoingStubbing<Channel> mockAndWhenChannel(Connection c) throws IOException { |
| 95 | + return when(c.createChannel()); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public OngoingStubbing<Optional<Channel>> mockAndWhenOptional(Connection c) throws IOException { |
| 100 | + return when(c.openChannel()); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public Optional<Channel> open(Connection c) throws IOException { |
| 105 | + return c.openChannel(); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + static class NumberedChannelCreationCallback implements TestConfigurator { |
| 110 | + |
| 111 | + @Override |
| 112 | + public OngoingStubbing<Channel> mockAndWhenChannel(Connection c) throws IOException { |
| 113 | + return when(c.createChannel(1)); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public OngoingStubbing<Optional<Channel>> mockAndWhenOptional(Connection c) throws IOException { |
| 118 | + return when(c.openChannel(1)); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public Optional<Channel> open(Connection c) throws IOException { |
| 123 | + return c.openChannel(1); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | +} |
0 commit comments