Skip to content

Commit 3061a77

Browse files
committed
Tests for all classes
1 parent fc3e5bb commit 3061a77

File tree

5 files changed

+329
-3
lines changed

5 files changed

+329
-3
lines changed

src/main/java/pl/wavesoftware/utils/mapstruct/jpa/collection/UninitializedMap.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
@RequiredArgsConstructor
1515
public final class UninitializedMap<K, V> implements Map<K, V> {
1616

17-
private final Class<?> type;
17+
private final Class<?> keyType;
18+
private final Class<?> valueType;
1819

1920
@Override
2021
public int size() {
@@ -79,10 +80,18 @@ public Set<Entry<K, V>> entrySet() {
7980
throw newLazyInitializationException();
8081
}
8182

83+
@Override
84+
public String toString() {
85+
return getClass().getSimpleName()
86+
+ "<" + keyType.getSimpleName() + ","
87+
+ valueType.getSimpleName() + ">";
88+
}
89+
8290
private RuntimeException newLazyInitializationException() {
8391
return new LazyInitializationException(
84-
"Trying to use uninitialized collection for type: List<"
85-
+ type.getSimpleName()
92+
"Trying to use uninitialized collection for type: Map<"
93+
+ keyType.getSimpleName() + ","
94+
+ valueType.getSimpleName()
8695
+ ">. You need to fetch this collection before using it, for ex. using " +
8796
"JOIN FETCH in JPQL. This exception prevents lazy loading n+1 problem."
8897
);

src/main/java/pl/wavesoftware/utils/mapstruct/jpa/collection/UninitializedSet.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ public void clear() {
8484
throw newLazyInitializationException();
8585
}
8686

87+
@Override
88+
public String toString() {
89+
return getClass().getSimpleName() + "<" + type.getSimpleName() + ">";
90+
}
91+
8792
private RuntimeException newLazyInitializationException() {
8893
return new LazyInitializationException(
8994
"Trying to use uninitialized collection for type: Set<"
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package pl.wavesoftware.utils.mapstruct.jpa.collection;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.junit.runners.Parameterized;
7+
import org.junit.runners.Parameterized.Parameters;
8+
9+
import java.lang.reflect.InvocationTargetException;
10+
import java.lang.reflect.Method;
11+
import java.lang.reflect.Modifier;
12+
import java.lang.reflect.Parameter;
13+
import java.util.ArrayList;
14+
import java.util.Arrays;
15+
import java.util.Collection;
16+
import java.util.List;
17+
import java.util.stream.Collectors;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;
21+
22+
/**
23+
* @author <a href="[email protected]">Krzysztof Suszyński</a>
24+
* @since 2018-05-06
25+
*/
26+
@RequiredArgsConstructor
27+
@RunWith(Parameterized.class)
28+
public class UninitializedListTest {
29+
30+
private final Method method;
31+
private final List<TestThing> list = new UninitializedList<>(TestThing.class);
32+
33+
@Parameters(name = "{0}")
34+
public static Iterable<Method> methods() {
35+
Method[] methods = UninitializedList.class.getDeclaredMethods();
36+
return Arrays.stream(methods)
37+
.filter(m -> Modifier.isPublic(m.getModifiers()))
38+
.filter(m -> !"toString".equals(m.getName()))
39+
.collect(Collectors.toList());
40+
}
41+
42+
@Test
43+
public void testToString() {
44+
assertThat(list.toString()).isEqualTo("UninitializedList<TestThing>");
45+
}
46+
47+
@Test
48+
public void testMethodThrowViaReflection() throws
49+
IllegalAccessException, InstantiationException {
50+
// given
51+
Object[] args = prepareArgs();
52+
53+
// when
54+
try {
55+
method.invoke(list, args);
56+
failBecauseExceptionWasNotThrown(InvocationTargetException.class);
57+
} catch (InvocationTargetException ex) {
58+
// then
59+
assertThat(ex).hasCauseInstanceOf(LazyInitializationException.class);
60+
assertThat(ex.getCause())
61+
.hasMessage(
62+
"Trying to use uninitialized collection for type: " +
63+
"List<TestThing>. You need to fetch this collection before using it, for ex. using " +
64+
"JOIN FETCH in JPQL. This exception prevents lazy loading n+1 problem."
65+
);
66+
}
67+
}
68+
69+
private Object[] prepareArgs() throws IllegalAccessException, InstantiationException {
70+
Object[] objects = new Object[method.getParameterCount()];
71+
for (int i = 0; i < method.getParameterCount(); i++) {
72+
Parameter parameter = method.getParameters()[i];
73+
Object obj = newInstanceOfParameter(parameter);
74+
objects[i] = obj;
75+
}
76+
return objects;
77+
}
78+
79+
private static Object newInstanceOfParameter(Parameter parameter) throws
80+
InstantiationException, IllegalAccessException {
81+
Class<?> type = parameter.getType();
82+
if (type.isPrimitive()) {
83+
if (type == int.class) {
84+
return 1;
85+
} else if (type == boolean.class) {
86+
return true;
87+
}
88+
}
89+
if (type.isArray()) {
90+
return new Object[3];
91+
}
92+
if (type == Collection.class) {
93+
return new ArrayList<>();
94+
}
95+
return parameter.getType().newInstance();
96+
}
97+
98+
private interface TestThing {
99+
100+
}
101+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package pl.wavesoftware.utils.mapstruct.jpa.collection;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.junit.runners.Parameterized;
7+
import org.junit.runners.Parameterized.Parameters;
8+
9+
import java.lang.reflect.InvocationTargetException;
10+
import java.lang.reflect.Method;
11+
import java.lang.reflect.Modifier;
12+
import java.lang.reflect.Parameter;
13+
import java.util.ArrayList;
14+
import java.util.Arrays;
15+
import java.util.Collection;
16+
import java.util.HashMap;
17+
import java.util.Map;
18+
import java.util.stream.Collectors;
19+
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;
22+
23+
/**
24+
* @author <a href="[email protected]">Krzysztof Suszyński</a>
25+
* @since 2018-05-06
26+
*/
27+
@RunWith(Parameterized.class)
28+
@RequiredArgsConstructor
29+
public class UninitializedMapTest {
30+
private final Method method;
31+
private final Map<Object, Object> list = new UninitializedMap<>(
32+
TestThing.class, OtherTestThing.class
33+
);
34+
35+
@Parameters(name = "{0}")
36+
public static Iterable<Method> methods() {
37+
Method[] methods = UninitializedMap.class.getDeclaredMethods();
38+
return Arrays.stream(methods)
39+
.filter(m -> Modifier.isPublic(m.getModifiers()))
40+
.filter(m -> !"toString".equals(m.getName()))
41+
.collect(Collectors.toList());
42+
}
43+
44+
@Test
45+
public void testToString() {
46+
assertThat(list.toString()).isEqualTo("UninitializedMap<TestThing,OtherTestThing>");
47+
}
48+
49+
@Test
50+
public void testMethodThrowViaReflection() throws
51+
IllegalAccessException, InstantiationException {
52+
// given
53+
Object[] args = prepareArgs();
54+
55+
// when
56+
try {
57+
method.invoke(list, args);
58+
failBecauseExceptionWasNotThrown(InvocationTargetException.class);
59+
} catch (InvocationTargetException ex) {
60+
// then
61+
assertThat(ex).hasCauseInstanceOf(LazyInitializationException.class);
62+
assertThat(ex.getCause())
63+
.hasMessage(
64+
"Trying to use uninitialized collection for type: " +
65+
"Map<TestThing,OtherTestThing>. You need to fetch this collection before using it, " +
66+
"for ex. using JOIN FETCH in JPQL. This exception prevents lazy loading n+1 problem."
67+
);
68+
}
69+
}
70+
71+
private Object[] prepareArgs() throws IllegalAccessException, InstantiationException {
72+
Object[] objects = new Object[method.getParameterCount()];
73+
for (int i = 0; i < method.getParameterCount(); i++) {
74+
Parameter parameter = method.getParameters()[i];
75+
Object obj = newInstanceOfParameter(parameter);
76+
objects[i] = obj;
77+
}
78+
return objects;
79+
}
80+
81+
private static Object newInstanceOfParameter(Parameter parameter) throws
82+
InstantiationException, IllegalAccessException {
83+
Class<?> type = parameter.getType();
84+
if (type.isPrimitive()) {
85+
if (type == int.class) {
86+
return 1;
87+
} else if (type == boolean.class) {
88+
return true;
89+
}
90+
}
91+
if (type.isArray()) {
92+
return new Object[1];
93+
}
94+
if (type == Map.class) {
95+
return new HashMap<>();
96+
}
97+
if (type == Collection.class) {
98+
return new ArrayList<>();
99+
}
100+
return parameter.getType().newInstance();
101+
}
102+
103+
private interface OtherTestThing {
104+
105+
}
106+
107+
private interface TestThing {
108+
109+
}
110+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package pl.wavesoftware.utils.mapstruct.jpa.collection;
2+
3+
4+
import lombok.RequiredArgsConstructor;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.junit.runners.Parameterized;
8+
import org.junit.runners.Parameterized.Parameters;
9+
10+
import java.lang.reflect.InvocationTargetException;
11+
import java.lang.reflect.Method;
12+
import java.lang.reflect.Modifier;
13+
import java.lang.reflect.Parameter;
14+
import java.util.ArrayList;
15+
import java.util.Arrays;
16+
import java.util.Collection;
17+
import java.util.Set;
18+
import java.util.stream.Collectors;
19+
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;
22+
23+
/**
24+
* @author <a href="[email protected]">Krzysztof Suszyński</a>
25+
* @since 2018-05-06
26+
*/
27+
@RequiredArgsConstructor
28+
@RunWith(Parameterized.class)
29+
public class UninitializedSetTest {
30+
private final Method method;
31+
private final Set<TestThing> set = new UninitializedSet<>(TestThing.class);
32+
33+
@Parameters(name = "{0}")
34+
public static Iterable<Method> methods() {
35+
Method[] methods = UninitializedSet.class.getDeclaredMethods();
36+
return Arrays.stream(methods)
37+
.filter(m -> Modifier.isPublic(m.getModifiers()))
38+
.filter(m -> !"toString".equals(m.getName()))
39+
.collect(Collectors.toList());
40+
}
41+
42+
@Test
43+
public void testToString() {
44+
assertThat(set.toString()).isEqualTo("UninitializedSet<TestThing>");
45+
}
46+
47+
@Test
48+
public void testMethodThrowViaReflection() throws
49+
IllegalAccessException, InstantiationException {
50+
// given
51+
Object[] args = prepareArgs();
52+
53+
// when
54+
try {
55+
method.invoke(set, args);
56+
failBecauseExceptionWasNotThrown(InvocationTargetException.class);
57+
} catch (InvocationTargetException ex) {
58+
// then
59+
assertThat(ex).hasCauseInstanceOf(LazyInitializationException.class);
60+
assertThat(ex.getCause())
61+
.hasMessage(
62+
"Trying to use uninitialized collection for type: " +
63+
"Set<TestThing>. You need to fetch this collection before using it, for ex. using " +
64+
"JOIN FETCH in JPQL. This exception prevents lazy loading n+1 problem."
65+
);
66+
}
67+
}
68+
69+
private Object[] prepareArgs() throws IllegalAccessException, InstantiationException {
70+
Object[] objects = new Object[method.getParameterCount()];
71+
for (int i = 0; i < method.getParameterCount(); i++) {
72+
Parameter parameter = method.getParameters()[i];
73+
Object obj = newInstanceOfParameter(parameter);
74+
objects[i] = obj;
75+
}
76+
return objects;
77+
}
78+
79+
private static Object newInstanceOfParameter(Parameter parameter) throws
80+
InstantiationException, IllegalAccessException {
81+
Class<?> type = parameter.getType();
82+
if (type.isPrimitive()) {
83+
if (type == int.class) {
84+
return 1;
85+
} else if (type == boolean.class) {
86+
return true;
87+
}
88+
}
89+
if (type.isArray()) {
90+
return new Object[1];
91+
}
92+
if (type == Collection.class) {
93+
return new ArrayList<>();
94+
}
95+
return parameter.getType().newInstance();
96+
}
97+
98+
private interface TestThing {
99+
100+
}
101+
}

0 commit comments

Comments
 (0)