Skip to content

Commit

Permalink
Added new methods
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-schnell committed Jan 21, 2024
1 parent d58b878 commit e7ab6c3
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 14 deletions.
98 changes: 92 additions & 6 deletions src/main/java/org/fuin/utils4j/JandexUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@
*/
package org.fuin.utils4j;

import org.jboss.jandex.DotName;
import org.jboss.jandex.Index;
import org.jboss.jandex.Indexer;
import org.jboss.jandex.*;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
Expand All @@ -35,6 +32,11 @@
*/
public final class JandexUtils {

/**
* Default location of the Jandex index file.
*/
public static final String DEFAULT_JANDEX_INDEX_FILE = "META-INF/jandex.idx";

/**
* Private default constructor.
*/
Expand Down Expand Up @@ -193,4 +195,88 @@ public static Class<?> loadClass(DotName name) {
return Utils4J.loadClass(name.toString());
}

/**
* Writes the index to a file.
* Wraps the possible {@link IOException} into a {@link RuntimeException}.
*
* @param file File to write to.
* @param index Index to save.
*/
public static void writeIndexR(final File file, final Index index) {
try {
writeIndex(file, index);
} catch (final IOException ex) {
throw new RuntimeException("Failed to write index to: " + file, ex);
}
}

/**
* Writes the index to a file.
*
* @param file File to write to.
* @param index Index to save.
* @throws IOException Failed to write to the file.
*/
public static void writeIndex(final File file, final Index index) throws IOException {
try (final OutputStream out = new FileOutputStream(file)) {
new IndexWriter(out).write(index);
}
}

/**
* Loads the index from standard "META-INF/jandex.idx" location.
* Wraps the possible {@link IOException} into a {@link RuntimeException}.
*
* @return Index.
*/
public static Index loadIndexResourceR() {
try {
return loadIndexResource();
} catch (final IOException ex) {
throw new RuntimeException("Failed to load: " + DEFAULT_JANDEX_INDEX_FILE, ex);
}
}

/**
* Loads the index from standard "META-INF/jandex.idx" location.
*
* @return Index.
* @throws IOException Failed to load the resource.
*/
public static Index loadIndexResource() throws IOException {
return loadIndexResource(DEFAULT_JANDEX_INDEX_FILE);
}

/**
* Loads the index from a resource in the classpath.
* Wraps the possible {@link IOException} into a {@link RuntimeException}.
*
* @param indexFilePathAndName Path and name of the index file resource.
* @return Index.
*/
public static Index loadIndexResourceR(final String indexFilePathAndName) {
try {
return loadIndexResource(indexFilePathAndName);
} catch (final IOException ex) {
throw new RuntimeException("Failed to write index to: " + indexFilePathAndName, ex);
}
}

/**
* Loads the index from a resource in the classpath.
*
* @param indexFilePathAndName Path and name of the index file resource.
* @return Index.
* @throws IOException Failed to load the resource.
*/
public static Index loadIndexResource(final String indexFilePathAndName) throws IOException {
final URL url = Thread.currentThread().getContextClassLoader().getResource(indexFilePathAndName);
if (url == null) {
throw new FileNotFoundException("Resource not found: " + indexFilePathAndName);
}
try (final InputStream input = url.openStream()) {
return new IndexReader(input).read();
}
}

}
71 changes: 63 additions & 8 deletions src/test/java/org/fuin/utils4j/JandexUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -36,12 +37,18 @@
// CHECKSTYLE:OFF
public class JandexUtilsTest {

private static final File TARGET_DIR = new File("target");

private static final File CLASSES_DIR = new File(TARGET_DIR, "classes");

private static final File TEST_CLASSES_DIR = new File(TARGET_DIR, "test-classes");

@Test
public final void testIndexClassFile() {

// PREPARE
final List<File> knownFiles = new ArrayList<File>();
final File classFile = new File("target/classes/" + JandexUtils.class.getName().replace('.', '/') + ".class");
final File classFile = new File(CLASSES_DIR, JandexUtils.class.getName().replace('.', '/') + ".class");
final Indexer indexer = new Indexer();

// TEST
Expand All @@ -58,11 +65,8 @@ public final void testIndexClassFile() {
@Test
public final void testIndexDirSimple() {

// PREPARE
final File classesDir = new File("target/classes");

// TEST
final Index index = JandexUtils.indexDir(classesDir);
final Index index = JandexUtils.indexDir(CLASSES_DIR);

// VERIFY
assertThat(index.getClassByName(DotName.createSimple(JandexUtils.class.getName()))).isNotNull();
Expand All @@ -74,8 +78,8 @@ public final void testIndexDirSimple() {
public final void testIndexDir() {

// PREPARE
final File jandexClassFile = new File("target/classes/" + JandexUtils.class.getName().replace('.', '/') + ".class");
final File utils4JClassFile = new File("target/classes/" + Utils4J.class.getName().replace('.', '/') + ".class");
final File jandexClassFile = new File(CLASSES_DIR, JandexUtils.class.getName().replace('.', '/') + ".class");
final File utils4JClassFile = new File(CLASSES_DIR, Utils4J.class.getName().replace('.', '/') + ".class");
final List<File> knownFiles = new ArrayList<File>();
final File classesDir = new File("target/classes");
final Indexer indexer = new Indexer();
Expand Down Expand Up @@ -115,7 +119,7 @@ public final void testIndexJar() {
public final void testIndexClasspath() throws IOException {

// PREPARE
final File jandexClassFile = new File("target/classes/" + JandexUtils.class.getName().replace('.', '/') + ".class")
final File jandexClassFile = new File(CLASSES_DIR, JandexUtils.class.getName().replace('.', '/') + ".class")
.getCanonicalFile();
final List<File> knownFiles = new ArrayList<File>();
final Indexer indexer = new Indexer();
Expand All @@ -142,5 +146,56 @@ public final void loadClassFailure() {
.hasMessageContaining("Failed to load class");
}

@Test
void testWriteIndexR() throws IOException {

// PREPARE
final Indexer indexer = new Indexer();
indexer.indexClass(JandexUtils.class);
final Index index = indexer.complete();
final String filename = this.getClass().getSimpleName() + ".index";
final File file = new File(CLASSES_DIR, filename);
file.delete();

// TEST
JandexUtils.writeIndexR(file, index);

// VERIFY
assertThat(file).exists();
final Index result = JandexUtils.loadIndexResourceR(filename);
assertThat(result.getClassByName(JandexUtils.class.getName())).isNotNull();
assertThat(result.getClassByName(JandexUtils.class.getName()).name().toString()).isEqualTo(JandexUtils.class.getName());

}

@Test
public final void loadIndexResource() throws IOException {
final Index index = JandexUtils.loadIndexResource("sample.index");
assertThat(index.getClassByName(JandexUtils.class.getName())).isNotNull();
assertThat(index.getClassByName(JandexUtils.class.getName()).name().toString()).isEqualTo(JandexUtils.class.getName());
}

@Test
public final void loadIndexResourceR() {
final Index index = JandexUtils.loadIndexResourceR("sample.index");
assertThat(index.getClassByName(JandexUtils.class.getName())).isNotNull();
assertThat(index.getClassByName(JandexUtils.class.getName()).name().toString()).isEqualTo(JandexUtils.class.getName());
}

@Test
public final void loadIndexResourceFailure() {
assertThatThrownBy(() -> JandexUtils.loadIndexResource("does-not-exist.index"))
.isInstanceOf(FileNotFoundException.class)
.hasMessage("Resource not found: does-not-exist.index");
}

@Test
public final void loadIndexResourceRFailure() {
assertThatThrownBy(() -> JandexUtils.loadIndexResourceR("does-not-exist.index"))
.isInstanceOf(RuntimeException.class)
.hasRootCauseInstanceOf(FileNotFoundException.class)
.hasRootCauseMessage("Resource not found: does-not-exist.index");
}

}
// CHECKSTYLE:ON
Binary file added src/test/resources/sample.index
Binary file not shown.

0 comments on commit e7ab6c3

Please sign in to comment.