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

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

Introduction

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

Prototype

public void addClass(String filename, Class<?> classToCopy) throws IOException 

Source Link

Usage

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));
    });//w  w w .j  a v a  2  s  .c  o m
}

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);//  w  ww .  j  a  v  a  2s  .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);// w  w  w . ja  v a 2 s .co 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 ww  . j  a v a2  s. c  o m*/
}

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:");
    }//from  w w  w  .j  a v a  2  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();//w  ww . java2  s  .c  o m
        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);//from w w  w . j  a va2 s.c  om
    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();
}