Skip to content

Commit

Permalink
chore: Dynamically create test jars for JarCommandTest
Browse files Browse the repository at this point in the history
Follow-up to #4205.
  • Loading branch information
meltsufin committed Mar 7, 2024
1 parent 2e95616 commit 456b831
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 4 deletions.
4 changes: 0 additions & 4 deletions .allstar/binary_artifacts.yaml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(
Collections.singletonList(javaFilePath.toFile()));
Iterable<String> 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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class HelloWorld {
public static void main(String[] args) {
System.out.print("Hello World");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dep;

public class A {
public static void getResult() {
System.out.print("Hello ");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dep2;

public class B {
public static void getResult() {
System.out.print("World");
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 456b831

Please sign in to comment.