Skip to content

Commit

Permalink
Implement JarProcessor to determine jar type. (#2797)
Browse files Browse the repository at this point in the history
* Implement JarProcessor to determine jar type and use Truth for tests
  • Loading branch information
mpeddada1 authored Oct 5, 2020
1 parent 9f5aabc commit c208133
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ subprojects {
MAVEN_PLUGIN_ANNOTATIONS: 'org.apache.maven.plugin-tools:maven-plugin-annotations:3.6.0',

//test
TRUTH: 'com.google.truth:truth:1.0.1',
JUNIT: 'junit:junit:4.13',
MAVEN_TESTING_HARNESS: 'org.apache.maven.plugin-testing:maven-plugin-testing-harness:3.3.0',
MAVEN_VERIFIER: 'org.apache.maven.shared:maven-verifier:1.7.2',
Expand Down
1 change: 1 addition & 0 deletions jib-cli/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies {
implementation dependencyStrings.PICOCLI

testImplementation dependencyStrings.JUNIT
testImplementation dependencyStrings.TRUTH
testImplementation dependencyStrings.MOCKITO_CORE
testImplementation dependencyStrings.SLF4J_API
testImplementation dependencyStrings.SYSTEM_RULES
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2020 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.google.cloud.tools.jib.cli.jar;

import java.io.IOException;
import java.nio.file.Path;
import java.util.jar.JarFile;

public class JarProcessor {

/**
* Jar Type.
*
* <ul>
* <li>{@code STANDARD} a regular jar.
* <li>{@code SPRING_BOOT} a spring boot fat jar.
* </ul>
*/
public enum JarType {
STANDARD,
SPRING_BOOT;
}

/**
* Determines whether the jar is a spring boot or regular jar, given a path to the jar.
*
* @param jarPath path to the jar.
* @return the jar type.
* @throws IOException if I/O error occurs when opening the file.
*/
public static JarType determineJarType(Path jarPath) throws IOException {
JarFile jarFile = new JarFile(jarPath.toFile());
if (jarFile.getEntry("BOOT-INF") != null) {
return JarType.SPRING_BOOT;
}
return JarType.STANDARD;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2020 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.google.cloud.tools.jib.cli.jar;

import static com.google.common.truth.Truth.assertThat;

import com.google.cloud.tools.jib.cli.jar.JarProcessor.JarType;
import com.google.common.io.Resources;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.Test;

public class JarProcessorTest {

private static final String SPRING_BOOT_RESOURCE_DIR = "jar/springboot/springboot_sample.jar";
private static final String STANDARD_RESOURCE_DIR = "jar/standard/standardJar.jar";

@Test
public void testDetermineJarType_springBoot() throws IOException, URISyntaxException {
Path springBootJar = Paths.get(Resources.getResource(SPRING_BOOT_RESOURCE_DIR).toURI());
JarType jarType = JarProcessor.determineJarType(springBootJar);
assertThat(jarType).isEqualTo(JarType.SPRING_BOOT);
}

@Test
public void testDetermineJarType_standard() throws IOException, URISyntaxException {
Path standardJar = Paths.get(Resources.getResource(STANDARD_RESOURCE_DIR).toURI());
JarType jarType = JarProcessor.determineJarType(standardJar);
assertThat(jarType).isEqualTo(JarType.STANDARD);
}
}
Binary file not shown.
Binary file not shown.

0 comments on commit c208133

Please sign in to comment.