提交 d9a48823 编写于 作者: T terrymanu

for checkstyle

上级 dbe89f6e
...@@ -50,80 +50,80 @@ import static org.mockito.Mockito.mock; ...@@ -50,80 +50,80 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
public final class ExecuteEventListenerTest { public final class ExecuteEventListenerTest {
private static final MockTracer TRACER = new MockTracer(new ThreadLocalActiveSpanSource(), private static final MockTracer TRACER = new MockTracer(new ThreadLocalActiveSpanSource(), MockTracer.Propagator.TEXT_MAP);
MockTracer.Propagator.TEXT_MAP);
private final ExecutorEngine executorEngine = new ExecutorEngine(5);
private final ExecutorEngine executorEngine = new ExecutorEngine(5);
@BeforeClass
@BeforeClass public static void init() {
public static void init() { ShardingJDBCTracer.init(TRACER);
ShardingJDBCTracer.init(TRACER); }
}
@AfterClass
@AfterClass public static void tearDown() throws Exception {
public static void tearDown() throws Exception { releaseTracer();
releaseTracer(); }
}
@Before
@Before public void before() {
public void before() { TRACER.reset();
TRACER.reset(); }
}
@Test
@Test public void assertSingleStatement() throws Exception {
public void assertSingleStatement() throws Exception { Statement statement = mock(Statement.class);
Statement statement = mock(Statement.class); when(statement.getConnection()).thenReturn(mock(Connection.class));
when(statement.getConnection()).thenReturn(mock(Connection.class)); executorEngine.execute(SQLType.DML, Collections.singleton(new StatementUnit(new SQLExecutionUnit("ds_0",
executorEngine.execute(SQLType.DML, Collections.singleton(new StatementUnit(new SQLExecutionUnit("ds_0", new SQLUnit("insert into ...", Collections.singletonList(Collections.<Object>singletonList(1)))), statement)), new ExecuteCallback<Integer>() {
new SQLUnit("insert into ...", Collections.singletonList(Collections.<Object>singletonList(1)))), statement)), new ExecuteCallback<Integer>() {
@Override
@Override public Integer execute(final BaseStatementUnit baseStatementUnit) {
public Integer execute(final BaseStatementUnit baseStatementUnit) { return 0;
return 0; }
} });
}); assertThat(TRACER.finishedSpans().size(), is(2));
assertThat(TRACER.finishedSpans().size(), is(2)); }
}
@Test
@Test public void assertMultiStatement() throws Exception {
public void assertMultiStatement() throws Exception { List<StatementUnit> statementUnitList = new ArrayList<>(2);
List<StatementUnit> statementUnitList = new ArrayList<>(2); Statement stm1 = mock(Statement.class);
Statement stm1 = mock(Statement.class); when(stm1.getConnection()).thenReturn(mock(Connection.class));
when(stm1.getConnection()).thenReturn(mock(Connection.class)); statementUnitList.add(new StatementUnit(new SQLExecutionUnit("ds_0", new SQLUnit("insert into ...", Collections.singletonList(Collections.<Object>singletonList(1)))), stm1));
statementUnitList.add(new StatementUnit(new SQLExecutionUnit("ds_0", new SQLUnit("insert into ...", Collections.singletonList(Collections.<Object>singletonList(1)))), stm1)); Statement stm2 = mock(Statement.class);
Statement stm2 = mock(Statement.class); when(stm2.getConnection()).thenReturn(mock(Connection.class));
when(stm2.getConnection()).thenReturn(mock(Connection.class)); statementUnitList.add(new StatementUnit(new SQLExecutionUnit("ds_0", new SQLUnit("insert into ...", Collections.singletonList(Collections.<Object>singletonList(1)))), stm2));
statementUnitList.add(new StatementUnit(new SQLExecutionUnit("ds_0", new SQLUnit("insert into ...", Collections.singletonList(Collections.<Object>singletonList(1)))), stm2)); executorEngine.execute(SQLType.DML, statementUnitList, new ExecuteCallback<Integer>() {
executorEngine.execute(SQLType.DML, statementUnitList, new ExecuteCallback<Integer>() {
@Override @Override
public Integer execute(final BaseStatementUnit baseStatementUnit) throws Exception { public Integer execute(final BaseStatementUnit baseStatementUnit) {
return 0; return 0;
} }
}); });
assertThat(TRACER.finishedSpans().size(), is(3)); assertThat(TRACER.finishedSpans().size(), is(3));
} }
@Test(expected = SQLException.class) @Test(expected = SQLException.class)
public void assertSQLException() throws Exception { public void assertSQLException() throws Exception {
Statement statement = mock(Statement.class); Statement statement = mock(Statement.class);
when(statement.getConnection()).thenReturn(mock(Connection.class)); when(statement.getConnection()).thenReturn(mock(Connection.class));
executorEngine.execute(SQLType.DQL, Collections.singleton(new StatementUnit(new SQLExecutionUnit("ds_0", executorEngine.execute(SQLType.DQL, Collections.singleton(new StatementUnit(new SQLExecutionUnit("ds_0",
new SQLUnit("select ...", Collections.singletonList(Collections.<Object>singletonList(1)))), statement)), new ExecuteCallback<Integer>() { new SQLUnit("select ...", Collections.singletonList(Collections.<Object>singletonList(1)))), statement)), new ExecuteCallback<Integer>() {
@Override @Override
public Integer execute(final BaseStatementUnit baseStatementUnit) throws Exception { public Integer execute(final BaseStatementUnit baseStatementUnit) throws Exception {
throw new SQLException(); throw new SQLException();
} }
}); });
} }
private static void releaseTracer() throws NoSuchFieldException, IllegalAccessException { private static void releaseTracer() throws NoSuchFieldException, IllegalAccessException {
Field tracerField = GlobalTracer.class.getDeclaredField("tracer"); Field tracerField = GlobalTracer.class.getDeclaredField("tracer");
tracerField.setAccessible(true); tracerField.setAccessible(true);
tracerField.set(GlobalTracer.class, NoopTracerFactory.create()); tracerField.set(GlobalTracer.class, NoopTracerFactory.create());
Field subscribersByTypeField = EventBus.class.getDeclaredField("subscribersByType"); Field subscribersByTypeField = EventBus.class.getDeclaredField("subscribersByType");
subscribersByTypeField.setAccessible(true); subscribersByTypeField.setAccessible(true);
subscribersByTypeField.set(EventBusInstance.getInstance(), HashMultimap.create()); subscribersByTypeField.set(EventBusInstance.getInstance(), HashMultimap.create());
} }
} }
...@@ -25,68 +25,65 @@ import io.opentracing.util.GlobalTracer; ...@@ -25,68 +25,65 @@ import io.opentracing.util.GlobalTracer;
import io.shardingsphere.core.exception.ShardingException; import io.shardingsphere.core.exception.ShardingException;
import io.shardingsphere.core.util.EventBusInstance; import io.shardingsphere.core.util.EventBusInstance;
import io.shardingsphere.opentracing.fixture.FooTracer; import io.shardingsphere.opentracing.fixture.FooTracer;
import static org.hamcrest.CoreMatchers.is;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.core.Is.isA; 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; import static org.mockito.Mockito.mock;
public final class ShardingJDBCTracerTest { public final class ShardingJDBCTracerTest {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
System.setProperty("shardingjdbc.opentracing.tracer.class", FooTracer.class.getName()); System.setProperty("shardingjdbc.opentracing.tracer.class", FooTracer.class.getName());
clearGlobalTracer(); clearGlobalTracer();
unregisterEventBus(); unregisterEventBus();
} }
@After @After
public void tearDown() { public void tearDown() {
System.getProperties().remove("shardingjdbc.opentracing.tracer.class"); System.getProperties().remove("shardingjdbc.opentracing.tracer.class");
} }
@Test @Test
public void assertDuplicatedLoading() { public void assertDuplicatedLoading() {
ShardingJDBCTracer.init(mock(Tracer.class)); ShardingJDBCTracer.init(mock(Tracer.class));
Tracer t1 = ShardingJDBCTracer.get(); Tracer t1 = ShardingJDBCTracer.get();
ShardingJDBCTracer.init(); ShardingJDBCTracer.init();
assertEquals(t1, ShardingJDBCTracer.get()); assertEquals(t1, ShardingJDBCTracer.get());
ShardingJDBCTracer.init(mock(Tracer.class)); ShardingJDBCTracer.init(mock(Tracer.class));
assertEquals(t1, ShardingJDBCTracer.get()); assertEquals(t1, ShardingJDBCTracer.get());
} }
@Test @Test
public void assertTracer() { public void assertTracer() {
assertThat((GlobalTracer) ShardingJDBCTracer.get(), isA(GlobalTracer.class)); assertThat((GlobalTracer) ShardingJDBCTracer.get(), isA(GlobalTracer.class));
assertTrue(GlobalTracer.isRegistered()); assertTrue(GlobalTracer.isRegistered());
assertThat(ShardingJDBCTracer.get(), is(ShardingJDBCTracer.get())); assertThat(ShardingJDBCTracer.get(), is(ShardingJDBCTracer.get()));
} }
@Test(expected = ShardingException.class) @Test(expected = ShardingException.class)
public void assertTracerClassError() { public void assertTracerClassError() {
System.setProperty("shardingjdbc.opentracing.tracer.class", "com.foo.FooTracer"); System.setProperty("shardingjdbc.opentracing.tracer.class", "com.foo.FooTracer");
ShardingJDBCTracer.get(); ShardingJDBCTracer.get();
}
}
private static void clearGlobalTracer() throws NoSuchFieldException, IllegalAccessException {
private static void clearGlobalTracer() throws NoSuchFieldException, IllegalAccessException { Field tracerField = GlobalTracer.class.getDeclaredField("tracer");
Field tracerField = GlobalTracer.class.getDeclaredField("tracer"); tracerField.setAccessible(true);
tracerField.setAccessible(true); tracerField.set(GlobalTracer.class, NoopTracerFactory.create());
tracerField.set(GlobalTracer.class, NoopTracerFactory.create()); }
} private static void unregisterEventBus() throws NoSuchFieldException, IllegalAccessException {
Field subscribersByTypeField = EventBus.class.getDeclaredField("subscribersByType");
private static void unregisterEventBus() throws NoSuchFieldException, IllegalAccessException { subscribersByTypeField.setAccessible(true);
Field subscribersByTypeField = EventBus.class.getDeclaredField("subscribersByType"); subscribersByTypeField.set(EventBusInstance.getInstance(), HashMultimap.create());
subscribersByTypeField.setAccessible(true); }
subscribersByTypeField.set(EventBusInstance.getInstance(), HashMultimap.create());
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册