Skip to content

Commit 57e8c73

Browse files
committed
The statements are now displayed in case of assertion error
1 parent 2000c5a commit 57e8c73

13 files changed

+126
-173
lines changed

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
11
package com.lemick.assertions;
22

3+
import java.util.List;
4+
import java.util.stream.Collectors;
5+
36
public class HibernateStatementAssertionResult implements HibernateStatementAssertionValidator {
47

58
public enum StatementType {SELECT, INSERT, UPDATE, DELETE}
69

710
private StatementType type;
8-
private int actual;
11+
private List<String> statements;
912
private int expected;
1013

11-
public HibernateStatementAssertionResult(StatementType type, int actual, int expected) {
14+
public HibernateStatementAssertionResult(StatementType type, List<String> statements, int expected) {
1215
this.type = type;
13-
this.actual = actual;
16+
this.statements = statements;
1417
this.expected = expected;
1518
}
1619

1720
public boolean isInError() {
18-
return actual != expected;
21+
return statements.size() != expected;
1922
}
2023

2124
public String getErrorMessage() {
22-
return "Expected " + expected + " " + type.name() + " but was " + actual;
25+
String header = "Expected " + expected + " " + type.name() + " but got " + statements.size() + ":" + System.lineSeparator();
26+
String statementsDetail = statements.stream()
27+
.map(statement -> " => '" + statement + "'")
28+
.collect(Collectors.joining(System.lineSeparator()));
29+
return header + statementsDetail;
2330
}
2431

2532
@Override

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public void validate() {
1818
.collect(Collectors.toList());
1919

2020
if (assertionsInError.size() > 0) {
21-
String errorMessages = assertionsInError.stream()
21+
String errorMessages = System.lineSeparator() + assertionsInError.stream()
2222
.map(HibernateStatementAssertionResult::getErrorMessage)
23-
.collect(Collectors.joining(System.lineSeparator()));
23+
.collect(Collectors.joining(System.lineSeparator() + System.lineSeparator()));
2424
throw new HibernateStatementCountException(errorMessages);
2525
}
2626
}

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

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

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

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

src/main/java/com/lemick/integration/hibernate/HibernateStatementCountListener.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
public interface HibernateStatementCountListener {
44

5-
void notifySelectStatement();
6-
void notifyUpdateStatement();
7-
void notifyInsertStatement();
8-
void notifyDeleteStatement();
5+
void notifySelectStatement(String sql);
6+
void notifyUpdateStatement(String sql);
7+
void notifyInsertStatement(String sql);
8+
void notifyDeleteStatement(String sql);
99
}
Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,56 @@
11
package com.lemick.integration.hibernate;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
5+
36
public class HibernateStatistics implements HibernateStatementCountListener{
47

5-
private int selectStatementCount = 0;
6-
private int updateStatementCount = 0;
7-
private int insertStatementCount = 0;
8-
private int deleteStatementCount = 0;
8+
private final List<String> selectStatements = new ArrayList<>();
9+
private final List<String> updateStatements = new ArrayList<>();
10+
private final List<String> insertStatements = new ArrayList<>();
11+
private final List<String> deleteStatements = new ArrayList<>();
912

1013
public void resetStatistics() {
11-
selectStatementCount = 0;
12-
updateStatementCount = 0;
13-
insertStatementCount = 0;
14-
deleteStatementCount = 0;
14+
selectStatements.clear();
15+
updateStatements.clear();
16+
insertStatements.clear();
17+
deleteStatements.clear();
18+
1519
}
1620

1721
@Override
18-
public void notifySelectStatement() {
19-
selectStatementCount++;
22+
public void notifySelectStatement(String sql) {
23+
selectStatements.add(sql);
2024
}
2125

2226
@Override
23-
public void notifyUpdateStatement() {
24-
updateStatementCount++;
27+
public void notifyUpdateStatement(String sql) {
28+
updateStatements.add(sql);
2529
}
2630

2731
@Override
28-
public void notifyInsertStatement() {
29-
insertStatementCount++;
32+
public void notifyInsertStatement(String sql) {
33+
insertStatements.add(sql);
3034
}
3135

3236
@Override
33-
public void notifyDeleteStatement() {
34-
deleteStatementCount++;
37+
public void notifyDeleteStatement(String sql) {
38+
deleteStatements.add(sql);
3539
}
3640

37-
public int getSelectStatementCount() {
38-
return selectStatementCount;
41+
public List<String> getSelectStatements() {
42+
return selectStatements;
3943
}
4044

41-
public int getUpdateStatementCount() {
42-
return updateStatementCount;
45+
public List<String> getUpdateStatements() {
46+
return updateStatements;
4347
}
4448

45-
public int getInsertStatementCount() {
46-
return insertStatementCount;
49+
public List<String> getInsertStatements() {
50+
return insertStatements;
4751
}
4852

49-
public int getDeleteStatementCount() {
50-
return deleteStatementCount;
53+
public List<String> getDeleteStatements() {
54+
return deleteStatements;
5155
}
5256
}

src/main/java/com/lemick/integration/hibernate/JSQLHibernateStatementParser.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,22 @@ public void parseSqlStatement(String sql, HibernateStatementCountListener statem
1818
statement.accept(new StatementVisitorAdapter() {
1919
@Override
2020
public void visit(Select select) {
21-
statementCountListener.notifySelectStatement();
21+
statementCountListener.notifySelectStatement(sql);
2222
}
2323

2424
@Override
2525
public void visit(Insert insert) {
26-
statementCountListener.notifyInsertStatement();
26+
statementCountListener.notifyInsertStatement(sql);
2727
}
2828

2929
@Override
3030
public void visit(Update update) {
31-
statementCountListener.notifyUpdateStatement();
31+
statementCountListener.notifyUpdateStatement(sql);
3232
}
3333

3434
@Override
3535
public void visit(Delete delete) {
36-
statementCountListener.notifyDeleteStatement();
36+
statementCountListener.notifyDeleteStatement(sql);
3737
}
3838
});
3939
} catch (JSQLParserException e) {

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33

44
import com.lemick.api.AssertHibernateSQLStatementCount;
5+
import com.lemick.assertions.HibernateStatementAssertionResult;
56
import com.lemick.assertions.HibernateStatementAssertionResults;
6-
import com.lemick.assertions.HibernateStatementAssertionsProvider;
77
import com.lemick.integration.hibernate.HibernateStatementCountInspector;
88
import com.lemick.integration.hibernate.HibernateStatistics;
99
import jakarta.persistence.EntityManager;
@@ -15,9 +15,10 @@
1515
import java.util.List;
1616
import java.util.function.Supplier;
1717

18+
import static com.lemick.assertions.HibernateStatementAssertionResult.StatementType.*;
19+
1820
public class HibernateStatementCountTestListener implements TestExecutionListener, Ordered {
1921

20-
private HibernateStatementAssertionsProvider hibernateStatementAssertionsProvider = new HibernateStatementAssertionsProvider();
2122
private Supplier<HibernateStatistics> statisticsSupplier = HibernateStatementCountInspector::getStatistics;
2223
private Supplier<Boolean> transactionAvailabilitySupplier = TestTransaction::isActive;
2324

@@ -57,10 +58,10 @@ private void flushExistingPersistenceContext(TestContext testContext, Supplier<B
5758

5859
private void doStatementCountEvaluation(AssertHibernateSQLStatementCount annotation) {
5960
HibernateStatementAssertionResults assertionResults = new HibernateStatementAssertionResults(List.of(
60-
hibernateStatementAssertionsProvider.generateSelectStatementAssertion(annotation.selects(), statisticsSupplier),
61-
hibernateStatementAssertionsProvider.generateUpdateStatementAssertion(annotation.updates(), statisticsSupplier),
62-
hibernateStatementAssertionsProvider.generateInsertStatementAssertion(annotation.inserts(), statisticsSupplier),
63-
hibernateStatementAssertionsProvider.generateDeleteStatementAssertion(annotation.deletes(), statisticsSupplier)
61+
new HibernateStatementAssertionResult(SELECT, statisticsSupplier.get().getSelectStatements(), annotation.selects()),
62+
new HibernateStatementAssertionResult(UPDATE, statisticsSupplier.get().getUpdateStatements(), annotation.updates()),
63+
new HibernateStatementAssertionResult(INSERT, statisticsSupplier.get().getInsertStatements(), annotation.inserts()),
64+
new HibernateStatementAssertionResult(DELETE, statisticsSupplier.get().getDeleteStatements(), annotation.deletes())
6465
));
6566
assertionResults.validate();
6667
}

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,48 @@
22

33
import org.junit.jupiter.api.Test;
44

5+
import java.util.List;
6+
57
import static com.lemick.assertions.HibernateStatementAssertionResult.StatementType.SELECT;
68
import static org.junit.jupiter.api.Assertions.*;
79

810
class HibernateStatementAssertionResultTest {
911

1012
@Test
1113
public void _isInError_true() {
12-
HibernateStatementAssertionResult model = new HibernateStatementAssertionResult(SELECT, 0, 1);
14+
HibernateStatementAssertionResult model = new HibernateStatementAssertionResult(SELECT, List.of("SELECT 1", "SELECT 2"), 1);
1315

1416
assertTrue(model.isInError(), "the result is in error");
15-
assertEquals("Expected 1 SELECT but was 0", model.getErrorMessage(), "the error message is correct");
1617
}
1718

1819
@Test
1920
public void _isInError_false() {
20-
HibernateStatementAssertionResult model = new HibernateStatementAssertionResult(SELECT, 1, 1);
21+
HibernateStatementAssertionResult model = new HibernateStatementAssertionResult(SELECT, List.of("SELECT 1"), 1);
2122

2223
assertFalse(model.isInError(), "the result is not in error");
2324
}
2425

2526
@Test
2627
public void _validate_does_not_throw() {
27-
HibernateStatementAssertionResult model = new HibernateStatementAssertionResult(SELECT, 1, 1);
28+
HibernateStatementAssertionResult model = new HibernateStatementAssertionResult(SELECT, List.of("SELECT 1"), 1);
2829

2930
assertDoesNotThrow(model::validate);
3031
}
3132

3233
@Test
3334
public void _validate_throws() {
34-
HibernateStatementAssertionResult model = new HibernateStatementAssertionResult(SELECT, 2, 1);
35+
HibernateStatementAssertionResult model = new HibernateStatementAssertionResult(SELECT, List.of("SELECT 1", "SELECT 2"), 1);
3536

3637
assertThrows(HibernateStatementCountException.class, model::validate);
3738
}
39+
40+
@Test
41+
public void _getErrorMessage() {
42+
HibernateStatementAssertionResult model = new HibernateStatementAssertionResult(SELECT, List.of("SELECT 1", "SELECT 2"), 1);
43+
44+
String expected = "Expected 1 SELECT but got 2:" + System.lineSeparator() +
45+
" => 'SELECT 1'" + System.lineSeparator() +
46+
" => 'SELECT 2'";
47+
assertEquals(expected, model.getErrorMessage(), "the error message is correct");
48+
}
3849
}

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
class HibernateStatementAssertionResultsTest {
1111

12+
static final String EOL = System.lineSeparator();
13+
1214
@Test
1315
public void _results_empty() {
1416
HibernateStatementAssertionResults model = new HibernateStatementAssertionResults(List.of());
@@ -19,17 +21,24 @@ public void _results_empty() {
1921
@Test
2022
public void _results_invalid() {
2123
HibernateStatementAssertionResults model = new HibernateStatementAssertionResults(List.of(
22-
new HibernateStatementAssertionResult(SELECT, 0, 2),
23-
new HibernateStatementAssertionResult(INSERT, 1, 1),
24-
new HibernateStatementAssertionResult(DELETE, 4, 2),
25-
new HibernateStatementAssertionResult(UPDATE, 3, 2)
24+
new HibernateStatementAssertionResult(SELECT, List.of("SELECT 1"), 2),
25+
new HibernateStatementAssertionResult(INSERT, List.of("INSERT 1","INSERT 2"), 1),
26+
new HibernateStatementAssertionResult(DELETE, List.of("DELETE 1"), 1),
27+
new HibernateStatementAssertionResult(UPDATE, List.of("UPDATE 1"), 2)
2628
));
2729

2830
HibernateStatementCountException actual = assertThrows(HibernateStatementCountException.class, model::validate, "validation error is thrown");
2931

30-
String expected = "Expected 2 SELECT but was 0" + System.lineSeparator() +
31-
"Expected 2 DELETE but was 4" + System.lineSeparator() +
32-
"Expected 2 UPDATE but was 3";
32+
String expected = EOL +
33+
"Expected 2 SELECT but got 1:" + EOL +
34+
" => 'SELECT 1'" + EOL +
35+
EOL +
36+
"Expected 1 INSERT but got 2:" + EOL +
37+
" => 'INSERT 1'" + EOL +
38+
" => 'INSERT 2'" + EOL +
39+
EOL +
40+
"Expected 2 UPDATE but got 1:" + EOL +
41+
" => 'UPDATE 1'";
3342
assertEquals(expected, actual.getMessage(), "the error message is correct");
3443
}
3544
}

0 commit comments

Comments
 (0)