Skip to content

Commit

Permalink
fixup! Create testtools project #173
Browse files Browse the repository at this point in the history
  • Loading branch information
hdsdi3g committed Jul 15, 2024
1 parent 2fb19bb commit d0cf6b3
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 14 deletions.
5 changes: 5 additions & 0 deletions testtools/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,10 @@
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
4 changes: 2 additions & 2 deletions testtools/src/main/java/tv/hd3g/commons/testtools/Fake.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
@Retention(RUNTIME)
public @interface Fake {

long min() default 0;
long min() default 0l;

long max() default Long.MAX_VALUE;
long max() default 0l;

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* This file is part of fflauncher.
* This file is part of testtools.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand Down Expand Up @@ -31,14 +31,20 @@

import net.datafaker.Faker;

public class MockToolsExtendsJunit implements BeforeEachCallback, AfterEachCallback {// TODO test MockToolsExtendsJunit
/**
* Used @ExtendWith(MockToolsExtendsJunit.class)
*/
public class MockToolsExtendsJunit implements BeforeEachCallback, AfterEachCallback {
static Faker faker = net.datafaker.Faker.instance();

Object getFake(final Field field, final Class<?> fromClass) {
final Class<?> type = field.getType();
final var name = field.getName();

final var fakeA = field.getAnnotation(Fake.class);
if (fakeA.min() > fakeA.max()) {
throw new ArithmeticException("[" + name + "] Annotation min can't be more than max");
}

if (type.isAssignableFrom(String.class)) {
return faker.numerify(name + "#####");
Expand All @@ -47,17 +53,25 @@ Object getFake(final Field field, final Class<?> fromClass) {
} else if (type.isEnum()) {
return faker.options().option(type.getEnumConstants());
} else if (type.isAssignableFrom(Integer.TYPE)) {
return faker.random().nextInt(
fakeA.min() < Integer.MIN_VALUE ? Integer.MIN_VALUE : (int) fakeA.min(),
fakeA.max() > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) fakeA.max());
if (fakeA.min() == fakeA.max()) {
return faker.random().nextInt();
}
return faker.random().nextInt((int) fakeA.min(), (int) fakeA.max());
} else if (type.isAssignableFrom(Long.TYPE)) {
if (fakeA.min() == fakeA.max()) {
return faker.random().nextLong();
}
return faker.random().nextLong(fakeA.min(), fakeA.max());
} else if (type.isAssignableFrom(Double.TYPE)) {
if (fakeA.min() == fakeA.max()) {
return faker.random().nextDouble();
}
return faker.random().nextDouble(fakeA.min(), fakeA.max());
} else if (type.isAssignableFrom(Float.TYPE)) {
return faker.random().getRandomInternal().nextFloat(
fakeA.min() < Float.MIN_VALUE ? Float.MIN_VALUE : (float) fakeA.min(),
fakeA.max() > Float.MAX_VALUE ? Float.MAX_VALUE : (float) fakeA.max());
if (fakeA.min() == fakeA.max()) {
return faker.random().nextFloat();
}
return faker.random().getRandomInternal().nextFloat(fakeA.min(), fakeA.max());
} else if (type.isAssignableFrom(Boolean.TYPE)) {
return faker.random().nextBoolean();
}
Expand All @@ -78,22 +92,22 @@ void apply(final Object instance) {

final Consumer<Field> setValue = f -> {
try {
f.set(instance, getFake(f, instance.getClass()));
} catch (IllegalArgumentException | IllegalAccessException e) {
f.set(instance, getFake(f, instance.getClass()));// NOSONAR S3011
} catch (final IllegalAccessException e) {
throw new IllegalStateException(e);
}
};

Stream.of(testClass.getDeclaredFields())
.filter(f -> f.getAnnotation(Fake.class) != null)
.peek(f -> f.setAccessible(true))
.peek(f -> f.setAccessible(true))// NOSONAR S3011
.forEach(setValue);
}

void check(final Object instance) {
Stream.of(instance.getClass().getDeclaredFields())
.filter(f -> f.getAnnotation(Mock.class) != null)
.peek(f -> f.setAccessible(true))
.peek(f -> f.setAccessible(true))// NOSONAR S3011
.map(f -> {
try {
return f.get(instance);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
/*
* This file is part of testtools.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* Copyright (C) hdsdi3g for hd3g.tv 2024
*
*/
package tv.hd3g.commons.testtools;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mockingDetails;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.File;
import java.util.List;
import java.util.function.Consumer;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestInstances;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.exceptions.verification.NoInteractionsWanted;

@ExtendWith(MockToolsExtendsJunit.class)
class MockToolsExtendsJunitTest {

@Mock
ExtensionContext context;
@Mock
TestInstances testInstances;
@Mock
List<Object> listObjects;

MockToolsExtendsJunit m;

@BeforeEach
void init() {
m = new MockToolsExtendsJunit();
}

@Captor
ArgumentCaptor<Consumer<Object>> consumerObjectCaptor;

@Nested
class BeforeAfterEach {

@BeforeEach
void init() {
when(context.getRequiredTestInstances()).thenReturn(testInstances);
when(testInstances.getAllInstances()).thenReturn(listObjects);
}

@AfterEach
void ends() {
verify(context, times(1)).getRequiredTestInstances();
verify(testInstances, times(1)).getAllInstances();
verify(listObjects, times(1)).forEach(any());
}

@Test
void testBefore() throws Exception {// NOSONAR S2699
m.beforeEach(context);
}

@Test
void testAfter() throws Exception {// NOSONAR S2699
m.afterEach(context);
}
}

@Test
void testApply() {
assertTrue(mockingDetails(context).isMock());
}

@Test
void testCheck() {
context.getDisplayName();
assertThrows(NoInteractionsWanted.class, () -> m.check(this));
verify(context, times(1)).getDisplayName();
}

@Fake
String fString;
@Fake
File fFile;
@Fake
E fEnum;

enum E {
A,
B,
C,
D;
}

@Fake
int fInt;
@Fake
long fLong;
@Fake
double fDouble;
@Fake
float fFloat;
@Fake
boolean fBoolean;

@Fake(min = -100, max = -5)
int fNegBoundInt;
@Fake(min = -100, max = -5)
long fNegBoundLong;
@Fake(min = -100, max = -5)
double fNegBoundDouble;
@Fake(min = -100, max = -5)
float fNegBoundFloat;

@Fake(min = 1, max = 50)
int fPosBoundInt;
@Fake(min = 1, max = 50)
long fPosBoundLong;
@Fake(min = 1, max = 50)
double fPosBoundDouble;
@Fake(min = 1, max = 50)
float fPosBoundFloat;

@Test
void testFake() {
assertThat(fString).isNotBlank();
assertThat(fFile).doesNotExist();
assertThat(fEnum).isNotNull();

assertThat(fInt).isNotZero();
assertThat(fLong).isNotZero();
assertThat(fDouble).isNotZero();
assertThat(fFloat).isNotZero();

assertThat(fNegBoundInt).isBetween(-100, -5);
assertThat(fNegBoundLong).isBetween(-100l, -5l);
assertThat(fNegBoundDouble).isBetween(-100d, -5d);
assertThat(fNegBoundFloat).isBetween(-100f, -5f);

assertThat(fPosBoundInt).isBetween(1, 50);
assertThat(fPosBoundLong).isBetween(1l, 50l);
assertThat(fPosBoundDouble).isBetween(1d, 50d);
assertThat(fPosBoundFloat).isBetween(1f, 50f);
}

static class TestFakeError {
@Fake(min = -5, max = -10)
int value;
}

@Test
void testFakeError() {
final var tfe = new TestFakeError();
assertThrows(ArithmeticException.class, () -> m.apply(tfe));
}

static class TestFakeNonManaged {
@Fake
System value;
}

@Test
void testFakeNonManaged() {
final var tfe = new TestFakeNonManaged();
assertThrows(IllegalArgumentException.class, () -> m.apply(tfe));
}

}

0 comments on commit d0cf6b3

Please sign in to comment.