Skip to content

Commit

Permalink
Merge pull request #230 from uhafner/return-serializable
Browse files Browse the repository at this point in the history
Add a method to SerializableTest to obtain a restored object
  • Loading branch information
uhafner authored Sep 16, 2020
2 parents 7d327d5 + c743ce1 commit 0d3caec
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,11 @@
<annotation>@edu.umd.cs.findbugs.annotations.CheckForNull</annotation>
<justification>Annotation should be save to change.</justification>
</item>
<item>
<code>java.annotation.added</code>
<annotation>@edu.hm.hafner.util.Generated</annotation>
<justification>Annotation should be save to change.</justification>
</item>
</revapi.ignore>
</analysisConfiguration>
</configuration>
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/edu/hm/hafner/util/FilteredLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import org.apache.commons.lang3.exception.ExceptionUtils;

Expand Down Expand Up @@ -163,4 +164,23 @@ public void merge(final FilteredLog other) {
infoMessages.addAll(other.infoMessages);
errorMessages.addAll(other.errorMessages);
}

@Override @Generated
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
FilteredLog that = (FilteredLog) o;
return maxLines == that.maxLines && lines == that.lines && title.equals(that.title)
&& infoMessages.equals(that.infoMessages)
&& errorMessages.equals(that.errorMessages);
}

@Override @Generated
public int hashCode() {
return Objects.hash(title, maxLines, lines, infoMessages, errorMessages);
}
}
28 changes: 25 additions & 3 deletions src/test/java/edu/hm/hafner/util/FilteredLogTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* @author Ullrich Hafner
*/
class FilteredLogTest {
class FilteredLogTest extends SerializableTest<FilteredLog> {
private static final String TITLE = "Title: ";

@Test
Expand Down Expand Up @@ -94,13 +94,35 @@ void shouldLogExceptions() {

@Test
void shouldLog20ErrorsByDefault() {
FilteredLog filteredLog = createLogWith20Elements();

assertThat(filteredLog.getErrorMessages()).hasSize(21).contains("error19").doesNotContain("error20");
assertThat(filteredLog.getInfoMessages()).hasSize(25).contains("info0").contains("info24");
}

private FilteredLog createLogWith20Elements() {
FilteredLog filteredLog = new FilteredLog(TITLE);

for (int i = 0; i < 25; i++) {
filteredLog.logError("error%d", i);
filteredLog.logInfo("info%d", i);
}
assertThat(filteredLog.getErrorMessages()).hasSize(21).contains("error19").doesNotContain("error20");
assertThat(filteredLog.getInfoMessages()).hasSize(25).contains("info0").contains("info24");
return filteredLog;
}

@Override
protected FilteredLog createSerializable() {
return createLogWith20Elements();
}

/** Actually tests {@link edu.hm.hafner.util.SerializableTest}. */
@Test
void shouldManuallyUseSerializationHelpers() {
FilteredLog serializable = createSerializable();

byte[] bytes = toByteArray(serializable);
FilteredLog restored = restore(bytes);

assertThat(restored).isEqualTo(serializable);
}
}
19 changes: 17 additions & 2 deletions src/test/java/edu/hm/hafner/util/SerializableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,28 @@ void shouldBeSerializable() {
* the byte stream of the serializable
*/
protected void assertThatSerializableCanBeRestoredFrom(final byte... serializedInstance) {
assertThat(restore(serializedInstance)).isEqualTo(createSerializable());
}

/**
* Deserializes the subject under test from an array of bytes.
*
* @param serializedInstance
* the byte stream of the serializable
* @return the deserialized instance
*/
@SuppressWarnings("unchecked")
protected T restore(final byte[] serializedInstance) {
Object object;

try (ObjectInputStream inputStream = new ObjectInputStream(new ByteArrayInputStream(serializedInstance))) {
Object resolved = inputStream.readObject();
assertThat(resolved).isEqualTo(createSerializable());
object = inputStream.readObject();
}
catch (IOException | ClassNotFoundException e) {
throw new AssertionError("Can't resolve instance from byte array", e);
}

return (T) object;
}

/**
Expand Down

0 comments on commit 0d3caec

Please sign in to comment.