Skip to content

Commit 0ec5a73

Browse files
committed
feat: more added tests regarding on replacing WeakHashMap caches with thread-safe maps
1 parent 658236d commit 0ec5a73

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.querydsl.core.alias;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import com.querydsl.core.types.Expression;
6+
import com.querydsl.core.types.dsl.Expressions;
7+
import org.junit.Test;
8+
9+
/** Functional-caching behaviour (non-concurrent) for AliasFactory. */
10+
public class AliasFactoryCachingTest {
11+
12+
private final AliasFactory factory =
13+
new AliasFactory(new DefaultPathFactory(), new DefaultTypeSystem());
14+
15+
@Test
16+
public void sameExpression_returnsSameAliasInstance() {
17+
Expression<Object> expr = Expressions.path(Object.class, "obj");
18+
Object first = factory.createAliasForExpr(Object.class, expr);
19+
Object second = factory.createAliasForExpr(Object.class, expr);
20+
21+
assertThat(first).isSameAs(second);
22+
}
23+
24+
@Test
25+
public void differentExpressions_returnDifferentAliasInstances() {
26+
Object first =
27+
factory.createAliasForExpr(Object.class, Expressions.path(Object.class, "obj1"));
28+
Object second =
29+
factory.createAliasForExpr(Object.class, Expressions.path(Object.class, "obj2"));
30+
31+
assertThat(first).isNotSameAs(second);
32+
}
33+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.querydsl.core.types;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.Test;
6+
7+
/** Verifies that TemplateFactory re-uses parsed Template objects. */
8+
public class TemplateFactoryCacheTest {
9+
10+
private final TemplateFactory factory = new TemplateFactory('\\');
11+
12+
@Test
13+
public void identicalTemplateString_isReturnedFromCache() {
14+
Template t1 = factory.create("{0}");
15+
Template t2 = factory.create("{0}");
16+
17+
assertThat(t1).isSameAs(t2);
18+
}
19+
20+
@Test
21+
public void distinctTemplateStrings_areDifferentInstances() {
22+
Template hello = factory.create("{0}");
23+
Template world = factory.create("{1}");
24+
25+
assertThat(hello).isNotSameAs(world);
26+
}
27+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.querydsl.core.types.dsl;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import com.querydsl.core.domain.QCat;
6+
import org.junit.Test;
7+
8+
/** Ensures ListPath index caching works as intended. */
9+
public class ListPathCachingTest {
10+
11+
private static final QCat CAT = QCat.cat;
12+
13+
@Test
14+
public void sameIndex_returnsSamePathInstance() {
15+
QCat first = CAT.kittens(0);
16+
QCat second = CAT.kittens(0);
17+
18+
assertThat(first).isSameAs(second);
19+
}
20+
21+
@Test
22+
public void differentIndices_returnDistinctPathInstances() {
23+
QCat zero = CAT.kittens(0);
24+
QCat one = CAT.kittens(1);
25+
26+
assertThat(zero).isNotSameAs(one);
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.querydsl.codegen.utils;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.net.URL;
6+
import javax.tools.JavaFileManager;
7+
import javax.tools.ToolProvider;
8+
import org.junit.Test;
9+
10+
/** Functional sanity-checks for MemFileSystemRegistry. */
11+
public class MemFileSystemRegistryFunctionalityTest {
12+
13+
@Test
14+
public void sameFileManager_returnsIdenticalPrefix() {
15+
JavaFileManager jfm =
16+
ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null);
17+
18+
String first = MemFileSystemRegistry.DEFAULT.getUrlPrefix(jfm);
19+
String second = MemFileSystemRegistry.DEFAULT.getUrlPrefix(jfm);
20+
21+
assertThat(first).isEqualTo(second);
22+
}
23+
24+
@Test
25+
public void prefix_canRoundTripToFileSystem() throws Exception {
26+
JavaFileManager jfm =
27+
ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null);
28+
29+
String prefix = MemFileSystemRegistry.DEFAULT.getUrlPrefix(jfm);
30+
// Construct a dummy URL within that prefix; path component can be empty.
31+
URL url = new URL(prefix);
32+
33+
assertThat(MemFileSystemRegistry.DEFAULT.getFileSystem(url)).isSameAs(jfm);
34+
}
35+
}

0 commit comments

Comments
 (0)