|
| 1 | +package org.testcontainers; |
| 2 | + |
| 3 | +import lombok.RequiredArgsConstructor; |
| 4 | +import org.assertj.core.api.Assertions; |
| 5 | +import org.junit.Test; |
| 6 | +import org.junit.runner.RunWith; |
| 7 | +import org.junit.runners.Parameterized; |
| 8 | +import org.junit.runners.Parameterized.Parameters; |
| 9 | +import org.objectweb.asm.ClassReader; |
| 10 | +import org.objectweb.asm.Opcodes; |
| 11 | +import org.objectweb.asm.Type; |
| 12 | +import org.objectweb.asm.tree.ClassNode; |
| 13 | +import org.objectweb.asm.tree.FieldNode; |
| 14 | +import org.objectweb.asm.tree.MethodNode; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.io.InputStream; |
| 18 | +import java.nio.file.FileVisitResult; |
| 19 | +import java.nio.file.Files; |
| 20 | +import java.nio.file.Path; |
| 21 | +import java.nio.file.SimpleFileVisitor; |
| 22 | +import java.nio.file.attribute.BasicFileAttributes; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.Arrays; |
| 25 | +import java.util.List; |
| 26 | + |
| 27 | +import static org.assertj.core.api.Assertions.assertThat; |
| 28 | + |
| 29 | +/** |
| 30 | + * This test checks that we don't expose any shaded class in our public API. |
| 31 | + * We use {@link Parameterized} runner here to create a test per public class in Testcontainers' JAR file. |
| 32 | + */ |
| 33 | +@RunWith(Parameterized.class) |
| 34 | +@RequiredArgsConstructor |
| 35 | +public class PublicBinaryAPITest extends AbstractJarFileTest { |
| 36 | + |
| 37 | + private static String SHADED_PACKAGE = "org.testcontainers.shaded."; |
| 38 | + private static String SHADED_PACKAGE_PATH = SHADED_PACKAGE.replaceAll("\\.", "/"); |
| 39 | + |
| 40 | + static { |
| 41 | + Assertions.registerFormatterForType(ClassNode.class, it -> it.name); |
| 42 | + Assertions.registerFormatterForType(FieldNode.class, it -> it.name); |
| 43 | + Assertions.registerFormatterForType(MethodNode.class, it -> it.name + it.desc); |
| 44 | + } |
| 45 | + |
| 46 | + @Parameters(name = "{0}") |
| 47 | + public static List<Object[]> data() throws Exception { |
| 48 | + List<Object[]> result = new ArrayList<>(); |
| 49 | + |
| 50 | + Files.walkFileTree(root, new SimpleFileVisitor<Path>() { |
| 51 | + |
| 52 | + @Override |
| 53 | + public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException { |
| 54 | + String fileName = path.toString(); |
| 55 | + |
| 56 | + if (!fileName.endsWith(".class")) { |
| 57 | + return super.visitFile(path, attrs); |
| 58 | + } |
| 59 | + |
| 60 | + if (!fileName.startsWith("/org/testcontainers/") ) { |
| 61 | + return super.visitFile(path, attrs); |
| 62 | + } |
| 63 | + |
| 64 | + if (fileName.startsWith("/" + SHADED_PACKAGE_PATH)) { |
| 65 | + return super.visitFile(path, attrs); |
| 66 | + } |
| 67 | + |
| 68 | + try(InputStream inputStream = Files.newInputStream(path)) { |
| 69 | + ClassReader reader = new ClassReader(inputStream); |
| 70 | + ClassNode node = new ClassNode(); |
| 71 | + reader.accept(node, ClassReader.SKIP_CODE); |
| 72 | + if ((node.access & Opcodes.ACC_PUBLIC) != 0) { |
| 73 | + result.add(new Object[]{ fileName, node }); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return super.visitFile(path, attrs); |
| 78 | + } |
| 79 | + }); |
| 80 | + return result; |
| 81 | + } |
| 82 | + |
| 83 | + private final String fileName; |
| 84 | + |
| 85 | + private final ClassNode classNode; |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testSuperClass() { |
| 89 | + assertThat(classNode.superName) |
| 90 | + .doesNotStartWith(SHADED_PACKAGE_PATH); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void testInterfaces() { |
| 95 | + assertThat(classNode.interfaces) |
| 96 | + .allSatisfy(it -> assertThat(it).doesNotStartWith(SHADED_PACKAGE_PATH)); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void testMethodReturnTypes() { |
| 101 | + assertThat(classNode.methods) |
| 102 | + .filteredOn(it -> (it.access & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED)) != 0) |
| 103 | + .allSatisfy(it -> assertThat(Type.getReturnType(it.desc).getClassName()).doesNotStartWith(SHADED_PACKAGE)); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testMethodArguments() { |
| 108 | + assertThat(classNode.methods) |
| 109 | + .filteredOn(it -> (it.access & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED)) != 0) |
| 110 | + .allSatisfy(method -> assertThat(Arrays.asList(Type.getArgumentTypes(method.desc))) |
| 111 | + .extracting(Type::getClassName) |
| 112 | + .allSatisfy(it -> assertThat(it).doesNotStartWith(SHADED_PACKAGE)) |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testFields() { |
| 118 | + assertThat(classNode.fields) |
| 119 | + .filteredOn(it -> (it.access & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED)) != 0) |
| 120 | + .allSatisfy(it -> assertThat(Type.getType(it.desc).getClassName()) |
| 121 | + .doesNotStartWith(SHADED_PACKAGE) |
| 122 | + ); |
| 123 | + } |
| 124 | +} |
0 commit comments