From c11b540ddcecbef9f05d40ed904fb89505e28cc9 Mon Sep 17 00:00:00 2001 From: Lennart Hensler Date: Thu, 15 Oct 2015 10:32:17 +0200 Subject: [PATCH] Hardened the getFunctionForDTO-Tests --- .../Test_StringMessages_de.properties | 3 +- .../Test_StringMessages_en.properties | 3 +- ...TestFunctionManagerAsFunctionProvider.java | 92 ++++++++++++++++--- 3 files changed, 83 insertions(+), 15 deletions(-) diff --git a/java/com.sap.sse.datamining.test/resources/stringmessages/Test_StringMessages_de.properties b/java/com.sap.sse.datamining.test/resources/stringmessages/Test_StringMessages_de.properties index f58acc7f3db..f9e08823bb1 100644 --- a/java/com.sap.sse.datamining.test/resources/stringmessages/Test_StringMessages_de.properties +++ b/java/com.sap.sse.datamining.test/resources/stringmessages/Test_StringMessages_de.properties @@ -10,4 +10,5 @@ LegNumber=Anzahl der Schenkel SailID=Segelnummer TestCluster=Test Cluster Deutsch increment=Inkrementieren -illegalDimension=Illegale Dimension \ No newline at end of file +illegalDimension=Illegale Dimension +DistanceTraveled=Gefahrene Distanz \ No newline at end of file diff --git a/java/com.sap.sse.datamining.test/resources/stringmessages/Test_StringMessages_en.properties b/java/com.sap.sse.datamining.test/resources/stringmessages/Test_StringMessages_en.properties index cb6577e9aaf..a27c517814a 100644 --- a/java/com.sap.sse.datamining.test/resources/stringmessages/Test_StringMessages_en.properties +++ b/java/com.sap.sse.datamining.test/resources/stringmessages/Test_StringMessages_en.properties @@ -10,4 +10,5 @@ LegNumber=Leg Number SailID=Sail ID TestCluster=Test Cluster English increment=Increment -illegalDimension=Illegal Dimension \ No newline at end of file +illegalDimension=Illegal Dimension +DistanceTraveled=Distance Traveled \ No newline at end of file diff --git a/java/com.sap.sse.datamining.test/src/com/sap/sse/datamining/impl/functions/TestFunctionManagerAsFunctionProvider.java b/java/com.sap.sse.datamining.test/src/com/sap/sse/datamining/impl/functions/TestFunctionManagerAsFunctionProvider.java index 4b74c35b3c0..91d72066541 100644 --- a/java/com.sap.sse.datamining.test/src/com/sap/sse/datamining/impl/functions/TestFunctionManagerAsFunctionProvider.java +++ b/java/com.sap.sse.datamining.test/src/com/sap/sse/datamining/impl/functions/TestFunctionManagerAsFunctionProvider.java @@ -19,6 +19,8 @@ import org.junit.Test; import com.sap.sse.datamining.ModifiableDataMiningServer; import com.sap.sse.datamining.components.DataRetrieverChainDefinition; import com.sap.sse.datamining.components.Processor; +import com.sap.sse.datamining.factories.DataMiningDTOFactory; +import com.sap.sse.datamining.factories.FunctionFactory; import com.sap.sse.datamining.functions.Function; import com.sap.sse.datamining.impl.components.DataRetrieverLevel; import com.sap.sse.datamining.impl.components.SimpleDataRetrieverChainDefinition; @@ -29,6 +31,7 @@ import com.sap.sse.datamining.test.data.Test_HasRaceContext; import com.sap.sse.datamining.test.data.Test_HasRaceContextImpl; import com.sap.sse.datamining.test.data.impl.SimpleClassWithMarkedMethods; import com.sap.sse.datamining.test.data.impl.Test_ExternalLibraryClass; +import com.sap.sse.datamining.test.domain.Test_Leg; import com.sap.sse.datamining.test.domain.Test_Named; import com.sap.sse.datamining.test.domain.Test_Regatta; import com.sap.sse.datamining.test.util.ExpectedFunctionRegistryUtil; @@ -149,28 +152,91 @@ public class TestFunctionManagerAsFunctionProvider { assertThat(server.getFunctionsFor(Test_HasLegOfCompetitorContextImpl.class), is(expectedFunctions)); } + @SuppressWarnings("unchecked") // Hamcrest requires type matching of actual and expected type, so the Functions have to be specific (without ) @Test - public void testGetFunctionForDTO() throws NoSuchMethodException, SecurityException { + public void testGetFunctionForDimensionDTO() throws NoSuchMethodException, SecurityException { + FunctionFactory functionFactory = FunctionTestsUtil.getFunctionFactory(); + DataMiningDTOFactory dtoFactory = FunctionTestsUtil.getDTOFactory(); + Method getRegattaMethod = Test_HasRaceContext.class.getMethod("getRace", new Class[0]); - Function getRegatta = FunctionTestsUtil.getFunctionFactory().createMethodWrappingFunction(getRegattaMethod); + Function getRegatta = functionFactory.createMethodWrappingFunction(getRegattaMethod); Method getNameMethod = Test_Named.class.getMethod("getName", new Class[0]); - Function getName = FunctionTestsUtil.getFunctionFactory().createMethodWrappingFunction(getNameMethod); - Function getRegattaName = FunctionTestsUtil.getFunctionFactory().createCompoundFunction(Arrays.asList(getRegatta, getName)); + Function getName = functionFactory.createMethodWrappingFunction(getNameMethod); + Function getRegattaName = functionFactory.createCompoundFunction(Arrays.asList(getRegatta, getName)); - FunctionDTO getRegattaNameDTO = FunctionTestsUtil.getDTOFactory().createFunctionDTO(getRegattaName, stringMessages, Locale.ENGLISH); - - @SuppressWarnings("unchecked") // Hamcrest requires type matching of actual and expected type, so the Functions have to be specific (without ) - Function providedFunction = (Function) server.getFunctionForDTO(getRegattaNameDTO); + FunctionDTO getRegattaNameDTO_English = dtoFactory.createFunctionDTO(getRegattaName, stringMessages, Locale.ENGLISH); + Function providedFunction = (Function) server.getFunctionForDTO(getRegattaNameDTO_English); assertThat(providedFunction, is(getRegattaName)); + FunctionDTO getRegattaNameDTO_German = dtoFactory.createFunctionDTO(getRegattaName, stringMessages, Locale.GERMAN); + providedFunction = (Function) server.getFunctionForDTO(getRegattaNameDTO_German); + assertThat(providedFunction, is(getRegattaName)); + FunctionDTO getRegattaNameDTO = dtoFactory.createFunctionDTO(getRegattaName); + providedFunction = (Function) server.getFunctionForDTO(getRegattaNameDTO); + assertThat(providedFunction, is(getRegattaName)); + } + + @SuppressWarnings("unchecked") // Hamcrest requires type matching of actual and expected type, so the Functions have to be specific (without ) + @Test + public void testGetFunctionForStatisticDTO() { + FunctionFactory functionFactory = FunctionTestsUtil.getFunctionFactory(); + DataMiningDTOFactory dtoFactory = FunctionTestsUtil.getDTOFactory(); + + Method getLegMethod = FunctionTestsUtil.getMethodFromClass(Test_HasLegOfCompetitorContext.class, "getLeg"); + Function getLeg = functionFactory.createMethodWrappingFunction(getLegMethod); + Method getDistanceTraveledMethod = FunctionTestsUtil.getMethodFromClass(Test_Leg.class, "getDistanceTraveled"); + Function getDistanceTraveled = functionFactory.createMethodWrappingFunction(getDistanceTraveledMethod); + Function getLegDistanceTraveled = functionFactory.createCompoundFunction(Arrays.asList(getLeg, getDistanceTraveled)); + + FunctionDTO getLegDistanceTraveledDTO_English = dtoFactory.createFunctionDTO(getLegDistanceTraveled, stringMessages, Locale.ENGLISH); + Function providedFunction = (Function) server.getFunctionForDTO(getLegDistanceTraveledDTO_English); + assertThat(providedFunction, is(getLegDistanceTraveled)); + FunctionDTO getLegDistanceTraveledDTO_German = dtoFactory.createFunctionDTO(getLegDistanceTraveled, stringMessages, Locale.GERMAN); + providedFunction = (Function) server.getFunctionForDTO(getLegDistanceTraveledDTO_German); + assertThat(providedFunction, is(getLegDistanceTraveled)); + FunctionDTO getLegDistanceTraveledDTO = dtoFactory.createFunctionDTO(getLegDistanceTraveled); + providedFunction = (Function) server.getFunctionForDTO(getLegDistanceTraveledDTO); + assertThat(providedFunction, is(getLegDistanceTraveled)); + } + + @SuppressWarnings("unchecked") // Hamcrest requires type matching of actual and expected type, so the Functions have to be specific (without ) + @Test + public void testGetFunctionForExternalFunctionDTO() { + FunctionFactory functionFactory = FunctionTestsUtil.getFunctionFactory(); + DataMiningDTOFactory dtoFactory = FunctionTestsUtil.getDTOFactory(); + + Method fooMethod = FunctionTestsUtil.getMethodFromClass(Test_ExternalLibraryClass.class, "foo"); + Function foo = functionFactory.createMethodWrappingFunction(fooMethod); + + FunctionDTO fooDTO_English = dtoFactory.createFunctionDTO(foo, stringMessages, Locale.ENGLISH); + Function providedFunction = (Function) server.getFunctionForDTO(fooDTO_English); + assertThat(providedFunction, is(foo)); + FunctionDTO fooDTO_German = dtoFactory.createFunctionDTO(foo, stringMessages, Locale.GERMAN); + providedFunction = (Function) server.getFunctionForDTO(fooDTO_German); + assertThat(providedFunction, is(foo)); + FunctionDTO fooDTO = dtoFactory.createFunctionDTO(foo); + providedFunction = (Function) server.getFunctionForDTO(fooDTO); + assertThat(providedFunction, is(foo)); } @Test public void testGetFunctionForUnregisteredDTO() { - Method illegalDimensionMethod = FunctionTestsUtil.getMethodFromClass(SimpleClassWithMarkedMethods.class, "illegalDimension"); - Function illegalDimension = FunctionTestsUtil.getFunctionFactory().createMethodWrappingFunction(illegalDimensionMethod); - FunctionDTO illegalDimensionDTO = FunctionTestsUtil.getDTOFactory().createFunctionDTO(illegalDimension, stringMessages, Locale.ENGLISH); - - assertThat(server.getFunctionForDTO(illegalDimensionDTO), is(nullValue())); + Method dimensionMethod = FunctionTestsUtil.getMethodFromClass(SimpleClassWithMarkedMethods.class, "dimension"); + Function dimension = FunctionTestsUtil.getFunctionFactory().createMethodWrappingFunction(dimensionMethod); + FunctionDTO dimensionDTO_English = FunctionTestsUtil.getDTOFactory().createFunctionDTO(dimension, stringMessages, Locale.ENGLISH); + assertThat(server.getFunctionForDTO(dimensionDTO_English), is(nullValue())); + FunctionDTO dimensionDTO_German = FunctionTestsUtil.getDTOFactory().createFunctionDTO(dimension, stringMessages, Locale.GERMAN); + assertThat(server.getFunctionForDTO(dimensionDTO_German), is(nullValue())); + FunctionDTO dimensionDTO = FunctionTestsUtil.getDTOFactory().createFunctionDTO(dimension); + assertThat(server.getFunctionForDTO(dimensionDTO), is(nullValue())); + + Method sideEffectFreeValueMethod = FunctionTestsUtil.getMethodFromClass(SimpleClassWithMarkedMethods.class, "sideEffectFreeValue"); + Function sideEffectFreeValue = FunctionTestsUtil.getFunctionFactory().createMethodWrappingFunction(sideEffectFreeValueMethod); + FunctionDTO sideEffectFreeValueDTO_English = FunctionTestsUtil.getDTOFactory().createFunctionDTO(sideEffectFreeValue, stringMessages, Locale.ENGLISH); + assertThat(server.getFunctionForDTO(sideEffectFreeValueDTO_English), is(nullValue())); + FunctionDTO sideEffectFreeValueDTO_German = FunctionTestsUtil.getDTOFactory().createFunctionDTO(sideEffectFreeValue, stringMessages, Locale.GERMAN); + assertThat(server.getFunctionForDTO(sideEffectFreeValueDTO_German), is(nullValue())); + FunctionDTO sideEffectFreeValueDTO = FunctionTestsUtil.getDTOFactory().createFunctionDTO(sideEffectFreeValue); + assertThat(server.getFunctionForDTO(sideEffectFreeValueDTO), is(nullValue())); } @Test