Skip to content

Commit

Permalink
for checkstyle
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed May 21, 2018
1 parent dbe89f6 commit d9a4882
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.<Object>singletonList(1)))), statement)), new ExecuteCallback<Integer>() {

@Override
public Integer execute(final BaseStatementUnit baseStatementUnit) {
return 0;
}
});
assertThat(TRACER.finishedSpans().size(), is(2));
}

@Test
public void assertMultiStatement() throws Exception {
List<StatementUnit> 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.<Object>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.<Object>singletonList(1)))), stm2));
executorEngine.execute(SQLType.DML, statementUnitList, new ExecuteCallback<Integer>() {
@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.<Object>singletonList(1)))), statement)), new ExecuteCallback<Integer>() {

@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.<Object>singletonList(1)))), statement)), new ExecuteCallback<Integer>() {
@Override
public Integer execute(final BaseStatementUnit baseStatementUnit) {
return 0;
}
});
assertThat(TRACER.finishedSpans().size(), is(2));
}
@Test
public void assertMultiStatement() throws Exception {
List<StatementUnit> 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.<Object>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.<Object>singletonList(1)))), stm2));
executorEngine.execute(SQLType.DML, statementUnitList, new ExecuteCallback<Integer>() {
@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.<Object>singletonList(1)))), statement)), new ExecuteCallback<Integer>() {
@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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit d9a4882

Please sign in to comment.