Minor test adjustments

This commit is contained in:
ljacqu 2016-02-21 20:23:36 +01:00
parent 1afe41d787
commit 203e954eea
3 changed files with 27 additions and 41 deletions

View File

@ -2,9 +2,9 @@ package fr.xephi.authme.datasource;
import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.security.crypts.HashedPassword; import fr.xephi.authme.security.crypts.HashedPassword;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description; import org.hamcrest.Description;
import org.hamcrest.Matcher; import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import java.util.Objects; import java.util.Objects;
@ -21,14 +21,10 @@ public final class AuthMeMatchers {
} }
public static Matcher<? super HashedPassword> equalToHash(final String hash, final String salt) { public static Matcher<? super HashedPassword> equalToHash(final String hash, final String salt) {
return new BaseMatcher<HashedPassword>() { return new TypeSafeMatcher<HashedPassword>() {
@Override @Override
public boolean matches(Object item) { public boolean matchesSafely(HashedPassword item) {
if (item instanceof HashedPassword) { return Objects.equals(hash, item.getHash()) && Objects.equals(salt, item.getSalt());
HashedPassword input = (HashedPassword) item;
return Objects.equals(hash, input.getHash()) && Objects.equals(salt, input.getSalt());
}
return false;
} }
@Override @Override
@ -44,17 +40,13 @@ public final class AuthMeMatchers {
public static Matcher<? super PlayerAuth> hasAuthBasicData(final String name, final String realName, public static Matcher<? super PlayerAuth> hasAuthBasicData(final String name, final String realName,
final String email, final String ip) { final String email, final String ip) {
return new BaseMatcher<PlayerAuth>() { return new TypeSafeMatcher<PlayerAuth>() {
@Override @Override
public boolean matches(Object item) { public boolean matchesSafely(PlayerAuth item) {
if (item instanceof PlayerAuth) { return Objects.equals(name, item.getNickname())
PlayerAuth input = (PlayerAuth) item; && Objects.equals(realName, item.getRealName())
return Objects.equals(name, input.getNickname()) && Objects.equals(email, item.getEmail())
&& Objects.equals(realName, input.getRealName()) && Objects.equals(ip, item.getIp());
&& Objects.equals(email, input.getEmail())
&& Objects.equals(ip, input.getIp());
}
return false;
} }
@Override @Override
@ -67,17 +59,13 @@ public final class AuthMeMatchers {
public static Matcher<? super PlayerAuth> hasAuthLocation(final double x, final double y, final double z, public static Matcher<? super PlayerAuth> hasAuthLocation(final double x, final double y, final double z,
final String world) { final String world) {
return new BaseMatcher<PlayerAuth>() { return new TypeSafeMatcher<PlayerAuth>() {
@Override @Override
public boolean matches(Object item) { public boolean matchesSafely(PlayerAuth item) {
if (item instanceof PlayerAuth) { return Objects.equals(x, item.getQuitLocX())
PlayerAuth input = (PlayerAuth) item; && Objects.equals(y, item.getQuitLocY())
return Objects.equals(x, input.getQuitLocX()) && Objects.equals(z, item.getQuitLocZ())
&& Objects.equals(y, input.getQuitLocY()) && Objects.equals(world, item.getWorld());
&& Objects.equals(z, input.getQuitLocZ())
&& Objects.equals(world, input.getWorld());
}
return false;
} }
@Override @Override

View File

@ -36,7 +36,7 @@ import static org.mockito.Mockito.when;
*/ */
public class SQLiteIntegrationTest { public class SQLiteIntegrationTest {
/** Mock for a settings instance. */ /** Mock of a settings instance. */
private static NewSetting settings; private static NewSetting settings;
/** Collection of SQL statements to execute for initialization of a test. */ /** Collection of SQL statements to execute for initialization of a test. */
private static String[] sqlInitialize; private static String[] sqlInitialize;
@ -70,7 +70,7 @@ public class SQLiteIntegrationTest {
} }
@Before @Before
public void initializeConnectionAndTable() throws SQLException, ClassNotFoundException { public void initializeConnectionAndTable() throws SQLException {
silentClose(con); silentClose(con);
Connection connection = DriverManager.getConnection("jdbc:sqlite::memory:"); Connection connection = DriverManager.getConnection("jdbc:sqlite::memory:");
try (Statement st = connection.createStatement()) { try (Statement st = connection.createStatement()) {
@ -88,14 +88,14 @@ public class SQLiteIntegrationTest {
DataSource dataSource = new SQLite(settings, con, false); DataSource dataSource = new SQLite(settings, con, false);
// when // when
boolean bobby = dataSource.isAuthAvailable("bobby"); boolean isBobbyAvailable = dataSource.isAuthAvailable("bobby");
boolean chris = dataSource.isAuthAvailable("chris"); boolean isChrisAvailable = dataSource.isAuthAvailable("chris");
boolean user = dataSource.isAuthAvailable("USER"); boolean isUserAvailable = dataSource.isAuthAvailable("USER");
// then // then
assertThat(bobby, equalTo(true)); assertThat(isBobbyAvailable, equalTo(true));
assertThat(chris, equalTo(false)); assertThat(isChrisAvailable, equalTo(false));
assertThat(user, equalTo(true)); assertThat(isUserAvailable, equalTo(true));
} }
@Test @Test
@ -109,8 +109,7 @@ public class SQLiteIntegrationTest {
HashedPassword userPassword = dataSource.getPassword("user"); HashedPassword userPassword = dataSource.getPassword("user");
// then // then
assertThat(bobbyPassword, equalToHash( assertThat(bobbyPassword, equalToHash("$SHA$11aa0706173d7272$dbba966"));
"$SHA$11aa0706173d7272$dbba96681c2ae4e0bfdf226d70fbbc5e4ee3d8071faa613bc533fe8a64817d10"));
assertThat(invalidPassword, nullValue()); assertThat(invalidPassword, nullValue());
assertThat(userPassword, equalToHash("b28c32f624a4eb161d6adc9acb5bfc5b", "f750ba32")); assertThat(userPassword, equalToHash("b28c32f624a4eb161d6adc9acb5bfc5b", "f750ba32"));
} }
@ -131,8 +130,7 @@ public class SQLiteIntegrationTest {
assertThat(bobbyAuth, hasAuthBasicData("bobby", "Bobby", "your@email.com", "123.45.67.89")); assertThat(bobbyAuth, hasAuthBasicData("bobby", "Bobby", "your@email.com", "123.45.67.89"));
assertThat(bobbyAuth, hasAuthLocation(1.05, 2.1, 4.2, "world")); assertThat(bobbyAuth, hasAuthLocation(1.05, 2.1, 4.2, "world"));
assertThat(bobbyAuth.getLastLogin(), equalTo(1449136800L)); assertThat(bobbyAuth.getLastLogin(), equalTo(1449136800L));
assertThat(bobbyAuth.getPassword(), equalToHash( assertThat(bobbyAuth.getPassword(), equalToHash("$SHA$11aa0706173d7272$dbba966"));
"$SHA$11aa0706173d7272$dbba96681c2ae4e0bfdf226d70fbbc5e4ee3d8071faa613bc533fe8a64817d10"));
assertThat(userAuth, hasAuthBasicData("user", "user", "user@example.org", "34.56.78.90")); assertThat(userAuth, hasAuthBasicData("user", "user", "user@example.org", "34.56.78.90"));
assertThat(userAuth, hasAuthLocation(124.1, 76.3, -127.8, "nether")); assertThat(userAuth, hasAuthLocation(124.1, 76.3, -127.8, "nether"));

View File

@ -17,6 +17,6 @@ CREATE TABLE authme (
); );
INSERT INTO authme (id, username, password, ip, lastlogin, x, y, z, world, email, isLogged, realname, salt) INSERT INTO authme (id, username, password, ip, lastlogin, x, y, z, world, email, isLogged, realname, salt)
VALUES (1,'bobby','$SHA$11aa0706173d7272$dbba96681c2ae4e0bfdf226d70fbbc5e4ee3d8071faa613bc533fe8a64817d10','123.45.67.89',1449136800,1.05,2.1,4.2,'world','your@email.com',0,'Bobby',NULL); VALUES (1,'bobby','$SHA$11aa0706173d7272$dbba966','123.45.67.89',1449136800,1.05,2.1,4.2,'world','your@email.com',0,'Bobby',NULL);
INSERT INTO authme (id, username, password, ip, lastlogin, x, y, z, world, email, isLogged, realname, salt) INSERT INTO authme (id, username, password, ip, lastlogin, x, y, z, world, email, isLogged, realname, salt)
VALUES (NULL,'user','b28c32f624a4eb161d6adc9acb5bfc5b','34.56.78.90',1453242857,124.1,76.3,-127.8,'nether','user@example.org',0,'user','f750ba32'); VALUES (NULL,'user','b28c32f624a4eb161d6adc9acb5bfc5b','34.56.78.90',1453242857,124.1,76.3,-127.8,'nether','user@example.org',0,'user','f750ba32');