Skip to content

Commit 0b91050

Browse files
authored
Merge pull request #35 from SentryMan/main
Unhandled Container Type Exception/Annotation Processor Tests
2 parents ff3dfea + b16ec54 commit 0b91050

File tree

7 files changed

+232
-4
lines changed

7 files changed

+232
-4
lines changed

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ build/
44
*.iml
55
.gradle
66
*.prefs
7-
jsonb-generator/.classpath
8-
jsonb-generator/.project
7+
*.classpath
8+
*.project
9+
*.class

jsonb-generator/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
<version>1.1</version>
2727
<scope>test</scope>
2828
</dependency>
29-
3029
</dependencies>
3130

3231

jsonb-generator/src/main/java/io/avaje/jsonb/generator/GenericType.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,12 @@ private String asTypeContainer() {
178178
return "Types.setOf(" + Util.shortName(param.topType()) + ".class)";
179179
case "java.util.stream.Stream":
180180
return "Types.streamOf(" + Util.shortName(param.topType()) + ".class)";
181+
default:
182+
throw new IllegalArgumentException(
183+
"Unsupported Container Type "
184+
+ containerType
185+
+ ", only java.util List/Set/Stream allowed");
181186
}
182-
return "FIXME: Unhandled Container Type " + containerType;
183187
}
184188

185189
String firstParamType() {
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package io.avaje.jsonb.generator;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
5+
6+
import java.io.File;
7+
import java.io.IOException;
8+
import java.io.PrintWriter;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
import java.nio.file.Paths;
12+
import java.util.Arrays;
13+
import java.util.Collections;
14+
import java.util.Comparator;
15+
import java.util.Set;
16+
17+
import javax.tools.JavaCompiler;
18+
import javax.tools.JavaCompiler.CompilationTask;
19+
import javax.tools.JavaFileObject;
20+
import javax.tools.JavaFileObject.Kind;
21+
import javax.tools.StandardJavaFileManager;
22+
import javax.tools.StandardLocation;
23+
import javax.tools.ToolProvider;
24+
25+
import org.junit.jupiter.api.AfterEach;
26+
import org.junit.jupiter.api.Test;
27+
28+
class ProcessorTest {
29+
30+
@AfterEach
31+
void deleteGeneratedFiles() throws IOException {
32+
try {
33+
Paths.get("io.avaje.jsonb.Jsonb$GeneratedComponent").toAbsolutePath().toFile().delete();
34+
Files.walk(Paths.get("io").toAbsolutePath())
35+
.sorted(Comparator.reverseOrder())
36+
.map(Path::toFile)
37+
.forEach(File::delete);
38+
} catch (final Exception e) {
39+
}
40+
}
41+
42+
@Test
43+
void testGeneration() throws Exception {
44+
final String source =
45+
Paths.get("src/test/java/io/avaje/jsonb/generator/models/valid")
46+
.toAbsolutePath()
47+
.toString();
48+
49+
final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
50+
final StandardJavaFileManager manager = compiler.getStandardFileManager(null, null, null);
51+
52+
manager.setLocation(StandardLocation.SOURCE_PATH, Arrays.asList(new File(source)));
53+
54+
final Set<Kind> fileKinds = Collections.singleton(Kind.SOURCE);
55+
56+
final Iterable<JavaFileObject> files =
57+
manager.list(StandardLocation.SOURCE_PATH, "", fileKinds, true);
58+
59+
final CompilationTask task =
60+
compiler.getTask(
61+
new PrintWriter(System.out), null, null, Arrays.asList("--release=8"), null, files);
62+
task.setProcessors(Arrays.asList(new Processor()));
63+
64+
assertThat(task.call()).isTrue();
65+
}
66+
67+
@Test
68+
void testImportFail() throws Exception {
69+
70+
final Iterable<JavaFileObject> files = getInvalidSourceFile("InvalidImport");
71+
72+
final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
73+
74+
final CompilationTask task =
75+
compiler.getTask(
76+
new PrintWriter(System.out), null, null, Arrays.asList("--release=8"), null, files);
77+
task.setProcessors(Arrays.asList(new Processor()));
78+
79+
assertThat(task.call()).isFalse();
80+
Files.walk(Paths.get("java").toAbsolutePath())
81+
.sorted(Comparator.reverseOrder())
82+
.map(Path::toFile)
83+
.forEach(File::delete);
84+
}
85+
86+
@Test
87+
void testInvalidCollection() throws Exception {
88+
89+
final Iterable<JavaFileObject> files = getInvalidSourceFile("InvalidCollection");
90+
91+
final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
92+
93+
final CompilationTask task =
94+
compiler.getTask(
95+
new PrintWriter(System.out), null, null, Arrays.asList("--release=8"), null, files);
96+
task.setProcessors(Arrays.asList(new Processor()));
97+
98+
assertThatExceptionOfType(RuntimeException.class)
99+
.isThrownBy(task::call)
100+
.havingCause()
101+
.isInstanceOf(IllegalArgumentException.class);
102+
}
103+
104+
private Iterable<JavaFileObject> getInvalidSourceFile(String name) throws Exception {
105+
final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
106+
final StandardJavaFileManager files = compiler.getStandardFileManager(null, null, null);
107+
108+
files.setLocation(
109+
StandardLocation.SOURCE_PATH,
110+
Arrays.asList(
111+
new File(
112+
Paths.get("src/test/java/io/avaje/jsonb/generator/models/invalid")
113+
.toAbsolutePath()
114+
.toString())));
115+
116+
final Set<Kind> fileKinds = Collections.singleton(Kind.SOURCE);
117+
final Iterable<JavaFileObject> jfos =
118+
files.list(StandardLocation.SOURCE_PATH, "", fileKinds, true);
119+
120+
for (final JavaFileObject jfo : jfos) {
121+
if (jfo.getName().contains(name)) return Collections.singleton(jfo);
122+
}
123+
124+
return null;
125+
}
126+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.avaje.jsonb.generator.models.invalid;
2+
3+
import java.util.ArrayList;
4+
5+
import io.avaje.jsonb.Json;
6+
7+
@Json
8+
public class InvalidCollection {
9+
private ArrayList<String> list;
10+
11+
public ArrayList<String> getList() {
12+
return list;
13+
}
14+
15+
public void setList(ArrayList<String> list) {
16+
this.list = list;
17+
}
18+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.avaje.jsonb.generator.models.invalid;
2+
3+
import java.util.AbstractMap.SimpleImmutableEntry;
4+
5+
import io.avaje.jsonb.Json;
6+
7+
@Json
8+
@Json.Import(SimpleImmutableEntry.class)
9+
public class InvalidImport {
10+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package io.avaje.jsonb.generator.models.valid;
2+
3+
import java.util.List;
4+
5+
import io.avaje.jsonb.Json;
6+
7+
@Json
8+
public class TestClass {
9+
10+
private String s;
11+
12+
private int i;
13+
14+
private Integer integer;
15+
16+
private char ch;
17+
18+
private Character chara;
19+
20+
private List<String> list;
21+
22+
23+
public String getS() {
24+
return s;
25+
}
26+
27+
public void setS(String s) {
28+
this.s = s;
29+
}
30+
31+
public int getI() {
32+
return i;
33+
}
34+
35+
public void setI(int i) {
36+
this.i = i;
37+
}
38+
39+
public Integer getInteger() {
40+
return integer;
41+
}
42+
43+
public void setInteger(Integer integer) {
44+
this.integer = integer;
45+
}
46+
47+
public char getCh() {
48+
return ch;
49+
}
50+
51+
public void setCh(char ch) {
52+
this.ch = ch;
53+
}
54+
55+
public Character getChara() {
56+
return chara;
57+
}
58+
59+
public void setChara(Character chara) {
60+
this.chara = chara;
61+
}
62+
63+
public List<String> getList() {
64+
return list;
65+
}
66+
67+
public void setList(List<String> list) {
68+
this.list = list;
69+
}
70+
}

0 commit comments

Comments
 (0)