some fixes

This commit is contained in:
anhefti 2018-11-14 16:13:53 +01:00
parent 2ac1b9cb75
commit 5c72132ebf
5 changed files with 12 additions and 45 deletions

View file

@ -8,25 +8,19 @@
package ch.ethz.seb.sebserver.gbl; package ch.ethz.seb.sebserver.gbl;
import java.util.Calendar;
import java.util.TimeZone;
import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter; import org.joda.time.format.DateTimeFormatter;
/** Global Constants used in SEB Server web-service as well as in web-gui component */ /** Global Constants used in SEB Server web-service as well as in web-gui component */
public interface Constants { public final class Constants {
/** Calendar using the UTC time-zone */
Calendar UTC = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
/** Date-Time formatter without milliseconds using UTC time-zone. Pattern is yyyy-MM-dd HH:mm:ss */ /** Date-Time formatter without milliseconds using UTC time-zone. Pattern is yyyy-MM-dd HH:mm:ss */
DateTimeFormatter DATE_TIME_PATTERN_UTC_NO_MILLIS = DateTimeFormat public static DateTimeFormatter DATE_TIME_PATTERN_UTC_NO_MILLIS = DateTimeFormat
.forPattern("yyyy-MM-dd HH:mm:ss") .forPattern("yyyy-MM-dd HH:mm:ss")
.withZoneUTC(); .withZoneUTC();
/** Date-Time formatter with milliseconds using UTC time-zone. Pattern is yyyy-MM-dd HH:mm:ss.S */ /** Date-Time formatter with milliseconds using UTC time-zone. Pattern is yyyy-MM-dd HH:mm:ss.S */
DateTimeFormatter DATE_TIME_PATTERN_UTC_MILLIS = DateTimeFormat public static DateTimeFormatter DATE_TIME_PATTERN_UTC_MILLIS = DateTimeFormat
.forPattern("yyyy-MM-dd HH:mm:ss.S") .forPattern("yyyy-MM-dd HH:mm:ss.S")
.withZoneUTC(); .withZoneUTC();

View file

@ -134,7 +134,7 @@ public final class Result<T> {
* *
* @param value resulting value * @param value resulting value
* @return Result instance contains a resulting value and no error */ * @return Result instance contains a resulting value and no error */
public static final <T> Result<T> of(final T value) { public static <T> Result<T> of(final T value) {
assert value != null : "value has null reference"; assert value != null : "value has null reference";
return new Result<>(value); return new Result<>(value);
} }
@ -143,7 +143,7 @@ public final class Result<T> {
* *
* @param error the error that is wrapped within the created Result * @param error the error that is wrapped within the created Result
* @return Result of specified error */ * @return Result of specified error */
public static final <T> Result<T> ofError(final Throwable error) { public static <T> Result<T> ofError(final Throwable error) {
assert error != null : "error has null reference"; assert error != null : "error has null reference";
return new Result<>(error); return new Result<>(error);
} }

View file

@ -22,7 +22,7 @@ public final class Utils {
* @param collection the Collection to extract the single and only element * @param collection the Collection to extract the single and only element
* @return The single element * @return The single element
* @throws IllegalArgumentException if the collection is null, or empty or has more then one element */ * @throws IllegalArgumentException if the collection is null, or empty or has more then one element */
public static final <T> Result<T> getSingle(final Collection<T> collection) { public static <T> Result<T> getSingle(final Collection<T> collection) {
if (collection == null || collection.isEmpty() || collection.size() > 1) { if (collection == null || collection.isEmpty() || collection.size() > 1) {
return Result.ofError( return Result.ofError(
new IllegalArgumentException( new IllegalArgumentException(
@ -38,7 +38,7 @@ public final class Utils {
* @param values elements of the new immutable Collection * @param values elements of the new immutable Collection
* @return an immutable Collection of specified type with given elements */ * @return an immutable Collection of specified type with given elements */
@SafeVarargs @SafeVarargs
public static final <T> Collection<T> immutableCollectionOf(final T... values) { public static <T> Collection<T> immutableCollectionOf(final T... values) {
if (values == null || values.length <= 0) { if (values == null || values.length <= 0) {
return Collections.emptyList(); return Collections.emptyList();
} }
@ -50,7 +50,7 @@ public final class Utils {
* @param values elements of the new immutable Set * @param values elements of the new immutable Set
* @return an immutable Set of specified type with given elements */ * @return an immutable Set of specified type with given elements */
@SafeVarargs @SafeVarargs
public static final <T> Set<T> immutableSetOf(final T... items) { public static <T> Set<T> immutableSetOf(final T... items) {
if (items == null || items.length <= 0) { if (items == null || items.length <= 0) {
return Collections.emptySet(); return Collections.emptySet();
} }

View file

@ -13,6 +13,8 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.Calendar;
import java.util.TimeZone;
import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.JdbcType;
@ -42,7 +44,7 @@ public class JodaTimeTypeResolver extends BaseTypeHandler<DateTime> {
ps.setTimestamp( ps.setTimestamp(
i, i,
new Timestamp(parameter.getMillis()), new Timestamp(parameter.getMillis()),
Constants.UTC); Calendar.getInstance(TimeZone.getTimeZone("UTC")));
} }
@Override @Override

View file

@ -9,49 +9,20 @@
package ch.ethz.seb.sebserver.webservice.batis; package ch.ethz.seb.sebserver.webservice.batis;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.*;
import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Timestamp;
import org.apache.ibatis.type.JdbcType;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.joda.time.DateTimeZone; import org.joda.time.DateTimeZone;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
import ch.ethz.seb.sebserver.gbl.Constants; import ch.ethz.seb.sebserver.gbl.Constants;
import ch.ethz.seb.sebserver.webservice.batis.JodaTimeTypeResolver;
public class JodaTimeTypeResolverTest { public class JodaTimeTypeResolverTest {
@Test
public void testSetNonNullParameter() throws SQLException {
final DateTime pointInTime = new DateTime(0, DateTimeZone.UTC);
final PreparedStatement statement = Mockito.mock(PreparedStatement.class);
final JodaTimeTypeResolver jodaTimeTypeResolver = new JodaTimeTypeResolver();
final Timestamp timestamp = new Timestamp(pointInTime.getMillis());
jodaTimeTypeResolver.setNonNullParameter(statement, 0, pointInTime, JdbcType.TIMESTAMP);
Mockito.verify(statement, times(1)).setTimestamp(0, timestamp, Constants.UTC);
verify(statement, never()).setTimestamp(anyInt(), any(Timestamp.class));
jodaTimeTypeResolver.setNonNullParameter(statement, 0, pointInTime, JdbcType.DATE);
Mockito.verify(statement, times(2)).setTimestamp(0, timestamp, Constants.UTC);
verify(statement, never()).setTimestamp(anyInt(), any(Timestamp.class));
jodaTimeTypeResolver.setNonNullParameter(statement, 0, pointInTime, JdbcType.DATETIMEOFFSET);
Mockito.verify(statement, times(3)).setTimestamp(0, timestamp, Constants.UTC);
verify(statement, never()).setTimestamp(anyInt(), any(Timestamp.class));
}
@Test @Test
public void testGetNullableResultExceptions() throws SQLException { public void testGetNullableResultExceptions() throws SQLException {
final String columnName = "timestamp"; final String columnName = "timestamp";