mirror of https://github.com/D45Hub/Futurum.git
Browse Source
Feature/unit tests (#21)
Feature/unit tests (#21)
* added test helper and exchanged exampleunittest with example factorytest * created reflection helper for testing - created reflection helper for testing - made helpers us stream api instead of CollectionHelper class * Removed JUnit Jupiter Dependency to make the unit tests work. * Added simple version of log(base) unit test. * Expanded Units Tests. * Added Factory Test. Co-authored-by: DSinMeliodas <58990548+DSinMeliodas@users.noreply.github.com> Co-authored-by: Denis Thiessen <mail@denisthiessen.de>feature/portation-to-godot
D45Hub
3 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 351 additions and 19 deletions
-
5.idea/jarRepositories.xml
-
1app/build.gradle
-
37app/src/test/java/com/futurumgame/base/ExampleFactoryTest.java
-
17app/src/test/java/com/futurumgame/base/ExampleUnitTest.java
-
99app/src/test/java/com/futurumgame/base/FactoryTest.java
-
105app/src/test/java/com/futurumgame/base/UnitsTest.java
-
52app/src/test/java/com/futurumgame/testhelpers/FactoryTestHelper.java
-
52app/src/test/java/com/futurumgame/testhelpers/ReflectionHelper.java
-
2build.gradle
@ -0,0 +1,37 @@ |
|||
package com.futurumgame.base; |
|||
|
|||
import com.futurumgame.base.factories.Factory; |
|||
import com.futurumgame.base.factories.basic.WaterMill; |
|||
import com.futurumgame.testhelpers.FactoryTestHelper; |
|||
import com.futurumgame.testhelpers.ReflectionHelper; |
|||
|
|||
import org.junit.Test; |
|||
|
|||
import java.lang.reflect.InvocationTargetException; |
|||
import java.util.ArrayList; |
|||
|
|||
import static org.junit.Assert.assertEquals; |
|||
import static org.junit.Assert.assertNotNull; |
|||
import static org.junit.Assert.assertTrue; |
|||
|
|||
/** |
|||
* Example factory testclass |
|||
*/ |
|||
public class ExampleFactoryTest { |
|||
|
|||
@Test |
|||
public void instanceTest() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { |
|||
assertNotNull(FactoryTestHelper.instanceFactory(WaterMill.class)); |
|||
Factory factory = FactoryTestHelper.instanceFactory(WaterMill.class, 50); |
|||
assertNotNull(factory); |
|||
} |
|||
|
|||
@Test |
|||
public void hasFactory(){ |
|||
String factory = "factory"; |
|||
ArrayList<Class[]> args = new ArrayList<>(); |
|||
args.add(new Class[0]); |
|||
args.add(new Class[]{int.class}); |
|||
assertTrue(ReflectionHelper.hasMethods(WaterMill.class, new String[]{factory, factory}, args)); |
|||
} |
|||
} |
@ -1,17 +0,0 @@ |
|||
package com.futurumgame.base; |
|||
|
|||
import org.junit.Test; |
|||
|
|||
import static org.junit.Assert.*; |
|||
|
|||
/** |
|||
* Example local unit test, which will execute on the development machine (host). |
|||
* |
|||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a> |
|||
*/ |
|||
public class ExampleUnitTest { |
|||
@Test |
|||
public void addition_isCorrect() { |
|||
assertEquals(4, 2 + 2); |
|||
} |
|||
} |
@ -0,0 +1,99 @@ |
|||
package com.futurumgame.base; |
|||
|
|||
import com.futurumgame.base.factories.Factory; |
|||
import com.futurumgame.base.factories.advanced.*; |
|||
import com.futurumgame.base.factories.basic.*; |
|||
import com.futurumgame.base.factories.ores.*; |
|||
import com.futurumgame.base.factories.smelteries.*; |
|||
import com.futurumgame.testhelpers.FactoryTestHelper; |
|||
import com.futurumgame.testhelpers.ReflectionHelper; |
|||
|
|||
import org.junit.Test; |
|||
|
|||
import java.lang.reflect.InvocationTargetException; |
|||
import java.util.ArrayList; |
|||
|
|||
import static org.junit.Assert.assertNotNull; |
|||
import static org.junit.Assert.assertTrue; |
|||
|
|||
public class FactoryTest { |
|||
|
|||
@Test |
|||
public void instanceTest() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { |
|||
|
|||
assertNotNull(FactoryTestHelper.instanceFactory(Forest.class)); |
|||
assertNotNull(FactoryTestHelper.instanceFactory(LimestoneQuarry.class)); |
|||
assertNotNull(FactoryTestHelper.instanceFactory(MortarMixer.class)); |
|||
assertNotNull(FactoryTestHelper.instanceFactory(StoneQuarry.class)); |
|||
assertNotNull(FactoryTestHelper.instanceFactory(ClayPit.class)); |
|||
assertNotNull(FactoryTestHelper.instanceFactory(DirtPit.class)); |
|||
assertNotNull(FactoryTestHelper.instanceFactory(GravelPit.class)); |
|||
assertNotNull(FactoryTestHelper.instanceFactory(OilPump.class)); |
|||
assertNotNull(FactoryTestHelper.instanceFactory(SandDune.class)); |
|||
assertNotNull(FactoryTestHelper.instanceFactory(WaterMill.class)); |
|||
assertNotNull(FactoryTestHelper.instanceFactory(CoalMine.class)); |
|||
assertNotNull(FactoryTestHelper.instanceFactory(CopperMine.class)); |
|||
assertNotNull(FactoryTestHelper.instanceFactory(TinMine.class)); |
|||
assertNotNull(FactoryTestHelper.instanceFactory(CopperSmeltery.class)); |
|||
assertNotNull(FactoryTestHelper.instanceFactory(TinSmeltery.class)); |
|||
|
|||
|
|||
Factory forest = FactoryTestHelper.instanceFactory(Forest.class, 50); |
|||
Factory limestoneQuarry = FactoryTestHelper.instanceFactory(LimestoneQuarry.class, 50); |
|||
Factory mortarMixer = FactoryTestHelper.instanceFactory(MortarMixer.class, 50); |
|||
Factory stoneQuarry = FactoryTestHelper.instanceFactory(StoneQuarry.class, 50); |
|||
Factory clayPit = FactoryTestHelper.instanceFactory(ClayPit.class, 50); |
|||
Factory dirtPit = FactoryTestHelper.instanceFactory(DirtPit.class, 50); |
|||
Factory gravelPit = FactoryTestHelper.instanceFactory(GravelPit.class, 50); |
|||
Factory oilPump = FactoryTestHelper.instanceFactory(OilPump.class, 50); |
|||
Factory sandDune = FactoryTestHelper.instanceFactory(SandDune.class, 50); |
|||
Factory waterMill = FactoryTestHelper.instanceFactory(WaterMill.class, 50); |
|||
Factory coalMine = FactoryTestHelper.instanceFactory(CoalMine.class, 50); |
|||
Factory copperMine = FactoryTestHelper.instanceFactory(CopperMine.class, 50); |
|||
Factory tinMine = FactoryTestHelper.instanceFactory(TinMine.class, 50); |
|||
Factory copperSmeltery = FactoryTestHelper.instanceFactory(CopperSmeltery.class, 50); |
|||
Factory tinSmeltery = FactoryTestHelper.instanceFactory(TinSmeltery.class, 50); |
|||
|
|||
assertNotNull(forest); |
|||
assertNotNull(limestoneQuarry); |
|||
assertNotNull(mortarMixer); |
|||
assertNotNull(stoneQuarry); |
|||
assertNotNull(clayPit); |
|||
assertNotNull(dirtPit); |
|||
assertNotNull(gravelPit); |
|||
assertNotNull(oilPump); |
|||
assertNotNull(sandDune); |
|||
assertNotNull(waterMill); |
|||
assertNotNull(coalMine); |
|||
assertNotNull(copperMine); |
|||
assertNotNull(tinMine); |
|||
assertNotNull(copperSmeltery); |
|||
assertNotNull(tinSmeltery); |
|||
} |
|||
|
|||
|
|||
@Test |
|||
public void hasFactoryTest(){ |
|||
String factory = "factory"; |
|||
ArrayList<Class[]> args = new ArrayList<>(); |
|||
args.add(new Class[0]); |
|||
args.add(new Class[]{int.class}); |
|||
|
|||
assertTrue(ReflectionHelper.hasMethods(Forest.class, new String[]{factory, factory}, args)); |
|||
assertTrue(ReflectionHelper.hasMethods(LimestoneQuarry.class, new String[]{factory, factory}, args)); |
|||
assertTrue(ReflectionHelper.hasMethods(MortarMixer.class, new String[]{factory, factory}, args)); |
|||
assertTrue(ReflectionHelper.hasMethods(StoneQuarry.class, new String[]{factory, factory}, args)); |
|||
assertTrue(ReflectionHelper.hasMethods(ClayPit.class, new String[]{factory, factory}, args)); |
|||
assertTrue(ReflectionHelper.hasMethods(DirtPit.class, new String[]{factory, factory}, args)); |
|||
assertTrue(ReflectionHelper.hasMethods(GravelPit.class, new String[]{factory, factory}, args)); |
|||
assertTrue(ReflectionHelper.hasMethods(OilPump.class, new String[]{factory, factory}, args)); |
|||
assertTrue(ReflectionHelper.hasMethods(SandDune.class, new String[]{factory, factory}, args)); |
|||
assertTrue(ReflectionHelper.hasMethods(WaterMill.class, new String[]{factory, factory}, args)); |
|||
assertTrue(ReflectionHelper.hasMethods(CoalMine.class, new String[]{factory, factory}, args)); |
|||
assertTrue(ReflectionHelper.hasMethods(CopperMine.class, new String[]{factory, factory}, args)); |
|||
assertTrue(ReflectionHelper.hasMethods(TinMine.class, new String[]{factory, factory}, args)); |
|||
assertTrue(ReflectionHelper.hasMethods(CopperSmeltery.class, new String[]{factory, factory}, args)); |
|||
assertTrue(ReflectionHelper.hasMethods(TinSmeltery.class, new String[]{factory, factory}, args)); |
|||
} |
|||
|
|||
} |
@ -0,0 +1,52 @@ |
|||
package com.futurumgame.testhelpers; |
|||
|
|||
import com.futurumgame.base.factories.Factory; |
|||
import com.futurumgame.base.resources.Resource; |
|||
|
|||
import java.lang.reflect.InvocationTargetException; |
|||
import java.lang.reflect.Method; |
|||
|
|||
public class FactoryTestHelper { |
|||
|
|||
private static final String factory = "factory"; |
|||
|
|||
private FactoryTestHelper() { |
|||
} |
|||
|
|||
/** |
|||
* Instances a non abstract factory class. This method is only intended for non abstract subclasses of {@link Factory}. |
|||
* |
|||
* @param clazz the class that should be initialized |
|||
* @param <T> the factory type |
|||
* @param <S> the resource type of the factory |
|||
* @return an instance of the specified factory class |
|||
* @throws NoSuchMethodException if the instancer method <b>factory</b> does not exist, this case should never be the case, the test should fail |
|||
* @throws InvocationTargetException if the method itself throws an exception, this case should never be the case, the test should fail |
|||
* @throws IllegalAccessException if the method is not public and thus not accessable, this case should never be the case, the test should fail |
|||
* @throws IllegalArgumentException if the method is not static and can thus not instance from a static context, this case should never be the case, the test should fail |
|||
*/ |
|||
public static <T extends Factory<S>, S extends Resource> T instanceFactory(Class<T> clazz) |
|||
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, IllegalArgumentException { |
|||
Method factoryFactory = clazz.getMethod(factory); |
|||
return (T) factoryFactory.invoke(null); |
|||
} |
|||
|
|||
/** |
|||
* Instances a non abstract factory class at a spezified level. This method is only intended for non abstract subclasses of {@link Factory}. |
|||
* |
|||
* @param clazz the class that should be initialized |
|||
* @param level the level the class should be initialized with |
|||
* @param <T> the factory type |
|||
* @param <S> the resource type of the factory |
|||
* @return an instance of the specified factory class |
|||
* @throws NoSuchMethodException if the instancer method <b>factory</b> does not exist, this case should never be the case, the test should fail |
|||
* @throws InvocationTargetException if the method itself throws an exception, this case should never be the case, the test should fail |
|||
* @throws IllegalAccessException if the method is not public and thus not accessable, this case should never be the case, the test should fail |
|||
* @throws IllegalArgumentException if the method is not static and can thus not instance from a static context, this case should never be the case, the test should fail |
|||
*/ |
|||
public static <T extends Factory<S>, S extends Resource> T instanceFactory(Class<T> clazz, int level) |
|||
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, IllegalArgumentException { |
|||
Method factoryFactory = clazz.getMethod(factory, int.class); |
|||
return (T) factoryFactory.invoke(null, level); |
|||
} |
|||
} |
@ -0,0 +1,52 @@ |
|||
package com.futurumgame.testhelpers; |
|||
|
|||
import java.lang.reflect.Method; |
|||
import java.util.Arrays; |
|||
import java.util.List; |
|||
|
|||
public class ReflectionHelper { |
|||
|
|||
private ReflectionHelper(){ |
|||
} |
|||
|
|||
/** |
|||
* Searches all methods of the specified class and returns a boolean value indicating whether the defined methods are contained or not. |
|||
* The methodnames should be in the same order as their corresponding arguments |
|||
* |
|||
* @param clazz the class that shall contain the methods |
|||
* @param methodNames the names of the methods to be searched for |
|||
* @param args the arguments of the methods to be searched for, each array corresponds to a specific method that reflects all types of the arguments the method has. |
|||
* They have to be in the same order as defined by the method that wil be searched for. If the method has no arguments an empty array should be passed on. |
|||
* @return <b>true</b>rue if and only if the class contains the defined methods, <b>false</b> otherwise |
|||
* @throws IllegalArgumentException if the methodNames and the args do not have the same length |
|||
*/ |
|||
public static boolean hasMethods(Class<?> clazz, String[] methodNames, List<Class[]> args) { |
|||
if (methodNames.length != args.size()) { |
|||
throw new IllegalArgumentException(); |
|||
} |
|||
Method[] methods = clazz.getMethods(); |
|||
for (int i = 0; i < methodNames.length; i++) { |
|||
final String methodName = methodNames[i]; |
|||
final Class[] params = args.get(i); |
|||
if (Arrays.stream(methods).allMatch(m -> isMethod(m, methodName, params))) { |
|||
continue; |
|||
} |
|||
return false; |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
private static boolean isMethod(Method method, String methodName, Class<?>[] args) { |
|||
Class<?>[] params = method.getParameterTypes(); |
|||
if(! method.getName().equals(methodName)|| params.length != args.length){ |
|||
return false; |
|||
} |
|||
for (int i = 0; i < params.length; i++) { |
|||
if(params[i].equals(args[i])){ |
|||
continue; |
|||
} |
|||
return false; |
|||
} |
|||
return true; |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue