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 1 commit
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 @@ -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);
mpeddada1 marked this conversation as resolved.
Show resolved Hide resolved

// compile the java file
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
assertThat(compiler).isNotNull();
mpeddada1 marked this conversation as resolved.
Show resolved Hide resolved
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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we use an if-else statement that proceeds with the jar building stage below if task.call() is true otherwise logs an error message if the compilation fails?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we just silently log the message, then we might not notice the failure.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry, good point!

[Optional] Alternatively, to have a clear separation between the "arrange", "act" and "assert" blocks in our tests - consider using Preconditions.checkState(task.call(), "<error message>");.


// 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();
mpeddada1 marked this conversation as resolved.
Show resolved Hide resolved
}
}
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