1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package de.aikiit.spamprotector;
19
20 import de.aikiit.spamprotector.converter.SpamProtector;
21 import de.aikiit.spamprotector.util.LocalizationHelper;
22 import org.junit.Test;
23
24 import java.io.File;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.util.Locale;
28 import java.util.concurrent.TimeUnit;
29 import java.util.jar.JarFile;
30
31 import static org.assertj.core.api.Assertions.assertThat;
32
33
34
35
36 public class ExecutableJarIT {
37
38 @Test
39 public void packagedApplicationIncludesServicesAndResources() throws Exception {
40 Path jar = Path.of(System.getProperty("executableJar")).toAbsolutePath();
41 try (JarFile archive = new JarFile(jar.toFile())) {
42 assertThat(archive.getManifest().getMainAttributes().getValue("Main-Class"))
43 .isEqualTo(AntiSpamApplication.class.getName());
44 }
45
46 String java = Path.of(System.getProperty("java.home"), "bin",
47 System.getProperty("os.name").startsWith("Windows") ? "java.exe" : "java").toString();
48 String classpath = jar + File.pathSeparator + System.getProperty("testClasses");
49 Path log = Files.createTempFile("spamschutz-executable-", ".log");
50 Process process = null;
51 try {
52
53 process = new ProcessBuilder(java, "-Djava.awt.headless=true", "-cp", classpath,
54 Probe.class.getName()).redirectErrorStream(true).redirectOutput(log.toFile()).start();
55 assertThat(process.waitFor(30, TimeUnit.SECONDS))
56 .as("Executable JAR smoke test completed within 30 seconds")
57 .isTrue();
58 String output = Files.readString(log);
59 assertThat(process.exitValue()).as("Process output: %s", output).isZero();
60 assertThat(output).contains("Executable JAR smoke test passed");
61 } finally {
62 if (process != null && process.isAlive()) {
63 process.destroyForcibly().waitFor();
64 }
65 Files.deleteIfExists(log);
66 }
67 }
68
69
70
71
72
73 public static final class Probe {
74
75 public static void main(String[] args) throws Exception {
76
77 String version = (String) Class.forName("de.aikiit.spamprotector.util.Version")
78 .getField("VERSION").get(null);
79 if (version.isEmpty() || version.contains("${")) {
80 throw new IllegalStateException("Missing Maven-filtered version: " + version);
81 }
82 for (String language : new String[]{"de", "en"}) {
83
84
85 Locale.setDefault("de".equals(language) ? Locale.GERMANY : Locale.US);
86 System.setProperty("user.language", language);
87 System.setProperty("user.country", "de".equals(language) ? "DE" : "US");
88
89 LocalizationHelper.setLocale();
90 String title = LocalizationHelper.getBundleString("spamschutz.main.title");
91 String expected = "de".equals(language) ? "SpamSchutz" : "SpamProtector";
92 if (!title.startsWith(expected)) {
93 throw new IllegalStateException("Missing localization for " + language + ": " + title);
94 }
95 }
96 String input = "hello@example.org";
97 if (!input.equals(SpamProtector.toPlain(SpamProtector.toEncoded(input)))) {
98 throw new IllegalStateException("Conversion roundtrip failed");
99 }
100 System.out.println("Executable JAR smoke test passed: " + version);
101 }
102 }
103 }