public static List<SocketExpectation> buildSocketExpectationList(Method method) { SocketExpectations socketExpectations = method.getAnnotation(SocketExpectations.class); SocketExpectation socketExpectation = method.getAnnotation(SocketExpectation.class); NoSocketsAllowed noSocketsAllowed = method.getAnnotation(NoSocketsAllowed.class); // If no annotations present, check the test class and its superclasses for (Class<?> testClass = method.getDeclaringClass(); null == socketExpectations && null == socketExpectation && null == noSocketsAllowed && !Object.class.equals(testClass); testClass = testClass.getSuperclass()) { socketExpectations = testClass.getAnnotation(SocketExpectations.class); socketExpectation = testClass.getAnnotation(SocketExpectation.class); noSocketsAllowed = testClass.getAnnotation(NoSocketsAllowed.class); } return AnnotationProcessor.buildSocketExpectationList(socketExpectation, socketExpectations, noSocketsAllowed); }
@Test(expected = IllegalArgumentException.class) @SocketExpectation(threads = Threads.ANY, connections = @Count(min = 2, max = 5, value = 3)) public void testIncorrectSocketExpectationAnnotation() throws NoSuchMethodException { AnnotationProcessor.buildSocketExpectationList( AnnotationProcessorTest.class.getMethod("testIncorrectSocketExpectationAnnotation") ); }
@Test @Expectation(threads = Threads.OTHERS, query = Query.INSERT, atLeast = 2, atMost = 5) public void testCorrectExpectationAnnotation() throws NoSuchMethodException { List<SqlExpectation> sqlExpectations = AnnotationProcessor.buildSqlExpectationList( AnnotationProcessorTest.class.getMethod("testCorrectExpectationAnnotation") ); assertNotNull(sqlExpectations); assertEquals(1, sqlExpectations.size()); SqlExpectation sqlExpectation = sqlExpectations.get(0); assertEquals(SqlStatement.INSERT, sqlExpectation.query()); assertEquals(Threads.OTHERS, sqlExpectation.threads()); assertEquals(2, sqlExpectation.count().min()); assertEquals(5, sqlExpectation.count().max()); assertEquals(-1, sqlExpectation.count().value()); assertEquals(-1, sqlExpectation.rows().value()); assertEquals(-1, sqlExpectation.rows().value()); assertEquals(-1, sqlExpectation.rows().value()); }
@Test @SocketExpectation(threads = Threads.OTHERS, connections = @Count(min = 2, max = 5)) public void testCorrectSocketExpectationAnnotation() throws NoSuchMethodException { List<SocketExpectation> socketExpectations = AnnotationProcessor.buildSocketExpectationList( AnnotationProcessorTest.class.getMethod("testCorrectSocketExpectationAnnotation") ); assertNotNull(socketExpectations); assertEquals(1, socketExpectations.size()); SocketExpectation socketExpectation = socketExpectations.get(0); assertEquals(Threads.OTHERS, socketExpectation.threads()); assertEquals(2, socketExpectation.connections().min()); assertEquals(5, socketExpectation.connections().max()); assertEquals(-1, socketExpectation.connections().value()); }
@Override public void beforeTestMethod(TestContext testContext) throws Exception { checkInitialized(); Method testMethod = getTestMethod(testContext); List<SocketExpectation> socketExpectationList = AnnotationProcessor.buildSocketExpectationList(testMethod); List<SqlExpectation> sqlExpectationList = AnnotationProcessor.buildSqlExpectationList(testMethod); setAttribute(testContext, SPY_ATTRIBUTE_NAME, spy); DisableSockets disableSockets = AnnotationProcessor.getAnnotationRecursive(testMethod, DisableSockets.class); setAttribute(testContext, DISABLE_SOCKETS_ATTRIBUTE_NAME, disableSockets);
sqlExpectationList = AnnotationProcessor.buildSqlExpectationList(method); } catch (IllegalArgumentException e) { fail(testResult, e.getMessage()); socketExpectationList = AnnotationProcessor.buildSocketExpectationList(method); } catch (IllegalArgumentException e) { fail(testResult, e.getMessage()); DisableSockets disableSockets = AnnotationProcessor.getAnnotationRecursive(method, DisableSockets.class);
public static List<SqlExpectation> buildSqlExpectationList(Method method) { SqlExpectations sqlExpectations = method.getAnnotation(SqlExpectations.class); SqlExpectation sqlExpectation = method.getAnnotation(SqlExpectation.class); NoSql noSql = method.getAnnotation(NoSql.class); if (null == sqlExpectations) sqlExpectations = Expectations.SqlExpectationsAdapter.adapter( method.getAnnotation(Expectations.class) ); if (null == sqlExpectation) sqlExpectation = Expectation.SqlExpectationAdapter.adapter( method.getAnnotation(Expectation.class) ); if (null == noSql && null != method.getAnnotation(NoQueriesAllowed.class)) noSql = method.getAnnotation(NoQueriesAllowed.class).annotationType().getAnnotation(NoSql.class); return buildSqlExpectationList(method.getDeclaringClass(), sqlExpectations, sqlExpectation, noSql); }
@Test public void testGetAnnotationRecursive() throws NoSuchMethodException { assertNotNull(AnnotationProcessor.getAnnotationRecursive( ConcreteClassWithoutAnnotation.class.getMethod("methodWithAnnotation"), DisableSockets.class) ); assertNull(AnnotationProcessor.getAnnotationRecursive( ConcreteClassWithoutAnnotation.class.getMethod("methodWithoutAnnotation"), DisableSockets.class) ); assertNotNull(AnnotationProcessor.getAnnotationRecursive( ConcreteClassWithAnnotation.class.getMethod("methodWithAnnotation"), DisableSockets.class) ); assertNotNull(AnnotationProcessor.getAnnotationRecursive( ConcreteClassWithAnnotation.class.getMethod("methodWithoutAnnotation"), DisableSockets.class) ); assertNotNull(AnnotationProcessor.getAnnotationRecursive( ConcreteClassExtendsBaseClassWithoutAnnotation.class.getMethod("methodWithAnnotation"), DisableSockets.class) ); assertNull(AnnotationProcessor.getAnnotationRecursive( ConcreteClassExtendsBaseClassWithoutAnnotation.class.getMethod("methodWithoutAnnotation"), DisableSockets.class) ); assertNotNull(AnnotationProcessor.getAnnotationRecursive( ConcreteClassExtendsBaseClassWithAnnotation.class.getMethod("methodWithAnnotation"), DisableSockets.class) ); assertNotNull(AnnotationProcessor.getAnnotationRecursive( ConcreteClassExtendsBaseClassWithAnnotation.class.getMethod("methodWithoutAnnotation"), DisableSockets.class) ); }
@Test @SqlExpectation(count = @Count(max = 3)) public void testParseExpectation() throws NoSuchMethodException { Range range = Range.parse(RangeTest.class.getMethod("testParseExpectation").getAnnotation(SqlExpectation.class).count()); assertEquals(3, range.max); }
@Test public void testNoSqlNoSocketsAllowedInClass() throws NoSuchMethodException { List<SocketExpectation> socketExpectations = AnnotationProcessor.buildSocketExpectationList( AnnotationProcessorTest.class.getMethod("testNoSqlNoSocketsAllowedInClass") ); assertNotNull(socketExpectations); assertEquals(1, socketExpectations.size()); SocketExpectation socketExpectation = socketExpectations.get(0); assertEquals(Threads.CURRENT, socketExpectation.threads()); assertEquals(-1, socketExpectation.connections().min()); assertEquals(-1, socketExpectation.connections().max()); assertEquals(0, socketExpectation.connections().value()); List<SqlExpectation> sqlExpectations = AnnotationProcessor.buildSqlExpectationList( AnnotationProcessorTest.class.getMethod("testNoSqlNoSocketsAllowedInClass") ); assertNotNull(sqlExpectations); assertEquals(1, sqlExpectations.size()); SqlExpectation sqlExpectation = sqlExpectations.get(0); assertEquals(SqlStatement.ANY, sqlExpectation.query()); assertEquals(Threads.CURRENT, sqlExpectation.threads()); assertEquals(-1, sqlExpectation.count().min()); assertEquals(-1, sqlExpectation.count().max()); assertEquals(0, sqlExpectation.count().value()); assertEquals(-1, sqlExpectation.rows().value()); assertEquals(-1, sqlExpectation.rows().value()); assertEquals(-1, sqlExpectation.rows().value()); }
@Test @SocketExpectations({ @SocketExpectation(threads = Threads.ANY, connections = @Count(min = 2, max = 5)), @SocketExpectation(threads = Threads.CURRENT, connections = @Count(min = 3, max = 6)) }) public void testCorrectSocketExpectationsAnnotation() throws NoSuchMethodException { List<SocketExpectation> socketExpectations = AnnotationProcessor.buildSocketExpectationList( AnnotationProcessorTest.class.getMethod("testCorrectSocketExpectationsAnnotation") ); assertNotNull(socketExpectations); assertEquals(2, socketExpectations.size()); { SocketExpectation socketExpectation = socketExpectations.get(0); assertEquals(Threads.ANY, socketExpectation.threads()); assertEquals(2, socketExpectation.connections().min()); assertEquals(5, socketExpectation.connections().max()); assertEquals(-1, socketExpectation.connections().value()); } { SocketExpectation socketExpectation = socketExpectations.get(1); assertEquals(Threads.CURRENT, socketExpectation.threads()); assertEquals(3, socketExpectation.connections().min()); assertEquals(6, socketExpectation.connections().max()); assertEquals(-1, socketExpectation.connections().value()); } }
public static List<SqlExpectation> buildSqlExpectationList(Class<?> declaringClass, SqlExpectations sqlExpectations, SqlExpectation sqlExpectation, NoSql noSql) { // If no annotations present, check the test class and its superclasses for (Class<?> testClass = declaringClass; null == sqlExpectations && null == sqlExpectation && null == noSql && !Object.class.equals(testClass); testClass = testClass.getSuperclass()) { sqlExpectations = testClass.getAnnotation(SqlExpectations.class); sqlExpectation = testClass.getAnnotation(SqlExpectation.class); noSql = testClass.getAnnotation(NoSql.class); if (null == sqlExpectations) sqlExpectations = Expectations.SqlExpectationsAdapter.adapter( testClass.getAnnotation(Expectations.class) ); if (null == sqlExpectation) sqlExpectation = Expectation.SqlExpectationAdapter.adapter( testClass.getAnnotation(Expectation.class) ); if (null == noSql && null != testClass.getAnnotation(NoQueriesAllowed.class)) noSql = testClass.getAnnotation(NoQueriesAllowed.class).annotationType().getAnnotation(NoSql.class); } return AnnotationProcessor.buildSqlExpectationList(sqlExpectations, sqlExpectation, noSql); }
@Test(expected = IllegalArgumentException.class) @SocketExpectations( @SocketExpectation(threads = Threads.ANY, connections = @Count(min = 2, max = 5, value = 3)) ) public void testIncorrectSocketExpectationsAnnotation() throws NoSuchMethodException { AnnotationProcessor.buildSocketExpectationList( AnnotationProcessorTest.class.getMethod("testIncorrectSocketExpectationsAnnotation") ); }
private static List<SocketExpectation> buildSocketExpectationList(Description description) { SocketExpectation socketExpectation = description.getAnnotation(SocketExpectation.class); SocketExpectations socketExpectations = description.getAnnotation(SocketExpectations.class); NoSocketsAllowed noSocketsAllowed = description.getAnnotation(NoSocketsAllowed.class); for (Class<?> testClass = description.getTestClass(); null == socketExpectations && null == socketExpectation && null == noSocketsAllowed && !Object.class.equals(testClass); testClass = testClass.getSuperclass()) { socketExpectations = testClass.getAnnotation(SocketExpectations.class); socketExpectation = testClass.getAnnotation(SocketExpectation.class); noSocketsAllowed = testClass.getAnnotation(NoSocketsAllowed.class); } return AnnotationProcessor.buildSocketExpectationList(socketExpectation, socketExpectations, noSocketsAllowed); }
}) public void testCorrectExpectationsAnnotation() throws NoSuchMethodException { List<SqlExpectation> sqlExpectations = AnnotationProcessor.buildSqlExpectationList( AnnotationProcessorTest.class.getMethod("testCorrectExpectationsAnnotation") ); assertEquals(SqlStatement.DELETE, sqlExpectation.query()); assertEquals(Threads.ANY, sqlExpectation.threads()); assertEquals(2, sqlExpectation.count().min()); assertEquals(5, sqlExpectation.count().max()); assertEquals(-1, sqlExpectation.count().value()); assertEquals(-1, sqlExpectation.rows().value()); assertEquals(-1, sqlExpectation.rows().value()); assertEquals(-1, sqlExpectation.rows().value()); assertEquals(SqlStatement.SELECT, sqlExpectation.query()); assertEquals(Threads.CURRENT, sqlExpectation.threads()); assertEquals(3, sqlExpectation.count().min()); assertEquals(6, sqlExpectation.count().max()); assertEquals(-1, sqlExpectation.count().value()); assertEquals(-1, sqlExpectation.rows().value()); assertEquals(-1, sqlExpectation.rows().value()); assertEquals(-1, sqlExpectation.rows().value());
@Test(expected = IllegalArgumentException.class) @Expectation(threads = Threads.ANY, query = Query.MERGE, atLeast = 2, atMost = 5, value = 3) public void testIncorrectExpectationAnnotation() throws NoSuchMethodException { AnnotationProcessor.buildSqlExpectationList( AnnotationProcessorTest.class.getMethod("testIncorrectExpectationAnnotation") ); }
@Test(expected = IllegalArgumentException.class) @Expectations( @Expectation(threads = Threads.ANY, query = Query.MERGE, atLeast = 2, atMost = 5, value = 3) ) public void testIncorrectExpectationsAnnotation() throws NoSuchMethodException { AnnotationProcessor.buildSqlExpectationList( AnnotationProcessorTest.class.getMethod("testIncorrectExpectationsAnnotation") ); }
private static List<SqlExpectation> buildSqlExpectationList(Description description) { SqlExpectations sqlExpectations = description.getAnnotation(SqlExpectations.class); SqlExpectation sqlExpectation = description.getAnnotation(SqlExpectation.class); NoSql noSql = description.getAnnotation(NoSql.class); if (null == sqlExpectations) sqlExpectations = Expectations.SqlExpectationsAdapter.adapter( description.getAnnotation(Expectations.class) ); if (null == sqlExpectation) sqlExpectation = Expectation.SqlExpectationAdapter.adapter( description.getAnnotation(Expectation.class) ); if (null == noSql && null != description.getAnnotation(NoQueriesAllowed.class)) noSql = description.getAnnotation(NoQueriesAllowed.class).annotationType().getAnnotation(NoSql.class); return AnnotationProcessor.buildSqlExpectationList(description.getTestClass(), sqlExpectations, sqlExpectation, noSql); }