Remove tests calling hidden constructor of util classes

- Newer versions of JaCoCo now ignore hidden constructors out of the box so we don't need the dummy method that calls the constructor for coverage anymore
This commit is contained in:
ljacqu 2019-11-03 11:27:18 +01:00
parent e31cb5bb9e
commit 39fbb4ac05
17 changed files with 3 additions and 141 deletions

View File

@ -6,9 +6,6 @@ import org.bukkit.entity.Player;
import org.mockito.Mockito;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URI;
@ -95,33 +92,6 @@ public final class TestHelper {
return logger;
}
/**
* Check that a class only has a hidden, zero-argument constructor, preventing the
* instantiation of such classes (utility classes). Invokes the hidden constructor
* as to register the code coverage.
*
* @param clazz The class to validate
*/
public static void validateHasOnlyPrivateEmptyConstructor(Class<?> clazz) {
Constructor<?>[] constructors = clazz.getDeclaredConstructors();
if (constructors.length > 1) {
throw new IllegalStateException("Class " + clazz.getSimpleName() + " has more than one constructor");
} else if (constructors[0].getParameterTypes().length != 0) {
throw new IllegalStateException("Constructor of " + clazz + " does not have empty parameter list");
} else if (!Modifier.isPrivate(constructors[0].getModifiers())) {
throw new IllegalStateException("Constructor of " + clazz + " is not private");
}
// Ugly hack to get coverage on the private constructors
// http://stackoverflow.com/questions/14077842/how-to-test-a-private-constructor-in-java-application
try {
constructors[0].setAccessible(true);
constructors[0].newInstance();
} catch (InvocationTargetException | InstantiationException | IllegalAccessException e) {
throw new UnsupportedOperationException(e);
}
}
/**
* Configures the player mock to return the given IP address.
*

View File

@ -1,6 +1,5 @@
package fr.xephi.authme.command;
import fr.xephi.authme.TestHelper;
import org.bukkit.ChatColor;
import org.junit.BeforeClass;
import org.junit.Test;
@ -83,12 +82,6 @@ public class CommandUtilsTest {
checkArgumentCount(command, 1, 3);
}
@Test
public void shouldHaveHiddenConstructor() {
// given / when / then
TestHelper.validateHasOnlyPrivateEmptyConstructor(CommandUtils.class);
}
@Test
public void shouldFormatSimpleArgument() {
// given

View File

@ -58,11 +58,6 @@ public class DebugSectionUtilsTest {
assertThat(DebugSectionUtils.formatLocation(null), equalTo("null"));
}
@Test
public void shouldHaveHiddenConstructor() {
TestHelper.validateHasOnlyPrivateEmptyConstructor(DebugSectionUtils.class);
}
@Test
public void shouldFetchMapInLimboService() {
// given

View File

@ -30,11 +30,6 @@ public class SqlDataSourceUtilsTest {
logger = TestHelper.setupLogger();
}
@Test
public void shouldHaveHiddenConstructor() {
TestHelper.validateHasOnlyPrivateEmptyConstructor(SqlDataSourceUtils.class);
}
@Test
public void shouldLogException() {
// given

View File

@ -38,10 +38,4 @@ public class PlayerAuthBuilderHelperTest {
assertThat(Math.abs(auth.getRegistrationDate() - System.currentTimeMillis()), lessThan(1000L));
assertThat(auth.getPassword(), equalToHash("myHash0001"));
}
@Test
public void shouldHaveHiddenConstructor() {
TestHelper.validateHasOnlyPrivateEmptyConstructor(PlayerAuthBuilderHelper.class);
}
}

View File

@ -12,9 +12,7 @@ import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;
import java.util.Arrays;
@ -110,11 +108,6 @@ public class MigrationServiceTest {
verifyNoMoreInteractions(settings, dataSource, sha256);
}
@Test
public void shouldHaveHiddenEmptyConstructorOnly() {
TestHelper.validateHasOnlyPrivateEmptyConstructor(MigrationService.class);
}
private static PlayerAuth authWithNickAndHash(String nick, String hash) {
return PlayerAuth.builder()
.name(nick)
@ -123,12 +116,9 @@ public class MigrationServiceTest {
}
private static void setSha256MockToUppercase(Sha256 sha256) {
given(sha256.computeHash(anyString(), anyString())).willAnswer(new Answer<HashedPassword>() {
@Override
public HashedPassword answer(InvocationOnMock invocation) {
String plainPassword = invocation.getArgument(0);
return new HashedPassword(plainPassword.toUpperCase(), null);
}
given(sha256.computeHash(anyString(), anyString())).willAnswer(invocation -> {
String plainPassword = invocation.getArgument(0);
return new HashedPassword(plainPassword.toUpperCase(), null);
});
}
}

View File

@ -294,12 +294,6 @@ public class CommandManagerTest {
verify(bukkitService).dispatchConsoleCommand("msg Bobby sad to see you go!");
}
@Test
public void shouldHaveHiddenConstructorInSettingsHolderClass() {
// given / when / then
TestHelper.validateHasOnlyPrivateEmptyConstructor(CommandSettingsHolder.class);
}
private void initManager() {
manager = new CommandManager(testFolder, bukkitService, geoIpService, commandMigrationService);
}

View File

@ -1,7 +1,6 @@
package fr.xephi.authme.settings.properties;
import ch.jalu.configme.configurationdata.ConfigurationData;
import fr.xephi.authme.TestHelper;
import org.junit.Test;
import static org.hamcrest.Matchers.closeTo;
@ -24,9 +23,4 @@ public class AuthMeSettingsRetrieverTest {
assertThat((double) configurationData.getProperties().size(),
closeTo(182, 10));
}
@Test
public void shouldHaveHiddenConstructor() {
TestHelper.validateHasOnlyPrivateEmptyConstructor(AuthMeSettingsRetriever.class);
}
}

View File

@ -98,13 +98,6 @@ public class SettingsClassConsistencyTest {
configData.getProperties(), hasSize((int) totalProperties));
}
@Test
public void shouldHaveHiddenEmptyConstructorOnly() {
for (Class<?> clazz : classes) {
TestHelper.validateHasOnlyPrivateEmptyConstructor(clazz);
}
}
private static boolean isValidConstantField(Field field) {
int modifiers = field.getModifiers();
return Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers);

View File

@ -1,7 +1,6 @@
package fr.xephi.authme.util;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.TestHelper;
import org.junit.Test;
import java.net.MalformedURLException;
@ -55,12 +54,6 @@ public class ExceptionUtilsTest {
assertThat(resultUoe, sameInstance(uoe));
}
@Test
public void shouldHaveHiddenConstructor() {
// given / when / then
TestHelper.validateHasOnlyPrivateEmptyConstructor(ExceptionUtils.class);
}
@Test
public void shouldFormatException() {
// given

View File

@ -185,12 +185,6 @@ public class FileUtilsTest {
assertThat(dirAsFile.isFile(), equalTo(true));
}
@Test
public void shouldHaveHiddenConstructor() {
TestHelper.validateHasOnlyPrivateEmptyConstructor(FileUtils.class);
}
@Test
public void shouldCreateCurrentTimestampString() {
// given / when

View File

@ -1,6 +1,5 @@
package fr.xephi.authme.util;
import fr.xephi.authme.TestHelper;
import org.junit.Test;
import static org.hamcrest.Matchers.equalTo;
@ -50,10 +49,4 @@ public class InternetProtocolUtilsTest {
assertThat(InternetProtocolUtils.isLoopbackAddress("127.0.0.1"), equalTo(true));
assertThat(InternetProtocolUtils.isLoopbackAddress("::1"), equalTo(true));
}
@Test
public void shouldHavePrivateConstructor() {
// given / when / then
TestHelper.validateHasOnlyPrivateEmptyConstructor(InternetProtocolUtils.class);
}
}

View File

@ -5,11 +5,9 @@ import org.bukkit.entity.Player;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.UUID;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
/**
@ -36,12 +34,6 @@ public class PlayerUtilsTest {
assertThat(result, equalTo(ip));
}
@Test
public void shouldHaveHiddenConstructor() {
// given / when / then
TestHelper.validateHasOnlyPrivateEmptyConstructor(PlayerUtils.class);
}
@Test
public void shouldCheckIfIsNpc() {
// given

View File

@ -1,6 +1,5 @@
package fr.xephi.authme.util;
import fr.xephi.authme.TestHelper;
import org.junit.Test;
import java.util.regex.Pattern;
@ -84,11 +83,4 @@ public class RandomStringUtilsTest {
// then - throw exception
}
@Test
public void shouldHaveHiddenConstructor() {
// given / when / then
TestHelper.validateHasOnlyPrivateEmptyConstructor(RandomStringUtils.class);
}
}

View File

@ -1,6 +1,5 @@
package fr.xephi.authme.util;
import fr.xephi.authme.TestHelper;
import org.junit.Test;
import static java.util.Arrays.asList;
@ -77,11 +76,6 @@ public class StringUtilsTest {
assertThat(StringUtils.getDifference("test", "something"), greaterThan(0.88));
}
@Test
public void shouldHaveHiddenConstructor() {
TestHelper.validateHasOnlyPrivateEmptyConstructor(StringUtils.class);
}
@Test
public void shouldCheckIfHasNeedleInWord() {
// given/when/then

View File

@ -55,12 +55,6 @@ public class UtilsTest {
assertThat(result.toString(), equalTo(".*?"));
}
@Test
public void shouldHavePrivateConstructorOnly() {
// given / when / then
TestHelper.validateHasOnlyPrivateEmptyConstructor(Utils.class);
}
@Test
public void shouldLogAndSendMessage() {
// given

View File

@ -1,6 +1,5 @@
package fr.xephi.authme.util.lazytags;
import fr.xephi.authme.TestHelper;
import org.junit.Test;
import java.util.function.Function;
@ -40,11 +39,4 @@ public class TagBuilderTest {
assertThat(tag, instanceOf(DependentTag.class));
assertThat(tag.getValue(24d), equalTo("26.4"));
}
@Test
public void shouldHaveHiddenConstructor() {
// given / when / then
TestHelper.validateHasOnlyPrivateEmptyConstructor(TagBuilder.class);
}
}