Browse Source

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
parent
commit
d2124105ff
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      .idea/jarRepositories.xml
  2. 1
      app/build.gradle
  3. 37
      app/src/test/java/com/futurumgame/base/ExampleFactoryTest.java
  4. 17
      app/src/test/java/com/futurumgame/base/ExampleUnitTest.java
  5. 99
      app/src/test/java/com/futurumgame/base/FactoryTest.java
  6. 105
      app/src/test/java/com/futurumgame/base/UnitsTest.java
  7. 52
      app/src/test/java/com/futurumgame/testhelpers/FactoryTestHelper.java
  8. 52
      app/src/test/java/com/futurumgame/testhelpers/ReflectionHelper.java
  9. 2
      build.gradle

5
.idea/jarRepositories.xml

@ -21,5 +21,10 @@
<option name="name" value="Google" />
<option name="url" value="https://dl.google.com/dl/android/maven2/" />
</remote-repository>
<remote-repository>
<option name="id" value="MavenRepo" />
<option name="name" value="MavenRepo" />
<option name="url" value="https://repo.maven.apache.org/maven2/" />
</remote-repository>
</component>
</project>

1
app/build.gradle

@ -38,7 +38,6 @@ dependencies {
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.gridlayout:gridlayout:1.0.0'
testImplementation 'org.junit.jupiter:junit-jupiter'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.mauriciotogneri:greencoffee:3.5.0'

37
app/src/test/java/com/futurumgame/base/ExampleFactoryTest.java

@ -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));
}
}

17
app/src/test/java/com/futurumgame/base/ExampleUnitTest.java

@ -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);
}
}

99
app/src/test/java/com/futurumgame/base/FactoryTest.java

@ -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));
}
}

105
app/src/test/java/com/futurumgame/base/UnitsTest.java

@ -7,6 +7,7 @@ import org.junit.Test;
import com.futurumgame.base.additionalDatatypes.Units;
public class UnitsTest {
@Test
public void testAddition() {
Units simpleTestUnit = new Units(5, 0.0);
@ -66,6 +67,8 @@ public class UnitsTest {
simpleTestUnit.subtract(Units.NegativeInfinity);
assertEquals(Units.PositiveInfinity, simpleTestUnit);
simpleTestUnit.subtract(Units.PositiveInfinity);
assertEquals(Units.Zero, simpleTestUnit);
}
@Test
@ -86,6 +89,14 @@ public class UnitsTest {
simpleTestUnit2.multiply(Units.NaN);
assertEquals(Units.NaN, simpleTestUnit2);
simpleTestUnit3.multiply(Units.NegativeInfinity);
assertEquals(Units.PositiveInfinity, simpleTestUnit3);
simpleTestUnit3.multiply(Units.PositiveInfinity);
assertEquals(Units.PositiveInfinity, simpleTestUnit3);
simpleTestUnit3.multiply(Units.NegativeInfinity);
assertEquals(Units.NegativeInfinity, simpleTestUnit3);
}
@Test
@ -103,6 +114,14 @@ public class UnitsTest {
simpleTestUnit.divide(Units.PositiveInfinity);
assertEquals(Units.Zero, simpleTestUnit);
simpleTestUnit2.divide(Units.Zero);
assertEquals(Units.NaN, simpleTestUnit2);
simpleTestUnit3.divide(Units.NegativeInfinity);
assertEquals(Units.Zero, simpleTestUnit3);
simpleTestUnit.divide(Units.NaN);
assertEquals(Units.NaN, simpleTestUnit);
}
@Test
@ -125,12 +144,16 @@ public class UnitsTest {
simpleTestUnit2.pow(5);
assertEquals(Units.NaN, simpleTestUnit2);
simpleTestUnit.pow(Double.NaN);
assertEquals(Units.NaN, simpleTestUnit);
}
@Test
public void testNthRoot() {
Units simpleTestUnit = new Units(125, 0.0);
Units simpleTestUnit2 = new Units(-125, 0.0);
Units simpleTestUnit3 = new Units(4.0, 0.0);
Units positiveInfinityUnit = Units.PositiveInfinity.copy();
@ -142,14 +165,84 @@ public class UnitsTest {
positiveInfinityUnit.nRoot(5.0);
assertEquals(Units.NaN, positiveInfinityUnit);
simpleTestUnit3.nRoot(-2.0);
assertEquals(new Units(0.5, 0.0), simpleTestUnit3);
simpleTestUnit3.nRoot(Double.NaN);
assertEquals(Units.NaN, simpleTestUnit3);
}
@Test
public void testLog10() {
Units simpleTestUnit = new Units(10, 20.0);
Units zeroUnit = new Units(0.0, 0.0);
Units negativeUnit = new Units(-15, 1.0);
Units positiveInfiniteUnit = Units.PositiveInfinity.copy();
Units negativeInfiniteUnit = Units.NegativeInfinity.copy();
Units nanUnit = Units.NaN.copy();
simpleTestUnit.log10();
zeroUnit.log10();
negativeUnit.log10();
positiveInfiniteUnit.log10();
negativeInfiniteUnit.log10();
nanUnit.log10();
assertEquals(new Units(2.1, 1.0), simpleTestUnit);
assertEquals(Units.NaN, zeroUnit);
assertEquals(Units.NaN, negativeUnit);
assertEquals(Units.PositiveInfinity, positiveInfiniteUnit);
assertEquals(Units.NaN, negativeInfiniteUnit);
assertEquals(Units.NaN, nanUnit);
}
@Test
public void testLog() {
double eValue = 2.718281828459045;
Units simpleTestUnit = new Units(16, 0.0);
Units zeroUnit = new Units(0.0, 0.0);
Units negativeUnit = new Units(-15, 1.0);
Units eTestUnit = new Units(eValue, 0.0);
Units positiveInfiniteUnit = Units.PositiveInfinity.copy();
Units negativeInfiniteUnit = Units.NegativeInfinity.copy();
Units nanUnit = Units.NaN.copy();
eTestUnit.pow(2.0);
eTestUnit.log(eValue);
assertEquals(new Units(2.0, 0.0), eTestUnit);
simpleTestUnit.log(2.0);
assertEquals(new Units(4.0, 0.0), simpleTestUnit);
zeroUnit.log(5.0);
assertEquals(Units.NaN, zeroUnit);
negativeUnit.log(3.0);
assertEquals(Units.NaN, negativeUnit);
simpleTestUnit.log(-4.0);
assertEquals(Units.NaN, simpleTestUnit);
positiveInfiniteUnit.log(3.0);
assertEquals(Units.PositiveInfinity, positiveInfiniteUnit);
negativeInfiniteUnit.log(4.0);
assertEquals(Units.NaN, negativeInfiniteUnit);
nanUnit.log(7.0);
assertEquals(Units.NaN, nanUnit);
eTestUnit.log(Double.NaN);
assertEquals(Units.NaN, eTestUnit);
simpleTestUnit = new Units(16, 0.0);
simpleTestUnit.log(Double.POSITIVE_INFINITY);
assertEquals(Units.Zero, simpleTestUnit);
}
@Test
@ -264,6 +357,8 @@ public class UnitsTest {
Units positiveInfiniteUnit = Units.PositiveInfinity.copy();
Units negativeInfiniteUnit = Units.NegativeInfinity.copy();
Units zeroUnit = Units.Zero.copy();
assertEquals(true, simpleTestUnit.isPositive());
assertEquals(false, simpleTestUnit.isNegative());
@ -275,6 +370,9 @@ public class UnitsTest {
assertEquals(false, negativeInfiniteUnit.isPositive());
assertEquals(true, negativeInfiniteUnit.isNegative());
assertEquals(false, zeroUnit.isPositive());
assertEquals(false, zeroUnit.isNegative());
}
@Test
@ -285,19 +383,24 @@ public class UnitsTest {
Units positiveInfiniteUnit = Units.PositiveInfinity.copy();
Units negativeInfiniteUnit = Units.NegativeInfinity.copy();
Units zeroUnit = Units.Zero.copy();
assertEquals(new Units(15, 0.0), simpleTestUnit.copy());
assertEquals(new Units(-23, 0.0), simpleTestUnit2.copy());
assertEquals(Units.PositiveInfinity, positiveInfiniteUnit.copy());
assertEquals(Units.NegativeInfinity, negativeInfiniteUnit.copy());
assertEquals(Units.Zero, zeroUnit.copy());
}
@Test
public void testValueEvaluation() {
Units simpleTestUnit = new Units(15, 1.0);
Units simpleTestUnit2 = new Units(-23, 0.0);
Units zeroUnit = new Units(0.0, 0.0);
assertEquals(150, simpleTestUnit.intValue());
assertEquals(-23, simpleTestUnit2.intValue());
assertEquals(0, zeroUnit.intValue());
}
}

52
app/src/test/java/com/futurumgame/testhelpers/FactoryTestHelper.java

@ -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);
}
}

52
app/src/test/java/com/futurumgame/testhelpers/ReflectionHelper.java

@ -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;
}
}

2
build.gradle

@ -1,6 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
google()
jcenter()
}
@ -14,6 +15,7 @@ buildscript {
allprojects {
repositories {
mavenCentral()
google()
jcenter()
}

Loading…
Cancel
Save