diff --git a/core/src/main/java/com/seibel/lod/core/api/external/items/enums/DhApiEnumAssembly.java b/api/src/main/java/com/seibel/lod/api/enums/DhApiEnumAssembly.java similarity index 97% rename from core/src/main/java/com/seibel/lod/core/api/external/items/enums/DhApiEnumAssembly.java rename to api/src/main/java/com/seibel/lod/api/enums/DhApiEnumAssembly.java index 3b48c2d65..54cb5c8c5 100644 --- a/core/src/main/java/com/seibel/lod/core/api/external/items/enums/DhApiEnumAssembly.java +++ b/api/src/main/java/com/seibel/lod/api/enums/DhApiEnumAssembly.java @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -package com.seibel.lod.core.api.external.items.enums; +package com.seibel.lod.api.enums; import com.seibel.lod.core.api.external.items.enums.override.DhApiOverrideEnumAssembly; import com.seibel.lod.core.api.external.items.enums.config.DhApiConfigEnumAssembly; diff --git a/core/src/test/java/tests/ApiEnumSyncTests.java b/api/src/test/java/tests/ApiEnumSyncTests.java similarity index 93% rename from core/src/test/java/tests/ApiEnumSyncTests.java rename to api/src/test/java/tests/ApiEnumSyncTests.java index 94fb8e0b9..0d74f8142 100644 --- a/core/src/test/java/tests/ApiEnumSyncTests.java +++ b/api/src/test/java/tests/ApiEnumSyncTests.java @@ -1,4 +1,4 @@ -package tests;/* +/* * This file is part of the Distant Horizons mod (formerly the LOD Mod), * licensed under the GNU LGPL v3 License. * @@ -17,7 +17,9 @@ package tests;/* * along with this program. If not, see . */ -import com.seibel.lod.core.api.external.items.enums.DhApiEnumAssembly; +package tests; + +import com.seibel.lod.api.enums.DhApiEnumAssembly; import com.seibel.lod.core.enums.rendering.EFogDrawMode; import com.seibel.lod.core.enums.CoreEnumAssembly; import com.seibel.lod.core.enums.config.EVerticalQuality; @@ -37,14 +39,6 @@ import java.util.ArrayList; public class ApiEnumSyncTests { - /** This is just a quick demo to confirm the testing system is set up correctly. */ - @Test - public void ExampleTests() - { - Assert.assertTrue("Example test 1", true); - Assert.assertFalse("Example test 2", false); - } - /** Make sure each DhApi enum has the same values as its corresponding core enum. */ @Test public void ConfirmEnumsAreSynced() diff --git a/api/src/test/java/tests/EnumTestHelper.java b/api/src/test/java/tests/EnumTestHelper.java new file mode 100644 index 000000000..9555f5b03 --- /dev/null +++ b/api/src/test/java/tests/EnumTestHelper.java @@ -0,0 +1,184 @@ +package tests;/* + * This file is part of the Distant Horizons mod (formerly the LOD Mod), + * licensed under the GNU LGPL v3 License. + * + * Copyright (C) 2020-2022 James Seibel + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +import org.junit.Assert; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.jar.JarEntry; +import java.util.jar.JarInputStream; + +/** + * A list of methods related to the Enum unit tests. + * + * @author James Seibel + * @version 2022-9-5 + */ +public class EnumTestHelper +{ + + /** + * Returns a list of every Enum in the package with the given full name. + * + * @param packageFullName includes the package path + */ + @SuppressWarnings("unchecked") + public static ArrayList>> getAllEnumsFromPackage(String packageFullName) + { + ArrayList>> enumList = new ArrayList<>(); + List> classesInPackage = getClassesInPackage(packageFullName, true, "DistantHorizons"); + + // get the enums from each file + Assert.assertTrue("No files found in the package [" + packageFullName + "].", classesInPackage.size() != 0); + for (Class clazz : classesInPackage) + { + // ignore internal classes + if (!clazz.getName().contains("$")) + { + // attempt to parse the file's class into an enum + if (Enum.class.isAssignableFrom(clazz)) + { + enumList.add((Class>) clazz); + } + else + { + System.out.println("The Class [" + clazz + "] isn't an enum."); + } + } + } + + return enumList; + } + + /** + * Returns every class in the given package.

+ * + * Originally from: + * https://stackoverflow.com/questions/28678026/how-can-i-get-all-class-files-in-a-specific-package-in-java + * + * @param packageName + * @param onlySearchJars if true only jar files will be searched, otherwise jars and loose files will be searched + * @param expectedJarPathString Only search jars that contain this string + */ + public static List> getClassesInPackage(String packageName, boolean onlySearchJars, String expectedJarPathString) + { + String path = packageName.replace('.', '/'); + List> classes = new ArrayList<>(); + String[] classPathEntries = System.getProperty("java.class.path").split( + System.getProperty("path.separator") + ); + + String name; + for (String classpathEntry : classPathEntries) + { + if (classpathEntry.endsWith(".jar") && classpathEntry.toLowerCase().contains(expectedJarPathString.toLowerCase())) + { + File jar = new File(classpathEntry); + try + { + JarInputStream is = new JarInputStream(new FileInputStream(jar)); + JarEntry entry; + while ((entry = is.getNextJarEntry()) != null) + { + name = entry.getName(); + if (name.endsWith(".class")) + { + if (name.contains(path) && name.endsWith(".class")) + { + try + { + String classPath = name.substring(0, entry.getName().length() - 6); + classPath = classPath.replaceAll("[\\|/]", "."); + classes.add(Class.forName(classPath)); + } + catch (ClassNotFoundException ex) + { + // the class wasn't found + System.err.println("The Class [" + packageName + "." + name + "] failed to load."); + } + } + } + } + } + catch (IOException e) + { + System.err.println("Error reading the jar [" + jar.getPath() + "]."); + } + } + else if (!onlySearchJars) + { + File base = new File(classpathEntry + File.separatorChar + path); + File[] files = base.listFiles(); + if (files != null) + { + for (File file : files) + { + try + { + name = file.getName(); + if (name.endsWith(".class")) + { + name = name.substring(0, name.length() - 6); + classes.add(Class.forName(packageName + "." + name)); + } + } + catch (ClassNotFoundException ex) + { + // the class wasn't found + System.err.println("The Class [" + packageName + "." + file.getName() + "] failed to load."); + } + } + } + } + } + + return classes; + } + + + /** + * Returns every loaded package that begins with the given string.
+ * + * Note: this will only search packages that have been loaded + * at some point during the JVM's lifetime. + * To Make sure the package(s) you want to find are loaded you can + * initialize an object from that package to load it. + */ + public static ArrayList findPackageNamesStartingWith(String packagePrefix) + { + ArrayList nestedPackages = new ArrayList<>(); + + // search all the loaded packages + Package[] packageArray = Package.getPackages(); + for (Package pack : packageArray) + { + String packageName = pack.getName(); + if (packageName.startsWith(packagePrefix)) + { + nestedPackages.add(packageName); + } + } + + return nestedPackages; + } + +} diff --git a/api/src/test/java/tests/BasicTest.java b/api/src/test/java/tests/ExampleTest.java similarity index 88% rename from api/src/test/java/tests/BasicTest.java rename to api/src/test/java/tests/ExampleTest.java index 850bca94b..de45feab0 100644 --- a/api/src/test/java/tests/BasicTest.java +++ b/api/src/test/java/tests/ExampleTest.java @@ -23,15 +23,16 @@ import org.junit.Assert; import org.junit.Test; /** + * This is just a quick demo to confirm the testing system is set up correctly. + * * @author James Seibel * @version 2022-9-5 */ -public class BasicTest +public class ExampleTest { - /** This is just a quick demo to confirm the testing system is set up correctly. */ @Test - public void ExampleTests() + public void DemoTest() { Assert.assertTrue("Example test 1", true); Assert.assertFalse("Example test 2", false); diff --git a/core/src/test/java/tests/EnumTestHelper.java b/core/src/test/java/tests/EnumTestHelper.java deleted file mode 100644 index b8507060f..000000000 --- a/core/src/test/java/tests/EnumTestHelper.java +++ /dev/null @@ -1,116 +0,0 @@ -package tests;/* - * This file is part of the Distant Horizons mod (formerly the LOD Mod), - * licensed under the GNU LGPL v3 License. - * - * Copyright (C) 2020-2022 James Seibel - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, version 3. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - */ - -import org.junit.Assert; - -import java.io.File; -import java.io.FilenameFilter; -import java.net.URL; -import java.util.ArrayList; - -/** - * A list of methods related to the Enum unit tests. - * - * @author James Seibel - * @version 6-9-2022 - */ -public class EnumTestHelper -{ - - /** - * Returns a list of every Enum in the package with the given full name. - * - * @param packageFullName includes the package path - */ - @SuppressWarnings("unchecked") - public static ArrayList>> getAllEnumsFromPackage(String packageFullName) - { - ArrayList>> enumList = new ArrayList<>(); - URL packageRoot = Thread.currentThread().getContextClassLoader().getResource(packageFullName.replace(".", "/")); - - // find all .class files in the package folder - Assert.assertNotNull("No enums found in the package [" + packageFullName + "].", packageRoot); // asserts can be used since this method should only ever be used in the context of unit tests - File[] files = new File(packageRoot.getFile()).listFiles(new FilenameFilter() - { - public boolean accept(File dir, String name) - { - return name.endsWith(".class"); - } - }); - - // get the enums from each file - Assert.assertNotNull("No files found in the package [" + packageFullName + "].", files); - for (File file : files) - { - String className = file.getName().replaceAll(".class$", ""); - - // ignore internal classes - if (!className.contains("$")) - { - String fullClassName = packageFullName + "." + className; - try - { - // attempt to parse the file's class into an enum - Class enumClass = Class.forName(fullClassName); - if (Enum.class.isAssignableFrom(enumClass)) - { - enumList.add((Class>) enumClass); - } - else - { - System.out.println("The Class [" + fullClassName + "] isn't an enum."); - } - } - catch (ClassNotFoundException e) - { - System.out.println("No enum found with the full name [" + fullClassName + "]."); - } - } - } - - return enumList; - } - - /** - * Returns every loaded package that begins with the given string. - * - * Note: this will only search packages that have been loaded - * at some point during the JVM's lifetime. - * To Make sure the package(s) you want to find are loaded you can - * initialize an object from that package to load it. - */ - public static ArrayList findPackageNamesStartingWith(String packagePrefix) - { - ArrayList nestedPackages = new ArrayList<>(); - - // search all the loaded packages - Package[] packageArray = Package.getPackages(); - for (Package pack : packageArray) - { - String packageName = pack.getName(); - if (packageName.startsWith(packagePrefix)) - { - nestedPackages.add(packageName); - } - } - - return nestedPackages; - } - -} diff --git a/core/src/test/java/tests/ExampleTest.java b/core/src/test/java/tests/ExampleTest.java new file mode 100644 index 000000000..de45feab0 --- /dev/null +++ b/core/src/test/java/tests/ExampleTest.java @@ -0,0 +1,41 @@ +/* + * This file is part of the Distant Horizons mod (formerly the LOD Mod), + * licensed under the GNU LGPL v3 License. + * + * Copyright (C) 2020-2022 James Seibel + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package tests; + +import org.junit.Assert; +import org.junit.Test; + +/** + * This is just a quick demo to confirm the testing system is set up correctly. + * + * @author James Seibel + * @version 2022-9-5 + */ +public class ExampleTest +{ + + @Test + public void DemoTest() + { + Assert.assertTrue("Example test 1", true); + Assert.assertFalse("Example test 2", false); + } + +}