Skip to content

Commit

Permalink
tests: remove duplicated testing code
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed Nov 28, 2023
1 parent c7eff79 commit 6ff304b
Show file tree
Hide file tree
Showing 30 changed files with 38 additions and 317 deletions.
5 changes: 0 additions & 5 deletions junit-servers-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,6 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

import static com.github.mjeanroy.junit.servers.testing.HttpTestUtils.localhost;
import static com.github.mjeanroy.junit.servers.testing.HttpTestUtils.url;
import static com.github.mjeanroy.junit.servers.utils.commons.Fields.readPrivate;
import static com.github.mjeanroy.junit.servers.testing.ReflectionTestUtils.readPrivate;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

import static com.github.mjeanroy.junit.servers.client.impl.apache.ApacheHttpClient.defaultApacheHttpClient;
import static com.github.mjeanroy.junit.servers.client.impl.apache.ApacheHttpClient.newApacheHttpClient;
import static com.github.mjeanroy.junit.servers.utils.commons.Fields.readPrivate;
import static com.github.mjeanroy.junit.servers.testing.ReflectionTestUtils.readPrivate;
import static org.assertj.core.api.Assertions.assertThat;

class ApacheHttpClientTest extends BaseHttpClientTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

import static com.github.mjeanroy.junit.servers.client.impl.async.AsyncHttpClient.defaultAsyncHttpClient;
import static com.github.mjeanroy.junit.servers.client.impl.async.AsyncHttpClient.newAsyncHttpClient;
import static com.github.mjeanroy.junit.servers.utils.commons.Fields.readPrivate;
import static com.github.mjeanroy.junit.servers.testing.ReflectionTestUtils.readPrivate;
import static org.assertj.core.api.Assertions.assertThat;

class AsyncHttpClientTest extends BaseHttpClientTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

import static com.github.mjeanroy.junit.servers.client.impl.ning.NingAsyncHttpClient.defaultAsyncHttpClient;
import static com.github.mjeanroy.junit.servers.client.impl.ning.NingAsyncHttpClient.newAsyncHttpClient;
import static com.github.mjeanroy.junit.servers.utils.commons.Fields.readPrivate;
import static com.github.mjeanroy.junit.servers.testing.ReflectionTestUtils.readPrivate;
import static org.assertj.core.api.Assertions.assertThat;

class NingAsyncHttpClientTest extends BaseHttpClientTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

import static com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient.defaultOkHttpClient;
import static com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient.newOkHttpClient;
import static com.github.mjeanroy.junit.servers.utils.commons.Fields.readPrivate;
import static com.github.mjeanroy.junit.servers.testing.ReflectionTestUtils.readPrivate;
import static org.assertj.core.api.Assertions.assertThat;

class OkHttpClientTest extends BaseHttpClientTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,20 @@

import com.github.mjeanroy.junit.servers.commons.fixtures.Bar;
import com.github.mjeanroy.junit.servers.commons.fixtures.FooAnnotation;
import com.github.mjeanroy.junit.servers.utils.commons.Fields;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;

import static com.github.mjeanroy.junit.servers.commons.reflect.Reflections.*;
import static com.github.mjeanroy.junit.servers.commons.reflect.Reflections.findAllFields;
import static com.github.mjeanroy.junit.servers.commons.reflect.Reflections.findStaticFieldsAnnotatedWith;
import static com.github.mjeanroy.junit.servers.commons.reflect.Reflections.findStaticMethodsAnnotatedWith;
import static com.github.mjeanroy.junit.servers.commons.reflect.Reflections.getter;
import static com.github.mjeanroy.junit.servers.commons.reflect.Reflections.invoke;
import static com.github.mjeanroy.junit.servers.commons.reflect.Reflections.setter;
import static com.github.mjeanroy.junit.servers.testing.ReflectionTestUtils.getPrivateField;
import static com.github.mjeanroy.junit.servers.testing.ReflectionTestUtils.getPrivateMethod;
import static org.assertj.core.api.Assertions.assertThat;

class ReflectionsTest {
Expand Down Expand Up @@ -61,7 +67,7 @@ void it_should_set_value_on_private_field() {
final Bar bar = new Bar(1, "foo");
final String newValue = "bar";

final Field field = Fields.getPrivateField(Bar.class, "name");
final Field field = getPrivateField(Bar.class, "name");

assertThat(field.isAccessible()).isFalse();
assertThat(bar.getName()).isEqualTo("foo");
Expand All @@ -76,7 +82,7 @@ void it_should_set_value_on_private_field() {
void it_should_get_value_on_private_field() {
final String actualValue = "foo";
final Bar bar = new Bar(1, actualValue);
final Field field = Fields.getPrivateField(Bar.class, "name");
final Field field = getPrivateField(Bar.class, "name");

assertThat(field.isAccessible()).isFalse();

Expand All @@ -88,7 +94,7 @@ void it_should_get_value_on_private_field() {

@Test
void it_should_get_value_on_static_private_field() {
final Field field = Fields.getPrivateField(Bar.class, "staticField");
final Field field = getPrivateField(Bar.class, "staticField");

assertThat(field.isAccessible()).isFalse();

Expand All @@ -100,7 +106,7 @@ void it_should_get_value_on_static_private_field() {

@Test
void it_should_get_value_on_static_private_method() {
final Method method = Fields.getPrivateMethod(Bar.class, "getStaticPrivateMethod");
final Method method = getPrivateMethod(Bar.class, "getStaticPrivateMethod");

assertThat(method.isAccessible()).isFalse();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
import java.lang.reflect.Field;

import static com.github.mjeanroy.junit.servers.engine.ConfigurationAnnotationHandler.newConfigurationAnnotationHandler;
import static com.github.mjeanroy.junit.servers.utils.commons.Fields.getPrivateField;
import static com.github.mjeanroy.junit.servers.utils.commons.Fields.readPrivate;
import static com.github.mjeanroy.junit.servers.testing.ReflectionTestUtils.getPrivateField;
import static com.github.mjeanroy.junit.servers.testing.ReflectionTestUtils.readPrivate;
import static org.assertj.core.api.Assertions.assertThat;

class ConfigurationAnnotationHandlerTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.github.mjeanroy.junit.servers.client.HttpClientStrategy;
import com.github.mjeanroy.junit.servers.servers.EmbeddedServer;
import com.github.mjeanroy.junit.servers.utils.builders.EmbeddedServerMockBuilder;
import com.github.mjeanroy.junit.servers.utils.commons.Fields;
import org.junit.jupiter.api.Test;

import java.lang.annotation.Annotation;
Expand All @@ -42,7 +41,8 @@
import java.lang.reflect.Field;

import static com.github.mjeanroy.junit.servers.engine.HttpClientAnnotationHandler.newHttpClientAnnotationHandler;
import static com.github.mjeanroy.junit.servers.utils.commons.Fields.readPrivate;
import static com.github.mjeanroy.junit.servers.testing.ReflectionTestUtils.getPrivateField;
import static com.github.mjeanroy.junit.servers.testing.ReflectionTestUtils.readPrivate;
import static org.assertj.core.api.Assertions.assertThat;

class HttpClientAnnotationHandlerTest {
Expand Down Expand Up @@ -128,7 +128,7 @@ private static void verifyAsyncHttpClient(HttpClient client) {
}

private static Field extractClientField(Class<?> targetClass) {
return Fields.getPrivateField(targetClass, "client");
return getPrivateField(targetClass, "client");
}

private static Annotation readAnnotation(Field field) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
import java.lang.reflect.Field;

import static com.github.mjeanroy.junit.servers.engine.ServerAnnotationHandler.newServerAnnotationHandler;
import static com.github.mjeanroy.junit.servers.utils.commons.Fields.getPrivateField;
import static com.github.mjeanroy.junit.servers.utils.commons.Fields.readPrivate;
import static com.github.mjeanroy.junit.servers.testing.ReflectionTestUtils.getPrivateField;
import static com.github.mjeanroy.junit.servers.testing.ReflectionTestUtils.readPrivate;
import static org.assertj.core.api.Assertions.assertThat;

class ServerAnnotationHandlerTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import org.apache.logging.log4j.Level;

import static com.github.mjeanroy.junit.servers.utils.commons.Fields.readPrivate;
import static com.github.mjeanroy.junit.servers.testing.ReflectionTestUtils.readPrivate;

class Log4jLoggerTest extends AbstractLoggerTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import ch.qos.logback.classic.Level;

import static com.github.mjeanroy.junit.servers.utils.commons.Fields.readPrivate;
import static com.github.mjeanroy.junit.servers.testing.ReflectionTestUtils.readPrivate;

class Slf4jLoggerTest extends AbstractLoggerTest {

Expand Down
5 changes: 0 additions & 5 deletions junit-servers-jetty-10/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,6 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
Expand Down
5 changes: 0 additions & 5 deletions junit-servers-jetty-11/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,6 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
Expand Down
5 changes: 0 additions & 5 deletions junit-servers-jetty-9/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,6 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
Expand Down
5 changes: 0 additions & 5 deletions junit-servers-jetty/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,6 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
Expand Down
5 changes: 4 additions & 1 deletion junit-servers-testing/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>${okhttp.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@
* THE SOFTWARE.
*/

package com.github.mjeanroy.junit.servers.utils.commons;
package com.github.mjeanroy.junit.servers.testing;

import org.apache.commons.lang3.reflect.FieldUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
* Static field utilities to use in tests.
* Static field utilities, used for testing only.
*/
public final class Fields {
public final class ReflectionTestUtils {

// Ensure non instantiation.
private Fields() {
private ReflectionTestUtils() {
}

/**
Expand Down Expand Up @@ -114,27 +114,4 @@ public static <T> T readPrivate(Object instance, String name) {
throw new AssertionError(ex);
}
}

/**
* Read private static field on given class.
*
* @param klass The class.
* @param name The field name.
* @param <T> Type of returned value.
* @return The field value.
*/
public static <T> T readPrivateStatic(Class<?> klass, String name) {
Field field = FieldUtils.getDeclaredField(klass, name, true);
FieldUtils.removeFinalModifier(field);

try {
@SuppressWarnings("unchecked")
T value = (T) FieldUtils.readStaticField(field, true);

return value;
}
catch (IllegalAccessException ex) {
throw new AssertionError(ex);
}
}
}
5 changes: 0 additions & 5 deletions junit-servers-tomcat-10/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,6 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import static com.github.mjeanroy.junit.servers.testing.HttpTestUtils.get;
import static com.github.mjeanroy.junit.servers.testing.HttpTestUtils.localhost;
import static com.github.mjeanroy.junit.servers.testing.IoTestUtils.getFileFromClasspath;
import static com.github.mjeanroy.junit.servers.tomcat10.tests.commons.Fields.readPrivate;
import static com.github.mjeanroy.junit.servers.testing.ReflectionTestUtils.readPrivate;
import static org.assertj.core.api.Assertions.assertThat;

class EmbeddedTomcatTest {
Expand Down

This file was deleted.

5 changes: 0 additions & 5 deletions junit-servers-tomcat-8/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,6 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import static com.github.mjeanroy.junit.servers.testing.HttpTestUtils.get;
import static com.github.mjeanroy.junit.servers.testing.HttpTestUtils.localhost;
import static com.github.mjeanroy.junit.servers.testing.IoTestUtils.getFileFromClasspath;
import static com.github.mjeanroy.junit.servers.tomcat8.tests.commons.Fields.readPrivate;
import static com.github.mjeanroy.junit.servers.testing.ReflectionTestUtils.readPrivate;
import static org.assertj.core.api.Assertions.assertThat;

class EmbeddedTomcatTest {
Expand Down
Loading

0 comments on commit 6ff304b

Please sign in to comment.