From 456b8318c90e48bbd6ed0a70fbc456cfeaf4486e Mon Sep 17 00:00:00 2001 From: Mike Eltsufin Date: Thu, 7 Mar 2024 16:16:24 +0000 Subject: [PATCH 1/2] chore: Dynamically create test jars for JarCommandTest Follow-up to #4205. --- .allstar/binary_artifacts.yaml | 4 -- .../cloud/tools/jib/cli/JarCommandTest.java | 62 ++++++++++++++++++ .../jarTest/standard/HelloWorld.java | 5 ++ .../resources/jarTest/standard/dep/A.java | 7 ++ .../resources/jarTest/standard/dep2/B.java | 7 ++ .../jarTest/standard/dependency1.jar | Bin 817 -> 0 bytes .../standard/directory/dependency2.jar | Bin 877 -> 0 bytes .../resources/jarTest/standard/jarWithCp.jar | Bin 772 -> 0 bytes .../jarTest/standard/noDependencyJar.jar | Bin 775 -> 0 bytes 9 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 jib-cli/src/integration-test/resources/jarTest/standard/HelloWorld.java create mode 100644 jib-cli/src/integration-test/resources/jarTest/standard/dep/A.java create mode 100644 jib-cli/src/integration-test/resources/jarTest/standard/dep2/B.java delete mode 100644 jib-cli/src/integration-test/resources/jarTest/standard/dependency1.jar delete mode 100644 jib-cli/src/integration-test/resources/jarTest/standard/directory/dependency2.jar delete mode 100644 jib-cli/src/integration-test/resources/jarTest/standard/jarWithCp.jar delete mode 100644 jib-cli/src/integration-test/resources/jarTest/standard/noDependencyJar.jar diff --git a/.allstar/binary_artifacts.yaml b/.allstar/binary_artifacts.yaml index cf26dacfe8..0b70480559 100644 --- a/.allstar/binary_artifacts.yaml +++ b/.allstar/binary_artifacts.yaml @@ -1,8 +1,4 @@ # Ignore reason: jars are used for testing purposes only ignorePaths: - - jib-cli/src/integration-test/resources/jarTest/standard/dependency1.jar - - jib-cli/src/integration-test/resources/jarTest/standard/directory/dependency2.jar - - jib-cli/src/integration-test/resources/jarTest/standard/jarWithCp.jar - - jib-cli/src/integration-test/resources/jarTest/standard/noDependencyJar.jar - jib-gradle-plugin/src/integration-test/resources/gradle/projects/default-target/libs/dependency-1.0.0.jar - jib-gradle-plugin/src/integration-test/resources/gradle/projects/simple/libs/dependency-1.0.0.jar \ No newline at end of file diff --git a/jib-cli/src/integration-test/java/com/google/cloud/tools/jib/cli/JarCommandTest.java b/jib-cli/src/integration-test/java/com/google/cloud/tools/jib/cli/JarCommandTest.java index 6b557196f2..c714d6f480 100644 --- a/jib-cli/src/integration-test/java/com/google/cloud/tools/jib/cli/JarCommandTest.java +++ b/jib-cli/src/integration-test/java/com/google/cloud/tools/jib/cli/JarCommandTest.java @@ -21,17 +21,30 @@ import com.google.cloud.tools.jib.Command; import com.google.cloud.tools.jib.api.HttpRequestTester; import com.google.common.io.Resources; +import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.net.URISyntaxException; import java.net.URL; +import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Arrays; +import java.util.Collections; import java.util.jar.Attributes; import java.util.jar.JarFile; +import java.util.jar.JarOutputStream; +import java.util.jar.Manifest; +import java.util.zip.ZipEntry; import javax.annotation.Nullable; +import javax.tools.JavaCompiler; +import javax.tools.JavaFileObject; +import javax.tools.StandardJavaFileManager; +import javax.tools.ToolProvider; import org.junit.After; +import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; import picocli.CommandLine; @@ -43,6 +56,14 @@ public class JarCommandTest { @Nullable private String containerName; + @BeforeClass + public static void createJars() throws IOException, URISyntaxException { + createJarFile("jarWithCp.jar", "HelloWorld", "dependency1.jar directory/dependency2.jar", "HelloWorld"); + createJarFile("noDependencyJar.jar", "HelloWorld", null, "HelloWorld"); + createJarFile("dependency1.jar", "dep/A", null, null); + createJarFile("directory/dependency2.jar", "dep2/B", null, null); + } + @After public void tearDown() throws IOException, InterruptedException { if (containerName != null) { @@ -315,4 +336,45 @@ public void testJar_baseImageSpecified() String output = new Command("docker", "run", "--rm", "cli-gcr-base").run(); assertThat(output).isEqualTo("Hello World"); } + + public static void createJarFile(String name, String className, String classPath, String mainClass) throws IOException, URISyntaxException { + Path javaFilePath = Paths.get(Resources.getResource("jarTest/standard/" + className + ".java").toURI()); + Path workingDir = Paths.get(Resources.getResource("jarTest/standard/").toURI()); + System.out.println("Working dir: " + workingDir); + + // compile the java file + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); + assertThat(compiler).isNotNull(); + StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); + Iterable compilationUnits = fileManager.getJavaFileObjectsFromFiles( + Collections.singletonList(javaFilePath.toFile())); + Iterable options = Arrays.asList("-source", "1.8", "-target", "1.8"); + JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, options, null, compilationUnits); + boolean success = task.call(); + assertThat(success).isTrue(); + + // Create a manifest file + Manifest manifest = new Manifest(); + Attributes attributes = new Attributes(); + attributes.putValue("Manifest-Version", "1.0"); + if (classPath != null) { + attributes.putValue("Class-Path", classPath); + } + if (mainClass != null) { + attributes.putValue("Main-Class", mainClass); + } + manifest.getMainAttributes().putAll(attributes); + + // Create JAR + File jarFile = workingDir.resolve(name).toFile(); + jarFile.getParentFile().mkdirs(); + FileOutputStream fileOutputStream = new FileOutputStream(jarFile); + JarOutputStream jarOutputStream = new JarOutputStream(fileOutputStream, manifest); + ZipEntry zipEntry = new ZipEntry(className + ".class"); + jarOutputStream.putNextEntry(zipEntry); + jarOutputStream.write(Files.readAllBytes(workingDir.resolve(className + ".class"))); + jarOutputStream.closeEntry(); + jarOutputStream.close(); + fileOutputStream.close(); + } } diff --git a/jib-cli/src/integration-test/resources/jarTest/standard/HelloWorld.java b/jib-cli/src/integration-test/resources/jarTest/standard/HelloWorld.java new file mode 100644 index 0000000000..558d6f9908 --- /dev/null +++ b/jib-cli/src/integration-test/resources/jarTest/standard/HelloWorld.java @@ -0,0 +1,5 @@ +public class HelloWorld { + public static void main(String[] args) { + System.out.print("Hello World"); + } +} diff --git a/jib-cli/src/integration-test/resources/jarTest/standard/dep/A.java b/jib-cli/src/integration-test/resources/jarTest/standard/dep/A.java new file mode 100644 index 0000000000..96d933ef79 --- /dev/null +++ b/jib-cli/src/integration-test/resources/jarTest/standard/dep/A.java @@ -0,0 +1,7 @@ +package dep; + +public class A { + public static void getResult() { + System.out.print("Hello "); + } +} diff --git a/jib-cli/src/integration-test/resources/jarTest/standard/dep2/B.java b/jib-cli/src/integration-test/resources/jarTest/standard/dep2/B.java new file mode 100644 index 0000000000..41ff5a9aa4 --- /dev/null +++ b/jib-cli/src/integration-test/resources/jarTest/standard/dep2/B.java @@ -0,0 +1,7 @@ +package dep2; + +public class B { + public static void getResult() { + System.out.print("World"); + } +} diff --git a/jib-cli/src/integration-test/resources/jarTest/standard/dependency1.jar b/jib-cli/src/integration-test/resources/jarTest/standard/dependency1.jar deleted file mode 100644 index 84b923b25b73cbe35af023b8ebbc6141b264e36b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 817 zcmWIWW@Zs#;Nak3$e5cG$bbYm8CV#6T|*poJ^kGD|D9rBU}gyLX6FE@V1gKP0m`PN7U(0~`F0MfC%K_29QBfO5{rv-C!F>=>>$$i-?KO~Y0K8F_gHreM2V+8 zUZ*7D7{el}d{_Ogz%rjnll)lZy~VloY8d3bY$msgf6zHM^Yh>TGjAV%e%rbs_)YbO zjn5XfmI=R>vwC55qu@gFhVw3ZS1g^A4dxik5I@>uyv1qa=hs@^6_xVplR9P>l^&ly zHH0TFB;@IocrM)=k0)3D5%^tjp=86$WbWH8>vv2!y0BSRe)o;!4QVxkJ(9O>$(*q8 zZ=RiUZuu+zF1hJl=_NB4DJ>Mg)}_&OJLrj4kZS*CLFe!CKI%Fp?!nC;uD5k1SV{R^ zXm_2z;4s7G=}X@Pl{-ug+}am^a9)Pv`^7oeFDRTowf$>HmFzp?9>2$|&-(AKR-L>? zOmM$k>|Z8Ote<7xaZZ$xfuRA21H2iTL>N#L4J<`~5)CSVrvgwCLf48Mji4lh0JcCT zTq{xn0-3e<)_u|QY&zRDW2&}V=_)#4# z{(SoPdrhi)SC|#6uk7q@zsMKmE17L-o|Df%F-&Ws*Neq}7dZGhE!_C!^86ihb7l#c zbv?WE{oGAH|L1NNF|5vNGa|yoa&A9cakXjXjQhtA`&fQ%R&`&y^=g5R zn*zij=W{VI0KKF(7uD~qK-rYk0waBd4|&m)@W7Nf=_ThR78mCRczYjq5cp?yc5&y$ zYuA2#d$90Am4U)NEjg_UE-klLmYLHwsP<3FjD>fPENkZ5CALlHi^Li3iQJVZFG%pH^ZW`GEN+{oUsM;IDyT1i zNJI6Q-;O=cyS1kB_yvSKof6NjI^*%>$Q?Yf5gAc8$~HE~vOV6YarwrfdEK=#6SSXw zHqJkDAuLUw_w$k6y=gUC{{s$QcKXyaO;by#s%~w^q21c9dd!iZ45ST9{x5uaYNwNl z>4$n*jZPo8!<>DI7t~i=IB;dtlw2qG5XsQTPk7(Vl&X?X-K=K1X-#eP@0QRT`WLKv z!(Z5!EuU7hN$SXsuP=Uyg93bF`Xv<+Mh1okAP(?mWD;ROO>VHX1WInG0G@t8i4R>X zay)_(8v@t@nQ*O0Ne^TK7X$A2Mwsvi$b`l|!o8sQNA?RS{t-YP=sU0$Xle-XW@Q7( NvH;;LAe{^LCIG?%2L1p5 diff --git a/jib-cli/src/integration-test/resources/jarTest/standard/jarWithCp.jar b/jib-cli/src/integration-test/resources/jarTest/standard/jarWithCp.jar deleted file mode 100644 index 0ae4bd379ae312c40db299f3daac89f13448f2c5..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 772 zcmWIWW@Zs#;Nak3@U6)WWIzI(3@i-3t|5-Po_=on|4uP5Ff#;rvvYt{FhP|C;M6Pv zQ~}rQ>*(j{<{BKL=j-O$yOHmZf`CK0(7%UnvInM}G`O|Yd-3GHXH4lw1lCzb{HTr= ze?I;Dy(ZPYE6j@3S9W%{U*wDOmCQCZ&&lVX7^XGR>&4=~3mkl$7H<4`dbL2u zO#xzr^FLOj`dt7h>yet1lOLX6l#`;DoRe5w?Am|U>#&1J>-pS&w+zibu3s0kW794J zg?CXbqDNczdoPg`3><8 z1|l5-+&PMT8)OsOANZFpe9hftY*(kW<9g~cj*C9xuhvB_T~#$@W#XfCOY}Gve>|O| zc|SENWup4$U6)+#HufgPnO$|dn<3qoH`VZIapcT5SC%>d;9z>wc4M>Mql)PYNA4c^ zU9wWl^x)|$*+rfbPgd0h#WyDO-jnhZmpim^#@6$_%*zsYr+?-L1)HAete&61K=EV*1sfxi2m@-O zfu#sgqCo}lQ~*js=vt9u1C(SCz!u1aYeh;x$R>bd4cTF!SVMq6Kqh(|26(fwfmAR9 L;cg&p40bmFA3XeV diff --git a/jib-cli/src/integration-test/resources/jarTest/standard/noDependencyJar.jar b/jib-cli/src/integration-test/resources/jarTest/standard/noDependencyJar.jar deleted file mode 100644 index 754cd1ba2e9bb0b7ce15fc48fa3033ab23f3737a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 775 zcmWIWW@Zs#;Nak3INF&X$bbYm8CV#6T|*poJ^kGD|D9rBU}gyLX6FE@V1g6WDQBXUI%hWN9{GL>U3PZW`=sY{5PK% z^3e3CrpA;9FJ~;=&XQ&zU*f(l;ij`elV|{8vBHq)$ From 2e800259a095861376d65551cb10861f002a5319 Mon Sep 17 00:00:00 2001 From: Mike Eltsufin Date: Wed, 13 Mar 2024 23:20:57 +0000 Subject: [PATCH 2/2] feedback --- .../cloud/tools/jib/cli/JarCommandTest.java | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/jib-cli/src/integration-test/java/com/google/cloud/tools/jib/cli/JarCommandTest.java b/jib-cli/src/integration-test/java/com/google/cloud/tools/jib/cli/JarCommandTest.java index c714d6f480..d1c2bce75b 100644 --- a/jib-cli/src/integration-test/java/com/google/cloud/tools/jib/cli/JarCommandTest.java +++ b/jib-cli/src/integration-test/java/com/google/cloud/tools/jib/cli/JarCommandTest.java @@ -20,6 +20,7 @@ import com.google.cloud.tools.jib.Command; import com.google.cloud.tools.jib.api.HttpRequestTester; +import com.google.common.base.Preconditions; import com.google.common.io.Resources; import java.io.File; import java.io.FileOutputStream; @@ -58,7 +59,8 @@ public class JarCommandTest { @BeforeClass public static void createJars() throws IOException, URISyntaxException { - createJarFile("jarWithCp.jar", "HelloWorld", "dependency1.jar directory/dependency2.jar", "HelloWorld"); + createJarFile( + "jarWithCp.jar", "HelloWorld", "dependency1.jar directory/dependency2.jar", "HelloWorld"); createJarFile("noDependencyJar.jar", "HelloWorld", null, "HelloWorld"); createJarFile("dependency1.jar", "dep/A", null, null); createJarFile("directory/dependency2.jar", "dep2/B", null, null); @@ -337,19 +339,22 @@ public void testJar_baseImageSpecified() assertThat(output).isEqualTo("Hello World"); } - public static void createJarFile(String name, String className, String classPath, String mainClass) throws IOException, URISyntaxException { - Path javaFilePath = Paths.get(Resources.getResource("jarTest/standard/" + className + ".java").toURI()); + public static void createJarFile( + String name, String className, String classPath, String mainClass) + throws IOException, URISyntaxException { + Path javaFilePath = + Paths.get(Resources.getResource("jarTest/standard/" + className + ".java").toURI()); Path workingDir = Paths.get(Resources.getResource("jarTest/standard/").toURI()); - System.out.println("Working dir: " + workingDir); // compile the java file JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); - assertThat(compiler).isNotNull(); + Preconditions.checkNotNull(compiler); StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); - Iterable compilationUnits = fileManager.getJavaFileObjectsFromFiles( - Collections.singletonList(javaFilePath.toFile())); + Iterable compilationUnits = + fileManager.getJavaFileObjectsFromFiles(Collections.singletonList(javaFilePath.toFile())); Iterable options = Arrays.asList("-source", "1.8", "-target", "1.8"); - JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, options, null, compilationUnits); + JavaCompiler.CompilationTask task = + compiler.getTask(null, fileManager, null, options, null, compilationUnits); boolean success = task.call(); assertThat(success).isTrue(); @@ -368,13 +373,12 @@ public static void createJarFile(String name, String className, String classPath // Create JAR File jarFile = workingDir.resolve(name).toFile(); jarFile.getParentFile().mkdirs(); - FileOutputStream fileOutputStream = new FileOutputStream(jarFile); - JarOutputStream jarOutputStream = new JarOutputStream(fileOutputStream, manifest); - ZipEntry zipEntry = new ZipEntry(className + ".class"); - jarOutputStream.putNextEntry(zipEntry); - jarOutputStream.write(Files.readAllBytes(workingDir.resolve(className + ".class"))); - jarOutputStream.closeEntry(); - jarOutputStream.close(); - fileOutputStream.close(); + try (FileOutputStream fileOutputStream = new FileOutputStream(jarFile); + JarOutputStream jarOutputStream = new JarOutputStream(fileOutputStream, manifest)) { + ZipEntry zipEntry = new ZipEntry(className + ".class"); + jarOutputStream.putNextEntry(zipEntry); + jarOutputStream.write(Files.readAllBytes(workingDir.resolve(className + ".class"))); + jarOutputStream.closeEntry(); + } } }