добавлен тест сериализации
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
package mc.core.h2db;
|
||||
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.support.GeneratedKeyHolder;
|
||||
import org.springframework.jdbc.support.KeyHolder;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
public class H2PlayerSerializer {
|
||||
private static final String SQL_INSERT = "INSERT INTO players " +
|
||||
"(uuid, name, location_x, location_y, location_z, location_yaw, location_pitch, location_world) " +
|
||||
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
|
||||
|
||||
public static void serialize(final H2Player player, final JdbcTemplate jdbcTemplate) throws SQLException {
|
||||
KeyHolder keyHolder = new GeneratedKeyHolder();
|
||||
int affectedRows = jdbcTemplate.update(connection -> {
|
||||
PreparedStatement stmt = connection.prepareStatement(SQL_INSERT, Statement.RETURN_GENERATED_KEYS);
|
||||
|
||||
stmt.setString(1, player.getUuid().toString());
|
||||
stmt.setString(2, player.getName());
|
||||
stmt.setDouble(3, player.getLocation().getX());
|
||||
stmt.setDouble(4, player.getLocation().getY());
|
||||
stmt.setDouble(5, player.getLocation().getZ());
|
||||
stmt.setFloat(6, player.getLocation().getYaw());
|
||||
stmt.setFloat(7, player.getLocation().getPitch());
|
||||
stmt.setString(8, player.getLocation().getWorld().getName());
|
||||
|
||||
return stmt;
|
||||
}, keyHolder);
|
||||
|
||||
if (affectedRows == 0) {
|
||||
throw new SQLException("Serialize player failed, no rows affected.");
|
||||
}
|
||||
|
||||
player.setId(keyHolder.getKey().intValue());
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package mc.core.h2db;
|
||||
|
||||
import mc.core.EntityLocation;
|
||||
import mc.core.world.World;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -11,16 +13,37 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.sql.SQLException;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = {"classpath:springTest.xml"})
|
||||
public class TestH2Database {
|
||||
@Autowired
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
private World mockWorld;
|
||||
|
||||
private void createMockWorld() {
|
||||
mockWorld = mock(World.class);
|
||||
when(mockWorld.getName()).thenReturn("mock_world");
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init() throws IOException {
|
||||
jdbcTemplate.execute(IOUtils.resourceToString("/sqls/create_tables.sql", StandardCharsets.UTF_8));
|
||||
createMockWorld();
|
||||
}
|
||||
|
||||
private H2Player buildPlayer(final String name, final EntityLocation location) {
|
||||
H2Player player = new H2Player();
|
||||
player.setUuid(UUID.randomUUID());
|
||||
player.setName(name);
|
||||
player.setLocation(location.clone());
|
||||
return player;
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -34,5 +57,23 @@ public class TestH2Database {
|
||||
jdbcTemplate.execute("SELECT COUNT(*) FROM players");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerialize() throws SQLException {
|
||||
final H2Player player = buildPlayer("player1", new EntityLocation(1.5d, 6.8d, 0.01d, 0f, 36.9f, mockWorld));
|
||||
H2PlayerSerializer.serialize(player, jdbcTemplate);
|
||||
|
||||
assertEquals(1, player.getId());
|
||||
|
||||
final String sql = "SELECT * FROM players WHERE id = ?";
|
||||
jdbcTemplate.query(sql, new Object[]{player.getId()}, (resultSet) -> {
|
||||
assertEquals(player.getId(), resultSet.getInt("id"));
|
||||
assertEquals(player.getName(), resultSet.getString("name"));
|
||||
assertEquals(player.getLocation().getX(), resultSet.getDouble("location_x"), 0.01d);
|
||||
assertEquals(player.getLocation().getY(), resultSet.getDouble("location_y"), 0.01d);
|
||||
assertEquals(player.getLocation().getZ(), resultSet.getDouble("location_z"), 0.01d);
|
||||
assertEquals(player.getLocation().getYaw(), resultSet.getFloat("location_yaw"), 0.01f);
|
||||
assertEquals(player.getLocation().getPitch(), resultSet.getFloat("location_pitch"), 0.01f);
|
||||
assertEquals(player.getLocation().getWorld().getName(), resultSet.getString("location_world"));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user