Skip to content

Commit

Permalink
Fix tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Aug 27, 2023
1 parent 39271e0 commit c64c518
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ public void testPreview() throws Exception {
assertThat(classFileVersion.isPreviewVersion(), is(true));
}

@Test
public void testClassFileParsing() throws Exception {
assertThat(ClassFileVersion.ofClassFile(new byte[]{0, 0, 0, 0, 0, (byte) minorVersion, 0, (byte) majorVersion}), is(ClassFileVersion.ofJavaVersion(javaVersion)));
}

@Test
public void testToString() {
assertThat(ClassFileVersion.ofJavaVersion(javaVersion).toString(), is("Java " + derivedVersion + " (" + minorMajorVersion + ")"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,6 @@ public void testIllegalClassFile() throws Exception {
ClassFileVersion.ofClassFile(new byte[0]);
}

@Test
public void testClassFileVersion() {
for (int i = 1; i < ClassFileVersion.latest().getJavaVersion(); i++) {
byte major = (byte) (44 + i);
byte minor = (byte) (i == 1 ? 3 : 0);

ClassFileVersion expected = ClassFileVersion.ofJavaVersion(i);
assertThat(ClassFileVersion.ofClassFile(new byte[]{0, 0, 0, 0, 0, minor, 0, major}), is(expected));
}
}

private static class Foo {
/* empty */
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package net.bytebuddy.build;

import net.bytebuddy.ByteBuddy;
import net.bytebuddy.asm.AsmVisitorWrapper;
import net.bytebuddy.description.field.FieldDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.matcher.ElementMatchers;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
Expand All @@ -10,12 +16,16 @@
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;

public class PluginEngineDefaultOtherTest {

Expand Down Expand Up @@ -44,6 +54,28 @@ public void testScanFindsNoPluginForByteBuddy() throws Exception {
assertThat(plugins.size(), is(0));
}

@Test
public void testMissingDependency() throws IOException {
File jar = temporaryFolder.newFile("source.jar");
JarOutputStream outputStream = new JarOutputStream(new FileOutputStream(jar));
try {
for (Class<?> type : Arrays.asList(TypeWithDependency.class, TypeWithoutDependency.class)) {
outputStream.putNextEntry(new JarEntry(type.getName().replace(".", "/") + ".class"));
outputStream.write(ClassFileLocator.ForClassLoader.read(type));
outputStream.closeEntry();
}
} finally {
outputStream.close();
}
Plugin.Engine.Summary summary = new Plugin.Engine.Default()
.withoutErrorHandlers()
.apply(jar, temporaryFolder.newFile("target.jar"), new Plugin.Factory.Simple(new MissingDependencyPlugin()));
assertThat(summary.getFailed().size(), is(1));
assertThat(summary.getFailed().keySet().iterator().next().getName(), is(TypeWithDependency.class.getName()));
assertThat(summary.getTransformed().size(), is(1));
assertThat(summary.getTransformed().get(0).getName(), is(TypeWithoutDependency.class.getName()));
}

private static class PluginClassLoader extends ClassLoader {

private final File file;
Expand All @@ -61,4 +93,33 @@ protected Enumeration<URL> findResources(String name) throws IOException {
return super.findResources(name);
}
}

private static class MissingDependencyPlugin implements Plugin {

public DynamicType.Builder<?> apply(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassFileLocator classFileLocator) {
return builder.visit(new AsmVisitorWrapper.ForDeclaredFields().field(ElementMatchers.<FieldDescription.InDefinedShape>any()));
}

public boolean matches(TypeDescription target) {
return target.represents(TypeWithoutDependency.class) || target.represents(TypeWithDependency.class);
}

public void close() {
/* do nothing */
}
}

public static class TypeDependency {
/* empty */
}

public static class TypeWithDependency {

@SuppressWarnings("unused")
private static final TypeDependency MISSING_DEPENDENCY = new TypeDependency();
}

public static final class TypeWithoutDependency {
/* empty */
}
}

0 comments on commit c64c518

Please sign in to comment.