|
| 1 | +package com.codename1.maven; |
| 2 | + |
| 3 | +import org.apache.maven.model.Build; |
| 4 | +import org.apache.maven.plugin.logging.SystemStreamLog; |
| 5 | +import org.apache.maven.project.MavenProject; |
| 6 | +import org.apache.tools.ant.Project; |
| 7 | +import org.apache.tools.ant.taskdefs.Java; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.junit.jupiter.api.io.TempDir; |
| 10 | + |
| 11 | +import java.io.File; |
| 12 | +import java.io.IOException; |
| 13 | +import java.nio.file.Files; |
| 14 | +import java.nio.file.Path; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.HashSet; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Properties; |
| 20 | + |
| 21 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 24 | + |
| 25 | +class CompileCSSMojoTest { |
| 26 | + |
| 27 | + @Test |
| 28 | + void addsLocalizationDirectoryToDesignerInvocation(@TempDir Path tempDir) throws Exception { |
| 29 | + Path projectDir = setupProject(tempDir, true); |
| 30 | + TestCompileCSSMojo mojo = createMojo(projectDir); |
| 31 | + |
| 32 | + mojo.executeImpl(); |
| 33 | + |
| 34 | + List<String> args = mojo.getRecordingJava().getCommandLineArguments(); |
| 35 | + assertTrue(args.contains("-l"), "Expected -l argument when localization directory exists"); |
| 36 | + int index = args.indexOf("-l"); |
| 37 | + assertTrue(index >= 0 && index + 1 < args.size(), "Expected localization directory argument after -l"); |
| 38 | + assertEquals(projectDir.resolve("src/main/l10n").toFile().getAbsolutePath(), args.get(index + 1)); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + void skipsLocalizationArgumentWhenDirectoryMissing(@TempDir Path tempDir) throws Exception { |
| 43 | + Path projectDir = setupProject(tempDir, false); |
| 44 | + TestCompileCSSMojo mojo = createMojo(projectDir); |
| 45 | + |
| 46 | + mojo.executeImpl(); |
| 47 | + |
| 48 | + List<String> args = mojo.getRecordingJava().getCommandLineArguments(); |
| 49 | + assertFalse(args.contains("-l"), "Did not expect -l argument without localization directory"); |
| 50 | + } |
| 51 | + |
| 52 | + private TestCompileCSSMojo createMojo(Path projectDir) throws IOException { |
| 53 | + MavenProject mavenProject = new MavenProject(); |
| 54 | + mavenProject.setFile(projectDir.resolve("pom.xml").toFile()); |
| 55 | + mavenProject.addCompileSourceRoot(projectDir.resolve("src/main/java").toString()); |
| 56 | + Build build = new Build(); |
| 57 | + build.setDirectory(projectDir.resolve("target").toString()); |
| 58 | + build.setOutputDirectory(projectDir.resolve("target/classes").toString()); |
| 59 | + mavenProject.setBuild(build); |
| 60 | + mavenProject.setArtifacts(new HashSet<>()); |
| 61 | + |
| 62 | + TestCompileCSSMojo mojo = new TestCompileCSSMojo(projectDir.resolve("designer.jar").toFile()); |
| 63 | + mojo.project = mavenProject; |
| 64 | + mojo.antProject = new Project(); |
| 65 | + mojo.antProject.setBaseDir(projectDir.toFile()); |
| 66 | + mojo.antProject.init(); |
| 67 | + mojo.pluginArtifacts = new ArrayList<>(); |
| 68 | + mojo.properties = new Properties(); |
| 69 | + mojo.properties.setProperty("codename1.cssTheme", "true"); |
| 70 | + mojo.setLog(new SystemStreamLog()); |
| 71 | + |
| 72 | + return mojo; |
| 73 | + } |
| 74 | + |
| 75 | + private Path setupProject(Path tempDir, boolean includeLocalization) throws IOException { |
| 76 | + Path projectDir = tempDir.resolve("project"); |
| 77 | + Files.createDirectories(projectDir); |
| 78 | + Files.createDirectories(projectDir.resolve("src/main/java")); |
| 79 | + Path cssDir = Files.createDirectories(projectDir.resolve("src/main/css")); |
| 80 | + Files.write(cssDir.resolve("theme.css"), Arrays.asList("/* test css */")); |
| 81 | + Files.write(projectDir.resolve("codenameone_settings.properties"), Arrays.asList("codename1.cssTheme=true")); |
| 82 | + Files.createDirectories(projectDir.resolve("target/classes")); |
| 83 | + Files.createFile(projectDir.resolve("designer.jar")); |
| 84 | + Files.write(projectDir.resolve("pom.xml"), Arrays.asList( |
| 85 | + "<project xmlns=\"http://maven.apache.org/POM/4.0.0\"", |
| 86 | + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"", |
| 87 | + " xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">", |
| 88 | + " <modelVersion>4.0.0</modelVersion>", |
| 89 | + " <groupId>com.codename1</groupId>", |
| 90 | + " <artifactId>test-project</artifactId>", |
| 91 | + " <version>1.0-SNAPSHOT</version>", |
| 92 | + "</project>" |
| 93 | + )); |
| 94 | + |
| 95 | + if (includeLocalization) { |
| 96 | + Path localizationDir = Files.createDirectories(projectDir.resolve("src/main/l10n")); |
| 97 | + Files.write(localizationDir.resolve("Messages.properties"), Arrays.asList("greeting=Hello")); |
| 98 | + } |
| 99 | + |
| 100 | + return projectDir; |
| 101 | + } |
| 102 | + |
| 103 | + private static class TestCompileCSSMojo extends CompileCSSMojo { |
| 104 | + private final File designerJar; |
| 105 | + private RecordingJava recordingJava; |
| 106 | + |
| 107 | + private TestCompileCSSMojo(File designerJar) { |
| 108 | + this.designerJar = designerJar; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public Java createJava() { |
| 113 | + recordingJava = new RecordingJava(); |
| 114 | + recordingJava.setProject(antProject); |
| 115 | + return recordingJava; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + protected void setupCef() { |
| 120 | + // Skip CEF setup during tests. |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + protected File getDesignerJar() { |
| 125 | + return designerJar; |
| 126 | + } |
| 127 | + |
| 128 | + RecordingJava getRecordingJava() { |
| 129 | + return recordingJava; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private static class RecordingJava extends Java { |
| 134 | + private List<String> commandLineArguments = new ArrayList<>(); |
| 135 | + |
| 136 | + @Override |
| 137 | + public int executeJava() { |
| 138 | + commandLineArguments = Arrays.asList(getCommandLine().getCommandline()); |
| 139 | + return 0; |
| 140 | + } |
| 141 | + |
| 142 | + List<String> getCommandLineArguments() { |
| 143 | + return commandLineArguments; |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments