From 5c72132ebfb932a3f753f86cea2a23f4e7da120f Mon Sep 17 00:00:00 2001 From: anhefti Date: Wed, 14 Nov 2018 16:13:53 +0100 Subject: [PATCH] some fixes --- .../ch/ethz/seb/sebserver/gbl/Constants.java | 12 ++----- .../ethz/seb/sebserver/gbl/util/Result.java | 4 +-- .../ch/ethz/seb/sebserver/gbl/util/Utils.java | 6 ++-- .../batis/JodaTimeTypeResolver.java | 4 ++- .../batis/JodaTimeTypeResolverTest.java | 31 +------------------ 5 files changed, 12 insertions(+), 45 deletions(-) diff --git a/src/main/java/ch/ethz/seb/sebserver/gbl/Constants.java b/src/main/java/ch/ethz/seb/sebserver/gbl/Constants.java index 3083b02a..a975e3f9 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gbl/Constants.java +++ b/src/main/java/ch/ethz/seb/sebserver/gbl/Constants.java @@ -8,25 +8,19 @@ 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.DateTimeFormatter; /** Global Constants used in SEB Server web-service as well as in web-gui component */ -public interface Constants { - - /** Calendar using the UTC time-zone */ - Calendar UTC = Calendar.getInstance(TimeZone.getTimeZone("UTC")); +public final class Constants { /** 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") .withZoneUTC(); /** 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") .withZoneUTC(); diff --git a/src/main/java/ch/ethz/seb/sebserver/gbl/util/Result.java b/src/main/java/ch/ethz/seb/sebserver/gbl/util/Result.java index 6d705c7a..ff78da47 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gbl/util/Result.java +++ b/src/main/java/ch/ethz/seb/sebserver/gbl/util/Result.java @@ -134,7 +134,7 @@ public final class Result { * * @param value resulting value * @return Result instance contains a resulting value and no error */ - public static final Result of(final T value) { + public static Result of(final T value) { assert value != null : "value has null reference"; return new Result<>(value); } @@ -143,7 +143,7 @@ public final class Result { * * @param error the error that is wrapped within the created Result * @return Result of specified error */ - public static final Result ofError(final Throwable error) { + public static Result ofError(final Throwable error) { assert error != null : "error has null reference"; return new Result<>(error); } diff --git a/src/main/java/ch/ethz/seb/sebserver/gbl/util/Utils.java b/src/main/java/ch/ethz/seb/sebserver/gbl/util/Utils.java index 5087ad82..5a07a1ef 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gbl/util/Utils.java +++ b/src/main/java/ch/ethz/seb/sebserver/gbl/util/Utils.java @@ -22,7 +22,7 @@ public final class Utils { * @param collection the Collection to extract the single and only element * @return The single element * @throws IllegalArgumentException if the collection is null, or empty or has more then one element */ - public static final Result getSingle(final Collection collection) { + public static Result getSingle(final Collection collection) { if (collection == null || collection.isEmpty() || collection.size() > 1) { return Result.ofError( new IllegalArgumentException( @@ -38,7 +38,7 @@ public final class Utils { * @param values elements of the new immutable Collection * @return an immutable Collection of specified type with given elements */ @SafeVarargs - public static final Collection immutableCollectionOf(final T... values) { + public static Collection immutableCollectionOf(final T... values) { if (values == null || values.length <= 0) { return Collections.emptyList(); } @@ -50,7 +50,7 @@ public final class Utils { * @param values elements of the new immutable Set * @return an immutable Set of specified type with given elements */ @SafeVarargs - public static final Set immutableSetOf(final T... items) { + public static Set immutableSetOf(final T... items) { if (items == null || items.length <= 0) { return Collections.emptySet(); } diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/batis/JodaTimeTypeResolver.java b/src/main/java/ch/ethz/seb/sebserver/webservice/batis/JodaTimeTypeResolver.java index af3e1a06..529652a2 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/batis/JodaTimeTypeResolver.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/batis/JodaTimeTypeResolver.java @@ -13,6 +13,8 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; +import java.util.Calendar; +import java.util.TimeZone; import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.JdbcType; @@ -42,7 +44,7 @@ public class JodaTimeTypeResolver extends BaseTypeHandler { ps.setTimestamp( i, new Timestamp(parameter.getMillis()), - Constants.UTC); + Calendar.getInstance(TimeZone.getTimeZone("UTC"))); } @Override diff --git a/src/test/java/ch/ethz/seb/sebserver/webservice/batis/JodaTimeTypeResolverTest.java b/src/test/java/ch/ethz/seb/sebserver/webservice/batis/JodaTimeTypeResolverTest.java index cbec583d..237b01bb 100644 --- a/src/test/java/ch/ethz/seb/sebserver/webservice/batis/JodaTimeTypeResolverTest.java +++ b/src/test/java/ch/ethz/seb/sebserver/webservice/batis/JodaTimeTypeResolverTest.java @@ -9,49 +9,20 @@ package ch.ethz.seb.sebserver.webservice.batis; import static org.junit.Assert.*; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyInt; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.when; -import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.sql.Timestamp; -import org.apache.ibatis.type.JdbcType; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.junit.Test; import org.mockito.Mockito; import ch.ethz.seb.sebserver.gbl.Constants; -import ch.ethz.seb.sebserver.webservice.batis.JodaTimeTypeResolver; 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 public void testGetNullableResultExceptions() throws SQLException { final String columnName = "timestamp";