|
| 1 | +package com.baeldung.jar; |
| 2 | + |
| 3 | +import com.google.common.collect.Sets; |
| 4 | +import org.junit.Assert; |
| 5 | +import org.junit.Test; |
| 6 | + |
| 7 | +import java.io.File; |
| 8 | +import java.io.IOException; |
| 9 | +import java.net.URISyntaxException; |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.Objects; |
| 12 | +import java.util.Set; |
| 13 | +import java.util.stream.Collectors; |
| 14 | + |
| 15 | +public class GetClassNamesFromJarUnitTest { |
| 16 | + private static final String JAR_PATH = "example-jar/stripe-0.0.1-SNAPSHOT.jar"; |
| 17 | + private static final Set<String> EXPECTED_CLASS_NAMES = Sets.newHashSet( |
| 18 | + "com.baeldung.stripe.StripeApplication", |
| 19 | + "com.baeldung.stripe.ChargeRequest", |
| 20 | + "com.baeldung.stripe.StripeService", |
| 21 | + "com.baeldung.stripe.ChargeRequest$Currency", |
| 22 | + "com.baeldung.stripe.ChargeController", |
| 23 | + "com.baeldung.stripe.CheckoutController"); |
| 24 | + |
| 25 | + @Test |
| 26 | + public void givenJarFilePath_whenLoadClassNames_thenGetClassNames() throws IOException, URISyntaxException { |
| 27 | + File jarFile = new File(Objects.requireNonNull(getClass().getClassLoader().getResource(JAR_PATH)).toURI()); |
| 28 | + Set<String> classNames = GetClassNamesFromJar.getClassNamesFromJarFile(jarFile); |
| 29 | + Assert.assertEquals(EXPECTED_CLASS_NAMES, classNames); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void givenJarFilePath_whenLoadClass_thenGetClassObjects() throws IOException, ClassNotFoundException, URISyntaxException { |
| 34 | + File jarFile = new File(Objects.requireNonNull(getClass().getClassLoader().getResource(JAR_PATH)).toURI()); |
| 35 | + Set<Class> classes = GetClassNamesFromJar.getClassesFromJarFile(jarFile); |
| 36 | + Set<String> names = classes.stream().map(Class::getName).collect(Collectors.toSet()); |
| 37 | + Assert.assertEquals(EXPECTED_CLASS_NAMES, names); |
| 38 | + } |
| 39 | +} |
0 commit comments