Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Dynamically create test jars for JarCommandTest #4210

Merged
merged 3 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -20,18 +20,32 @@

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;
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 +57,15 @@ 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 +338,47 @@ 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());

// compile the java file
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
Preconditions.checkNotNull(compiler);
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();
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();
}
}
}
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.
Loading