Skip to content

Commit 0efdaa6

Browse files
committed
polish the pom file and improve integration tests
1 parent 653d076 commit 0efdaa6

File tree

2 files changed

+95
-159
lines changed

2 files changed

+95
-159
lines changed

pom.xml

Lines changed: 81 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,34 @@
44
<groupId>com.antkorwin</groupId>
55
<artifactId>better-strings</artifactId>
66
<packaging>jar</packaging>
7-
<version>1.0</version>
7+
<version>1.5</version>
88
<name>BetterStrings Java Plugin</name>
9-
<url>http://maven.apache.org</url>
9+
<description>String interpolation plugin for Java 8..13</description>
10+
11+
<url>https://github.com/antkorwin/better-strings</url>
12+
<scm>
13+
<url>https://github.com/antkorwin/better-strings</url>
14+
<connection>scm:git:git://github.com/antkorwin/better-strings.git</connection>
15+
<developerConnection>scm:git:ssh://git@github.com/antkorwin/better-strings.git</developerConnection>
16+
<tag>HEAD</tag>
17+
</scm>
18+
19+
<issueManagement>
20+
<url>https://github.com/antkorwin/better-strings/issues</url>
21+
</issueManagement>
22+
23+
<developers>
24+
<developer>
25+
<id>antkorwin</id>
26+
<name>Anatoliy Korovin</name>
27+
<email>antkorwin@gmail.com</email>
28+
<url>antkorwin.com</url>
29+
<roles>
30+
<role>architect</role>
31+
<role>developer</role>
32+
</roles>
33+
</developer>
34+
</developers>
1035

1136
<properties>
1237
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -19,17 +44,48 @@
1944
<junit-platform.version>1.3.2</junit-platform.version>
2045
</properties>
2146

22-
<dependencies>
2347

24-
<dependency>
25-
<groupId>com.sun</groupId>
26-
<artifactId>tools</artifactId>
27-
<version>1.4.2</version>
28-
<scope>system</scope>
29-
<systemPath>${java.home}/../lib/tools.jar</systemPath>
30-
</dependency>
48+
<distributionManagement>
49+
<snapshotRepository>
50+
<id>ossrh</id>
51+
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
52+
</snapshotRepository>
53+
<repository>
54+
<id>ossrh</id>
55+
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
56+
</repository>
57+
</distributionManagement>
58+
<repositories>
59+
<repository>
60+
<id>jitpack.io</id>
61+
<url>https://jitpack.io</url>
62+
</repository>
63+
</repositories>
64+
65+
66+
<profiles>
67+
68+
<profile>
69+
<id>jdk-tools</id>
70+
<activation>
71+
<jdk>1.8</jdk>
72+
</activation>
73+
<dependencies>
74+
<dependency>
75+
<groupId>com.sun</groupId>
76+
<artifactId>tools</artifactId>
77+
<version>1.8.0</version>
78+
<scope>system</scope>
79+
<systemPath>${java.home}/../lib/tools.jar</systemPath>
80+
</dependency>
81+
</dependencies>
82+
</profile>
83+
84+
</profiles>
85+
3186

32-
<!-- Junit 5 -->
87+
<dependencies>
88+
<!-- region Junit 5 -->
3389
<dependency>
3490
<groupId>org.junit.jupiter</groupId>
3591
<artifactId>junit-jupiter-api</artifactId>
@@ -60,14 +116,19 @@
60116
<version>3.11.1</version>
61117
<scope>test</scope>
62118
</dependency>
63-
<!-- Junit 5 -->
64-
119+
<!-- endregion Junit 5 -->
120+
<dependency>
121+
<groupId>com.github.jupiter-tools</groupId>
122+
<artifactId>compile-test</artifactId>
123+
<version>-2a36c0f3f0-1</version>
124+
<scope>test</scope>
125+
</dependency>
65126
</dependencies>
66127

67128

68129
<build>
69-
<plugins>
70130

131+
<plugins>
71132
<plugin>
72133
<groupId>org.apache.maven.plugins</groupId>
73134
<artifactId>maven-compiler-plugin</artifactId>
@@ -78,17 +139,10 @@
78139
<compilerArgument>-proc:none</compilerArgument>
79140
<showWarnings>true</showWarnings>
80141
</configuration>
81-
<executions>
82-
<execution>
83-
<id>default-compile</id>
84-
<goals>
85-
<goal>compile</goal>
86-
</goals>
87-
</execution>
88-
</executions>
89142
</plugin>
90143

91144
<plugin>
145+
<groupId>org.apache.maven.plugins</groupId>
92146
<artifactId>maven-jar-plugin</artifactId>
93147
<version>2.4</version>
94148
</plugin>
@@ -106,6 +160,11 @@
106160
</configuration>
107161
</plugin>
108162

163+
<plugin>
164+
<groupId>org.apache.maven.plugins</groupId>
165+
<artifactId>maven-shade-plugin</artifactId>
166+
<version>3.1.0</version>
167+
</plugin>
109168
</plugins>
110169

111170
<resources>
Lines changed: 14 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,7 @@
11
package com.antkorwin.betterstrings;
22

3-
import java.io.ByteArrayOutputStream;
4-
import java.io.IOException;
5-
import java.io.OutputStream;
6-
import java.lang.reflect.Method;
7-
import java.net.URI;
8-
import java.util.ArrayList;
9-
import java.util.Arrays;
10-
import java.util.Iterator;
11-
import java.util.List;
12-
import javax.tools.FileObject;
13-
import javax.tools.ForwardingJavaFileManager;
14-
import javax.tools.JavaCompiler;
15-
import javax.tools.JavaFileManager;
16-
import javax.tools.JavaFileObject;
17-
import javax.tools.SimpleJavaFileObject;
18-
import javax.tools.ToolProvider;
19-
3+
import com.jupitertools.compiletest.CompileTest;
4+
import org.intellij.lang.annotations.Language;
205
import org.junit.jupiter.api.Test;
216

227
import static org.assertj.core.api.Assertions.assertThat;
@@ -25,129 +10,21 @@
2510
class BetterStringsProcessorTest {
2611

2712
@Test
28-
void name() throws Exception {
13+
void compileTest() {
2914

30-
String program =
31-
"public class Test { " +
32-
"public static String hello(){ " +
33-
"String x = \"Ho!\"; " +
34-
"return \"Hey-${x}\";" +
35-
"}" +
36-
" }";
15+
@Language("Java") String classCode = "public class Test { " +
16+
" public static String hello(){ " +
17+
" String x = \"Ho!\"; " +
18+
" return \"Hey-${x}\";" +
19+
" }" +
20+
"}";
3721

38-
ClassLoader classLoader = getCompiledClassLoader("Test", program);
39-
Class<?> testClass = classLoader.loadClass("Test");
40-
Method method = testClass.getMethod("hello", null);
41-
String result = (String) method.invoke(null);
22+
Object result = new CompileTest().classCode("Test", classCode)
23+
.processor(new BetterStringsProcessor())
24+
.compile()
25+
.loadClass("Test")
26+
.invokeStatic("hello");
4227

4328
assertThat(result).isEqualTo("Hey-Ho!");
4429
}
45-
46-
private ClassLoader getCompiledClassLoader(String className, String program) {
47-
48-
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
49-
JavaFileObject compilationUnit = new StringJavaFileObject(className, program);
50-
51-
SimpleJavaFileManager fileManager =
52-
new SimpleJavaFileManager(compiler.getStandardFileManager(null,
53-
null,
54-
null));
55-
56-
JavaCompiler.CompilationTask compilationTask =
57-
compiler.getTask(null,
58-
fileManager,
59-
null,
60-
null,
61-
null,
62-
Arrays.asList(compilationUnit));
63-
64-
compilationTask.setProcessors(Arrays.asList(new BetterStringsProcessor()));
65-
compilationTask.call();
66-
67-
return new CompiledClassLoader(fileManager.getGeneratedOutputFiles());
68-
}
69-
70-
private static class StringJavaFileObject extends SimpleJavaFileObject {
71-
private final String code;
72-
73-
public StringJavaFileObject(String name, String code) {
74-
super(URI.create("string:///" + name.replace('.', '/') + Kind.SOURCE.extension),
75-
Kind.SOURCE);
76-
this.code = code;
77-
}
78-
79-
@Override
80-
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
81-
return code;
82-
}
83-
}
84-
85-
private static class CompiledClassLoader extends ClassLoader {
86-
private final List<ClassJavaFileObject> files;
87-
88-
private CompiledClassLoader(List<ClassJavaFileObject> files) {
89-
this.files = files;
90-
}
91-
92-
@Override
93-
protected Class<?> findClass(String name) throws ClassNotFoundException {
94-
Iterator<ClassJavaFileObject> itr = files.iterator();
95-
while (itr.hasNext()) {
96-
ClassJavaFileObject file = itr.next();
97-
if (file.getClassName().equals(name)) {
98-
itr.remove();
99-
byte[] bytes = file.getBytes();
100-
return super.defineClass(name, bytes, 0, bytes.length);
101-
}
102-
}
103-
return super.findClass(name);
104-
}
105-
}
106-
107-
private static class ClassJavaFileObject extends SimpleJavaFileObject {
108-
private final ByteArrayOutputStream outputStream;
109-
private final String className;
110-
111-
protected ClassJavaFileObject(String className, Kind kind) {
112-
super(URI.create("mem:///" + className.replace('.', '/') + kind.extension), kind);
113-
this.className = className;
114-
outputStream = new ByteArrayOutputStream();
115-
}
116-
117-
@Override
118-
public OutputStream openOutputStream() throws IOException {
119-
return outputStream;
120-
}
121-
122-
public byte[] getBytes() {
123-
return outputStream.toByteArray();
124-
}
125-
126-
public String getClassName() {
127-
return className;
128-
}
129-
}
130-
131-
private static class SimpleJavaFileManager extends ForwardingJavaFileManager {
132-
private final List<ClassJavaFileObject> outputFiles;
133-
134-
protected SimpleJavaFileManager(JavaFileManager fileManager) {
135-
super(fileManager);
136-
outputFiles = new ArrayList<>();
137-
}
138-
139-
@Override
140-
public JavaFileObject getJavaFileForOutput(Location location,
141-
String className,
142-
JavaFileObject.Kind kind,
143-
FileObject sibling) throws IOException {
144-
ClassJavaFileObject file = new ClassJavaFileObject(className, kind);
145-
outputFiles.add(file);
146-
return file;
147-
}
148-
149-
public List<ClassJavaFileObject> getGeneratedOutputFiles() {
150-
return outputFiles;
151-
}
152-
}
15330
}

0 commit comments

Comments
 (0)