diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index be75b2586..bc8796a7c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -61,14 +61,10 @@ jobs: profiles: browsers directives: -DbrowsersTests moduleName: it - env: - EDGE_BINARY: /usr/bin/microsoft-edge - os: macos-latest profiles: macos directives: -DmacosTests moduleName: it-macos - env: - EDGE_BINARY: /usr/bin/microsoft-edge - os: windows-latest profiles: browsers directives: -DbrowsersTests diff --git a/docs/Gemfile.lock b/docs/Gemfile.lock index 4353c85d1..3d0674460 100644 --- a/docs/Gemfile.lock +++ b/docs/Gemfile.lock @@ -239,7 +239,7 @@ GEM rb-fsevent (0.11.2) rb-inotify (0.10.1) ffi (~> 1.0) - rexml (3.3.9) + rexml (3.4.2) rouge (3.26.0) ruby2_keywords (0.0.5) rubyzip (2.3.2) diff --git a/it-bidi/src/test/resources/configuration.yaml b/it-bidi/src/test/resources/configuration.yaml index 0ecf33e73..332584c27 100644 --- a/it-bidi/src/test/resources/configuration.yaml +++ b/it-bidi/src/test/resources/configuration.yaml @@ -14,8 +14,6 @@ drivers: edge: args: - --headless=new - experimentalOptions: - binary: ${EDGE_BINARY:-} video: frames: diff --git a/it/src/test/resources/configuration.yaml b/it/src/test/resources/configuration.yaml index 3c1c2a00a..49d6aa900 100644 --- a/it/src/test/resources/configuration.yaml +++ b/it/src/test/resources/configuration.yaml @@ -13,8 +13,6 @@ drivers: edge: args: - --headless=new - experimentalOptions: - binary: ${EDGE_BINARY:-} video: frames: diff --git a/pom.xml b/pom.xml index eaff64cbe..4dd06bb9d 100644 --- a/pom.xml +++ b/pom.xml @@ -47,7 +47,7 @@ true 5.13.4 - 5.19.0 + 5.20.0 2.20.0 8.12.6 4.38.0 @@ -124,7 +124,7 @@ org.projectlombok lombok - 1.18.40 + 1.18.42 @@ -250,7 +250,7 @@ net.datafaker datafaker - 2.4.4 + 2.5.1 @@ -323,7 +323,7 @@ com.puppycrawl.tools checkstyle - 11.0.1 + 11.1.0 com.github.sevntu-checkstyle @@ -365,7 +365,7 @@ org.apache.maven.plugins maven-compiler-plugin - 3.14.0 + 3.14.1 21 21 @@ -485,7 +485,7 @@ org.codehaus.mojo flatten-maven-plugin - 1.7.2 + 1.7.3 true ossrh diff --git a/spectrum/pom.xml b/spectrum/pom.xml index 7fed88531..cad716f5b 100644 --- a/spectrum/pom.xml +++ b/spectrum/pom.xml @@ -328,7 +328,7 @@ org.apache.maven.plugins maven-javadoc-plugin - 3.11.3 + 3.12.0 -Xdoclint:none @@ -363,7 +363,7 @@ org.sonatype.central central-publishing-maven-plugin - 0.8.0 + 0.9.0 true central diff --git a/spectrum/src/main/java/io/github/giulong/spectrum/utils/Reflections.java b/spectrum/src/main/java/io/github/giulong/spectrum/utils/Reflections.java index 80373cb85..d9d994578 100644 --- a/spectrum/src/main/java/io/github/giulong/spectrum/utils/Reflections.java +++ b/spectrum/src/main/java/io/github/giulong/spectrum/utils/Reflections.java @@ -59,14 +59,14 @@ public static Field getField(final String fieldName, final Object object) { return field; } - @SneakyThrows - public static Object getFieldValue(final String fieldName, final Object object) { - log.trace("Getting value of field {}.{}", object.getClass().getSimpleName(), fieldName); - return getField(fieldName, object).get(object); - } + @SafeVarargs + public static T getFieldValue(final String fieldName, final Object object, final T... reified) { + if (reified == null || reified.length > 0) { + throw new IllegalArgumentException("Do not pass arguments as last parameter"); + } - public static T getFieldValue(final String fieldName, final Object object, final Class clazz) { - return clazz.cast(getFieldValue(fieldName, object)); + final Object value = getValueOf(getField(fieldName, object), object); + return getClassOf(reified).cast(value); } public static void setField(final String fieldName, final Object object, final Object value) { @@ -112,6 +112,11 @@ public static List getAnnotatedFieldsValues(final Object object, final Cl .toList(); } + @SuppressWarnings("unchecked") + public static Class getClassOf(final T[] array) { + return (Class) array.getClass().getComponentType(); + } + @SneakyThrows static Object getValueOf(final Field field, final Object object) { return field.get(object); diff --git a/spectrum/src/main/java/io/github/giulong/spectrum/utils/YamlUtils.java b/spectrum/src/main/java/io/github/giulong/spectrum/utils/YamlUtils.java index 86267dff2..d3b59bfc8 100644 --- a/spectrum/src/main/java/io/github/giulong/spectrum/utils/YamlUtils.java +++ b/spectrum/src/main/java/io/github/giulong/spectrum/utils/YamlUtils.java @@ -135,7 +135,7 @@ SimpleModule buildDynamicModuleFor(final Class clazz, final String file) { @SneakyThrows T read(final ObjectReader reader, final String file, final Class clazz) { - return reader.readValue(classLoader.getResource(file), clazz); + return reader.readValue(classLoader.getResourceAsStream(file), clazz); } T read(final FileProvider fileProvider, final String file, final Class clazz) { @@ -175,6 +175,6 @@ void updateWithFile(final T t, final String file, final FileProvider filePro fileProvider .augment(yamlMapper) .withValueToUpdate(t) - .readValue(classLoader.getResource(fileFound)); + .readValue(classLoader.getResourceAsStream(fileFound)); } } diff --git a/spectrum/src/test/java/io/github/giulong/spectrum/drivers/AppiumGenericTest.java b/spectrum/src/test/java/io/github/giulong/spectrum/drivers/AppiumGenericTest.java index 06abd6c0d..de4c9966c 100644 --- a/spectrum/src/test/java/io/github/giulong/spectrum/drivers/AppiumGenericTest.java +++ b/spectrum/src/test/java/io/github/giulong/spectrum/drivers/AppiumGenericTest.java @@ -51,9 +51,8 @@ void beforeEach() { @Test @DisplayName("buildCapabilities should build a new instance of capabilities and set the capabilities from the yaml on it") void buildCapabilitiesAbsoluteAppPath() { - MockedConstruction desiredCapabilitiesMockedConstruction = mockConstruction(MutableCapabilities.class, (mock, context) -> { - assertEquals(capabilities, context.arguments().getFirst()); - }); + MockedConstruction desiredCapabilitiesMockedConstruction = mockConstruction(MutableCapabilities.class, + (mock, context) -> assertEquals(capabilities, context.arguments().getFirst())); when(configuration.getDrivers()).thenReturn(drivers); when(drivers.getAppiumGeneric()).thenReturn(appiumGenericConfiguration); @@ -61,7 +60,7 @@ void buildCapabilitiesAbsoluteAppPath() { appiumGeneric.buildCapabilities(); - final Capabilities actual = Reflections.getFieldValue("capabilities", appiumGeneric, Capabilities.class); + final Capabilities actual = Reflections.getFieldValue("capabilities", appiumGeneric); assertEquals(desiredCapabilitiesMockedConstruction.constructed().getFirst(), actual); desiredCapabilitiesMockedConstruction.close(); diff --git a/spectrum/src/test/java/io/github/giulong/spectrum/drivers/EspressoTest.java b/spectrum/src/test/java/io/github/giulong/spectrum/drivers/EspressoTest.java index bdd5ce905..6345a1332 100644 --- a/spectrum/src/test/java/io/github/giulong/spectrum/drivers/EspressoTest.java +++ b/spectrum/src/test/java/io/github/giulong/spectrum/drivers/EspressoTest.java @@ -50,9 +50,8 @@ void buildCapabilities() { final String appPath = path.toString(); final String appAbsolutePath = path.toAbsolutePath().toString(); - MockedConstruction desiredCapabilitiesMockedConstruction = mockConstruction(EspressoOptions.class, (mock, context) -> { - assertEquals(capabilities, context.arguments().getFirst()); - }); + MockedConstruction desiredCapabilitiesMockedConstruction = mockConstruction(EspressoOptions.class, + (mock, context) -> assertEquals(capabilities, context.arguments().getFirst())); when(configuration.getDrivers()).thenReturn(drivers); when(drivers.getEspresso()).thenReturn(espressoConfiguration); @@ -62,7 +61,7 @@ void buildCapabilities() { espresso.buildCapabilities(); - final EspressoOptions actual = Reflections.getFieldValue("capabilities", espresso, EspressoOptions.class); + final EspressoOptions actual = Reflections.getFieldValue("capabilities", espresso); assertEquals(desiredCapabilitiesMockedConstruction.constructed().getFirst(), actual); verify(capabilities).put(APP_CAPABILITY, appAbsolutePath); @@ -75,9 +74,8 @@ void buildCapabilities() { void buildCapabilitiesAbsoluteAppPath() { final String appPath = Path.of("absolute", "path").toAbsolutePath().toString(); - MockedConstruction desiredCapabilitiesMockedConstruction = mockConstruction(EspressoOptions.class, (mock, context) -> { - assertEquals(capabilities, context.arguments().getFirst()); - }); + MockedConstruction desiredCapabilitiesMockedConstruction = mockConstruction(EspressoOptions.class, + (mock, context) -> assertEquals(capabilities, context.arguments().getFirst())); when(configuration.getDrivers()).thenReturn(drivers); when(drivers.getEspresso()).thenReturn(espressoConfiguration); @@ -87,7 +85,7 @@ void buildCapabilitiesAbsoluteAppPath() { espresso.buildCapabilities(); - final EspressoOptions actual = Reflections.getFieldValue("capabilities", espresso, EspressoOptions.class); + final EspressoOptions actual = Reflections.getFieldValue("capabilities", espresso); assertEquals(desiredCapabilitiesMockedConstruction.constructed().getFirst(), actual); desiredCapabilitiesMockedConstruction.close(); diff --git a/spectrum/src/test/java/io/github/giulong/spectrum/drivers/Mac2Test.java b/spectrum/src/test/java/io/github/giulong/spectrum/drivers/Mac2Test.java index ad7886184..2a319089b 100644 --- a/spectrum/src/test/java/io/github/giulong/spectrum/drivers/Mac2Test.java +++ b/spectrum/src/test/java/io/github/giulong/spectrum/drivers/Mac2Test.java @@ -50,9 +50,8 @@ void beforeEach() { @Test @DisplayName("buildCapabilities should build a new instance of mac2Options and set the capabilities from the yaml on it") void buildCapabilitiesAbsoluteAppPath() { - MockedConstruction desiredCapabilitiesMockedConstruction = mockConstruction(Mac2Options.class, (mock, context) -> { - assertEquals(capabilities, context.arguments().getFirst()); - }); + MockedConstruction desiredCapabilitiesMockedConstruction = mockConstruction(Mac2Options.class, + (mock, context) -> assertEquals(capabilities, context.arguments().getFirst())); when(configuration.getDrivers()).thenReturn(drivers); when(drivers.getMac2()).thenReturn(mac2Configuration); @@ -60,7 +59,7 @@ void buildCapabilitiesAbsoluteAppPath() { mac2.buildCapabilities(); - final Mac2Options actual = Reflections.getFieldValue("capabilities", mac2, Mac2Options.class); + final Mac2Options actual = Reflections.getFieldValue("capabilities", mac2); assertEquals(desiredCapabilitiesMockedConstruction.constructed().getFirst(), actual); desiredCapabilitiesMockedConstruction.close(); diff --git a/spectrum/src/test/java/io/github/giulong/spectrum/drivers/UiAutomator2Test.java b/spectrum/src/test/java/io/github/giulong/spectrum/drivers/UiAutomator2Test.java index 3dd136c31..57b7af9db 100644 --- a/spectrum/src/test/java/io/github/giulong/spectrum/drivers/UiAutomator2Test.java +++ b/spectrum/src/test/java/io/github/giulong/spectrum/drivers/UiAutomator2Test.java @@ -51,9 +51,8 @@ void buildCapabilities() { final String appPath = path.toString(); final String appAbsolutePath = path.toAbsolutePath().toString(); - MockedConstruction desiredCapabilitiesMockedConstruction = mockConstruction(UiAutomator2Options.class, (mock, context) -> { - assertEquals(capabilities, context.arguments().getFirst()); - }); + MockedConstruction desiredCapabilitiesMockedConstruction = mockConstruction(UiAutomator2Options.class, + (mock, context) -> assertEquals(capabilities, context.arguments().getFirst())); when(configuration.getDrivers()).thenReturn(drivers); when(drivers.getUiAutomator2()).thenReturn(uiAutomator2Configuration); @@ -63,7 +62,7 @@ void buildCapabilities() { uiAutomator2.buildCapabilities(); - final UiAutomator2Options actual = Reflections.getFieldValue("capabilities", uiAutomator2, UiAutomator2Options.class); + final UiAutomator2Options actual = Reflections.getFieldValue("capabilities", uiAutomator2); assertEquals(desiredCapabilitiesMockedConstruction.constructed().getFirst(), actual); verify(capabilities).put(APP_CAPABILITY, appAbsolutePath); @@ -77,9 +76,8 @@ void buildCapabilities() { void buildCapabilitiesAbsoluteAppPath() { final String appPath = Path.of("absolute", "path").toAbsolutePath().toString(); - MockedConstruction desiredCapabilitiesMockedConstruction = mockConstruction(UiAutomator2Options.class, (mock, context) -> { - assertEquals(capabilities, context.arguments().getFirst()); - }); + MockedConstruction desiredCapabilitiesMockedConstruction = mockConstruction(UiAutomator2Options.class, + (mock, context) -> assertEquals(capabilities, context.arguments().getFirst())); when(configuration.getDrivers()).thenReturn(drivers); when(drivers.getUiAutomator2()).thenReturn(uiAutomator2Configuration); @@ -89,7 +87,7 @@ void buildCapabilitiesAbsoluteAppPath() { uiAutomator2.buildCapabilities(); - final UiAutomator2Options actual = Reflections.getFieldValue("capabilities", uiAutomator2, UiAutomator2Options.class); + final UiAutomator2Options actual = Reflections.getFieldValue("capabilities", uiAutomator2); assertEquals(desiredCapabilitiesMockedConstruction.constructed().getFirst(), actual); desiredCapabilitiesMockedConstruction.close(); diff --git a/spectrum/src/test/java/io/github/giulong/spectrum/drivers/WindowsTest.java b/spectrum/src/test/java/io/github/giulong/spectrum/drivers/WindowsTest.java index 97a653bd2..895234997 100644 --- a/spectrum/src/test/java/io/github/giulong/spectrum/drivers/WindowsTest.java +++ b/spectrum/src/test/java/io/github/giulong/spectrum/drivers/WindowsTest.java @@ -79,9 +79,8 @@ void configureWaitsOf() { @Test @DisplayName("buildCapabilities should build a new instance of windowsOptions and set the capabilities from the yaml on it") void buildCapabilitiesAbsoluteAppPath() { - MockedConstruction desiredCapabilitiesMockedConstruction = mockConstruction(WindowsOptions.class, (mock, context) -> { - assertEquals(capabilities, context.arguments().getFirst()); - }); + MockedConstruction desiredCapabilitiesMockedConstruction = mockConstruction(WindowsOptions.class, + (mock, context) -> assertEquals(capabilities, context.arguments().getFirst())); when(configuration.getDrivers()).thenReturn(drivers); when(drivers.getWindows()).thenReturn(windowsConfiguration); @@ -89,7 +88,7 @@ void buildCapabilitiesAbsoluteAppPath() { windows.buildCapabilities(); - final WindowsOptions actual = Reflections.getFieldValue("capabilities", windows, WindowsOptions.class); + final WindowsOptions actual = Reflections.getFieldValue("capabilities", windows); assertEquals(desiredCapabilitiesMockedConstruction.constructed().getFirst(), actual); desiredCapabilitiesMockedConstruction.close(); diff --git a/spectrum/src/test/java/io/github/giulong/spectrum/drivers/XCUITestTest.java b/spectrum/src/test/java/io/github/giulong/spectrum/drivers/XCUITestTest.java index 8b80d20c8..1a106e16a 100644 --- a/spectrum/src/test/java/io/github/giulong/spectrum/drivers/XCUITestTest.java +++ b/spectrum/src/test/java/io/github/giulong/spectrum/drivers/XCUITestTest.java @@ -56,9 +56,8 @@ void buildCapabilities() { final String appPath = path.toString(); final String appAbsolutePath = path.toAbsolutePath().toString(); - MockedConstruction desiredCapabilitiesMockedConstruction = mockConstruction(XCUITestOptions.class, (mock, context) -> { - assertEquals(capabilities, context.arguments().getFirst()); - }); + MockedConstruction desiredCapabilitiesMockedConstruction = mockConstruction(XCUITestOptions.class, + (mock, context) -> assertEquals(capabilities, context.arguments().getFirst())); when(configuration.getDrivers()).thenReturn(drivers); when(drivers.getXcuiTest()).thenReturn(xcuiTestConfiguration); @@ -68,7 +67,7 @@ void buildCapabilities() { xcuiTest.buildCapabilities(); - final XCUITestOptions actual = Reflections.getFieldValue("capabilities", xcuiTest, XCUITestOptions.class); + final XCUITestOptions actual = Reflections.getFieldValue("capabilities", xcuiTest); assertEquals(desiredCapabilitiesMockedConstruction.constructed().getFirst(), actual); verify(capabilities).put(APP_CAPABILITY, appAbsolutePath); @@ -82,9 +81,8 @@ void buildCapabilities() { void buildCapabilitiesAbsoluteAppPath() { final String appPath = Path.of("absolute", "path").toAbsolutePath().toString(); - MockedConstruction desiredCapabilitiesMockedConstruction = mockConstruction(XCUITestOptions.class, (mock, context) -> { - assertEquals(capabilities, context.arguments().getFirst()); - }); + MockedConstruction desiredCapabilitiesMockedConstruction = mockConstruction(XCUITestOptions.class, + (mock, context) -> assertEquals(capabilities, context.arguments().getFirst())); when(configuration.getDrivers()).thenReturn(drivers); when(drivers.getXcuiTest()).thenReturn(xcuiTestConfiguration); @@ -94,7 +92,7 @@ void buildCapabilitiesAbsoluteAppPath() { xcuiTest.buildCapabilities(); - final XCUITestOptions actual = Reflections.getFieldValue("capabilities", xcuiTest, XCUITestOptions.class); + final XCUITestOptions actual = Reflections.getFieldValue("capabilities", xcuiTest); assertEquals(desiredCapabilitiesMockedConstruction.constructed().getFirst(), actual); desiredCapabilitiesMockedConstruction.close(); diff --git a/spectrum/src/test/java/io/github/giulong/spectrum/internals/AppiumLogTest.java b/spectrum/src/test/java/io/github/giulong/spectrum/internals/AppiumLogTest.java index 678d1c890..6b22b945d 100644 --- a/spectrum/src/test/java/io/github/giulong/spectrum/internals/AppiumLogTest.java +++ b/spectrum/src/test/java/io/github/giulong/spectrum/internals/AppiumLogTest.java @@ -55,7 +55,7 @@ void writeFlush() { final char c = '\n'; appiumLog.write(c); - final StringBuffer stringBuffer = Reflections.getFieldValue("stringBuffer", appiumLog, StringBuffer.class); + final StringBuffer stringBuffer = Reflections.getFieldValue("stringBuffer", appiumLog); verify(stringBuffer).setLength(0); } @@ -67,7 +67,7 @@ void flush() { assertEquals(stringBuffers.getFirst(), Reflections.getFieldValue("stringBuffer", appiumLog)); appiumLog.flush(); - final StringBuffer stringBuffer = Reflections.getFieldValue("stringBuffer", appiumLog, StringBuffer.class); + final StringBuffer stringBuffer = Reflections.getFieldValue("stringBuffer", appiumLog); verify(stringBuffer).setLength(0); } diff --git a/spectrum/src/test/java/io/github/giulong/spectrum/internals/DriverLogTest.java b/spectrum/src/test/java/io/github/giulong/spectrum/internals/DriverLogTest.java index 11f74af32..8f6792b88 100644 --- a/spectrum/src/test/java/io/github/giulong/spectrum/internals/DriverLogTest.java +++ b/spectrum/src/test/java/io/github/giulong/spectrum/internals/DriverLogTest.java @@ -55,7 +55,7 @@ void writeFlush() { final char c = '\n'; driverLog.write(c); - final StringBuffer stringBuffer = Reflections.getFieldValue("stringBuffer", driverLog, StringBuffer.class); + final StringBuffer stringBuffer = Reflections.getFieldValue("stringBuffer", driverLog); verify(stringBuffer).setLength(0); } @@ -67,7 +67,7 @@ void flush() { assertEquals(stringBuffers.getFirst(), Reflections.getFieldValue("stringBuffer", driverLog)); driverLog.flush(); - final StringBuffer stringBuffer = Reflections.getFieldValue("stringBuffer", driverLog, StringBuffer.class); + final StringBuffer stringBuffer = Reflections.getFieldValue("stringBuffer", driverLog); verify(stringBuffer).setLength(0); } diff --git a/spectrum/src/test/java/io/github/giulong/spectrum/internals/page_factory/SpectrumLocatingElementHandlerTest.java b/spectrum/src/test/java/io/github/giulong/spectrum/internals/page_factory/SpectrumLocatingElementHandlerTest.java index 5d907b9d8..bcc695c13 100644 --- a/spectrum/src/test/java/io/github/giulong/spectrum/internals/page_factory/SpectrumLocatingElementHandlerTest.java +++ b/spectrum/src/test/java/io/github/giulong/spectrum/internals/page_factory/SpectrumLocatingElementHandlerTest.java @@ -50,7 +50,7 @@ class SpectrumLocatingElementHandlerTest { void constructor(final boolean secured) { final SpectrumLocatingElementHandler handler = new SpectrumLocatingElementHandler(elementLocator, secured); - Assertions.assertEquals(secured, Reflections.getFieldValue("secured", handler, Boolean.class)); + Assertions.assertEquals(secured, Reflections.getFieldValue("secured", handler)); } @SuppressWarnings({"checkstyle:IllegalThrows"}) diff --git a/spectrum/src/test/java/io/github/giulong/spectrum/utils/HtmlUtilsTest.java b/spectrum/src/test/java/io/github/giulong/spectrum/utils/HtmlUtilsTest.java index 5e19e248d..2af33c8b5 100644 --- a/spectrum/src/test/java/io/github/giulong/spectrum/utils/HtmlUtilsTest.java +++ b/spectrum/src/test/java/io/github/giulong/spectrum/utils/HtmlUtilsTest.java @@ -93,10 +93,10 @@ void sessionOpened() { htmlUtils.sessionOpened(); - assertEquals(videoTemplate, Reflections.getFieldValue("videoTemplate", htmlUtils, String.class)); - assertEquals(divTemplate, Reflections.getFieldValue("divTemplate", htmlUtils, String.class)); - assertEquals(divFrameTemplate, Reflections.getFieldValue("frameTemplate", htmlUtils, String.class)); - assertEquals(divImageTemplate, Reflections.getFieldValue("inlineImageTemplate", htmlUtils, String.class)); + assertEquals(videoTemplate, Reflections.getFieldValue("videoTemplate", htmlUtils)); + assertEquals(divTemplate, Reflections.getFieldValue("divTemplate", htmlUtils)); + assertEquals(divFrameTemplate, Reflections.getFieldValue("frameTemplate", htmlUtils)); + assertEquals(divImageTemplate, Reflections.getFieldValue("inlineImageTemplate", htmlUtils)); } @Test diff --git a/spectrum/src/test/java/io/github/giulong/spectrum/utils/ReflectionsTest.java b/spectrum/src/test/java/io/github/giulong/spectrum/utils/ReflectionsTest.java index a0dcd81d7..0f0bb856d 100644 --- a/spectrum/src/test/java/io/github/giulong/spectrum/utils/ReflectionsTest.java +++ b/spectrum/src/test/java/io/github/giulong/spectrum/utils/ReflectionsTest.java @@ -5,17 +5,14 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; -import org.junit.jupiter.params.provider.ValueSource; +import org.junit.jupiter.params.provider.*; import java.lang.reflect.Field; import java.lang.reflect.ParameterizedType; import java.util.List; import java.util.stream.Stream; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.params.provider.Arguments.arguments; class ReflectionsTest { @@ -76,7 +73,7 @@ void getFieldNotFound() { } @Test - @DisplayName("getFieldValue should return the value of the field with the provided name on the provided object") + @DisplayName("getFieldValue should return the value of the field with the provided name on the provided object, casted to the generic class") void getFieldValue() { final String fieldName = "fieldString"; final String value = "value"; @@ -85,14 +82,19 @@ void getFieldValue() { assertEquals(value, Reflections.getFieldValue(fieldName, dummy)); } - @Test - @DisplayName("getFieldValue should return the value of the field with the provided name on the provided object, casted to the provided class") - void getFieldValueCast() { - final String fieldName = "fieldString"; - final String value = "value"; - final Dummy dummy = new Dummy(value); + @DisplayName("getFieldValue should throw an IllegalArgumentException if reified args are passed") + @ParameterizedTest(name = "with reified {0}") + @NullSource + @MethodSource("getFieldValueExceptionValuesProvider") + void getFieldValueException(final Dummy[] reified) { + final Exception exception = assertThrows(IllegalArgumentException.class, () -> Reflections.getFieldValue("fieldString", new Dummy("value"), reified)); + assertEquals("Do not pass arguments as last parameter", exception.getMessage()); + } - assertEquals(value, Reflections.getFieldValue(fieldName, dummy, String.class)); + static Stream getFieldValueExceptionValuesProvider() { + return Stream.of( + arguments((Object) new Dummy[]{new Dummy("value")}) + ); } @Test diff --git a/spectrum/src/test/java/io/github/giulong/spectrum/utils/YamlUtilsTest.java b/spectrum/src/test/java/io/github/giulong/spectrum/utils/YamlUtilsTest.java index d9b67d114..ac0766cea 100644 --- a/spectrum/src/test/java/io/github/giulong/spectrum/utils/YamlUtilsTest.java +++ b/spectrum/src/test/java/io/github/giulong/spectrum/utils/YamlUtilsTest.java @@ -17,7 +17,6 @@ import java.io.IOException; import java.io.InputStream; -import java.net.URL; import java.util.Objects; import java.util.Set; @@ -79,7 +78,7 @@ void construction() { "LogSummaryReporter", "TxtSummaryReporter", "HtmlSummaryReporter" - ), Reflections.getFieldValue("yamlMapper", yamlUtils, YAMLMapper.class).getRegisteredModuleIds()); + ), ((YAMLMapper) Reflections.getFieldValue("yamlMapper", yamlUtils)).getRegisteredModuleIds()); assertEquals(Set.of( "jackson-datatype-jsr310", @@ -88,9 +87,9 @@ void construction() { "boolean", "Level", "Duration" - ), Reflections.getFieldValue("dynamicConfYamlMapper", yamlUtils, YAMLMapper.class).getRegisteredModuleIds()); + ), ((YAMLMapper) Reflections.getFieldValue("dynamicConfYamlMapper", yamlUtils)).getRegisteredModuleIds()); - assertFalse(Reflections.getFieldValue("writer", yamlUtils, ObjectWriter.class).isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS)); + assertFalse(((ObjectWriter) Reflections.getFieldValue("writer", yamlUtils)).isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS)); } @Test @@ -163,7 +162,7 @@ void readDynamicDeserializable() throws IOException { Reflections.setField("dynamicConfYamlMapper", yamlUtils, yamlMapper); when(yamlMapper.reader()).thenReturn(reader); - when(reader.readValue(any(URL.class), eq(clazz))).thenReturn(testYaml); + when(reader.readValue(any(InputStream.class), eq(clazz))).thenReturn(testYaml); when(reader.withValueToUpdate(testYaml)).thenReturn(reader); when(reader.readValue(jsonNode)).thenReturn(testYaml); @@ -180,7 +179,7 @@ void updateWithFile(final String file) throws IOException { when(fileProvider.find(file)).thenReturn(file); when(fileProvider.augment(yamlMapper)).thenReturn(reader); when(reader.withValueToUpdate(testYaml)).thenReturn(reader); - when(reader.readValue(any(URL.class))).thenReturn(testYaml); + when(reader.readValue(any(InputStream.class))).thenReturn(testYaml); yamlUtils.updateWithClientFile(testYaml, file); } @@ -209,7 +208,7 @@ void updateWithInternalFile() throws IOException { when(fileProvider.find(file)).thenReturn(file); when(fileProvider.augment(yamlMapper)).thenReturn(reader); when(reader.withValueToUpdate(testYaml)).thenReturn(reader); - when(reader.readValue(any(URL.class))).thenReturn(testYaml); + when(reader.readValue(any(InputStream.class))).thenReturn(testYaml); yamlUtils.updateWithInternalFile(testYaml, file); }