Java tutorial
/** * Copyright (c) Acroquest Technology Co, Ltd. All Rights Reserved. * Please read the associated COPYRIGHTS file for more details. * * THE SOFTWARE IS PROVIDED BY Acroquest Technolog Co., Ltd., * WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDER BE LIABLE FOR ANY * CLAIM, DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING * OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. */ package acromusashi.stream.component.rabbitmq; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.junit.Assume; import org.junit.Rule; import org.junit.experimental.theories.DataPoints; import org.junit.experimental.theories.Theories; import org.junit.experimental.theories.Theory; import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; import org.springframework.amqp.rabbit.core.RabbitTemplate; @RunWith(Theories.class) public class AmqpTemplateFactoryTest { /** ???? */ private static final String DEFINED_QUEUE_NAME = "definedQueueName"; /** ??????? */ private static final String NON_DEFINED_QUEUE_NAME = "nonDefinedQueue"; /** ?Rule */ @Rule public ExpectedException expectedException = ExpectedException.none(); /** ?????? */ private AmqpTemplateFactory factory; /** * ??? * * @target {@link AmqpTemplateFactory#getAmqpTemplate(String)} * @test ???(??null:) * condition:: ??:null * result:: ???? * * @test ???(??null:) * condition:: ??:null * result:: ?????? * * @test ???(??:) * condition:: ??: * result:: ???? * * @test ???(??:) * condition:: ??: * result:: ?????? * * @test ???(????:??) * condition:: ??:???? * result:: ?????? * * @test ???(????:ConnectionFactory?) * condition:: ??:???? * result:: ??ConnectionFactory?????? * * @test ???(????:?) * condition:: ??:???? * result:: 2???1????????? * * @test ???(???????:) * condition:: ??:??????? * result:: ???? * * @test ???(???????:) * condition:: ??:??????? * result:: ?????? */ @DataPoints public static Fixture[] createFixture_QueueName() { List<Fixture> patterns = new ArrayList<Fixture>(); // null patterns.add(new Fixture(null, new RabbitmqCommunicateException("QueueName is not defined."))); // patterns.add(new Fixture("", new RabbitmqCommunicateException("QueueNames ProcessList is not defined. QueueName={0}"))); // ???? CachingConnectionFactory connectionFactory = new CachingConnectionFactory(); connectionFactory.setChannelCacheSize(10); patterns.add(new Fixture(DEFINED_QUEUE_NAME, new RabbitTemplate(connectionFactory))); // ??????? patterns.add(new Fixture(NON_DEFINED_QUEUE_NAME, new RabbitmqCommunicateException("QueueNames ProcessList is not defined. QueueName={0}"))); return patterns.toArray(new Fixture[patterns.size()]); } @Theory public void testGetAmqpTemplate_NormalCase(Fixture fixture) throws RabbitmqCommunicateException { // ??? Assume.assumeTrue(fixture.expectedException == null); // String queueName = fixture.queueName; RabbitTemplate expected = fixture.expectedTemplate; // Mock AbstractContextBuilder mockContextBuilder = mock(AbstractContextBuilder.class); doReturn(Arrays.asList("localhost:5672", "172.0.0.1:5673")).when(mockContextBuilder) .getProcessList(DEFINED_QUEUE_NAME); doReturn(expected.getConnectionFactory()).when(mockContextBuilder).getConnectionFactory(DEFINED_QUEUE_NAME); doReturn(expected).when(mockContextBuilder).getAmqpTemplate(DEFINED_QUEUE_NAME); this.factory = new AmqpTemplateFactory(mockContextBuilder); // RabbitTemplate firstActual = (RabbitTemplate) this.factory.getAmqpTemplate(queueName); RabbitTemplate secondActual = (RabbitTemplate) this.factory.getAmqpTemplate(queueName); // // AmqpTemplateConnectionFactory???equals????????? // ?????ContextBuilder????????? verify(mockContextBuilder, times(1)).getAmqpTemplate(DEFINED_QUEUE_NAME); // ConnectionFactory????????????()?? // ???????equals???????? assertThat(((CachingConnectionFactory) firstActual.getConnectionFactory()).getChannelCacheSize(), is(((CachingConnectionFactory) expected.getConnectionFactory()).getChannelCacheSize())); // ???????(?)????? assertTrue(firstActual == secondActual); } @Theory public void testGetAmqpTemplate_ExceptionCase(Fixture fixture) throws RabbitmqCommunicateException { // ??? Assume.assumeTrue(fixture.expectedException != null); // String queueName = fixture.queueName; expectedException.expect(fixture.expectedException.getClass()); if (fixture.expectedException.getMessage() != null) { expectedException.expectMessage(is(fixture.expectedException.getMessage())); } AbstractContextBuilder mockContextBuilder = mock(AbstractContextBuilder.class); doReturn(Arrays.asList("localhost:5672", "172.0.0.1:5673")).when(mockContextBuilder) .getProcessList(DEFINED_QUEUE_NAME); this.factory = new AmqpTemplateFactory(mockContextBuilder); // this.factory.getAmqpTemplate(queueName); } private static class Fixture { /** ?? */ String queueName; /** (???) */ RabbitTemplate expectedTemplate; /** () */ final Exception expectedException; /** * @param queueName * @param expectedTemplate */ public Fixture(String queueName, RabbitTemplate expectedTemplate) { this.queueName = queueName; this.expectedTemplate = expectedTemplate; this.expectedException = null; } /** * @param queueName * @param expectedTemplate * @param expectedException */ public Fixture(String queueName, Exception expectedException) { this.queueName = queueName; this.expectedTemplate = null; this.expectedException = expectedException; } } }