Skip to content

Commit

Permalink
GH-3948 add test
Browse files Browse the repository at this point in the history
  • Loading branch information
hmottestad committed Jun 6, 2022
1 parent e9bba90 commit 60aec1b
Showing 1 changed file with 70 additions and 5 deletions.
Expand Up @@ -12,8 +12,15 @@
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.DateTimeException;
Expand Down Expand Up @@ -79,7 +86,6 @@ public abstract class LiteralTest {
* Creates a test literal instance.
*
* @param label the label of the literal
*
* @return a new instance of the concrete literal class under test
*/
protected abstract Literal literal(String label);
Expand All @@ -89,7 +95,6 @@ public abstract class LiteralTest {
*
* @param label the label of the literal
* @param language the language of the literal
*
* @return a new instance of the concrete literal class under test
*/
protected abstract Literal literal(String label, String language);
Expand All @@ -99,7 +104,6 @@ public abstract class LiteralTest {
*
* @param label the label of the literal
* @param datatype the datatype of the literal
*
* @return a new instance of the concrete literal class under test
*/
protected abstract Literal literal(String label, IRI datatype);
Expand All @@ -109,7 +113,6 @@ public abstract class LiteralTest {
*
* @param label the label of the literal
* @param datatype the CoreDatatype of the literal
*
* @return a new instance of the concrete literal class under test
*/
protected abstract Literal literal(String label, CoreDatatype datatype);
Expand All @@ -118,7 +121,6 @@ public abstract class LiteralTest {
* Creates a test datatype IRI instance.
*
* @param iri the IRI of the datatype
*
* @return a new instance of the concrete datatype class under test
*/
protected abstract IRI datatype(String iri);
Expand Down Expand Up @@ -1501,4 +1503,67 @@ public final void testCoreDatatypeEqualsAndHashCodeXSDString() {
assertThat(plain.hashCode()).isEqualTo(typed.hashCode());
}

@Test
public final void testSerializationWithCoreDatatypeXsd() {
Literal literal = literal("1", datatype(XSD_INT));

byte[] bytes = objectToBytes(literal);
Literal roundTrip = (Literal) bytesToObject(bytes);

assertEquals(CoreDatatype.XSD.INT, roundTrip.getCoreDatatype());
}

@Test
public final void testSerializationWithCoreDatatypeRdfLangString() {
Literal literal = literal("hei", "no");
assertEquals(CoreDatatype.RDF.LANGSTRING, literal.getCoreDatatype());

byte[] bytes = objectToBytes(literal);
Literal roundTrip = (Literal) bytesToObject(bytes);

assertEquals(CoreDatatype.RDF.LANGSTRING, roundTrip.getCoreDatatype());
}

@Test
public final void testSerializationWithCoreDatatypeGEO() {
Literal literal = literal("1", CoreDatatype.GEO.WKT_LITERAL);

byte[] bytes = objectToBytes(literal);
Literal roundTrip = (Literal) bytesToObject(bytes);

assertEquals(CoreDatatype.GEO.WKT_LITERAL, roundTrip.getCoreDatatype());
}

@Test
public final void testSerializationWithCoreDatatype4() {
Literal literal = literal("1", datatype("http://example.org/dt1"));
assertEquals(CoreDatatype.XSD.NONE, literal.getCoreDatatype());

byte[] bytes = objectToBytes(literal);
Literal roundTrip = (Literal) bytesToObject(bytes);

assertEquals(CoreDatatype.XSD.NONE, roundTrip.getCoreDatatype());
}

private byte[] objectToBytes(Serializable object) {
try (var byteArrayOutputStream = new ByteArrayOutputStream()) {
try (var objectOutputStream = new ObjectOutputStream(byteArrayOutputStream)) {
objectOutputStream.writeObject(object);
}
return byteArrayOutputStream.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private Object bytesToObject(byte[] str) {
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(str)) {
try (ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream)) {
return objectInputStream.readObject();
}
} catch (IOException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
}

}

0 comments on commit 60aec1b

Please sign in to comment.