Skip to content

Commit

Permalink
Make Duration, DataSize, and Size serializable (#2975)
Browse files Browse the repository at this point in the history
Closes #2971
  • Loading branch information
joschi committed Oct 12, 2019
1 parent 2787260 commit f63a56c
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 6 deletions.
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

import java.io.Serializable;
import java.util.Collections;
import java.util.Locale;
import java.util.SortedMap;
Expand All @@ -19,9 +20,10 @@
* @see DataSizeUnit
* @since 2.0
*/
public class DataSize implements Comparable<DataSize> {
private static final Pattern SIZE_PATTERN = Pattern.compile("(\\d+)\\s*(\\S*)");
public class DataSize implements Comparable<DataSize>, Serializable {
private static final long serialVersionUID = 8517642678733072800L;

private static final Pattern SIZE_PATTERN = Pattern.compile("(\\d+)\\s*(\\S*)");
private static final SortedMap<String, DataSizeUnit> SUFFIXES;

static {
Expand Down
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
Expand All @@ -13,9 +14,10 @@

import static java.util.Objects.requireNonNull;

public class Duration implements Comparable<Duration> {
private static final Pattern DURATION_PATTERN = Pattern.compile("(\\d+)\\s*(\\S+)");
public class Duration implements Comparable<Duration>, Serializable {
private static final long serialVersionUID = 1445611723318059801L;

private static final Pattern DURATION_PATTERN = Pattern.compile("(\\d+)\\s*(\\S+)");
private static final Map<String, TimeUnit> SUFFIXES;

static {
Expand Down
6 changes: 4 additions & 2 deletions dropwizard-util/src/main/java/io/dropwizard/util/Size.java
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

import java.io.Serializable;
import java.util.Collections;
import java.util.Locale;
import java.util.SortedMap;
Expand All @@ -16,9 +17,10 @@
* @deprecated Use {@link DataSize} for correct SI and IEC prefixes.
*/
@Deprecated
public class Size implements Comparable<Size> {
private static final Pattern SIZE_PATTERN = Pattern.compile("(\\d+)\\s*(\\S+)");
public class Size implements Comparable<Size>, Serializable {
private static final long serialVersionUID = 6790991929249604526L;

private static final Pattern SIZE_PATTERN = Pattern.compile("(\\d+)\\s*(\\S+)");
private static final SortedMap<String, SizeUnit> SUFFIXES;

static {
Expand Down
23 changes: 23 additions & 0 deletions dropwizard-util/src/test/java/io/dropwizard/util/DataSizeTest.java
Expand Up @@ -3,7 +3,11 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
Expand Down Expand Up @@ -693,4 +697,23 @@ void testFromSize() {
assertThat(DataSize.fromSize(Size.gigabytes(5L))).isEqualTo(DataSize.gibibytes(5L));
assertThat(DataSize.fromSize(Size.terabytes(5L))).isEqualTo(DataSize.tebibytes(5L));
}

@Test
void testSerialization() throws IOException, ClassNotFoundException {
final DataSize size = DataSize.kibibytes(42L);
final byte[] bytes;
try (final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream)) {
objectOutputStream.writeObject(size);
bytes = outputStream.toByteArray();
}

try (final ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
final ObjectInputStream objectInputStream = new ObjectInputStream(inputStream)) {
final Object o = objectInputStream.readObject();
assertThat(o)
.isInstanceOf(DataSize.class)
.isEqualTo(size);
}
}
}
23 changes: 23 additions & 0 deletions dropwizard-util/src/test/java/io/dropwizard/util/DurationTest.java
Expand Up @@ -3,7 +3,11 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.concurrent.TimeUnit;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -864,4 +868,23 @@ public void deserializesCorrectlyWithJackson() throws IOException {
assertThat(mapper.readValue("\"1 day\"", Duration.class)).isEqualTo(Duration.days(1L));
assertThat(mapper.readValue("\"2 days\"", Duration.class)).isEqualTo(Duration.days(2L));
}

@Test
void testSerialization() throws IOException, ClassNotFoundException {
final Duration duration = Duration.minutes(42L);
final byte[] bytes;
try (final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream)) {
objectOutputStream.writeObject(duration);
bytes = outputStream.toByteArray();
}

try (final ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
final ObjectInputStream objectInputStream = new ObjectInputStream(inputStream)) {
final Object o = objectInputStream.readObject();
assertThat(o)
.isInstanceOf(Duration.class)
.isEqualTo(duration);
}
}
}
23 changes: 23 additions & 0 deletions dropwizard-util/src/test/java/io/dropwizard/util/SizeTest.java
Expand Up @@ -3,7 +3,11 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
Expand Down Expand Up @@ -585,4 +589,23 @@ void testToDataSize() {
assertThat(Size.gigabytes(5L).toDataSize()).isEqualTo(DataSize.gibibytes(5L));
assertThat(Size.terabytes(5L).toDataSize()).isEqualTo(DataSize.tebibytes(5L));
}

@Test
void testSerialization() throws IOException, ClassNotFoundException {
final Size size = Size.megabytes(42L);
final byte[] bytes;
try (final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream)) {
objectOutputStream.writeObject(size);
bytes = outputStream.toByteArray();
}

try (final ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
final ObjectInputStream objectInputStream = new ObjectInputStream(inputStream)) {
final Object o = objectInputStream.readObject();
assertThat(o)
.isInstanceOf(Size.class)
.isEqualTo(size);
}
}
}

0 comments on commit f63a56c

Please sign in to comment.