Skip to content

Commit a565346

Browse files
committed
Add L2C tracking of hits, misses and puts | Renaming
1 parent f8cdd6e commit a565346

29 files changed

+526
-88
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ The assertion will work seamlessly whether you're testing Spring repositories or
2929
blogPostRepository.save(post_2);
3030
}
3131

32-
If the actual count is different, an exception is thrown with the executed statements:
32+
If the actual count is different, an exception is thrown with the executed statements:
3333

34-
com.mickaelb.assertions.HibernateStatementCountException:
34+
com.mickaelb.assertions.HibernateAssertCountException:
3535
Expected 5 INSERT but got 6:
3636
=> '/* insert com.lemick.demo.entity.BlogPost */ insert into blog_post (id, title) values (default, ?)'
3737
=> '/* insert com.lemick.demo.entity.PostComment */ insert into post_comment (id, blog_post_id, content) values (default, ?, ?)'
@@ -54,7 +54,7 @@ The assertion will work seamlessly whether you're testing Spring repositories or
5454
spring:
5555
jpa:
5656
properties:
57-
hibernate.session_factory.statement_inspector: com.mickaelb.integration.hibernate.HibernateStatementCountInspector
57+
hibernate.session_factory.statement_inspector: com.mickaelb.integration.hibernate.HibernateStatementInspector
5858

5959
3. Register the Spring TestListener that will launch the SQL inspection if the annotation is present:
6060

@@ -71,5 +71,5 @@ The assertion will work seamlessly whether you're testing Spring repositories or
7171

7272
* **OR** by adding a **META-INF/spring.factories** file that contains the definition, that will register the listener for all your tests:
7373

74-
org.springframework.test.context.TestExecutionListener=com.mickaelb.integration.spring.HibernateStatementCountTestListener
74+
org.springframework.test.context.TestExecutionListener=com.mickaelb.integration.spring.HibernateAssertTestListener
7575

pom.xml

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

77
<groupId>com.mickaelb</groupId>
88
<artifactId>hibernate-query-asserts</artifactId>
9-
<version>1.0.0-SNAPSHOT</version>
9+
<version>1.0.0</version>
1010

1111
<name>${project.groupId}:${project.artifactId}</name>
1212
<description>A library that can assert SQL statement count generated by Hibernate in Spring tests</description>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.mickaelb.api;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Target(ElementType.METHOD)
9+
@Retention(RetentionPolicy.RUNTIME)
10+
public @interface AssertHibernateL2CCount {
11+
12+
int hits() default 0;
13+
14+
int misses() default 0;
15+
16+
int puts() default 0;
17+
}

src/main/java/com/mickaelb/assertions/HibernateStatementCountException.java

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
* NOT Thread-Safe since it is meant to be used by Spring tests that are not multi-threaded
77
* ThreadLocal does not work because a test can span on multiple threads (ex: a test executing a HTTP request on the server)
88
*/
9-
public class HibernateStatementCountInspector implements StatementInspector {
9+
public class HibernateStatementInspector implements StatementInspector {
1010

11-
private static final HibernateStatistics statistics = new HibernateStatistics();
11+
private static final HibernateStatementStatistics statistics = new HibernateStatementStatistics();
1212

1313
private HibernateStatementParser statementParser = new JSQLHibernateStatementParser();
1414

@@ -18,7 +18,7 @@ public String inspect(String sql) {
1818
return sql;
1919
}
2020

21-
public static HibernateStatistics getStatistics() {
21+
public static HibernateStatementStatistics getStatistics() {
2222
return statistics;
2323
}
2424

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.mickaelb.integration.hibernate;
22

3-
public interface HibernateStatementCountListener {
3+
public interface HibernateStatementListener {
44

55
void notifySelectStatement(String sql);
66
void notifyUpdateStatement(String sql);

src/main/java/com/mickaelb/integration/hibernate/HibernateStatementParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
public interface HibernateStatementParser {
44

5-
void parseSqlStatement(String sql, HibernateStatementCountListener statementCountListener);
5+
void parseSqlStatement(String sql, HibernateStatementListener statementCountListener);
66
}

src/main/java/com/mickaelb/integration/hibernate/HibernateStatistics.java renamed to src/main/java/com/mickaelb/integration/hibernate/HibernateStatementStatistics.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6-
public class HibernateStatistics implements HibernateStatementCountListener{
6+
public class HibernateStatementStatistics implements HibernateStatementListener {
77

88
private final List<String> selectStatements = new ArrayList<>();
99
private final List<String> updateStatements = new ArrayList<>();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
public class JSQLHibernateStatementParser implements HibernateStatementParser {
1313

1414
@Override
15-
public void parseSqlStatement(String sql, HibernateStatementCountListener statementCountListener) {
15+
public void parseSqlStatement(String sql, HibernateStatementListener statementCountListener) {
1616
try {
1717
Statement statement = CCJSqlParserUtil.parse(sql);
1818
statement.accept(new StatementVisitorAdapter() {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.mickaelb.integration.spring;
2+
3+
import org.springframework.test.context.TestContext;
4+
5+
public interface AssertTestListener {
6+
7+
void beforeTestClass(TestContext testContext);
8+
void beforeTestMethod(TestContext testContext);
9+
void afterTestMethod(TestContext testContext);
10+
}

0 commit comments

Comments
 (0)