Example usage for org.springframework.boot.loader.tools TestJarFile TestJarFile

List of usage examples for org.springframework.boot.loader.tools TestJarFile TestJarFile

Introduction

In this page you can find the example usage for org.springframework.boot.loader.tools TestJarFile TestJarFile.

Prototype

public TestJarFile(File temporaryFolder) throws IOException 

Source Link

Usage

From source file:org.springframework.boot.loader.tools.RepackagerTests.java

@Before
public void setup() throws IOException {
    this.testJarFile = new TestJarFile(this.temporaryFolder);
}

From source file:org.springframework.boot.loader.tools.RepackagerTests.java

@Test
public void libraries() throws Exception {
    TestJarFile libJar = new TestJarFile(this.temporaryFolder);
    libJar.addClass("a/b/C.class", ClassWithoutMainMethod.class, JAN_1_1985);
    File libJarFile = libJar.getFile();
    File libJarFileToUnpack = libJar.getFile();
    File libNonJarFile = this.temporaryFolder.newFile();
    FileCopyUtils.copy(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, libNonJarFile);
    this.testJarFile.addClass("a/b/C.class", ClassWithMainMethod.class);
    this.testJarFile.addFile("BOOT-INF/lib/" + libJarFileToUnpack.getName(), libJarFileToUnpack);
    File file = this.testJarFile.getFile();
    libJarFile.setLastModified(JAN_1_1980);
    Repackager repackager = new Repackager(file);
    repackager.repackage((callback) -> {
        callback.library(new Library(libJarFile, LibraryScope.COMPILE));
        callback.library(new Library(libJarFileToUnpack, LibraryScope.COMPILE, true));
        callback.library(new Library(libNonJarFile, LibraryScope.COMPILE));
    });//from   w  w w.ja  v a 2  s.co  m
    assertThat(hasEntry(file, "BOOT-INF/lib/" + libJarFile.getName())).isTrue();
    assertThat(hasEntry(file, "BOOT-INF/lib/" + libJarFileToUnpack.getName())).isTrue();
    assertThat(hasEntry(file, "BOOT-INF/lib/" + libNonJarFile.getName())).isFalse();
    JarEntry entry = getEntry(file, "BOOT-INF/lib/" + libJarFile.getName());
    assertThat(entry.getTime()).isEqualTo(JAN_1_1985);
    entry = getEntry(file, "BOOT-INF/lib/" + libJarFileToUnpack.getName());
    assertThat(entry.getComment()).startsWith("UNPACK:");
    assertThat(entry.getComment().length()).isEqualTo(47);
}

From source file:org.springframework.boot.loader.tools.RepackagerTests.java

@Test
public void duplicateLibraries() throws Exception {
    TestJarFile libJar = new TestJarFile(this.temporaryFolder);
    libJar.addClass("a/b/C.class", ClassWithoutMainMethod.class);
    File libJarFile = libJar.getFile();
    this.testJarFile.addClass("a/b/C.class", ClassWithMainMethod.class);
    File file = this.testJarFile.getFile();
    Repackager repackager = new Repackager(file);
    this.thrown.expect(IllegalStateException.class);
    this.thrown.expectMessage("Duplicate library");
    repackager.repackage((callback) -> {
        callback.library(new Library(libJarFile, LibraryScope.COMPILE, false));
        callback.library(new Library(libJarFile, LibraryScope.COMPILE, false));
    });/*  ww w .j av  a2  s  . c  om*/
}

From source file:org.springframework.boot.loader.tools.RepackagerTests.java

@Test
public void customLayout() throws Exception {
    TestJarFile libJar = new TestJarFile(this.temporaryFolder);
    libJar.addClass("a/b/C.class", ClassWithoutMainMethod.class);
    File libJarFile = libJar.getFile();
    this.testJarFile.addClass("a/b/C.class", ClassWithMainMethod.class);
    File file = this.testJarFile.getFile();
    Repackager repackager = new Repackager(file);
    Layout layout = mock(Layout.class);
    LibraryScope scope = mock(LibraryScope.class);
    given(layout.getLauncherClassName()).willReturn("testLauncher");
    given(layout.getLibraryDestination(anyString(), eq(scope))).willReturn("test/");
    given(layout.getLibraryDestination(anyString(), eq(LibraryScope.COMPILE))).willReturn("test-lib/");
    repackager.setLayout(layout);//from   w ww  .  j a va2  s . c  o  m
    repackager.repackage((callback) -> callback.library(new Library(libJarFile, scope)));
    assertThat(hasEntry(file, "test/" + libJarFile.getName())).isTrue();
    assertThat(getManifest(file).getMainAttributes().getValue("Spring-Boot-Lib")).isEqualTo("test-lib/");
    assertThat(getManifest(file).getMainAttributes().getValue("Main-Class")).isEqualTo("testLauncher");
}

From source file:org.springframework.boot.loader.tools.RepackagerTests.java

@Test
public void customLayoutNoBootLib() throws Exception {
    TestJarFile libJar = new TestJarFile(this.temporaryFolder);
    libJar.addClass("a/b/C.class", ClassWithoutMainMethod.class);
    File libJarFile = libJar.getFile();
    this.testJarFile.addClass("a/b/C.class", ClassWithMainMethod.class);
    File file = this.testJarFile.getFile();
    Repackager repackager = new Repackager(file);
    Layout layout = mock(Layout.class);
    LibraryScope scope = mock(LibraryScope.class);
    given(layout.getLauncherClassName()).willReturn("testLauncher");
    repackager.setLayout(layout);/*from  w  w w.  j  a  va  2  s.c  o m*/
    repackager.repackage((callback) -> callback.library(new Library(libJarFile, scope)));
    assertThat(getManifest(file).getMainAttributes().getValue("Spring-Boot-Lib")).isNull();
    assertThat(getManifest(file).getMainAttributes().getValue("Main-Class")).isEqualTo("testLauncher");
}

From source file:org.springframework.boot.loader.tools.RepackagerTests.java

@Test
public void dontRecompressZips() throws Exception {
    TestJarFile nested = new TestJarFile(this.temporaryFolder);
    nested.addClass("a/b/C.class", ClassWithoutMainMethod.class);
    File nestedFile = nested.getFile();
    this.testJarFile.addFile("test/nested.jar", nestedFile);
    this.testJarFile.addClass("A.class", ClassWithMainMethod.class);
    File file = this.testJarFile.getFile();
    Repackager repackager = new Repackager(file);
    repackager.repackage((callback) -> callback.library(new Library(nestedFile, LibraryScope.COMPILE)));

    try (JarFile jarFile = new JarFile(file)) {
        assertThat(jarFile.getEntry("BOOT-INF/lib/" + nestedFile.getName()).getMethod())
                .isEqualTo(ZipEntry.STORED);
        assertThat(jarFile.getEntry("BOOT-INF/classes/test/nested.jar").getMethod()).isEqualTo(ZipEntry.STORED);
    }// w  w w.j ava 2s.com
}

From source file:org.springframework.boot.loader.tools.RepackagerTests.java

@Test
public void unpackLibrariesTakePrecedenceOverExistingSourceEntries() throws Exception {
    TestJarFile nested = new TestJarFile(this.temporaryFolder);
    nested.addClass("a/b/C.class", ClassWithoutMainMethod.class);
    File nestedFile = nested.getFile();
    String name = "BOOT-INF/lib/" + nestedFile.getName();
    this.testJarFile.addFile(name, nested.getFile());
    this.testJarFile.addClass("A.class", ClassWithMainMethod.class);
    File file = this.testJarFile.getFile();
    Repackager repackager = new Repackager(file);
    repackager.repackage((callback) -> callback.library(new Library(nestedFile, LibraryScope.COMPILE, true)));
    try (JarFile jarFile = new JarFile(file)) {
        assertThat(jarFile.getEntry(name).getComment()).startsWith("UNPACK:");
    }/*  w  w w .  j a  v a2  s . c  o m*/
}

From source file:org.springframework.boot.loader.tools.RepackagerTests.java

@Test
public void existingSourceEntriesTakePrecedenceOverStandardLibraries() throws Exception {
    TestJarFile nested = new TestJarFile(this.temporaryFolder);
    nested.addClass("a/b/C.class", ClassWithoutMainMethod.class);
    File nestedFile = nested.getFile();
    this.testJarFile.addFile("BOOT-INF/lib/" + nestedFile.getName(), nested.getFile());
    this.testJarFile.addClass("A.class", ClassWithMainMethod.class);
    File file = this.testJarFile.getFile();
    Repackager repackager = new Repackager(file);
    long sourceLength = nestedFile.length();
    repackager.repackage((callback) -> {
        nestedFile.delete();/*from   w  ww .j  a  v  a2s  . com*/
        File toZip = RepackagerTests.this.temporaryFolder.newFile();
        ZipUtil.packEntry(toZip, nestedFile);
        callback.library(new Library(nestedFile, LibraryScope.COMPILE));
    });
    try (JarFile jarFile = new JarFile(file)) {
        assertThat(jarFile.getEntry("BOOT-INF/lib/" + nestedFile.getName()).getSize()).isEqualTo(sourceLength);
    }
}

From source file:org.springframework.boot.loader.tools.RepackagerTests.java

@Test
public void layoutCanOmitLibraries() throws IOException {
    TestJarFile libJar = new TestJarFile(this.temporaryFolder);
    libJar.addClass("a/b/C.class", ClassWithoutMainMethod.class);
    File libJarFile = libJar.getFile();
    this.testJarFile.addClass("a/b/C.class", ClassWithMainMethod.class);
    File file = this.testJarFile.getFile();
    Repackager repackager = new Repackager(file);
    Layout layout = mock(Layout.class);
    LibraryScope scope = mock(LibraryScope.class);
    repackager.setLayout(layout);// www. j a  v  a2  s  .  c  o  m
    repackager.repackage((callback) -> callback.library(new Library(libJarFile, scope)));
    assertThat(getEntryNames(file)).containsExactly("META-INF/", "META-INF/MANIFEST.MF", "a/", "a/b/",
            "a/b/C.class");
}

From source file:org.springframework.boot.loader.tools.RepackagerTests.java

private File createLibrary() throws IOException {
    TestJarFile library = new TestJarFile(this.temporaryFolder);
    library.addClass("com/example/library/Library.class", ClassWithoutMainMethod.class);
    return library.getFile();
}