Skip to content

Commit 381238e

Browse files
committed
assertions can be done without annotation | Listener clean statistics even without annotation
1 parent 0c870b6 commit 381238e

11 files changed

+146
-77
lines changed

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,14 @@
4949
<scope>test</scope>
5050
</dependency>
5151
</dependencies>
52+
53+
<build>
54+
<plugins>
55+
<plugin>
56+
<groupId>org.apache.maven.plugins</groupId>
57+
<artifactId>maven-surefire-plugin</artifactId>
58+
<version>3.0.0-M6</version>
59+
</plugin>
60+
</plugins>
61+
</build>
5262
</project>

src/main/java/com/lemick/api/AssertSQLStatementCount.java renamed to src/main/java/com/lemick/api/AssertHibernateSQLStatementCount.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
@Target(ElementType.METHOD)
99
@Retention(RetentionPolicy.RUNTIME)
10-
public @interface AssertSQLStatementCount {
10+
public @interface AssertHibernateSQLStatementCount {
1111

1212
int selects() default 0;
1313

src/main/java/com/lemick/assertions/HibernateStatementAssertUtils.java

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/main/java/com/lemick/assertions/HibernateStatementAssertionResult.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.lemick.assertions;
22

3-
public class HibernateStatementAssertionResult {
3+
public class HibernateStatementAssertionResult implements HibernateStatementAssertionValidator {
44

55
public enum StatementType {SELECT, INSERT, UPDATE, DELETE}
66

@@ -21,4 +21,11 @@ public boolean isInError() {
2121
public String getErrorMessage() {
2222
return "Expected " + expected + " " + type.name() + " but was " + actual;
2323
}
24+
25+
@Override
26+
public void validate() {
27+
if (this.isInError()) {
28+
throw new HibernateStatementCountException(this.getErrorMessage());
29+
}
30+
}
2431
}

src/main/java/com/lemick/assertions/HibernateStatementAssertionResults.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
import java.util.List;
44
import java.util.stream.Collectors;
55

6-
public class HibernateStatementAssertionResults {
6+
public class HibernateStatementAssertionResults implements HibernateStatementAssertionValidator {
77

8-
private List<HibernateStatementAssertionResult> assertionResults;
8+
private final List<HibernateStatementAssertionResult> assertionResults;
99

1010
public HibernateStatementAssertionResults(List<HibernateStatementAssertionResult> statementAssertionResults) {
1111
this.assertionResults = statementAssertionResults;
1212
}
1313

14+
@Override
1415
public void validate() {
1516
List<HibernateStatementAssertionResult> assertionsInError = assertionResults.stream()
1617
.filter(HibernateStatementAssertionResult::isInError)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.lemick.assertions;
2+
3+
public interface HibernateStatementAssertionValidator {
4+
void validate();
5+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.lemick.assertions;
2+
3+
4+
import com.lemick.integration.hibernate.HibernateStatementCountInspector;
5+
import com.lemick.integration.hibernate.HibernateStatistics;
6+
7+
import java.util.function.Supplier;
8+
9+
import static com.lemick.assertions.HibernateStatementAssertionResult.StatementType.*;
10+
11+
public class HibernateStatementAssertionsProvider {
12+
13+
public HibernateStatementAssertionResult generateInsertStatementAssertion(int expectedInsertStatementCount, Supplier<HibernateStatistics> statisticsSupplier) {
14+
return new HibernateStatementAssertionResult(INSERT, statisticsSupplier.get().getInsertStatementCount(), expectedInsertStatementCount);
15+
}
16+
17+
public HibernateStatementAssertionResult generateUpdateStatementAssertion(int expectedUpdateStatementCount, Supplier<HibernateStatistics> statisticsSupplier) {
18+
return new HibernateStatementAssertionResult(UPDATE, statisticsSupplier.get().getUpdateStatementCount(), expectedUpdateStatementCount);
19+
}
20+
21+
public HibernateStatementAssertionResult generateSelectStatementAssertion(int expectedSelectStatementCount, Supplier<HibernateStatistics> statisticsSupplier) {
22+
return new HibernateStatementAssertionResult(SELECT, statisticsSupplier.get().getSelectStatementCount(), expectedSelectStatementCount);
23+
}
24+
25+
public HibernateStatementAssertionResult generateDeleteStatementAssertion(int expectedDeleteStatementCount, Supplier<HibernateStatistics> statisticsSupplier) {
26+
return new HibernateStatementAssertionResult(DELETE, statisticsSupplier.get().getDeleteStatementCount(), expectedDeleteStatementCount);
27+
}
28+
29+
public void resetStatistics() {
30+
HibernateStatementCountInspector.resetStatistics();
31+
}
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.lemick.assertions;
2+
3+
4+
import com.lemick.integration.hibernate.HibernateStatementCountInspector;
5+
import com.lemick.integration.hibernate.HibernateStatistics;
6+
7+
import java.util.function.Supplier;
8+
9+
/**
10+
* Static assertion class usable in tests
11+
*/
12+
public class HibernateStatementAssertionsUtils {
13+
14+
private static final HibernateStatementAssertionsProvider hibernateStatementAssertionsProvider = new HibernateStatementAssertionsProvider();
15+
private static final Supplier<HibernateStatistics> statisticsSupplier = HibernateStatementCountInspector::getStatistics;
16+
17+
public static void assertInsertStatementCount(int expectedInsertStatementCount) {
18+
hibernateStatementAssertionsProvider.generateInsertStatementAssertion(expectedInsertStatementCount, statisticsSupplier).validate();
19+
}
20+
21+
public static void assertUpdateStatementCount(int expectedUpdateStatementCount) {
22+
hibernateStatementAssertionsProvider.generateUpdateStatementAssertion(expectedUpdateStatementCount, statisticsSupplier).validate();
23+
}
24+
25+
public static void assertSelectStatementCount(int expectedSelectStatementCount) {
26+
hibernateStatementAssertionsProvider.generateSelectStatementAssertion(expectedSelectStatementCount, statisticsSupplier).validate();
27+
}
28+
29+
public static void assertDeleteStatementCount(int expectedDeleteStatementCount) {
30+
hibernateStatementAssertionsProvider.generateDeleteStatementAssertion(expectedDeleteStatementCount, statisticsSupplier).validate();
31+
}
32+
}

src/main/java/com/lemick/integration/spring/HibernateStatementCountTestListener.java

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,37 @@
11
package com.lemick.integration.spring;
22

33

4-
import com.lemick.api.AssertSQLStatementCount;
5-
import com.lemick.assertions.HibernateStatementAssertUtils;
4+
import com.lemick.api.AssertHibernateSQLStatementCount;
65
import com.lemick.assertions.HibernateStatementAssertionResults;
6+
import com.lemick.assertions.HibernateStatementAssertionsProvider;
7+
import com.lemick.integration.hibernate.HibernateStatementCountInspector;
8+
import com.lemick.integration.hibernate.HibernateStatistics;
79
import org.springframework.core.Ordered;
810
import org.springframework.test.context.TestContext;
911
import org.springframework.test.context.TestExecutionListener;
1012

1113
import java.util.List;
14+
import java.util.function.Supplier;
1215

13-
/**
14-
* TODO
15-
* Permettre les asserts dans le test
16-
* Augmenter les type d'assert (mise en cache, etc...)
17-
* Classe Thread-safe
18-
*/
1916
public class HibernateStatementCountTestListener implements TestExecutionListener, Ordered {
2017

21-
private HibernateStatementAssertUtils hibernateStatementAssertUtils = new HibernateStatementAssertUtils();
18+
private HibernateStatementAssertionsProvider hibernateStatementAssertionsProvider = new HibernateStatementAssertionsProvider();
19+
private Supplier<HibernateStatistics> statisticsSupplier = HibernateStatementCountInspector::getStatistics;
2220

2321
@Override
2422
public void beforeTestMethod(TestContext testContext) {
25-
AssertSQLStatementCount annotation = testContext.getTestMethod().getAnnotation(AssertSQLStatementCount.class);
26-
if (annotation != null) {
27-
hibernateStatementAssertUtils.resetCounts();
28-
}
23+
hibernateStatementAssertionsProvider.resetStatistics();
2924
}
3025

3126
@Override
3227
public void afterTestMethod(TestContext testContext) {
33-
AssertSQLStatementCount annotation = testContext.getTestMethod().getAnnotation(AssertSQLStatementCount.class);
28+
AssertHibernateSQLStatementCount annotation = testContext.getTestMethod().getAnnotation(AssertHibernateSQLStatementCount.class);
3429
if (annotation != null) {
3530
HibernateStatementAssertionResults assertionResults = new HibernateStatementAssertionResults(List.of(
36-
hibernateStatementAssertUtils.assertSelectStatementCount(annotation.selects()),
37-
hibernateStatementAssertUtils.assertUpdateStatementCount(annotation.updates()),
38-
hibernateStatementAssertUtils.assertInsertStatementCount(annotation.inserts()),
39-
hibernateStatementAssertUtils.assertDeleteStatementCount(annotation.deletes())
31+
hibernateStatementAssertionsProvider.generateSelectStatementAssertion(annotation.selects(), statisticsSupplier),
32+
hibernateStatementAssertionsProvider.generateUpdateStatementAssertion(annotation.updates(), statisticsSupplier),
33+
hibernateStatementAssertionsProvider.generateInsertStatementAssertion(annotation.inserts(), statisticsSupplier),
34+
hibernateStatementAssertionsProvider.generateDeleteStatementAssertion(annotation.deletes(), statisticsSupplier)
4035
));
4136
assertionResults.validate();
4237
}

src/test/java/com/lemick/assertions/HibernateStatementAssertionResultTest.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,31 @@
88
class HibernateStatementAssertionResultTest {
99

1010
@Test
11-
public void _result_in_error() {
11+
public void _isInError_true() {
1212
HibernateStatementAssertionResult model = new HibernateStatementAssertionResult(SELECT, 0, 1);
1313

1414
assertTrue(model.isInError(), "the result is in error");
1515
assertEquals("Expected 1 SELECT but was 0", model.getErrorMessage(), "the error message is correct");
1616
}
1717

1818
@Test
19-
public void _result_not_in_error() {
19+
public void _isInError_false() {
2020
HibernateStatementAssertionResult model = new HibernateStatementAssertionResult(SELECT, 1, 1);
2121

2222
assertFalse(model.isInError(), "the result is not in error");
2323
}
24+
25+
@Test
26+
public void _validate_does_not_throw() {
27+
HibernateStatementAssertionResult model = new HibernateStatementAssertionResult(SELECT, 1, 1);
28+
29+
assertDoesNotThrow(model::validate);
30+
}
31+
32+
@Test
33+
public void _validate_throws() {
34+
HibernateStatementAssertionResult model = new HibernateStatementAssertionResult(SELECT, 2, 1);
35+
36+
assertThrows(HibernateStatementCountException.class, model::validate);
37+
}
2438
}

0 commit comments

Comments
 (0)