diff --git a/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/ExecuteEventListenerTest.java b/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/ExecuteEventListenerTest.java index 8a565d8bd8a58..7f3677e38eb19 100644 --- a/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/ExecuteEventListenerTest.java +++ b/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/ExecuteEventListenerTest.java @@ -50,80 +50,80 @@ import static org.mockito.Mockito.when; public final class ExecuteEventListenerTest { - - private static final MockTracer TRACER = new MockTracer(new ThreadLocalActiveSpanSource(), - MockTracer.Propagator.TEXT_MAP); - - private final ExecutorEngine executorEngine = new ExecutorEngine(5); - - @BeforeClass - public static void init() { - ShardingJDBCTracer.init(TRACER); - } - - @AfterClass - public static void tearDown() throws Exception { - releaseTracer(); - } - - @Before - public void before() { - TRACER.reset(); - } - - @Test - public void assertSingleStatement() throws Exception { - Statement statement = mock(Statement.class); - when(statement.getConnection()).thenReturn(mock(Connection.class)); - executorEngine.execute(SQLType.DML, Collections.singleton(new StatementUnit(new SQLExecutionUnit("ds_0", - new SQLUnit("insert into ...", Collections.singletonList(Collections.singletonList(1)))), statement)), new ExecuteCallback() { - - @Override - public Integer execute(final BaseStatementUnit baseStatementUnit) { - return 0; - } - }); - assertThat(TRACER.finishedSpans().size(), is(2)); - } - - @Test - public void assertMultiStatement() throws Exception { - List statementUnitList = new ArrayList<>(2); - Statement stm1 = mock(Statement.class); - when(stm1.getConnection()).thenReturn(mock(Connection.class)); - statementUnitList.add(new StatementUnit(new SQLExecutionUnit("ds_0", new SQLUnit("insert into ...", Collections.singletonList(Collections.singletonList(1)))), stm1)); - Statement stm2 = mock(Statement.class); - when(stm2.getConnection()).thenReturn(mock(Connection.class)); - statementUnitList.add(new StatementUnit(new SQLExecutionUnit("ds_0", new SQLUnit("insert into ...", Collections.singletonList(Collections.singletonList(1)))), stm2)); - executorEngine.execute(SQLType.DML, statementUnitList, new ExecuteCallback() { - @Override - public Integer execute(final BaseStatementUnit baseStatementUnit) throws Exception { - return 0; - } - }); - assertThat(TRACER.finishedSpans().size(), is(3)); - } - - @Test(expected = SQLException.class) - public void assertSQLException() throws Exception { - Statement statement = mock(Statement.class); - when(statement.getConnection()).thenReturn(mock(Connection.class)); - executorEngine.execute(SQLType.DQL, Collections.singleton(new StatementUnit(new SQLExecutionUnit("ds_0", - new SQLUnit("select ...", Collections.singletonList(Collections.singletonList(1)))), statement)), new ExecuteCallback() { - - @Override - public Integer execute(final BaseStatementUnit baseStatementUnit) throws Exception { - throw new SQLException(); - } - }); - } - - private static void releaseTracer() throws NoSuchFieldException, IllegalAccessException { - Field tracerField = GlobalTracer.class.getDeclaredField("tracer"); - tracerField.setAccessible(true); - tracerField.set(GlobalTracer.class, NoopTracerFactory.create()); - Field subscribersByTypeField = EventBus.class.getDeclaredField("subscribersByType"); - subscribersByTypeField.setAccessible(true); - subscribersByTypeField.set(EventBusInstance.getInstance(), HashMultimap.create()); - } + + private static final MockTracer TRACER = new MockTracer(new ThreadLocalActiveSpanSource(), MockTracer.Propagator.TEXT_MAP); + + private final ExecutorEngine executorEngine = new ExecutorEngine(5); + + @BeforeClass + public static void init() { + ShardingJDBCTracer.init(TRACER); + } + + @AfterClass + public static void tearDown() throws Exception { + releaseTracer(); + } + + @Before + public void before() { + TRACER.reset(); + } + + @Test + public void assertSingleStatement() throws Exception { + Statement statement = mock(Statement.class); + when(statement.getConnection()).thenReturn(mock(Connection.class)); + executorEngine.execute(SQLType.DML, Collections.singleton(new StatementUnit(new SQLExecutionUnit("ds_0", + new SQLUnit("insert into ...", Collections.singletonList(Collections.singletonList(1)))), statement)), new ExecuteCallback() { + + @Override + public Integer execute(final BaseStatementUnit baseStatementUnit) { + return 0; + } + }); + assertThat(TRACER.finishedSpans().size(), is(2)); + } + + @Test + public void assertMultiStatement() throws Exception { + List statementUnitList = new ArrayList<>(2); + Statement stm1 = mock(Statement.class); + when(stm1.getConnection()).thenReturn(mock(Connection.class)); + statementUnitList.add(new StatementUnit(new SQLExecutionUnit("ds_0", new SQLUnit("insert into ...", Collections.singletonList(Collections.singletonList(1)))), stm1)); + Statement stm2 = mock(Statement.class); + when(stm2.getConnection()).thenReturn(mock(Connection.class)); + statementUnitList.add(new StatementUnit(new SQLExecutionUnit("ds_0", new SQLUnit("insert into ...", Collections.singletonList(Collections.singletonList(1)))), stm2)); + executorEngine.execute(SQLType.DML, statementUnitList, new ExecuteCallback() { + + @Override + public Integer execute(final BaseStatementUnit baseStatementUnit) { + return 0; + } + }); + assertThat(TRACER.finishedSpans().size(), is(3)); + } + + @Test(expected = SQLException.class) + public void assertSQLException() throws Exception { + Statement statement = mock(Statement.class); + when(statement.getConnection()).thenReturn(mock(Connection.class)); + executorEngine.execute(SQLType.DQL, Collections.singleton(new StatementUnit(new SQLExecutionUnit("ds_0", + new SQLUnit("select ...", Collections.singletonList(Collections.singletonList(1)))), statement)), new ExecuteCallback() { + + @Override + public Integer execute(final BaseStatementUnit baseStatementUnit) throws Exception { + throw new SQLException(); + } + }); + } + + private static void releaseTracer() throws NoSuchFieldException, IllegalAccessException { + Field tracerField = GlobalTracer.class.getDeclaredField("tracer"); + tracerField.setAccessible(true); + tracerField.set(GlobalTracer.class, NoopTracerFactory.create()); + Field subscribersByTypeField = EventBus.class.getDeclaredField("subscribersByType"); + subscribersByTypeField.setAccessible(true); + subscribersByTypeField.set(EventBusInstance.getInstance(), HashMultimap.create()); + } } diff --git a/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/ShardingJDBCTracerTest.java b/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/ShardingJDBCTracerTest.java index bf488b564e293..ce78f44444fe6 100644 --- a/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/ShardingJDBCTracerTest.java +++ b/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/ShardingJDBCTracerTest.java @@ -25,68 +25,65 @@ import io.shardingsphere.core.exception.ShardingException; import io.shardingsphere.core.util.EventBusInstance; import io.shardingsphere.opentracing.fixture.FooTracer; - -import static org.hamcrest.CoreMatchers.is; - import org.junit.After; import org.junit.Before; import org.junit.Test; import java.lang.reflect.Field; +import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.core.Is.isA; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; public final class ShardingJDBCTracerTest { - - @Before - public void setUp() throws Exception { - System.setProperty("shardingjdbc.opentracing.tracer.class", FooTracer.class.getName()); - clearGlobalTracer(); - unregisterEventBus(); - } - - @After - public void tearDown() { - System.getProperties().remove("shardingjdbc.opentracing.tracer.class"); - } - - @Test - public void assertDuplicatedLoading() { - ShardingJDBCTracer.init(mock(Tracer.class)); - Tracer t1 = ShardingJDBCTracer.get(); - ShardingJDBCTracer.init(); - assertEquals(t1, ShardingJDBCTracer.get()); - ShardingJDBCTracer.init(mock(Tracer.class)); - assertEquals(t1, ShardingJDBCTracer.get()); - } - - @Test - public void assertTracer() { - assertThat((GlobalTracer) ShardingJDBCTracer.get(), isA(GlobalTracer.class)); - assertTrue(GlobalTracer.isRegistered()); - assertThat(ShardingJDBCTracer.get(), is(ShardingJDBCTracer.get())); - } - - @Test(expected = ShardingException.class) - public void assertTracerClassError() { - System.setProperty("shardingjdbc.opentracing.tracer.class", "com.foo.FooTracer"); - ShardingJDBCTracer.get(); - - } - - private static void clearGlobalTracer() throws NoSuchFieldException, IllegalAccessException { - Field tracerField = GlobalTracer.class.getDeclaredField("tracer"); - tracerField.setAccessible(true); - tracerField.set(GlobalTracer.class, NoopTracerFactory.create()); - - } - - private static void unregisterEventBus() throws NoSuchFieldException, IllegalAccessException { - Field subscribersByTypeField = EventBus.class.getDeclaredField("subscribersByType"); - subscribersByTypeField.setAccessible(true); - subscribersByTypeField.set(EventBusInstance.getInstance(), HashMultimap.create()); - } - + + @Before + public void setUp() throws Exception { + System.setProperty("shardingjdbc.opentracing.tracer.class", FooTracer.class.getName()); + clearGlobalTracer(); + unregisterEventBus(); + } + + @After + public void tearDown() { + System.getProperties().remove("shardingjdbc.opentracing.tracer.class"); + } + + @Test + public void assertDuplicatedLoading() { + ShardingJDBCTracer.init(mock(Tracer.class)); + Tracer t1 = ShardingJDBCTracer.get(); + ShardingJDBCTracer.init(); + assertEquals(t1, ShardingJDBCTracer.get()); + ShardingJDBCTracer.init(mock(Tracer.class)); + assertEquals(t1, ShardingJDBCTracer.get()); + } + + @Test + public void assertTracer() { + assertThat((GlobalTracer) ShardingJDBCTracer.get(), isA(GlobalTracer.class)); + assertTrue(GlobalTracer.isRegistered()); + assertThat(ShardingJDBCTracer.get(), is(ShardingJDBCTracer.get())); + } + + @Test(expected = ShardingException.class) + public void assertTracerClassError() { + System.setProperty("shardingjdbc.opentracing.tracer.class", "com.foo.FooTracer"); + ShardingJDBCTracer.get(); + } + + private static void clearGlobalTracer() throws NoSuchFieldException, IllegalAccessException { + Field tracerField = GlobalTracer.class.getDeclaredField("tracer"); + tracerField.setAccessible(true); + tracerField.set(GlobalTracer.class, NoopTracerFactory.create()); + } + + private static void unregisterEventBus() throws NoSuchFieldException, IllegalAccessException { + Field subscribersByTypeField = EventBus.class.getDeclaredField("subscribersByType"); + subscribersByTypeField.setAccessible(true); + subscribersByTypeField.set(EventBusInstance.getInstance(), HashMultimap.create()); + } }