001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hbase.shaded;
019
020import static org.junit.jupiter.api.Assertions.assertNotNull;
021import static org.junit.jupiter.api.Assertions.assertNull;
022import static org.junit.jupiter.api.Assumptions.assumeFalse;
023
024import java.io.File;
025import java.util.jar.JarEntry;
026import java.util.jar.JarFile;
027import org.apache.hadoop.hbase.HBaseTestingUtil;
028import org.apache.hadoop.hbase.testclassification.ClientTests;
029import org.apache.hadoop.hbase.testclassification.SmallTests;
030import org.junit.jupiter.api.Tag;
031import org.junit.jupiter.api.Test;
032
033@Tag(ClientTests.TAG)
034@Tag(SmallTests.TAG)
035public class TestShadedTestingUtilJarContents {
036
037  @Test
038  public void testNoBundledJUnitLibraries() throws Exception {
039    File path =
040      new File(HBaseTestingUtil.class.getProtectionDomain().getCodeSource().getLocation().toURI());
041    assumeFalse(path.isDirectory(), "Shaded JAR contents can only be checked after packaging");
042    try (JarFile jar = new JarFile(path)) {
043      JarEntry unexpectedEntry = jar.stream().filter(entry -> {
044        String name = entry.getName();
045        return name.startsWith("META-INF/services/org.junit.platform.") || name.endsWith(".class")
046          && (name.startsWith("org/junit/") && !name.equals("org/junit/Assert.class")
047            || name.startsWith("org/opentest4j/") || name.startsWith("org/apiguardian/")
048            || name.matches("META-INF/versions/[0-9]+/org/junit/.*"));
049      }).findFirst().orElse(null);
050      assertNull(unexpectedEntry, () -> "Unexpected bundled entry: " + unexpectedEntry);
051      // HBase's JUnit 4 shim is needed by bundled Hadoop test utilities.
052      assertNotNull(jar.getEntry("org/junit/Assert.class"));
053      assertNotNull(jar.getEntry("META-INF/services/org.junit.jupiter.api.extension.Extension"));
054    }
055  }
056}