Skip to content

Commit

Permalink
add ut
Browse files Browse the repository at this point in the history
  • Loading branch information
LiuYu committed Nov 10, 2023
1 parent ccc7927 commit 8259c7e
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 17 deletions.
Binary file removed sofa-ark-parent/support/ark-tools/appDest
Binary file not shown.
Binary file removed sofa-ark-parent/support/ark-tools/dest
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ public interface MainClassTimeoutWarningListener {
/**
* An {@code EntryTransformer} that renames entries by applying a prefix.
*/
static final class RenamingEntryTransformer implements JarWriter.EntryTransformer {
static final class RenamingEntryTransformer implements JarWriter.EntryTransformer {

private final String namePrefix;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.jar.Manifest;

import static com.alipay.sofa.ark.tools.LibraryScope.MODULE;
import static org.junit.Assert.assertEquals;

/**
* @author lylingzhen
Expand All @@ -38,9 +39,10 @@ public class JarWriterTest {

private JarWriter jarWriter;

private File file = new File("./JarWriterTest");
private File file = new File("./JarWriterTest");

private String jarFilePath = this.getClass().getClassLoader().getResource("test-jar.jar").getFile();
private String jarFilePath = this.getClass().getClassLoader().getResource("test-jar.jar")
.getFile();

@Before
public void setUp() throws IOException {
Expand Down Expand Up @@ -71,7 +73,8 @@ public void testWriteMethods() throws IOException {
jarWriter.writeNestedLibrary("./", library);
jarWriter.writeLoaderClasses(new JarInputStream(new FileInputStream(jarFilePath)));

ZipHeaderPeekInputStream zipHeaderPeekInputStream = new ZipHeaderPeekInputStream(new FileInputStream(jarFilePath));
zipHeaderPeekInputStream.read();
ZipHeaderPeekInputStream zipHeaderPeekInputStream = new ZipHeaderPeekInputStream(
new FileInputStream(jarFilePath));
assertEquals(80, zipHeaderPeekInputStream.read());
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.alipay.sofa.ark.tools;

import org.junit.Before;
Expand All @@ -11,7 +27,8 @@

public class MainClassFinderTest {

private String jarFilePath = this.getClass().getClassLoader().getResource("test-jar.jar").getFile();
private String jarFilePath = this.getClass().getClassLoader().getResource("test-jar.jar")
.getFile();

private JarFile jarFile;

Expand All @@ -23,6 +40,6 @@ public void setUp() throws IOException {
@Test
public void testFindSingleMainClass() throws IOException {
assertEquals("com.alipay.sofa.ark.sample.springbootdemo.SpringbootDemoApplication",
findSingleMainClass(jarFile, "", ""));
findSingleMainClass(jarFile, "", ""));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
import java.lang.reflect.Field;
import java.net.URL;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import static com.alipay.sofa.ark.tools.LibraryScope.CONTAINER;
import static com.alipay.sofa.ark.tools.LibraryScope.MODULE;
import static com.alipay.sofa.ark.tools.LibraryScope.*;
import static com.alipay.sofa.ark.tools.Repackager.isZip;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Sets.newHashSet;
Expand All @@ -44,9 +44,10 @@ public class RepackagerTest {

private Repackager repackager;

private String jarFilePath = this.getClass().getClassLoader().getResource("test-jar.jar").getFile();
private String jarFilePath = this.getClass().getClassLoader().getResource("test-jar.jar")
.getFile();

private File jarFile = new File(jarFilePath);
private File jarFile = new File(jarFilePath);

@Before
public void setUp() {
Expand Down Expand Up @@ -78,7 +79,7 @@ public void testRepackage() throws Exception {

Field field = Repackager.class.getDeclaredField("arkContainerLibrary");
field.setAccessible(true);
field.set(repackager, new Library("sofa-ark-2.0.jar", jarFile, CONTAINER,true));
field.set(repackager, new Library("sofa-ark-2.0.jar", jarFile, CONTAINER, true));

repackager.setArkVersion("2.0");
repackager.setBaseDir(new File(this.getClass().getClassLoader().getResource("").getFile()));
Expand All @@ -91,11 +92,15 @@ public void testRepackage() throws Exception {
field.setAccessible(true);
field.set(repackager, new GitInfo());

repackager.repackage(new File("./dest"), new File("./module"), callback -> {
Library library = new Library(jarFile, MODULE);
Library library = new Library(jarFile, PLUGIN);
repackager.repackage(new File("./target/dest"), new File("./target/module"), callback -> {
callback.library(library);
});

field = Repackager.class.getDeclaredField("arkModuleLibraries");
field.setAccessible(true);
assertEquals(newArrayList(library), field.get(repackager));
assertEquals(MODULE, ((List<Library>) field.get(repackager)).get(0).getScope());
assertEquals("com.alipay.sofa.ark.sample.springbootdemo.SpringbootDemoApplication",
repackager.findMainMethodWithTimeoutWarning(new JarFile(jarFile)));
}
Expand Down Expand Up @@ -134,10 +139,11 @@ public void testPrepareDeclaredLibraries() throws Exception {

@Test
public void testRenamingEntryTransformer() {
RenamingEntryTransformer renamingEntryTransformer = new RenamingEntryTransformer("my-prefix");
RenamingEntryTransformer renamingEntryTransformer = new RenamingEntryTransformer(
"my-prefix");
JarEntry jarEntry = new JarEntry("my-entry");
jarEntry.setComment("xxx");
jarEntry.setExtra(new byte[]{});
jarEntry.setExtra(new byte[] {});
jarEntry.setSize(1);
jarEntry.setMethod(STORED);
jarEntry.setCrc(1);
Expand All @@ -157,4 +163,4 @@ public void testOtherMethods() {
repackager.setDenyImportResources(null);
repackager.setInjectPluginExportPackages(null);
}
}
}

0 comments on commit 8259c7e

Please sign in to comment.