diff --git a/src/main/java/fr/inria/corese/core/next/api/io/serialization/SerializerFactory.java b/src/main/java/fr/inria/corese/core/next/api/io/serialization/SerializerFactory.java index f31bcdf48..2deb675b4 100644 --- a/src/main/java/fr/inria/corese/core/next/api/io/serialization/SerializerFactory.java +++ b/src/main/java/fr/inria/corese/core/next/api/io/serialization/SerializerFactory.java @@ -18,7 +18,7 @@ public interface SerializerFactory { /** * Creates a new RDF serializer for the specified format and model. - * + * * @param format The {@link RDFFormat} to use for serialization. * @param model The {@link Model} to be serialized. * @param config The {@link IOOptions} configuration to use for @@ -27,4 +27,15 @@ public interface SerializerFactory { * model. */ RDFSerializer createSerializer(RDFFormat format, Model model, IOOptions config); -} + + /** + * Creates a new RDF serializer for the specified format and model + * using the default configuration for that format. + * + * @param format The {@link RDFFormat} to use for serialization. + * @param model The {@link Model} to be serialized. + * @return A new instance of an RDF serializer for the specified format and + * model with default configuration. + */ + RDFSerializer createSerializer(RDFFormat format, Model model); +} \ No newline at end of file diff --git a/src/main/java/fr/inria/corese/core/next/impl/io/serialization/SerializerFactory.java b/src/main/java/fr/inria/corese/core/next/impl/io/serialization/SerializerFactory.java index ee8086166..b06573298 100644 --- a/src/main/java/fr/inria/corese/core/next/impl/io/serialization/SerializerFactory.java +++ b/src/main/java/fr/inria/corese/core/next/impl/io/serialization/SerializerFactory.java @@ -5,30 +5,30 @@ import fr.inria.corese.core.next.api.base.io.RDFFormat; import fr.inria.corese.core.next.api.io.IOOptions; import fr.inria.corese.core.next.api.io.serialization.RDFSerializer; +import fr.inria.corese.core.next.impl.exception.SerializationException; import fr.inria.corese.core.next.impl.io.common.JSONLDOptions; import fr.inria.corese.core.next.impl.io.serialization.canonical.RDFC10Canonicalizer; -import fr.inria.corese.core.next.impl.io.serialization.canonical.RDFC10SerializerOptions; import fr.inria.corese.core.next.impl.io.serialization.canonical.RDFC10Serializer; -import fr.inria.corese.core.next.impl.io.serialization.nquads.NQuadsSerializerOptions; +import fr.inria.corese.core.next.impl.io.serialization.canonical.RDFC10SerializerOptions; +import fr.inria.corese.core.next.impl.io.serialization.jsonld.JSONLDSerializer; import fr.inria.corese.core.next.impl.io.serialization.nquads.NQuadsSerializer; -import fr.inria.corese.core.next.impl.io.serialization.ntriples.NTriplesSerializerOptions; +import fr.inria.corese.core.next.impl.io.serialization.nquads.NQuadsSerializerOptions; import fr.inria.corese.core.next.impl.io.serialization.ntriples.NTriplesSerializer; -import fr.inria.corese.core.next.impl.io.serialization.rdfxml.RDFXMLSerializerOption; +import fr.inria.corese.core.next.impl.io.serialization.ntriples.NTriplesSerializerOptions; import fr.inria.corese.core.next.impl.io.serialization.rdfxml.RDFXMLSerializer; -import fr.inria.corese.core.next.impl.io.serialization.trig.TriGSerializerOptions; +import fr.inria.corese.core.next.impl.io.serialization.rdfxml.RDFXMLSerializerOption; import fr.inria.corese.core.next.impl.io.serialization.trig.TriGSerializer; -import fr.inria.corese.core.next.impl.io.serialization.turtle.TurtleSerializerOptions; +import fr.inria.corese.core.next.impl.io.serialization.trig.TriGSerializerOptions; import fr.inria.corese.core.next.impl.io.serialization.turtle.TurtleSerializer; -import fr.inria.corese.core.next.impl.io.serialization.jsonld.JSONLDSerializer; +import fr.inria.corese.core.next.impl.io.serialization.turtle.TurtleSerializerOptions; import fr.inria.corese.core.next.impl.temp.CoreseAdaptedValueFactory; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Objects; import java.util.function.BiFunction; +import java.util.function.Function; /** * Default implementation of {@link fr.inria.corese.core.next.api.io.serialization.SerializerFactory}. @@ -46,9 +46,8 @@ */ public class SerializerFactory implements fr.inria.corese.core.next.api.io.serialization.SerializerFactory { - private static final Logger logger = LoggerFactory.getLogger(SerializerFactory.class); - private final Map> registry; + private final Map> defaultRegistry; private final ValueFactory coreseValueFactory; /** @@ -64,103 +63,107 @@ public SerializerFactory() { this.coreseValueFactory = new CoreseAdaptedValueFactory(); Map> tempRegistry = new HashMap<>(); + Map> tempDefaultRegistry = new HashMap<>(); tempRegistry.put(RDFFormat.TURTLE, (model, genericConfig) -> { if (genericConfig instanceof TurtleSerializerOptions specificConfig) { return new TurtleSerializer(model, specificConfig); - } else { - logger.warn("Provided config for TURTLE is not TurtleConfig (was {}). Using default TurtleConfig.", - genericConfig.getClass().getSimpleName()); - return new TurtleSerializer(model, TurtleSerializerOptions.defaultConfig()); } + throw new SerializationException( + "Invalid configuration type. Expected TurtleSerializerOptions, got: " + + genericConfig.getClass().getSimpleName(), + RDFFormat.TURTLE.getName() + ); }); + tempDefaultRegistry.put(RDFFormat.TURTLE, TurtleSerializer::new); tempRegistry.put(RDFFormat.NTRIPLES, (model, genericConfig) -> { if (genericConfig instanceof NTriplesSerializerOptions specificConfig) { return new NTriplesSerializer(model, specificConfig); - } else { - logger.warn( - "Provided config for NTRIPLES is not NTriplesConfig (was {}). Using default NTriplesConfig.", - genericConfig.getClass().getSimpleName()); - return new NTriplesSerializer(model, NTriplesSerializerOptions.defaultConfig()); } + throw new SerializationException( + "Invalid configuration type. Expected NTriplesSerializerOptions, got: " + + genericConfig.getClass().getSimpleName(), + RDFFormat.NTRIPLES.getName() + ); }); + tempDefaultRegistry.put(RDFFormat.NTRIPLES, NTriplesSerializer::new); tempRegistry.put(RDFFormat.NQUADS, (model, genericConfig) -> { if (genericConfig instanceof NQuadsSerializerOptions specificConfig) { return new NQuadsSerializer(model, specificConfig); - } else { - logger.warn("Provided config for NQUADS is not NQuadsConfig (was {}). Using default NQuadsConfig.", - genericConfig.getClass().getSimpleName()); - return new NQuadsSerializer(model, NQuadsSerializerOptions.defaultConfig()); } + throw new SerializationException( + "Invalid configuration type. Expected NQuadsSerializerOptions, got: " + + genericConfig.getClass().getSimpleName(), + RDFFormat.NQUADS.getName() + ); }); + tempDefaultRegistry.put(RDFFormat.NQUADS, NQuadsSerializer::new); tempRegistry.put(RDFFormat.TRIG, (model, genericConfig) -> { if (genericConfig instanceof TriGSerializerOptions specificConfig) { return new TriGSerializer(model, specificConfig); - } else { - logger.warn("Provided config for TRIG is not TriGConfig (was {}). Using default TriGConfig.", - genericConfig.getClass().getSimpleName()); - return new TriGSerializer(model, TriGSerializerOptions.defaultConfig()); } + throw new SerializationException( + "Invalid configuration type. Expected TriGSerializerOptions, got: " + + genericConfig.getClass().getSimpleName(), + RDFFormat.TRIG.getName() + ); }); + tempDefaultRegistry.put(RDFFormat.TRIG, TriGSerializer::new); tempRegistry.put(RDFFormat.RDFXML, (model, genericConfig) -> { if (genericConfig instanceof RDFXMLSerializerOption specificConfig) { return new RDFXMLSerializer(model, specificConfig); - } else { - logger.warn("Provided config for RDFXML is not RDFXmlConfig (was {}). Using default RDFXmlConfig.", - genericConfig.getClass().getSimpleName()); - return new RDFXMLSerializer(model, RDFXMLSerializerOption.defaultConfig()); } + throw new SerializationException( + "Invalid configuration type. Expected RDFXMLSerializerOption, got: " + + genericConfig.getClass().getSimpleName(), + RDFFormat.RDFXML.getName() + ); }); + tempDefaultRegistry.put(RDFFormat.RDFXML, RDFXMLSerializer::new); tempRegistry.put(RDFFormat.JSONLD, (model, genericConfig) -> { if (genericConfig instanceof JSONLDOptions specificConfig) { return new JSONLDSerializer(model, specificConfig); - } else { - logger.warn( - "Provided config for JSONLD is not TitaniumJSONLDProcessorOption (was {}). Using default TitaniumJSONLDProcessorOption.", - genericConfig.getClass().getSimpleName()); - return new JSONLDSerializer(model, new JSONLDOptions.Builder().build()); } + throw new SerializationException( + "Invalid configuration type. Expected JSONLDOptions, got: " + + genericConfig.getClass().getSimpleName(), + RDFFormat.JSONLD.getName() + ); }); + tempDefaultRegistry.put(RDFFormat.JSONLD, JSONLDSerializer::new); tempRegistry.put(RDFFormat.RDFC_1_0, (model, genericConfig) -> { if (genericConfig instanceof RDFC10SerializerOptions specificConfig) { - RDFC10Canonicalizer canonicalizer = new RDFC10Canonicalizer( + RDFC10Canonicalizer rdfc10Canonicalizer = new RDFC10Canonicalizer( specificConfig.getHashAlgorithm(), specificConfig.getPermutationLimit(), coreseValueFactory ); - return new RDFC10Serializer(model, specificConfig, canonicalizer); - } else { - logger.warn("Provided config for RDFC_1_0 is not CanonicalOption (was {}). Using default CanonicalOption.", - genericConfig != null ? genericConfig.getClass().getSimpleName() : "null"); - RDFC10SerializerOptions defaultConfig = RDFC10SerializerOptions.defaultConfig(); - RDFC10Canonicalizer canonicalizer = new RDFC10Canonicalizer( - defaultConfig.getHashAlgorithm(), - defaultConfig.getPermutationLimit(), - coreseValueFactory - ); - return new RDFC10Serializer(model, defaultConfig, canonicalizer); + return new RDFC10Serializer(model, specificConfig, rdfc10Canonicalizer); } + throw new SerializationException( + "Invalid configuration type. Expected RDFC10SerializerOptions, got: " + + (genericConfig != null ? genericConfig.getClass().getSimpleName() : "null"), + RDFFormat.RDFC_1_0.getName() + ); }); - - - tempRegistry.put(RDFFormat.JSONLD, (model, genericConfig) -> { - if (genericConfig instanceof JSONLDOptions specificConfig) { - return new JSONLDSerializer(model, specificConfig); - } else { - logger.warn( - "Provided config for JSONLD is not TitaniumJSONLDProcessorOption (was {}). Using default TitaniumJSONLDProcessorOption.", - genericConfig.getClass().getSimpleName()); - return new JSONLDSerializer(model, new JSONLDOptions.Builder().build()); - } + tempDefaultRegistry.put(RDFFormat.RDFC_1_0, model -> { + RDFC10SerializerOptions defaultConfig = RDFC10SerializerOptions.defaultConfig(); + RDFC10Canonicalizer rdfc10Canonicalizer = new RDFC10Canonicalizer( + defaultConfig.getHashAlgorithm(), + defaultConfig.getPermutationLimit(), + coreseValueFactory + ); + return new RDFC10Serializer(model, defaultConfig, rdfc10Canonicalizer); }); this.registry = Collections.unmodifiableMap(tempRegistry); + this.defaultRegistry = Collections.unmodifiableMap(tempDefaultRegistry); } /** @@ -176,8 +179,8 @@ public SerializerFactory() { * format. * @throws NullPointerException if any of the arguments (format, model, * config) are null. - * @throws IllegalArgumentException if the provided format is not supported by - * this factory. + * @throws SerializationException if the provided format is not supported by + * this factory or if the configuration type is invalid. */ @Override public RDFSerializer createSerializer(RDFFormat format, Model model, IOOptions config) { @@ -189,9 +192,43 @@ public RDFSerializer createSerializer(RDFFormat format, Model model, IOOptions c BiFunction constructor = registry.get(format); if (constructor == null) { - throw new IllegalArgumentException("Unsupported RDFFormat: " + format.getName()); + throw new SerializationException( + "Unsupported RDF format: " + format.getName(), + format.getName() + ); } return constructor.apply(model, config); } + + /** + * Creates an {@link RDFSerializer} instance for the specified format and model + * using the default configuration for that format. + * + * @param format the {@link RDFFormat} for which to create the serializer. Must + * not be null. + * @param model the {@link Model} to be serialized. Must not be null. + * @return a new instance of {@link RDFSerializer} configured for the specified + * format with default configuration. + * @throws NullPointerException if any of the arguments (format, model) are null. + * @throws SerializationException if the provided format is not supported by + * this factory. + */ + @Override + public RDFSerializer createSerializer(RDFFormat format, Model model) { + + Objects.requireNonNull(format, "RDFFormat cannot be null"); + Objects.requireNonNull(model, "Model cannot be null"); + + Function constructor = defaultRegistry.get(format); + + if (constructor == null) { + throw new SerializationException( + "Unsupported RDF format: " + format.getName(), + format.getName() + ); + } + + return constructor.apply(model); + } } \ No newline at end of file diff --git a/src/test/java/fr/inria/corese/core/next/api/model/ValueFactoryTest.java b/src/test/java/fr/inria/corese/core/next/api/model/ValueFactoryTest.java index 725599f87..717729403 100644 --- a/src/test/java/fr/inria/corese/core/next/api/model/ValueFactoryTest.java +++ b/src/test/java/fr/inria/corese/core/next/api/model/ValueFactoryTest.java @@ -5,7 +5,6 @@ import fr.inria.corese.core.next.api.ValueFactory; import fr.inria.corese.core.next.impl.common.BasicIRI; import fr.inria.corese.core.next.impl.common.literal.XSD; -import fr.inria.corese.core.next.impl.exception.IncorrectFormatException; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -21,7 +20,8 @@ import java.time.temporal.TemporalAccessor; import java.util.Date; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; public abstract class ValueFactoryTest { @@ -33,10 +33,8 @@ public abstract class ValueFactoryTest { @Test public void testCreateIRI() { String correctIRI = "http://example.org"; - String incorrectIRI = "test"; assertNotNull(this.valueFactory.createIRI(correctIRI)); -// assertThrows(IncorrectFormatException.class, () -> this.valueFactory.createIRI(incorrectIRI)); } @Test @@ -48,7 +46,7 @@ public void testCreateBNode() { assertNotNull(nodesCorese123); assertNotNull(nodesCoreseRandom); assertNotNull(nodesCoreseRandom.getID()); - assertEquals(nodesCorese123.getID(), "corese123"); + assertEquals("corese123", nodesCorese123.getID()); } @Test diff --git a/src/test/java/fr/inria/corese/core/next/impl/common/prefix/PrefixHandlerTest.java b/src/test/java/fr/inria/corese/core/next/impl/common/prefix/PrefixHandlerTest.java index 322905c10..b2f9ab0d1 100644 --- a/src/test/java/fr/inria/corese/core/next/impl/common/prefix/PrefixHandlerTest.java +++ b/src/test/java/fr/inria/corese/core/next/impl/common/prefix/PrefixHandlerTest.java @@ -62,9 +62,7 @@ void testGetNonExistentPrefix() { @Test @DisplayName("Should throw IllegalArgumentException if prefix is null") void testSetPrefixNullPrefix() { - assertThrows(IllegalArgumentException.class, () -> { - handler.setPrefix(null, "http://example.org/"); - }); + assertThrows(IllegalArgumentException.class, () -> handler.setPrefix(null, "http://example.org/")); } /** @@ -73,9 +71,7 @@ void testSetPrefixNullPrefix() { @Test @DisplayName("Should throw IllegalArgumentException if namespace is null") void testSetPrefixNullNamespace() { - assertThrows(IllegalArgumentException.class, () -> { - handler.setPrefix("ex", null); - }); + assertThrows(IllegalArgumentException.class, () -> handler.setPrefix("ex", null)); } @@ -220,9 +216,7 @@ void testExpandPrefixUnknownPrefix() { @Test @DisplayName("Should throw IllegalArgumentException if expansion input is null") void testExpandPrefixNullInput() { - assertThrows(IllegalArgumentException.class, () -> { - handler.expandPrefix(null); - }); + assertThrows(IllegalArgumentException.class, () -> handler.expandPrefix(null)); } /** @@ -243,9 +237,7 @@ void testCompressIRI() { @Test @DisplayName("Should throw IllegalArgumentException if compression input is null") void testCompressIRINullInput() { - assertThrows(IllegalArgumentException.class, () -> { - handler.compressIRI(null); - }); + assertThrows(IllegalArgumentException.class, () -> handler.compressIRI(null)); } /** @@ -312,9 +304,7 @@ void testCopyFrom() { @Test @DisplayName("Should throw IllegalArgumentException if copy source is null") void testCopyFromNull() { - assertThrows(IllegalArgumentException.class, () -> { - handler.copyFrom(null); - }); + assertThrows(IllegalArgumentException.class, () -> handler.copyFrom(null)); } /** diff --git a/src/test/java/fr/inria/corese/core/next/impl/io/option/JSONLDOptionsTest.java b/src/test/java/fr/inria/corese/core/next/impl/io/option/JSONLDOptionsTest.java index 5288511b6..df046a960 100644 --- a/src/test/java/fr/inria/corese/core/next/impl/io/option/JSONLDOptionsTest.java +++ b/src/test/java/fr/inria/corese/core/next/impl/io/option/JSONLDOptionsTest.java @@ -14,7 +14,7 @@ */ class JSONLDOptionsTest { - private fr.inria.corese.core.next.impl.io.common.JSONLDOptions optionAllTrue = new fr.inria.corese.core.next.impl.io.common.JSONLDOptions.Builder().base("http://example.org/AllTrue") + private final fr.inria.corese.core.next.impl.io.common.JSONLDOptions optionAllTrue = new fr.inria.corese.core.next.impl.io.common.JSONLDOptions.Builder().base("http://example.org/AllTrue") .extractAllScripts(true) .compactToRelative(true) .compactArrays(true) @@ -23,7 +23,7 @@ class JSONLDOptionsTest { .useNativeTypes(true) .build(); - private fr.inria.corese.core.next.impl.io.common.JSONLDOptions optionAllFalse = new fr.inria.corese.core.next.impl.io.common.JSONLDOptions.Builder().base("http://example.org/AllFalse") + private final fr.inria.corese.core.next.impl.io.common.JSONLDOptions optionAllFalse = new fr.inria.corese.core.next.impl.io.common.JSONLDOptions.Builder().base("http://example.org/AllFalse") .extractAllScripts(false) .compactArrays(false) .compactToRelative(false) diff --git a/src/test/java/fr/inria/corese/core/next/impl/io/parser/ParserFactoryTest.java b/src/test/java/fr/inria/corese/core/next/impl/io/parser/ParserFactoryTest.java index 78bd4cbe8..2f4cedb6b 100644 --- a/src/test/java/fr/inria/corese/core/next/impl/io/parser/ParserFactoryTest.java +++ b/src/test/java/fr/inria/corese/core/next/impl/io/parser/ParserFactoryTest.java @@ -16,8 +16,8 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; /** * Unit tests for the ParserFactory class. @@ -49,7 +49,7 @@ void setUp() { void testCreateRDFParserWithConfig_JSONLD() { RDFParser parser = parserFactory.createRDFParser(RDFFormat.JSONLD, mockModel, mockValueFactory, mockParserOptions); assertNotNull(parser); - assertTrue(parser instanceof JSONLDParser); + assertInstanceOf(JSONLDParser.class, parser); } @Test @@ -57,7 +57,7 @@ void testCreateRDFParserWithConfig_JSONLD() { void testCreateRDFParserWithConfig_TURTLE() { RDFParser parser = parserFactory.createRDFParser(RDFFormat.TURTLE, mockModel, mockValueFactory, mockParserOptions); assertNotNull(parser); - assertTrue(parser instanceof TurtleParser); + assertInstanceOf(TurtleParser.class, parser); } @Test @@ -65,7 +65,7 @@ void testCreateRDFParserWithConfig_TURTLE() { void testCreateRDFParserWithConfig_NTRIPLES() { RDFParser parser = parserFactory.createRDFParser(RDFFormat.NTRIPLES, mockModel, mockValueFactory, mockParserOptions); assertNotNull(parser); - assertTrue(parser instanceof NTriplesParser); + assertInstanceOf(NTriplesParser.class, parser); } @Test @@ -73,7 +73,7 @@ void testCreateRDFParserWithConfig_NTRIPLES() { void testCreateRDFParserWithConfig_NQUADS() { RDFParser parser = parserFactory.createRDFParser(RDFFormat.NQUADS, mockModel, mockValueFactory, mockParserOptions); assertNotNull(parser); - assertTrue(parser instanceof NQuadsParser); + assertInstanceOf(NQuadsParser.class, parser); } @@ -82,7 +82,7 @@ void testCreateRDFParserWithConfig_NQUADS() { void testCreateRDFParserWithoutConfig_JSONLD() { RDFParser parser = parserFactory.createRDFParser(RDFFormat.JSONLD, mockModel, mockValueFactory); assertNotNull(parser); - assertTrue(parser instanceof JSONLDParser); + assertInstanceOf(JSONLDParser.class, parser); } @Test @@ -90,7 +90,7 @@ void testCreateRDFParserWithoutConfig_JSONLD() { void testCreateRDFParserWithoutConfig_TURTLE() { RDFParser parser = parserFactory.createRDFParser(RDFFormat.TURTLE, mockModel, mockValueFactory); assertNotNull(parser); - assertTrue(parser instanceof TurtleParser); + assertInstanceOf(TurtleParser.class, parser); } @Test @@ -98,15 +98,8 @@ void testCreateRDFParserWithoutConfig_TURTLE() { void testCreateRDFParserWithoutConfig_NTRIPLES() { RDFParser parser = parserFactory.createRDFParser(RDFFormat.NTRIPLES, mockModel, mockValueFactory); assertNotNull(parser); - assertTrue(parser instanceof NTriplesParser); + assertInstanceOf(NTriplesParser.class, parser); } - @Test - @DisplayName("createRDFParser (without config) should return ANTLRNQuadsParser for N-QUADS format") - void testCreateRDFParserWithoutConfig_NQUADS() { - RDFParser parser = parserFactory.createRDFParser(RDFFormat.NQUADS, mockModel, mockValueFactory); - assertNotNull(parser); - assertTrue(parser instanceof NQuadsParser); - } } diff --git a/src/test/java/fr/inria/corese/core/next/impl/io/parser/jsonld/JSONLDCircularTest.java b/src/test/java/fr/inria/corese/core/next/impl/io/parser/jsonld/JSONLDCircularTest.java index c510f8514..f196531b5 100644 --- a/src/test/java/fr/inria/corese/core/next/impl/io/parser/jsonld/JSONLDCircularTest.java +++ b/src/test/java/fr/inria/corese/core/next/impl/io/parser/jsonld/JSONLDCircularTest.java @@ -1,48 +1,36 @@ package fr.inria.corese.core.next.impl.io.parser.jsonld; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.io.ByteArrayInputStream; -import java.io.StringWriter; -import java.nio.charset.StandardCharsets; - -import fr.inria.corese.core.next.impl.io.common.JSONLDOptions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import fr.inria.corese.core.next.api.BNode; -import fr.inria.corese.core.next.api.IRI; -import fr.inria.corese.core.next.api.Literal; -import fr.inria.corese.core.next.api.Model; -import fr.inria.corese.core.next.api.ValueFactory; +import fr.inria.corese.core.next.api.*; import fr.inria.corese.core.next.api.base.io.RDFFormat; import fr.inria.corese.core.next.api.io.parser.RDFParser; import fr.inria.corese.core.next.api.io.serialization.RDFSerializer; +import fr.inria.corese.core.next.impl.io.common.JSONLDOptions; import fr.inria.corese.core.next.impl.io.parser.ParserFactory; import fr.inria.corese.core.next.impl.io.serialization.SerializerFactory; import fr.inria.corese.core.next.impl.temp.CoreseAdaptedValueFactory; import fr.inria.corese.core.next.impl.temp.CoreseModel; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayInputStream; +import java.io.StringWriter; +import java.nio.charset.StandardCharsets; + +import static org.junit.jupiter.api.Assertions.*; /** * Circular tests for JSON-LD parser and serializer integration. * These tests verify that data can be correctly serialized to JSON-LD format * and then parsed back to an equivalent model (round-trip testing). - * * The circular testing approach ensures that the parser and serializer * are compatible and preserve data integrity across format transformations. - * * JSON-LD supports both namespaces and named graphs, and has unique features * like @context handling, so additional considerations are included. */ @DisplayName("JSON-LD Circular Integration Tests") class JSONLDCircularTest { - private static final Logger logger = LoggerFactory.getLogger(JSONLDCircularTest.class); private ValueFactory valueFactory; private fr.inria.corese.core.next.api.io.serialization.SerializerFactory serializerFactory; @@ -259,9 +247,8 @@ private Model createSpecialCharactersTestModel() { * * @param originalModel The model to serialize and parse back * @return The model resulting from parsing the serialized data - * @throws Exception If serialization or parsing fails */ - private Model performRoundTrip(Model originalModel) throws Exception { + private Model performRoundTrip(Model originalModel) { // Serialize to JSON-LD RDFSerializer serializer = serializerFactory.createSerializer( RDFFormat.JSONLD, originalModel, defaultConfig); @@ -272,8 +259,9 @@ private Model performRoundTrip(Model originalModel) throws Exception { // Verify serialization produced content (only check for non-empty models) assertNotNull(serializedContent, "Serialized content should not be null"); - if (originalModel.size() > 0) { - assertTrue(serializedContent.length() > 0, "Serialized content should not be empty for non-empty models"); + if (!originalModel.isEmpty()) { + assertFalse(serializedContent.isEmpty(), + "Serialized content should not be empty for non-empty models"); } // Parse back from JSON-LD @@ -290,7 +278,7 @@ private Model performRoundTrip(Model originalModel) throws Exception { @Test @DisplayName("Round-trip test with simple model containing basic IRIs and literals") - void testRoundTripWithSimpleModel() throws Exception { + void testRoundTripWithSimpleModel() { // Given: A simple model with basic triples Model originalModel = createSimpleTestModel(); @@ -306,7 +294,7 @@ void testRoundTripWithSimpleModel() throws Exception { @Test @DisplayName("Round-trip test with model containing named graphs") - void testRoundTripWithNamedGraphs() throws Exception { + void testRoundTripWithNamedGraphs() { // Given: A model with triples in different named graphs Model originalModel = createNamedGraphsTestModel(); @@ -322,7 +310,7 @@ void testRoundTripWithNamedGraphs() throws Exception { @Test @DisplayName("Round-trip test with complex model containing diverse RDF value types") - void testRoundTripWithComplexModel() throws Exception { + void testRoundTripWithComplexModel() { // Given: A complex model with various RDF constructs Model originalModel = createComplexTestModel(); @@ -338,7 +326,7 @@ void testRoundTripWithComplexModel() throws Exception { @Test @DisplayName("Round-trip test with empty model") - void testRoundTripWithEmptyModel() throws Exception { + void testRoundTripWithEmptyModel() { // Given: An empty model Model originalModel = new CoreseModel(); @@ -353,7 +341,7 @@ void testRoundTripWithEmptyModel() throws Exception { @Test @DisplayName("Round-trip test with model containing only typed literals") - void testRoundTripWithTypedLiterals() throws Exception { + void testRoundTripWithTypedLiterals() { // Given: A model with various typed literals Model originalModel = createTypedLiteralsTestModel(); @@ -369,7 +357,7 @@ void testRoundTripWithTypedLiterals() throws Exception { @Test @DisplayName("Round-trip test with model containing only language-tagged literals") - void testRoundTripWithLanguageTaggedLiterals() throws Exception { + void testRoundTripWithLanguageTaggedLiterals() { // Given: A model with language-tagged literals Model originalModel = createLanguageTaggedLiteralsTestModel(); @@ -385,7 +373,7 @@ void testRoundTripWithLanguageTaggedLiterals() throws Exception { @Test @DisplayName("Round-trip test with model containing only blank nodes") - void testRoundTripWithBlankNodes() throws Exception { + void testRoundTripWithBlankNodes() { // Given: A model with blank nodes as subjects and objects Model originalModel = createBlankNodesTestModel(); @@ -402,7 +390,7 @@ void testRoundTripWithBlankNodes() throws Exception { @Test @DisplayName("Round-trip test with model containing special characters and escape sequences") - void testRoundTripWithSpecialCharacters() throws Exception { + void testRoundTripWithSpecialCharacters() { // Given: A model with special characters and escape sequences Model originalModel = createSpecialCharactersTestModel(); diff --git a/src/test/java/fr/inria/corese/core/next/impl/io/parser/nquads/NQuadsCircularTest.java b/src/test/java/fr/inria/corese/core/next/impl/io/parser/nquads/NQuadsCircularTest.java index ad9327d46..2ea8b638d 100644 --- a/src/test/java/fr/inria/corese/core/next/impl/io/parser/nquads/NQuadsCircularTest.java +++ b/src/test/java/fr/inria/corese/core/next/impl/io/parser/nquads/NQuadsCircularTest.java @@ -1,22 +1,6 @@ package fr.inria.corese.core.next.impl.io.parser.nquads; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.io.ByteArrayInputStream; -import java.io.StringWriter; -import java.nio.charset.StandardCharsets; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import fr.inria.corese.core.next.api.BNode; -import fr.inria.corese.core.next.api.IRI; -import fr.inria.corese.core.next.api.Literal; -import fr.inria.corese.core.next.api.Model; -import fr.inria.corese.core.next.api.ValueFactory; +import fr.inria.corese.core.next.api.*; import fr.inria.corese.core.next.api.base.io.RDFFormat; import fr.inria.corese.core.next.api.io.parser.RDFParser; import fr.inria.corese.core.next.api.io.serialization.RDFSerializer; @@ -25,15 +9,22 @@ import fr.inria.corese.core.next.impl.io.serialization.nquads.NQuadsSerializerOptions; import fr.inria.corese.core.next.impl.temp.CoreseAdaptedValueFactory; import fr.inria.corese.core.next.impl.temp.CoreseModel; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayInputStream; +import java.io.StringWriter; +import java.nio.charset.StandardCharsets; + +import static org.junit.jupiter.api.Assertions.*; /** * Circular tests for N-Quads parser and serializer integration. * These tests verify that data can be correctly serialized to N-Quads format * and then parsed back to an equivalent model (round-trip testing). - * * The circular testing approach ensures that the parser and serializer * are compatible and preserve data integrity across format transformations. - * * N-Quads supports named graphs, so additional tests are included for * quad-based (subject, predicate, object, context) scenarios. */ @@ -255,9 +246,8 @@ private Model createSpecialCharactersTestModel() { * * @param originalModel The model to serialize and parse back * @return The model resulting from parsing the serialized data - * @throws Exception If serialization or parsing fails */ - private Model performRoundTrip(Model originalModel) throws Exception { + private Model performRoundTrip(Model originalModel) { // Serialize to N-Quads RDFSerializer serializer = serializerFactory.createSerializer( RDFFormat.NQUADS, originalModel, defaultConfig); @@ -268,8 +258,9 @@ private Model performRoundTrip(Model originalModel) throws Exception { // Verify serialization produced content (only check for non-empty models) assertNotNull(serializedContent, "Serialized content should not be null"); - if (originalModel.size() > 0) { - assertTrue(serializedContent.length() > 0, "Serialized content should not be empty for non-empty models"); + if (!originalModel.isEmpty()) { + assertFalse(serializedContent.isEmpty(), + "Serialized content should not be empty for non-empty models"); } // Parse back from N-Quads @@ -286,7 +277,7 @@ private Model performRoundTrip(Model originalModel) throws Exception { @Test @DisplayName("Round-trip test with simple model containing basic IRIs and literals") - void testRoundTripWithSimpleModel() throws Exception { + void testRoundTripWithSimpleModel() { // Given: A simple model with basic triples Model originalModel = createSimpleTestModel(); @@ -302,7 +293,7 @@ void testRoundTripWithSimpleModel() throws Exception { @Test @DisplayName("Round-trip test with model containing named graphs (quads)") - void testRoundTripWithNamedGraphs() throws Exception { + void testRoundTripWithNamedGraphs() { // Given: A model with quads in different named graphs Model originalModel = createNamedGraphsTestModel(); @@ -318,7 +309,7 @@ void testRoundTripWithNamedGraphs() throws Exception { @Test @DisplayName("Round-trip test with complex model containing diverse RDF value types") - void testRoundTripWithComplexModel() throws Exception { + void testRoundTripWithComplexModel() { // Given: A complex model with various RDF constructs Model originalModel = createComplexTestModel(); @@ -334,7 +325,7 @@ void testRoundTripWithComplexModel() throws Exception { @Test @DisplayName("Round-trip test with empty model") - void testRoundTripWithEmptyModel() throws Exception { + void testRoundTripWithEmptyModel() { // Given: An empty model Model originalModel = new CoreseModel(); @@ -349,7 +340,7 @@ void testRoundTripWithEmptyModel() throws Exception { @Test @DisplayName("Round-trip test with model containing only typed literals") - void testRoundTripWithTypedLiterals() throws Exception { + void testRoundTripWithTypedLiterals() { // Given: A model with various typed literals Model originalModel = createTypedLiteralsTestModel(); @@ -365,7 +356,7 @@ void testRoundTripWithTypedLiterals() throws Exception { @Test @DisplayName("Round-trip test with model containing only language-tagged literals") - void testRoundTripWithLanguageTaggedLiterals() throws Exception { + void testRoundTripWithLanguageTaggedLiterals() { // Given: A model with language-tagged literals Model originalModel = createLanguageTaggedLiteralsTestModel(); @@ -381,7 +372,7 @@ void testRoundTripWithLanguageTaggedLiterals() throws Exception { @Test @DisplayName("Round-trip test with model containing only blank nodes") - void testRoundTripWithBlankNodes() throws Exception { + void testRoundTripWithBlankNodes() { // Given: A model with blank nodes as subjects and objects Model originalModel = createBlankNodesTestModel(); @@ -398,7 +389,7 @@ void testRoundTripWithBlankNodes() throws Exception { @Test @DisplayName("Round-trip test with model containing special characters and escape sequences") - void testRoundTripWithSpecialCharacters() throws Exception { + void testRoundTripWithSpecialCharacters() { // Given: A model with special characters and escape sequences Model originalModel = createSpecialCharactersTestModel(); diff --git a/src/test/java/fr/inria/corese/core/next/impl/io/parser/nquads/NQuadsListenerTest.java b/src/test/java/fr/inria/corese/core/next/impl/io/parser/nquads/NQuadsListenerTest.java index 1890222b4..8869b2700 100644 --- a/src/test/java/fr/inria/corese/core/next/impl/io/parser/nquads/NQuadsListenerTest.java +++ b/src/test/java/fr/inria/corese/core/next/impl/io/parser/nquads/NQuadsListenerTest.java @@ -57,15 +57,12 @@ void setUp() { lenient().when(mockValueFactory.createIRI(anyString())).thenAnswer(invocation -> { String uri = invocation.getArgument(0); - if (uri.equals("http://example.org/test")) return mockIRI; - if (uri.equals("http://example.org/datatype")) return mockDatatypeIRI; - if (uri.equals("http://example.org/graph")) return mockGraphIRI; - if (uri.equals("http://example.org/escaped>uri")) return mock(IRI.class); - if (uri.equals("http://example.org/s ubject")) return mock(IRI.class); - if (uri.equals("http://example.org/path/€")) return mock(IRI.class); - if (uri.equals("http://example.org/path>with\\ mockIRI; + case "http://example.org/datatype" -> mockDatatypeIRI; + case "http://example.org/graph" -> mockGraphIRI; + default -> mock(IRI.class); + }; }); lenient().when(mockValueFactory.createBNode(anyString())).thenAnswer(invocation -> { diff --git a/src/test/java/fr/inria/corese/core/next/impl/io/parser/nquads/NQuadsParserTest.java b/src/test/java/fr/inria/corese/core/next/impl/io/parser/nquads/NQuadsParserTest.java index 656c9f2b6..f5bff4918 100644 --- a/src/test/java/fr/inria/corese/core/next/impl/io/parser/nquads/NQuadsParserTest.java +++ b/src/test/java/fr/inria/corese/core/next/impl/io/parser/nquads/NQuadsParserTest.java @@ -66,17 +66,15 @@ void setUp() { lenient().when(mockValueFactory.createIRI(anyString())).thenAnswer(invocation -> { String uri = invocation.getArgument(0); - if (uri.equals("http://example.org/subject")) return mockSubjectIRI; - if (uri.equals("http://example.org/predicate")) return mockPredicateIRI; - if (uri.equals("http://example.org/object")) return mockObjectIRI; - if (uri.equals("http://example.org/graph")) return mockGraphIRI; - if (uri.equals("http://www.w3.org/2001/XMLSchema#integer")) return mockDatatypeIRI; - if (uri.equals("http://example.org/escaped>uri")) return mockEscapedIRI; - if (uri.equals("http://example.org/s ubject")) return mock(IRI.class); - if (uri.equals("http://example.org/path/€")) return mock(IRI.class); - if (uri.equals("http://example.org/path>with\\ mockSubjectIRI; + case "http://example.org/predicate" -> mockPredicateIRI; + case "http://example.org/object" -> mockObjectIRI; + case "http://example.org/graph" -> mockGraphIRI; + case "http://www.w3.org/2001/XMLSchema#integer" -> mockDatatypeIRI; + case "http://example.org/escaped>uri" -> mockEscapedIRI; + default -> mock(IRI.class); + }; }); lenient().when(mockValueFactory.createBNode(anyString())).thenAnswer(invocation -> { diff --git a/src/test/java/fr/inria/corese/core/next/impl/io/parser/ntriples/NTriplesCircularTest.java b/src/test/java/fr/inria/corese/core/next/impl/io/parser/ntriples/NTriplesCircularTest.java index c98fbb1b3..bbfc741be 100644 --- a/src/test/java/fr/inria/corese/core/next/impl/io/parser/ntriples/NTriplesCircularTest.java +++ b/src/test/java/fr/inria/corese/core/next/impl/io/parser/ntriples/NTriplesCircularTest.java @@ -1,22 +1,6 @@ package fr.inria.corese.core.next.impl.io.parser.ntriples; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.io.ByteArrayInputStream; -import java.io.StringWriter; -import java.nio.charset.StandardCharsets; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import fr.inria.corese.core.next.api.BNode; -import fr.inria.corese.core.next.api.IRI; -import fr.inria.corese.core.next.api.Literal; -import fr.inria.corese.core.next.api.Model; -import fr.inria.corese.core.next.api.ValueFactory; +import fr.inria.corese.core.next.api.*; import fr.inria.corese.core.next.api.base.io.RDFFormat; import fr.inria.corese.core.next.api.io.parser.RDFParser; import fr.inria.corese.core.next.api.io.serialization.RDFSerializer; @@ -25,12 +9,20 @@ import fr.inria.corese.core.next.impl.io.serialization.ntriples.NTriplesSerializerOptions; import fr.inria.corese.core.next.impl.temp.CoreseAdaptedValueFactory; import fr.inria.corese.core.next.impl.temp.CoreseModel; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayInputStream; +import java.io.StringWriter; +import java.nio.charset.StandardCharsets; + +import static org.junit.jupiter.api.Assertions.*; /** * Circular tests for N-Triples parser and serializer integration. * These tests verify that data can be correctly serialized to N-Triples format * and then parsed back to an equivalent model (round-trip testing). - * * The circular testing approach ensures that the parser and serializer * are compatible and preserve data integrity across format transformations. */ @@ -223,9 +215,8 @@ private Model createSpecialCharactersTestModel() { * * @param originalModel The model to serialize and parse back * @return The model resulting from parsing the serialized data - * @throws Exception If serialization or parsing fails */ - private Model performRoundTrip(Model originalModel) throws Exception { + private Model performRoundTrip(Model originalModel) { // Serialize to N-Triples RDFSerializer serializer = serializerFactory.createSerializer( RDFFormat.NTRIPLES, originalModel, defaultConfig); @@ -254,7 +245,7 @@ private Model performRoundTrip(Model originalModel) throws Exception { @Test @DisplayName("Round-trip test with simple model containing basic IRIs and literals") - void testRoundTripWithSimpleModel() throws Exception { + void testRoundTripWithSimpleModel() { // Given: A simple model with basic triples Model originalModel = createSimpleTestModel(); @@ -270,7 +261,7 @@ void testRoundTripWithSimpleModel() throws Exception { @Test @DisplayName("Round-trip test with complex model containing diverse RDF value types") - void testRoundTripWithComplexModel() throws Exception { + void testRoundTripWithComplexModel() { // Given: A complex model with various RDF constructs Model originalModel = createComplexTestModel(); @@ -286,7 +277,7 @@ void testRoundTripWithComplexModel() throws Exception { @Test @DisplayName("Round-trip test with empty model") - void testRoundTripWithEmptyModel() throws Exception { + void testRoundTripWithEmptyModel() { // Given: An empty model Model originalModel = new CoreseModel(); @@ -301,7 +292,7 @@ void testRoundTripWithEmptyModel() throws Exception { @Test @DisplayName("Round-trip test with model containing only typed literals") - void testRoundTripWithTypedLiterals() throws Exception { + void testRoundTripWithTypedLiterals() { // Given: A model with various typed literals Model originalModel = createTypedLiteralsTestModel(); @@ -317,7 +308,7 @@ void testRoundTripWithTypedLiterals() throws Exception { @Test @DisplayName("Round-trip test with model containing only language-tagged literals") - void testRoundTripWithLanguageTaggedLiterals() throws Exception { + void testRoundTripWithLanguageTaggedLiterals() { // Given: A model with language-tagged literals Model originalModel = createLanguageTaggedLiteralsTestModel(); @@ -333,7 +324,7 @@ void testRoundTripWithLanguageTaggedLiterals() throws Exception { @Test @DisplayName("Round-trip test with model containing only blank nodes") - void testRoundTripWithBlankNodes() throws Exception { + void testRoundTripWithBlankNodes() { // Given: A model with blank nodes as subjects and objects Model originalModel = createBlankNodesTestModel(); @@ -350,7 +341,7 @@ void testRoundTripWithBlankNodes() throws Exception { @Test @DisplayName("Round-trip test with model containing special characters and escape sequences") - void testRoundTripWithSpecialCharacters() throws Exception { + void testRoundTripWithSpecialCharacters() { // Given: A model with special characters and escape sequences Model originalModel = createSpecialCharactersTestModel(); diff --git a/src/test/java/fr/inria/corese/core/next/impl/io/parser/ntriples/NTriplesListenerTest.java b/src/test/java/fr/inria/corese/core/next/impl/io/parser/ntriples/NTriplesListenerTest.java index 042bc8dea..3a75bd055 100644 --- a/src/test/java/fr/inria/corese/core/next/impl/io/parser/ntriples/NTriplesListenerTest.java +++ b/src/test/java/fr/inria/corese/core/next/impl/io/parser/ntriples/NTriplesListenerTest.java @@ -51,12 +51,11 @@ void setUp() { lenient().when(mockValueFactory.createIRI(anyString())).thenAnswer(invocation -> { String uri = invocation.getArgument(0); - if (uri.equals("http://example.org/test")) return mockIRI; - if (uri.equals("http://example.org/datatype")) return mockDatatypeIRI; - if (uri.equals("http://example.org/escaped>iri")) return mock(IRI.class); - if (uri.equals("http://example.org/s ubject")) return mock(IRI.class); - if (uri.equals("http://example.org/path/€")) return mock(IRI.class); - return mock(IRI.class); + return switch (uri) { + case "http://example.org/test" -> mockIRI; + case "http://example.org/datatype" -> mockDatatypeIRI; + default -> mock(IRI.class); + }; }); lenient().when(mockValueFactory.createBNode(anyString())).thenAnswer(invocation -> { diff --git a/src/test/java/fr/inria/corese/core/next/impl/io/parser/ntriples/NTriplesParserTest.java b/src/test/java/fr/inria/corese/core/next/impl/io/parser/ntriples/NTriplesParserTest.java index 72f66b14d..9bc2a2335 100644 --- a/src/test/java/fr/inria/corese/core/next/impl/io/parser/ntriples/NTriplesParserTest.java +++ b/src/test/java/fr/inria/corese/core/next/impl/io/parser/ntriples/NTriplesParserTest.java @@ -62,12 +62,14 @@ void setUp() { lenient().when(mockValueFactory.createIRI(anyString())).thenAnswer(invocation -> { String uri = invocation.getArgument(0); - if (uri.equals("http://example.org/subject")) return mockSubjectIRI; - if (uri.equals("http://example.org/predicate")) return mockPredicateIRI; - if (uri.equals("http://example.org/object")) return mockObjectIRI; - if (uri.equals("http://www.w3.org/2001/XMLSchema#integer")) return mockDatatypeIRI; - if (uri.equals("http://example.org/escaped>uri")) return mockEscapedIRI; - return mock(IRI.class); + return switch (uri) { + case "http://example.org/subject" -> mockSubjectIRI; + case "http://example.org/predicate" -> mockPredicateIRI; + case "http://example.org/object" -> mockObjectIRI; + case "http://www.w3.org/2001/XMLSchema#integer" -> mockDatatypeIRI; + case "http://example.org/escaped>uri" -> mockEscapedIRI; + default -> mock(IRI.class); + }; }); lenient().when(mockValueFactory.createBNode(anyString())).thenAnswer(invocation -> { diff --git a/src/test/java/fr/inria/corese/core/next/impl/io/parser/rdfxml/RDFXMLCircularTest.java b/src/test/java/fr/inria/corese/core/next/impl/io/parser/rdfxml/RDFXMLCircularTest.java index 2d55e83ce..997cc0a64 100644 --- a/src/test/java/fr/inria/corese/core/next/impl/io/parser/rdfxml/RDFXMLCircularTest.java +++ b/src/test/java/fr/inria/corese/core/next/impl/io/parser/rdfxml/RDFXMLCircularTest.java @@ -25,6 +25,12 @@ * and then parsed back to an equivalent model (round-trip testing). * The circular testing approach ensures that the parser and serializer * are compatible and preserve data integrity across format transformations. + * RDF/XML supports namespaces, so additional tests are included for prefix + * handling. + * NOTE: These tests are currently disabled because they cannot work yet. + * We need to wait for the RDF/XML parser implementation from PR #176: + * Once the parser is implemented, these tests can be enabled to verify + * the round-trip functionality between the parser and serializer. */ @DisplayName("RDF/XML Circular Integration Tests") class RDFXMLCircularTest { @@ -228,7 +234,8 @@ private Model performRoundTrip(Model originalModel) { // Verify serialization produced content (only check for non-empty models) assertNotNull(serializedContent, "Serialized content should not be null"); if (!originalModel.isEmpty()) { - assertFalse(serializedContent.isEmpty(), "Serialized content should not be empty for non-empty models"); + assertFalse(serializedContent.isEmpty(), + "Serialized content should not be empty for non-empty models"); } // Parse back from RDF/XML @@ -384,7 +391,7 @@ private String valueToString(Value v) { @Test @DisplayName("Round-trip test with simple model containing basic IRIs and literals") - void testRoundTripWithSimpleModel() { + void testRoundTripWithSimpleModel() { // Given: A simple model with basic triples Model originalModel = createSimpleTestModel(); @@ -396,7 +403,7 @@ void testRoundTripWithSimpleModel() { @Test @DisplayName("Round-trip test with complex model containing diverse RDF value types") - void testRoundTripWithComplexModel() { + void testRoundTripWithComplexModel() { // Given: A complex model with various RDF constructs Model originalModel = createComplexTestModel(); @@ -410,7 +417,7 @@ void testRoundTripWithComplexModel() { @Test @DisplayName("Round-trip test with empty model") - void testRoundTripWithEmptyModel() { + void testRoundTripWithEmptyModel() { // Given: An empty model Model originalModel = new CoreseModel(); @@ -424,7 +431,7 @@ void testRoundTripWithEmptyModel() { @Test @DisplayName("Round-trip test with model containing only typed literals") - void testRoundTripWithTypedLiterals() { + void testRoundTripWithTypedLiterals() { // Given: A model with various typed literals Model originalModel = createTypedLiteralsTestModel(); // When: Performing round-trip serialization and parsing @@ -435,7 +442,7 @@ void testRoundTripWithTypedLiterals() { @Test @DisplayName("Round-trip test with model containing only language-tagged literals") - void testRoundTripWithLanguageTaggedLiterals() { + void testRoundTripWithLanguageTaggedLiterals() { // Given: A model with language-tagged literals Model originalModel = createLanguageTaggedLiteralsTestModel(); @@ -447,7 +454,7 @@ void testRoundTripWithLanguageTaggedLiterals() { @Test @DisplayName("Round-trip test with model containing only blank nodes") - void testRoundTripWithBlankNodes() { + void testRoundTripWithBlankNodes() { // Given: A model with blank nodes as subjects and objects Model originalModel = createBlankNodesTestModel(); @@ -466,7 +473,7 @@ void testRoundTripWithBlankNodes() { @Test @DisplayName("Round-trip test with model containing special characters and escape sequences") - void testRoundTripWithSpecialCharacters() { + void testRoundTripWithSpecialCharacters() { // Given: A model with special characters and escape sequences Model originalModel = createSpecialCharactersTestModel(); diff --git a/src/test/java/fr/inria/corese/core/next/impl/io/parser/rdfxml/RDFXMLParserTest.java b/src/test/java/fr/inria/corese/core/next/impl/io/parser/rdfxml/RDFXMLParserTest.java index ffd915f96..9367d24f9 100644 --- a/src/test/java/fr/inria/corese/core/next/impl/io/parser/rdfxml/RDFXMLParserTest.java +++ b/src/test/java/fr/inria/corese/core/next/impl/io/parser/rdfxml/RDFXMLParserTest.java @@ -45,7 +45,7 @@ private Model parseRdfXml(String rdfXml) throws Exception { /** * Helper method to print the model. * - * @param model The {@link Model} whose statements should be printed. + * @param model The model to print. */ private void printModel(Model model) { model.forEach(stmt -> { @@ -79,9 +79,9 @@ private void printModel(Model model) { } /** - * Test node elements with IRIs. + * Tests node elements with IRIs. * - * @throws Exception if parsing fails. + * @throws Exception If parsing fails. */ @Test public void testNodeElementsWithIRIs() throws Exception { @@ -108,9 +108,9 @@ public void testNodeElementsWithIRIs() throws Exception { } /** - * Test parsing a basic RDF/XML + * Tests a basic RDF/XML file. * - * @throws Exception if parsing fails. + * @throws Exception If parsing fails. */ @Test public void testBasicRdfParsing() throws Exception { @@ -130,9 +130,9 @@ public void testBasicRdfParsing() throws Exception { } /** - * Test a RDF/XML file with Complete description of all graph paths + * Tests a RDF/XML file with complete description of all graph paths. * - * @throws Exception if parsing fails. + * @throws Exception If parsing fails. */ @Test public void testExample3CompleteDescriptionOfAllGraphPaths() throws Exception { @@ -158,7 +158,6 @@ public void testExample3CompleteDescriptionOfAllGraphPaths() throws Exception { - RDF 1.2 XML Syntax @@ -170,9 +169,9 @@ public void testExample3CompleteDescriptionOfAllGraphPaths() throws Exception { } /** - * Test RDF/XML File Using multiple property elements on a node element + * Tests RDF/XML file using multiple property elements on a node element. * - * @throws Exception if parsing fails. + * @throws Exception If parsing fails. */ @Test public void testExample4UsingMultiplePropertyElements() throws Exception { @@ -202,9 +201,9 @@ public void testExample4UsingMultiplePropertyElements() throws Exception { } /** - * Test RDF/XML with Empty property elements + * Tests RDF/XML with empty property elements. * - * @throws Exception if parsing fails. + * @throws Exception If parsing fails. */ @Test public void testExample5EmptyPropertyElements() throws Exception { @@ -225,17 +224,15 @@ public void testExample5EmptyPropertyElements() throws Exception { """.trim(); - Model model = parseRdfXml(rdfXml); printModel(model); assertEquals(4, model.size(), "Expected four RDF statements"); - } /** - * Test a RDF/XML file with Replacing property elements with string literal content into property attributes + * Tests a RDF/XML file with replacing property elements with string literal content into property attributes. * - * @throws Exception if parsing fails. + * @throws Exception If parsing fails. */ @Test public void testExample6ReplacingPropertyElementsWithStringLiteral() throws Exception { @@ -257,13 +254,12 @@ public void testExample6ReplacingPropertyElementsWithStringLiteral() throws Exce Model model = parseRdfXml(rdfXml); printModel(model); assertEquals(4, model.size(), "Expected four RDF statements"); - } /** - * Test a Complete RDF/XML + * Tests a complete RDF/XML. * - * @throws Exception if parsing fails. + * @throws Exception If parsing fails. */ @Test public void testExample7CompleteRDFXML() throws Exception { @@ -289,9 +285,9 @@ public void testExample7CompleteRDFXML() throws Exception { } /** - * Test a Complete example of xml:lang + * Tests a complete example of xml:lang. * - * @throws Exception if parsing fails. + * @throws Exception If parsing fails. */ @Test public void testExample8CompleteExampleXmlLang() throws Exception { @@ -321,9 +317,9 @@ public void testExample8CompleteExampleXmlLang() throws Exception { } /** - * Test parsing an RDF/XML document that uses + * Tests a complete example of rdf:datatype. * - * @throws Exception if parsing fails. + * @throws Exception If parsing fails. */ @Test public void testExample11CompleteExamplerdfDatatype() throws Exception { @@ -340,17 +336,16 @@ public void testExample11CompleteExamplerdfDatatype() throws Exception { """.trim(); Model model = parseRdfXml(rdfXml); printModel(model); - assertEquals(1, model.size(), "Expected four RDF statements"); + assertEquals(1, model.size(), "Expected one RDF statement"); } /** - * Test a Complete RDF/XML file with a description of graph using rdf:nodeID + * Tests a complete RDF/XML file with a description of graph using rdf:nodeID. * - * @throws Exception if parsing fails. + * @throws Exception If parsing fails. */ @Test public void testExample12CompleteRDFXMLUsingRdfNodeID() throws Exception { - String rdfXml = """ @@ -533,17 +520,16 @@ public void testExample18ComplexExampleUsingRdfListProperties() throws Exception Model model = parseRdfXml(rdfXml); printModel(model); - assertEquals(4, model.size(), "Expected three RDF statements"); + assertEquals(4, model.size(), "Expected four RDF statements"); } /** - * Test a Complete example using rdf:li + * Tests a complete example using rdf:li. * - * @throws Exception if parsing fails. + * @throws Exception If parsing fails. */ @Test public void testExample19CompleteExampleUsingRdfliProperties() throws Exception { - String rdfXml = """ @@ -559,14 +545,13 @@ public void testExample19CompleteExampleUsingRdfliProperties() throws Exception Model model = parseRdfXml(rdfXml); printModel(model); - assertEquals(4, model.size(), "Expected three RDF statements"); - + assertEquals(4, model.size(), "Expected four RDF statements"); } /** - * Test a Complete example of a RDF collection + * Tests a complete example of a RDF collection. * - * @throws Exception if parsing fails. + * @throws Exception If parsing fails. */ @Test public void testExample20CompleteExampleOfARdfCollectionOfNodes() throws Exception { @@ -588,13 +573,13 @@ public void testExample20CompleteExampleOfARdfCollectionOfNodes() throws Excepti Model model = parseRdfXml(rdfXml); printModel(model); - assertEquals(7, model.size(), "Expected three RDF statements"); + assertEquals(7, model.size(), "Expected seven RDF statements"); } /** - * Test a Complete example of rdf:ID reifying a property element + * Tests a complete example of rdf:ID reifying a property element. * - * @throws Exception if parsing fails. + * @throws Exception If parsing fails. */ @Test public void testExample21CompleteExampleOfRdfID() throws Exception { @@ -612,6 +597,5 @@ public void testExample21CompleteExampleOfRdfID() throws Exception { Model model = parseRdfXml(rdfXml); printModel(model); assertEquals(1, model.size(), "Expected one RDF statement"); - } } \ No newline at end of file diff --git a/src/test/java/fr/inria/corese/core/next/impl/io/parser/trig/TriGCircularTest.java b/src/test/java/fr/inria/corese/core/next/impl/io/parser/trig/TriGCircularTest.java index 9cdbbe15d..0fa9d4720 100644 --- a/src/test/java/fr/inria/corese/core/next/impl/io/parser/trig/TriGCircularTest.java +++ b/src/test/java/fr/inria/corese/core/next/impl/io/parser/trig/TriGCircularTest.java @@ -1,9 +1,5 @@ package fr.inria.corese.core.next.impl.io.parser.trig; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - import java.io.ByteArrayInputStream; import java.io.StringWriter; import java.nio.charset.StandardCharsets; @@ -26,14 +22,14 @@ import fr.inria.corese.core.next.impl.temp.CoreseAdaptedValueFactory; import fr.inria.corese.core.next.impl.temp.CoreseModel; +import static org.junit.jupiter.api.Assertions.*; + /** * Circular tests for TriG parser and serializer integration. * These tests verify that data can be correctly serialized to TriG format * and then parsed back to an equivalent model (round-trip testing). - * * The circular testing approach ensures that the parser and serializer * are compatible and preserve data integrity across format transformations. - * * TriG supports both namespaces and named graphs, so additional tests are * included for these specific features. */ @@ -254,9 +250,8 @@ private Model createSpecialCharactersTestModel() { * * @param originalModel The model to serialize and parse back * @return The model resulting from parsing the serialized data - * @throws Exception If serialization or parsing fails */ - private Model performRoundTrip(Model originalModel) throws Exception { + private Model performRoundTrip(Model originalModel) { // Serialize to TriG RDFSerializer serializer = serializerFactory.createSerializer( RDFFormat.TRIG, originalModel, defaultConfig); @@ -267,10 +262,10 @@ private Model performRoundTrip(Model originalModel) throws Exception { // Verify serialization produced content (only check for non-empty models) assertNotNull(serializedContent, "Serialized content should not be null"); - if (originalModel.size() > 0) { - assertTrue(serializedContent.length() > 0, "Serialized content should not be empty for non-empty models"); + if (!originalModel.isEmpty()) { + assertFalse(serializedContent.isEmpty(), + "Serialized content should not be empty for non-empty models"); } - // Parse back from TriG Model deserializedModel = new CoreseModel(); RDFParser parser = parserFactory.createRDFParser( @@ -285,7 +280,7 @@ private Model performRoundTrip(Model originalModel) throws Exception { @Test @DisplayName("Round-trip test with simple model containing basic IRIs and literals") - void testRoundTripWithSimpleModel() throws Exception { + void testRoundTripWithSimpleModel() { // Given: A simple model with basic triples Model originalModel = createSimpleTestModel(); @@ -301,7 +296,7 @@ void testRoundTripWithSimpleModel() throws Exception { @Test @DisplayName("Round-trip test with model containing named graphs") - void testRoundTripWithNamedGraphs() throws Exception { + void testRoundTripWithNamedGraphs() { // Given: A model with triples in different named graphs Model originalModel = createNamedGraphsTestModel(); @@ -317,7 +312,7 @@ void testRoundTripWithNamedGraphs() throws Exception { @Test @DisplayName("Round-trip test with complex model containing diverse RDF value types") - void testRoundTripWithComplexModel() throws Exception { + void testRoundTripWithComplexModel() { // Given: A complex model with various RDF constructs Model originalModel = createComplexTestModel(); @@ -333,7 +328,7 @@ void testRoundTripWithComplexModel() throws Exception { @Test @DisplayName("Round-trip test with empty model") - void testRoundTripWithEmptyModel() throws Exception { + void testRoundTripWithEmptyModel() { // Given: An empty model Model originalModel = new CoreseModel(); @@ -348,7 +343,7 @@ void testRoundTripWithEmptyModel() throws Exception { @Test @DisplayName("Round-trip test with model containing only typed literals") - void testRoundTripWithTypedLiterals() throws Exception { + void testRoundTripWithTypedLiterals() { // Given: A model with various typed literals Model originalModel = createTypedLiteralsTestModel(); @@ -364,7 +359,7 @@ void testRoundTripWithTypedLiterals() throws Exception { @Test @DisplayName("Round-trip test with model containing only language-tagged literals") - void testRoundTripWithLanguageTaggedLiterals() throws Exception { + void testRoundTripWithLanguageTaggedLiterals() { // Given: A model with language-tagged literals Model originalModel = createLanguageTaggedLiteralsTestModel(); @@ -380,7 +375,7 @@ void testRoundTripWithLanguageTaggedLiterals() throws Exception { @Test @DisplayName("Round-trip test with model containing only blank nodes") - void testRoundTripWithBlankNodes() throws Exception { + void testRoundTripWithBlankNodes() { // Given: A model with blank nodes as subjects and objects Model originalModel = createBlankNodesTestModel(); @@ -397,7 +392,7 @@ void testRoundTripWithBlankNodes() throws Exception { @Test @DisplayName("Round-trip test with model containing special characters and escape sequences") - void testRoundTripWithSpecialCharacters() throws Exception { + void testRoundTripWithSpecialCharacters() { // Given: A model with special characters and escape sequences Model originalModel = createSpecialCharactersTestModel(); diff --git a/src/test/java/fr/inria/corese/core/next/impl/io/parser/trig/TriGParserTest.java b/src/test/java/fr/inria/corese/core/next/impl/io/parser/trig/TriGParserTest.java index 974fa5b27..c9fa3c205 100644 --- a/src/test/java/fr/inria/corese/core/next/impl/io/parser/trig/TriGParserTest.java +++ b/src/test/java/fr/inria/corese/core/next/impl/io/parser/trig/TriGParserTest.java @@ -31,10 +31,9 @@ class TriGParserTest { * @param trigData a string of rdf data in trig format * @param baseURI the base uri * @return Corese rdf model - * @throws Exception */ - private Model parseFromString(String trigData, String baseURI) throws Exception { + private Model parseFromString(String trigData, String baseURI) { Model model = new CoreseModel(); ValueFactory factory = new CoreseAdaptedValueFactory(); RDFParser parser = new TriGParser(model, factory); @@ -47,7 +46,7 @@ private Model parseFromString(String trigData, String baseURI) throws Exception * @param model */ private void printModel(Model model) { - model.stream().forEach(stmt -> { + model.forEach(stmt -> { Value obj = stmt.getObject(); String subjectString = stmt.getSubject().stringValue(); String predicateString = stmt.getPredicate().stringValue(); @@ -78,7 +77,7 @@ private void printModel(Model model) { } @Test - void testNamedGraphParsing() throws Exception { + void testNamedGraphParsing() { String trig = """ @prefix ex: . ex:Graph1 { @@ -95,7 +94,7 @@ void testNamedGraphParsing() throws Exception { } @Test - void testDocumentThatContainsOneGraphExample1() throws Exception { + void testDocumentThatContainsOneGraphExample1() { String trig = """ # This document encodes one graph. @prefix ex: . @@ -120,7 +119,7 @@ void testDocumentThatContainsOneGraphExample1() throws Exception { } @Test - void testDocumentThatContainsTwoGraphExample() throws Exception { + void testDocumentThatContainsTwoGraphExample() { String trig = """ # This document contains a same data as the # previous example. @@ -160,7 +159,7 @@ void testDocumentThatContainsTwoGraphExample() throws Exception { } @Test - void testNestedBlankNodesWithSharedIdentifiers() throws Exception { + void testNestedBlankNodesWithSharedIdentifiers() { String trig = """ @prefix ex: . diff --git a/src/test/java/fr/inria/corese/core/next/impl/io/parser/turtle/TurtleCircularTest.java b/src/test/java/fr/inria/corese/core/next/impl/io/parser/turtle/TurtleCircularTest.java index 86aa34e7c..848c1336e 100644 --- a/src/test/java/fr/inria/corese/core/next/impl/io/parser/turtle/TurtleCircularTest.java +++ b/src/test/java/fr/inria/corese/core/next/impl/io/parser/turtle/TurtleCircularTest.java @@ -1,9 +1,5 @@ package fr.inria.corese.core.next.impl.io.parser.turtle; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - import java.io.ByteArrayInputStream; import java.io.StringWriter; import java.nio.charset.StandardCharsets; @@ -26,11 +22,12 @@ import fr.inria.corese.core.next.impl.temp.CoreseAdaptedValueFactory; import fr.inria.corese.core.next.impl.temp.CoreseModel; +import static org.junit.jupiter.api.Assertions.*; + /** * Circular tests for Turtle parser and serializer integration. * These tests verify that data can be correctly serialized to Turtle format * and then parsed back to an equivalent model (round-trip testing). - * * The circular testing approach ensures that the parser and serializer * are compatible and preserve data integrity across format transformations. */ @@ -228,9 +225,8 @@ private Model createSpecialCharactersTestModel() { * * @param originalModel The model to serialize and parse back * @return The model resulting from parsing the serialized data - * @throws Exception If serialization or parsing fails */ - private Model performRoundTrip(Model originalModel) throws Exception { + private Model performRoundTrip(Model originalModel) { // Serialize to Turtle RDFSerializer serializer = serializerFactory.createSerializer( RDFFormat.TURTLE, originalModel, defaultConfig); @@ -241,8 +237,9 @@ private Model performRoundTrip(Model originalModel) throws Exception { // Verify serialization produced content (only check for non-empty models) assertNotNull(serializedContent, "Serialized content should not be null"); - if (originalModel.size() > 0) { - assertTrue(serializedContent.length() > 0, "Serialized content should not be empty for non-empty models"); + if (!originalModel.isEmpty()) { + assertFalse(serializedContent.isEmpty(), + "Serialized content should not be empty for non-empty models"); } // Parse back from Turtle @@ -259,7 +256,7 @@ private Model performRoundTrip(Model originalModel) throws Exception { @Test @DisplayName("Round-trip test with simple model containing basic IRIs and literals") - void testRoundTripWithSimpleModel() throws Exception { + void testRoundTripWithSimpleModel() { // Given: A simple model with basic triples Model originalModel = createSimpleTestModel(); @@ -275,7 +272,7 @@ void testRoundTripWithSimpleModel() throws Exception { @Test @DisplayName("Round-trip test with complex model containing diverse RDF value types") - void testRoundTripWithComplexModel() throws Exception { + void testRoundTripWithComplexModel() { // Given: A complex model with various RDF constructs Model originalModel = createComplexTestModel(); @@ -291,7 +288,7 @@ void testRoundTripWithComplexModel() throws Exception { @Test @DisplayName("Round-trip test with empty model") - void testRoundTripWithEmptyModel() throws Exception { + void testRoundTripWithEmptyModel() { // Given: An empty model Model originalModel = new CoreseModel(); @@ -306,7 +303,7 @@ void testRoundTripWithEmptyModel() throws Exception { @Test @DisplayName("Round-trip test with model containing only typed literals") - void testRoundTripWithTypedLiterals() throws Exception { + void testRoundTripWithTypedLiterals() { // Given: A model with various typed literals Model originalModel = createTypedLiteralsTestModel(); @@ -322,7 +319,7 @@ void testRoundTripWithTypedLiterals() throws Exception { @Test @DisplayName("Round-trip test with model containing only language-tagged literals") - void testRoundTripWithLanguageTaggedLiterals() throws Exception { + void testRoundTripWithLanguageTaggedLiterals() { // Given: A model with language-tagged literals Model originalModel = createLanguageTaggedLiteralsTestModel(); @@ -338,7 +335,7 @@ void testRoundTripWithLanguageTaggedLiterals() throws Exception { @Test @DisplayName("Round-trip test with model containing only blank nodes") - void testRoundTripWithBlankNodes() throws Exception { + void testRoundTripWithBlankNodes() { // Given: A model with blank nodes as subjects and objects Model originalModel = createBlankNodesTestModel(); @@ -355,7 +352,7 @@ void testRoundTripWithBlankNodes() throws Exception { @Test @DisplayName("Round-trip test with model containing special characters and escape sequences") - void testRoundTripWithSpecialCharacters() throws Exception { + void testRoundTripWithSpecialCharacters() { // Given: A model with special characters and escape sequences Model originalModel = createSpecialCharactersTestModel(); diff --git a/src/test/java/fr/inria/corese/core/next/impl/io/parser/turtle/TurtleListenerTest.java b/src/test/java/fr/inria/corese/core/next/impl/io/parser/turtle/TurtleListenerTest.java index 59b2a98f1..689748c7d 100644 --- a/src/test/java/fr/inria/corese/core/next/impl/io/parser/turtle/TurtleListenerTest.java +++ b/src/test/java/fr/inria/corese/core/next/impl/io/parser/turtle/TurtleListenerTest.java @@ -1,16 +1,15 @@ package fr.inria.corese.core.next.impl.io.parser.turtle; +import fr.inria.corese.core.next.api.Model; import fr.inria.corese.core.next.api.ValueFactory; import fr.inria.corese.core.next.impl.parser.antlr.TurtleLexer; import fr.inria.corese.core.next.impl.parser.antlr.TurtleParser; -import fr.inria.corese.core.next.api.Model; import fr.inria.corese.core.next.impl.temp.CoreseAdaptedValueFactory; import fr.inria.corese.core.next.impl.temp.CoreseModel; import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.CharStreams; import org.antlr.v4.runtime.CommonTokenStream; import org.antlr.v4.runtime.tree.ParseTree; -import org.antlr.v4.runtime.tree.ParseTreeListener; import org.antlr.v4.runtime.tree.ParseTreeWalker; import org.junit.jupiter.api.Test; import org.slf4j.Logger; @@ -46,7 +45,7 @@ private Model parseAndPrintModel(String turtleData) throws Exception { Model model = new CoreseModel(); TurtleListener listener = new TurtleListener(model, factory, null); ParseTreeWalker walker = new ParseTreeWalker(); - walker.walk((ParseTreeListener) listener, tree); + walker.walk(listener, tree); return model; } diff --git a/src/test/java/fr/inria/corese/core/next/impl/io/parser/turtle/TurtleParserTest.java b/src/test/java/fr/inria/corese/core/next/impl/io/parser/turtle/TurtleParserTest.java index 008f7959d..ce402962b 100644 --- a/src/test/java/fr/inria/corese/core/next/impl/io/parser/turtle/TurtleParserTest.java +++ b/src/test/java/fr/inria/corese/core/next/impl/io/parser/turtle/TurtleParserTest.java @@ -6,8 +6,6 @@ import fr.inria.corese.core.next.impl.temp.CoreseAdaptedValueFactory; import fr.inria.corese.core.next.impl.temp.CoreseModel; import org.junit.jupiter.api.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.io.StringReader; @@ -21,10 +19,8 @@ */ public class TurtleParserTest { - private static final Logger logger = LoggerFactory.getLogger(TurtleListener.class); - @Test - public void testParseWithPrefixAndTriple() throws Exception { + public void testParseWithPrefixAndTriple() { String turtle = " @prefix ex: . " + "ex:Alice ex:knows ex:Bob ."; diff --git a/src/test/java/fr/inria/corese/core/next/impl/io/serialization/SerializerFactoryTest.java b/src/test/java/fr/inria/corese/core/next/impl/io/serialization/SerializerFactoryTest.java index 673e5e3bb..d2d041c09 100644 --- a/src/test/java/fr/inria/corese/core/next/impl/io/serialization/SerializerFactoryTest.java +++ b/src/test/java/fr/inria/corese/core/next/impl/io/serialization/SerializerFactoryTest.java @@ -1,28 +1,32 @@ package fr.inria.corese.core.next.impl.io.serialization; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.mockConstruction; - -import fr.inria.corese.core.next.api.io.IOOptions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.mockito.MockedConstruction; - import fr.inria.corese.core.next.api.Model; import fr.inria.corese.core.next.api.base.io.RDFFormat; +import fr.inria.corese.core.next.api.io.IOOptions; import fr.inria.corese.core.next.api.io.serialization.RDFSerializer; -import fr.inria.corese.core.next.impl.io.serialization.jsonld.JSONLDSerializer; +import fr.inria.corese.core.next.impl.exception.SerializationException; +import fr.inria.corese.core.next.impl.io.common.JSONLDOptions; import fr.inria.corese.core.next.impl.io.serialization.canonical.RDFC10Serializer; +import fr.inria.corese.core.next.impl.io.serialization.canonical.RDFC10SerializerOptions; +import fr.inria.corese.core.next.impl.io.serialization.jsonld.JSONLDSerializer; import fr.inria.corese.core.next.impl.io.serialization.nquads.NQuadsSerializer; +import fr.inria.corese.core.next.impl.io.serialization.nquads.NQuadsSerializerOptions; import fr.inria.corese.core.next.impl.io.serialization.ntriples.NTriplesSerializer; +import fr.inria.corese.core.next.impl.io.serialization.ntriples.NTriplesSerializerOptions; import fr.inria.corese.core.next.impl.io.serialization.rdfxml.RDFXMLSerializer; +import fr.inria.corese.core.next.impl.io.serialization.rdfxml.RDFXMLSerializerOption; import fr.inria.corese.core.next.impl.io.serialization.trig.TriGSerializer; +import fr.inria.corese.core.next.impl.io.serialization.trig.TriGSerializerOptions; import fr.inria.corese.core.next.impl.io.serialization.turtle.TurtleSerializer; +import fr.inria.corese.core.next.impl.io.serialization.turtle.TurtleSerializerOptions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.mockito.MockedConstruction; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockConstruction; /** * Unit tests for the {@link SerializerFactory} class. @@ -34,23 +38,22 @@ class SerializerFactoryTest { private SerializerFactory factory; private Model mockModel; - private IOOptions mockConfig; @BeforeEach void setUp() { factory = new SerializerFactory(); mockModel = mock(Model.class); - mockConfig = mock(IOOptions.class); } @Test @DisplayName("createSerializer should return TurtleSerializer for TURTLE format") void createSerializer_shouldReturnTurtleSerializer_forTurtleFormat() { + TurtleSerializerOptions config = TurtleSerializerOptions.defaultConfig(); try (MockedConstruction mockedConstruction = mockConstruction(TurtleSerializer.class)) { - RDFSerializer serializer = factory.createSerializer(RDFFormat.TURTLE, mockModel, mockConfig); + RDFSerializer serializer = factory.createSerializer(RDFFormat.TURTLE, mockModel, config); assertNotNull(serializer); - assertTrue(serializer instanceof TurtleSerializer); + assertInstanceOf(TurtleSerializer.class, serializer); assertEquals(1, mockedConstruction.constructed().size(), "TurtleSerializer constructor should be called once"); } @@ -59,11 +62,12 @@ void createSerializer_shouldReturnTurtleSerializer_forTurtleFormat() { @Test @DisplayName("createSerializer should return NTriplesSerializer for NTRIPLES format") void createSerializer_shouldReturnNTriplesSerializer_forNTriplesFormat() { + NTriplesSerializerOptions config = NTriplesSerializerOptions.defaultConfig(); try (MockedConstruction mockedConstruction = mockConstruction(NTriplesSerializer.class)) { - RDFSerializer serializer = factory.createSerializer(RDFFormat.NTRIPLES, mockModel, mockConfig); + RDFSerializer serializer = factory.createSerializer(RDFFormat.NTRIPLES, mockModel, config); assertNotNull(serializer); - assertTrue(serializer instanceof NTriplesSerializer); + assertInstanceOf(NTriplesSerializer.class, serializer); assertEquals(1, mockedConstruction.constructed().size(), "NTriplesSerializer constructor should be called once"); } @@ -72,11 +76,12 @@ void createSerializer_shouldReturnNTriplesSerializer_forNTriplesFormat() { @Test @DisplayName("createSerializer should return NQuadsSerializer for NQUADS format") void createSerializer_shouldReturnNQuadsSerializer_forNQuadsFormat() { + NQuadsSerializerOptions config = NQuadsSerializerOptions.defaultConfig(); try (MockedConstruction mockedConstruction = mockConstruction(NQuadsSerializer.class)) { - RDFSerializer serializer = factory.createSerializer(RDFFormat.NQUADS, mockModel, mockConfig); + RDFSerializer serializer = factory.createSerializer(RDFFormat.NQUADS, mockModel, config); assertNotNull(serializer); - assertTrue(serializer instanceof NQuadsSerializer); + assertInstanceOf(NQuadsSerializer.class, serializer); assertEquals(1, mockedConstruction.constructed().size(), "NQuadsSerializer constructor should be called once"); } @@ -85,11 +90,12 @@ void createSerializer_shouldReturnNQuadsSerializer_forNQuadsFormat() { @Test @DisplayName("createSerializer should return TriGSerializer for TRIG format") void createSerializer_shouldReturnTriGSerializer_forTriGFormat() { + TriGSerializerOptions config = TriGSerializerOptions.defaultConfig(); try (MockedConstruction mockedConstruction = mockConstruction(TriGSerializer.class)) { - RDFSerializer serializer = factory.createSerializer(RDFFormat.TRIG, mockModel, mockConfig); + RDFSerializer serializer = factory.createSerializer(RDFFormat.TRIG, mockModel, config); assertNotNull(serializer); - assertTrue(serializer instanceof TriGSerializer); + assertInstanceOf(TriGSerializer.class, serializer); assertEquals(1, mockedConstruction.constructed().size(), "TriGSerializer constructor should be called once"); } @@ -98,11 +104,12 @@ void createSerializer_shouldReturnTriGSerializer_forTriGFormat() { @Test @DisplayName("createSerializer should return XmlSerializer for RDFXML format") void createSerializer_shouldReturnXmlSerializer_forRdfXmlFormat() { + RDFXMLSerializerOption config = RDFXMLSerializerOption.defaultConfig(); try (MockedConstruction mockedConstruction = mockConstruction(RDFXMLSerializer.class)) { - RDFSerializer serializer = factory.createSerializer(RDFFormat.RDFXML, mockModel, mockConfig); + RDFSerializer serializer = factory.createSerializer(RDFFormat.RDFXML, mockModel, config); assertNotNull(serializer); - assertTrue(serializer instanceof RDFXMLSerializer); + assertInstanceOf(RDFXMLSerializer.class, serializer); assertEquals(1, mockedConstruction.constructed().size(), "XmlSerializer constructor should be called once"); } } @@ -110,11 +117,12 @@ void createSerializer_shouldReturnXmlSerializer_forRdfXmlFormat() { @Test @DisplayName("createSerializer should return CanonicalSerializer for CANONICAL_RDF format") void createSerializer_shouldReturnCanonicalSerializer_forCanonicalRdfFormat() { + RDFC10SerializerOptions config = RDFC10SerializerOptions.defaultConfig(); try (MockedConstruction mockedConstruction = mockConstruction(RDFC10Serializer.class)) { - RDFSerializer serializer = factory.createSerializer(RDFFormat.RDFC_1_0, mockModel, mockConfig); + RDFSerializer serializer = factory.createSerializer(RDFFormat.RDFC_1_0, mockModel, config); assertNotNull(serializer); - assertTrue(serializer instanceof RDFC10Serializer); + assertInstanceOf(RDFC10Serializer.class, serializer); assertEquals(1, mockedConstruction.constructed().size(), "CanonicalSerializer constructor should be called once"); } } @@ -122,29 +130,42 @@ void createSerializer_shouldReturnCanonicalSerializer_forCanonicalRdfFormat() { @Test @DisplayName("createSerializer should return JSONLDSerializer for JSONLD format") void createSerializer_shouldReturnJSONLDSerializer_forJSONLDFormat() { + JSONLDOptions config = new JSONLDOptions.Builder().build(); try (MockedConstruction mockedConstruction = mockConstruction(JSONLDSerializer.class)) { - RDFSerializer serializer = factory.createSerializer(RDFFormat.JSONLD, mockModel, mockConfig); + RDFSerializer serializer = factory.createSerializer(RDFFormat.JSONLD, mockModel, config); assertNotNull(serializer); - assertTrue(serializer instanceof JSONLDSerializer); + assertInstanceOf(JSONLDSerializer.class, serializer); assertEquals(1, mockedConstruction.constructed().size(), "JSONLDSerializer constructor should be called once"); } } + @Test + @DisplayName("createSerializer should throw SerializationException for wrong config type") + void createSerializer_shouldThrowSerializationException_forWrongConfigType() { + IOOptions wrongConfig = mock(IOOptions.class); + + assertThrows(SerializationException.class, + () -> factory.createSerializer(RDFFormat.TURTLE, mockModel, wrongConfig), + "Should throw SerializationException for wrong config type"); + } + @Test @DisplayName("createSerializer should throw NullPointerException for a null format") void createSerializer_shouldThrowNPE_forNullFormat() { + TurtleSerializerOptions config = TurtleSerializerOptions.defaultConfig(); assertThrows(NullPointerException.class, - () -> factory.createSerializer(null, mockModel, mockConfig), + () -> factory.createSerializer(null, mockModel, config), "Should throw NullPointerException for null RDFFormat"); } @Test @DisplayName("createSerializer should throw NullPointerException for a null model") void createSerializer_shouldThrowNPE_forNullModel() { + TurtleSerializerOptions config = TurtleSerializerOptions.defaultConfig(); assertThrows(NullPointerException.class, - () -> factory.createSerializer(RDFFormat.TURTLE, null, mockConfig), + () -> factory.createSerializer(RDFFormat.TURTLE, null, config), "Should throw NullPointerException for null Model"); } @@ -155,4 +176,111 @@ void createSerializer_shouldThrowNPE_forNullConfig() { () -> factory.createSerializer(RDFFormat.TURTLE, mockModel, null), "Should throw NullPointerException for null SerializationConfig"); } -} + + @Test + @DisplayName("createSerializer without config should return TurtleSerializer for TURTLE format") + void createSerializerWithoutConfig_shouldReturnTurtleSerializer_forTurtleFormat() { + try (MockedConstruction mockedConstruction = mockConstruction(TurtleSerializer.class)) { + RDFSerializer serializer = factory.createSerializer(RDFFormat.TURTLE, mockModel); + + assertNotNull(serializer); + assertInstanceOf(TurtleSerializer.class, serializer); + assertEquals(1, mockedConstruction.constructed().size(), + "TurtleSerializer constructor should be called once"); + } + } + + @Test + @DisplayName("createSerializer without config should return NTriplesSerializer for NTRIPLES format") + void createSerializerWithoutConfig_shouldReturnNTriplesSerializer_forNTriplesFormat() { + try (MockedConstruction mockedConstruction = mockConstruction(NTriplesSerializer.class)) { + RDFSerializer serializer = factory.createSerializer(RDFFormat.NTRIPLES, mockModel); + + assertNotNull(serializer); + assertInstanceOf(NTriplesSerializer.class, serializer); + assertEquals(1, mockedConstruction.constructed().size(), + "NTriplesSerializer constructor should be called once"); + } + } + + @Test + @DisplayName("createSerializer without config should return NQuadsSerializer for NQUADS format") + void createSerializerWithoutConfig_shouldReturnNQuadsSerializer_forNQuadsFormat() { + try (MockedConstruction mockedConstruction = mockConstruction(NQuadsSerializer.class)) { + RDFSerializer serializer = factory.createSerializer(RDFFormat.NQUADS, mockModel); + + assertNotNull(serializer); + assertInstanceOf(NQuadsSerializer.class, serializer); + assertEquals(1, mockedConstruction.constructed().size(), + "NQuadsSerializer constructor should be called once"); + } + } + + @Test + @DisplayName("createSerializer without config should return TriGSerializer for TRIG format") + void createSerializerWithoutConfig_shouldReturnTriGSerializer_forTriGFormat() { + try (MockedConstruction mockedConstruction = mockConstruction(TriGSerializer.class)) { + RDFSerializer serializer = factory.createSerializer(RDFFormat.TRIG, mockModel); + + assertNotNull(serializer); + assertInstanceOf(TriGSerializer.class, serializer); + assertEquals(1, mockedConstruction.constructed().size(), + "TriGSerializer constructor should be called once"); + } + } + + @Test + @DisplayName("createSerializer without config should return XmlSerializer for RDFXML format") + void createSerializerWithoutConfig_shouldReturnXmlSerializer_forRdfXmlFormat() { + try (MockedConstruction mockedConstruction = mockConstruction(RDFXMLSerializer.class)) { + RDFSerializer serializer = factory.createSerializer(RDFFormat.RDFXML, mockModel); + + assertNotNull(serializer); + assertInstanceOf(RDFXMLSerializer.class, serializer); + assertEquals(1, mockedConstruction.constructed().size(), + "XmlSerializer constructor should be called once"); + } + } + + @Test + @DisplayName("createSerializer without config should return CanonicalSerializer for CANONICAL_RDF format") + void createSerializerWithoutConfig_shouldReturnCanonicalSerializer_forCanonicalRdfFormat() { + try (MockedConstruction mockedConstruction = mockConstruction(RDFC10Serializer.class)) { + RDFSerializer serializer = factory.createSerializer(RDFFormat.RDFC_1_0, mockModel); + + assertNotNull(serializer); + assertInstanceOf(RDFC10Serializer.class, serializer); + assertEquals(1, mockedConstruction.constructed().size(), + "CanonicalSerializer constructor should be called once"); + } + } + + @Test + @DisplayName("createSerializer without config should return JSONLDSerializer for JSONLD format") + void createSerializerWithoutConfig_shouldReturnJSONLDSerializer_forJSONLDFormat() { + try (MockedConstruction mockedConstruction = mockConstruction(JSONLDSerializer.class)) { + RDFSerializer serializer = factory.createSerializer(RDFFormat.JSONLD, mockModel); + + assertNotNull(serializer); + assertInstanceOf(JSONLDSerializer.class, serializer); + assertEquals(1, mockedConstruction.constructed().size(), + "JSONLDSerializer constructor should be called once"); + } + } + + @Test + @DisplayName("createSerializer without config should throw NullPointerException for a null format") + void createSerializerWithoutConfig_shouldThrowNPE_forNullFormat() { + assertThrows(NullPointerException.class, + () -> factory.createSerializer(null, mockModel), + "Should throw NullPointerException for null RDFFormat"); + } + + @Test + @DisplayName("createSerializer without config should throw NullPointerException for a null model") + void createSerializerWithoutConfig_shouldThrowNPE_forNullModel() { + assertThrows(NullPointerException.class, + () -> factory.createSerializer(RDFFormat.TURTLE, null), + "Should throw NullPointerException for null Model"); + } +} \ No newline at end of file diff --git a/src/test/java/fr/inria/corese/core/next/impl/io/serialization/TestStatementFactory.java b/src/test/java/fr/inria/corese/core/next/impl/io/serialization/TestStatementFactory.java index b16e046a1..16daebc0c 100644 --- a/src/test/java/fr/inria/corese/core/next/impl/io/serialization/TestStatementFactory.java +++ b/src/test/java/fr/inria/corese/core/next/impl/io/serialization/TestStatementFactory.java @@ -68,7 +68,7 @@ public Resource createBlankNode(String id) { /** * Creates a mocked IRI object with a given URI string. * - * @param uri The string URI for the IRI (e.g., "http://example.org/Person"). + * @param uri The string URI for the IRI. * @return A mocked IRI instance. */ public IRI createIRI(String uri) { diff --git a/src/test/java/fr/inria/corese/core/next/impl/io/serialization/canonical/RDFC10SerializerTest.java b/src/test/java/fr/inria/corese/core/next/impl/io/serialization/canonical/RDFC10SerializerTest.java index 564ef29cf..7233acf46 100644 --- a/src/test/java/fr/inria/corese/core/next/impl/io/serialization/canonical/RDFC10SerializerTest.java +++ b/src/test/java/fr/inria/corese/core/next/impl/io/serialization/canonical/RDFC10SerializerTest.java @@ -9,6 +9,7 @@ import fr.inria.corese.core.next.impl.io.serialization.SerializerFactory; import fr.inria.corese.core.next.impl.temp.CoreseAdaptedValueFactory; import fr.inria.corese.core.next.impl.temp.CoreseModel; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -63,7 +64,6 @@ class RDFC10SerializerTest { @Mock private BNode actualBNodeB3; - @Mock private IRI mockIRIP; @Mock @@ -80,13 +80,13 @@ class RDFC10SerializerTest { @Mock private Literal mockLiteral2; - private RDFC10Serializer serializer; private RDFC10SerializerOptions defaultConfig; + private AutoCloseable closeable; @BeforeEach void setUp() { - MockitoAnnotations.openMocks(this); + closeable = MockitoAnnotations.openMocks(this); defaultConfig = RDFC10SerializerOptions.defaultConfig(); setupBasicMocks(); @@ -101,6 +101,11 @@ protected void writeValue(Writer w, Value v) throws IOException { }; } + @AfterEach + void tearDown() throws Exception { + closeable.close(); + } + /** * Configures the basic stringValue() and isBNode() behavior for all mock RDF elements. * This ensures consistency across tests. @@ -119,7 +124,6 @@ private void setupBasicMocks() { when(mockIRIQ.isBNode()).thenReturn(false); when(mockIRIR.isBNode()).thenReturn(false); - when(mockLiteral1.stringValue()).thenReturn("\"literal1\""); when(mockLiteral2.stringValue()).thenReturn("\"literal2\""); when(mockLiteral1.isBNode()).thenReturn(false); @@ -134,7 +138,6 @@ private void setupBasicMocks() { when(mockBNodeE2.isBNode()).thenReturn(true); when(mockBNodeE3.isBNode()).thenReturn(true); - when(canonicalBNodeC0.stringValue()).thenReturn("_:c14n0"); when(canonicalBNodeC1.stringValue()).thenReturn("_:c14n1"); when(canonicalBNodeC2.stringValue()).thenReturn("_:c14n2"); @@ -193,7 +196,6 @@ void testConstructorNullModel() { new RDFC10Serializer(null, defaultConfig, mockCanonicalizer)); } - @Test @DisplayName("Constructor with null config should throw NullPointerException") void testConstructorNullConfig() { @@ -245,7 +247,6 @@ void testSerializeSimpleStatement() throws SerializationException { verify(mockCanonicalizer).canonicalize(any(Model.class)); } - @Test @DisplayName("Serialization with context (named graph)") void testSerializeWithContext() throws SerializationException { @@ -295,17 +296,16 @@ void testSerializeFigure3() { assertNotNull(canonicalOutput, "Canonical output should not be null"); assertFalse(canonicalOutput.isEmpty(), "Canonical output should not be empty"); String actual = canonicalOutput.trim().replace("\r\n", "\n"); - String expected = " _:c14n2 .\n" + - " _:c14n3 .\n" + - "_:c14n0 _:c14n1 .\n" + - "_:c14n2 _:c14n1 .\n" + - "_:c14n3 _:c14n0 ."; + String expected = """ + _:c14n2 . + _:c14n3 . + _:c14n0 _:c14n1 . + _:c14n2 _:c14n1 . + _:c14n3 _:c14n0 ."""; assertEquals(expected, actual, "Canonical output should match expected format"); - } - @Test @DisplayName("Test serialization with figure2.ttl") void testSerializeFigure2() { @@ -316,11 +316,11 @@ void testSerializeFigure2() { String actual = canonicalOutput.trim().replace("\r\n", "\n"); - String expected = " _:c14n0 .\n" + - " _:c14n1 .\n" + - "_:c14n0 .\n" + - "_:c14n1 ."; - + String expected = """ + _:c14n0 . + _:c14n1 . + _:c14n0 . + _:c14n1 ."""; assertEquals(expected, actual, "Canonical output should match RDFC-1.0 specification"); } @@ -352,4 +352,4 @@ private String serializeToRdfCanonical(String resourcePath) { serializer.write(writer); return writer.toString(); } -} +} \ No newline at end of file diff --git a/src/test/java/fr/inria/corese/core/next/impl/io/serialization/nquads/NQuadsSerializerTest.java b/src/test/java/fr/inria/corese/core/next/impl/io/serialization/nquads/NQuadsSerializerTest.java index 1aae46771..e657521d5 100644 --- a/src/test/java/fr/inria/corese/core/next/impl/io/serialization/nquads/NQuadsSerializerTest.java +++ b/src/test/java/fr/inria/corese/core/next/impl/io/serialization/nquads/NQuadsSerializerTest.java @@ -22,7 +22,6 @@ class NQuadsSerializerTest { private Model model; - private NQuadsSerializerOptions config; private NQuadsSerializer nQuadsSerializer; private TestStatementFactory factory; @@ -41,7 +40,7 @@ class NQuadsSerializerTest { @BeforeEach void setUp() { model = mock(Model.class); - config = NQuadsSerializerOptions.defaultConfig(); + NQuadsSerializerOptions config = NQuadsSerializerOptions.defaultConfig(); nQuadsSerializer = new NQuadsSerializer(model, config); factory = new TestStatementFactory(); @@ -266,11 +265,14 @@ void shouldHandleLiteralsWithLanguageTags() throws SerializationException { NQuadsSerializer serializer = new NQuadsSerializer(currentTestModel, NQuadsSerializerOptions.defaultConfig()); serializer.write(writer); + String languageTag = mockLiteralHelloEn.getLanguage() + .orElseThrow(() -> new AssertionError("Expected language tag to be present")); + String expectedOutput = String.format("<%s> <%s> \"%s\"@%s", mockExPerson.stringValue(), factory.createIRI("http://example.org/greeting").stringValue(), escapeNQuadsString(hello), - mockLiteralHelloEn.getLanguage().get()) + " .\n"; + languageTag) + " .\n"; assertEquals(expectedOutput, writer.toString()); } @@ -330,16 +332,8 @@ private String escapeNQuadsString(String s) { default: if (c <= 0x1F || c == 0x7F) { sb.append(String.format("\\u%04X", (int) c)); - } else if (c >= 0x80 && c <= 0xFFFF) { + } else if (c >= 0x80) { sb.append(String.format("\\u%04X", (int) c)); - } else if (Character.isHighSurrogate(c)) { - int codePoint = s.codePointAt(i); - if (Character.isValidCodePoint(codePoint)) { - sb.append(String.format("\\U%08X", codePoint)); - i++; - } else { - sb.append(c); - } } else { sb.append(c); } diff --git a/src/test/java/fr/inria/corese/core/next/impl/io/serialization/ntriples/NTriplesSerializerTest.java b/src/test/java/fr/inria/corese/core/next/impl/io/serialization/ntriples/NTriplesSerializerTest.java index 68a0c3ca3..338593811 100644 --- a/src/test/java/fr/inria/corese/core/next/impl/io/serialization/ntriples/NTriplesSerializerTest.java +++ b/src/test/java/fr/inria/corese/core/next/impl/io/serialization/ntriples/NTriplesSerializerTest.java @@ -23,7 +23,6 @@ class NTriplesSerializerTest { private Model model; - private NTriplesSerializerOptions config; private NTriplesSerializer nTriplesSerializer; private TestStatementFactory factory; @@ -42,7 +41,7 @@ class NTriplesSerializerTest { @BeforeEach void setUp() { model = mock(Model.class); - config = NTriplesSerializerOptions.defaultConfig(); + NTriplesSerializerOptions config = NTriplesSerializerOptions.defaultConfig(); nTriplesSerializer = new NTriplesSerializer(model, config); factory = new TestStatementFactory(); @@ -273,11 +272,14 @@ void shouldHandleLiteralsWithLanguageTags() throws SerializationException { NTriplesSerializer serializer = new NTriplesSerializer(currentTestModel, NTriplesSerializerOptions.defaultConfig()); serializer.write(writer); + String languageTag = mockLiteralHelloEn.getLanguage() + .orElseThrow(() -> new AssertionError("Expected language tag to be present")); + String expectedOutput = String.format("<%s> <%s> \"%s\"@%s", mockExPerson.stringValue(), factory.createIRI("http://example.org/greeting").stringValue(), escapeNTriplesString(hello), - mockLiteralHelloEn.getLanguage().get()) + " .\n"; + languageTag) + " .\n"; Assertions.assertEquals(expectedOutput, writer.toString()); } diff --git a/src/test/java/fr/inria/corese/core/next/impl/io/serialization/rdfxml/RDFXMLSerializerTest.java b/src/test/java/fr/inria/corese/core/next/impl/io/serialization/rdfxml/RDFXMLSerializerTest.java index 06bd476cd..777d0bf5a 100644 --- a/src/test/java/fr/inria/corese/core/next/impl/io/serialization/rdfxml/RDFXMLSerializerTest.java +++ b/src/test/java/fr/inria/corese/core/next/impl/io/serialization/rdfxml/RDFXMLSerializerTest.java @@ -7,6 +7,7 @@ import fr.inria.corese.core.next.impl.io.serialization.TestStatementFactory; import fr.inria.corese.core.next.impl.io.serialization.option.LiteralDatatypePolicyEnum; import fr.inria.corese.core.next.impl.io.serialization.option.PrefixOrderingEnum; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -29,18 +30,22 @@ class RDFXMLSerializerTest { private Model mockModel; RDFXMLSerializerOption mockConfig; private TestStatementFactory factory; - private StringWriter writer; + private AutoCloseable closeable; @BeforeEach void setUp() { - MockitoAnnotations.openMocks(this); + closeable = MockitoAnnotations.openMocks(this); writer = new StringWriter(); factory = new TestStatementFactory(); - mockConfig = RDFXMLSerializerOption.defaultConfig(); } + @AfterEach + void tearDown() throws Exception { + closeable.close(); + } + @Test @DisplayName("Should serialize a simple IRI triple with auto-declared namespaces") diff --git a/src/test/java/fr/inria/corese/core/next/impl/io/serialization/trig/TriGSerializerOptionsTest.java b/src/test/java/fr/inria/corese/core/next/impl/io/serialization/trig/TriGSerializerOptionsTest.java index bc8e9496d..4224a9dc2 100644 --- a/src/test/java/fr/inria/corese/core/next/impl/io/serialization/trig/TriGSerializerOptionsTest.java +++ b/src/test/java/fr/inria/corese/core/next/impl/io/serialization/trig/TriGSerializerOptionsTest.java @@ -12,9 +12,6 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import java.util.HashMap; -import java.util.Map; - import static org.junit.jupiter.api.Assertions.*; /** diff --git a/src/test/java/fr/inria/corese/core/next/impl/io/serialization/trig/TriGSerializerTest.java b/src/test/java/fr/inria/corese/core/next/impl/io/serialization/trig/TriGSerializerTest.java index bf9b41582..02e375c32 100644 --- a/src/test/java/fr/inria/corese/core/next/impl/io/serialization/trig/TriGSerializerTest.java +++ b/src/test/java/fr/inria/corese/core/next/impl/io/serialization/trig/TriGSerializerTest.java @@ -5,14 +5,12 @@ import fr.inria.corese.core.next.api.Statement; import fr.inria.corese.core.next.impl.common.literal.RDF; import fr.inria.corese.core.next.impl.common.literal.XSD; +import fr.inria.corese.core.next.impl.exception.SerializationException; import fr.inria.corese.core.next.impl.io.serialization.TestStatementFactory; import fr.inria.corese.core.next.impl.io.serialization.option.LiteralDatatypePolicyEnum; -import fr.inria.corese.core.next.impl.io.serialization.util.SerializationConstants; -import fr.inria.corese.core.next.impl.exception.SerializationException; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import java.io.IOException; import java.io.StringWriter; import java.util.Collections; import java.util.stream.Stream; @@ -44,10 +42,9 @@ void setUp() { * and that standard prefixes are declared and used. * * @throws SerializationException if a serialization error occurs. - * @throws IOException if an I/O error occurs during writing. */ @Test - void testBasicTriGSerialization() throws SerializationException, IOException { + void testBasicTriGSerialization() throws SerializationException { Statement mockStatement = factory.createStatement( factory.createIRI("http://example.org/ns/person1"), factory.createIRI("http://example.org/ns/hasName"), @@ -89,10 +86,9 @@ void testBasicTriGSerialization() throws SerializationException, IOException { * Verifies that `rdf:type` is serialized as `a` when the option is enabled. * * @throws SerializationException if a serialization error occurs. - * @throws IOException if an I/O error occurs during writing. */ @Test - void testRdfTypeShortcut() throws SerializationException, IOException { + void testRdfTypeShortcut() throws SerializationException { Statement mockStatement = factory.createStatement( factory.createIRI("http://example.org/ns/person1"), @@ -136,10 +132,9 @@ void testRdfTypeShortcut() throws SerializationException, IOException { * Verifies that the language tag is appended correctly. * * @throws SerializationException if a serialization error occurs. - * @throws IOException if an I/O error occurs during writing. */ @Test - void testLiteralWithLanguageTag() throws SerializationException, IOException { + void testLiteralWithLanguageTag() throws SerializationException { Statement mockStatement = factory.createStatement( factory.createIRI("http://example.org/data/book1"), @@ -186,10 +181,9 @@ void testLiteralWithLanguageTag() throws SerializationException, IOException { * Verifies that the datatype is printed when `ALWAYS_TYPED` policy is used. * * @throws SerializationException if a serialization error occurs. - * @throws IOException if an I/O error occurs during writing. */ @Test - void testLiteralWithExplicitXsdStringType() throws SerializationException, IOException { + void testLiteralWithExplicitXsdStringType() throws SerializationException { IRI mockDatatype = XSD.STRING.getIRI(); Statement mockStatement = factory.createStatement( factory.createIRI("http://example.org/data/book2"), @@ -237,10 +231,9 @@ void testLiteralWithExplicitXsdStringType() throws SerializationException, IOExc * Verifies that the `@base` directive is included in the output. * * @throws SerializationException if a serialization error occurs. - * @throws IOException if an I/O error occurs during writing. */ @Test - void testBaseIRI() throws SerializationException, IOException { + void testBaseIRI() throws SerializationException { Statement mockStatement = factory.createStatement( factory.createIRI("http://example.org/base/resource1"), factory.createIRI("http://example.org/base/prop"), @@ -284,13 +277,12 @@ void testBaseIRI() throws SerializationException, IOException { * Verifies that only prefix declarations (if auto-declared) are written, with no statements. * * @throws SerializationException if a serialization error occurs. - * @throws IOException if an I/O error occurs during writing. */ @Test - void testEmptyModel() throws SerializationException, IOException { + void testEmptyModel() throws SerializationException { Model emptyModel = mock(Model.class); - when(emptyModel.iterator()).thenAnswer(invocation -> Collections.emptyList().iterator()); + when(emptyModel.iterator()).thenAnswer(invocation -> Collections.emptyIterator()); when(emptyModel.stream()) .thenReturn(Stream.empty()) .thenReturn(Stream.empty()); @@ -343,9 +335,7 @@ void testStrictModeInvalidLiteral() throws SerializationException { TriGSerializer triGSerializer = new TriGSerializer(mockModel, strictConfig); - SerializationException thrown = assertThrows(SerializationException.class, () -> { - triGSerializer.write(writer); - }); + SerializationException thrown = assertThrows(SerializationException.class, () -> triGSerializer.write(writer)); assertEquals("TriG", thrown.getFormatName()); @@ -379,9 +369,7 @@ void testStrictModeInvalidIRICharacters() throws SerializationException { TriGSerializer triGSerializer = new TriGSerializer(mockModel, strictConfig); - SerializationException thrown = assertThrows(SerializationException.class, () -> { - triGSerializer.write(writer); - }); + SerializationException thrown = assertThrows(SerializationException.class, () -> triGSerializer.write(writer)); assertEquals("TriG", thrown.getFormatName()); @@ -393,10 +381,9 @@ void testStrictModeInvalidIRICharacters() throws SerializationException { * Verifies that the literal is wrapped in triple quotes `"""` when `useMultilineLiterals` is true. * * @throws SerializationException if a serialization error occurs. - * @throws IOException if an I/O error occurs during writing. */ @Test - void testMultilineLiteralSerialization() throws SerializationException, IOException { + void testMultilineLiteralSerialization() throws SerializationException { String multilineText = "This is the first line.\nThis is the second line."; Statement mockStatement = factory.createStatement( @@ -443,10 +430,9 @@ void testMultilineLiteralSerialization() throws SerializationException, IOExcept * Verifies that the graph name and graph block are correctly formatted. * * @throws SerializationException if a serialization error occurs. - * @throws IOException if an I/O error occurs during writing. */ @Test - void testBasicTrigSerializationWithNamedGraph() throws SerializationException, IOException { + void testBasicTrigSerializationWithNamedGraph() throws SerializationException { Statement mockStatement = factory.createStatement( factory.createIRI("http://example.org/data/person1"), factory.createIRI("http://example.org/data/name"), diff --git a/src/test/java/fr/inria/corese/core/next/impl/io/serialization/turtle/TurtleSerializerOptionsTest.java b/src/test/java/fr/inria/corese/core/next/impl/io/serialization/turtle/TurtleSerializerOptionsTest.java index cf2c9eece..8a96d7856 100644 --- a/src/test/java/fr/inria/corese/core/next/impl/io/serialization/turtle/TurtleSerializerOptionsTest.java +++ b/src/test/java/fr/inria/corese/core/next/impl/io/serialization/turtle/TurtleSerializerOptionsTest.java @@ -12,9 +12,6 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import java.util.HashMap; -import java.util.Map; - import static org.junit.jupiter.api.Assertions.*; /** diff --git a/src/test/java/fr/inria/corese/core/next/impl/io/serialization/turtle/TurtleSerializerTest.java b/src/test/java/fr/inria/corese/core/next/impl/io/serialization/turtle/TurtleSerializerTest.java index d4d2b2f99..2ab5eb370 100644 --- a/src/test/java/fr/inria/corese/core/next/impl/io/serialization/turtle/TurtleSerializerTest.java +++ b/src/test/java/fr/inria/corese/core/next/impl/io/serialization/turtle/TurtleSerializerTest.java @@ -4,12 +4,9 @@ import fr.inria.corese.core.next.impl.common.literal.RDF; import fr.inria.corese.core.next.impl.common.literal.XSD; import fr.inria.corese.core.next.impl.common.prefix.PrefixHandler; -import fr.inria.corese.core.next.impl.io.parser.ParserFactory; -import fr.inria.corese.core.next.impl.io.serialization.SerializerFactory; +import fr.inria.corese.core.next.impl.exception.SerializationException; import fr.inria.corese.core.next.impl.io.serialization.TestStatementFactory; import fr.inria.corese.core.next.impl.io.serialization.option.LiteralDatatypePolicyEnum; -import fr.inria.corese.core.next.impl.io.serialization.util.SerializationConstants; -import fr.inria.corese.core.next.impl.exception.SerializationException; import fr.inria.corese.core.next.impl.temp.CoreseAdaptedValueFactory; import fr.inria.corese.core.next.impl.temp.CoreseModel; import org.junit.jupiter.api.BeforeEach; @@ -18,7 +15,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.IOException; import java.io.StringWriter; import java.util.Arrays; import java.util.Collections; @@ -50,10 +46,9 @@ void setUp() { * and that standard prefixes are declared and used. * * @throws SerializationException if a serialization error occurs. - * @throws IOException if an I/O error occurs during writing. */ @Test - void testBasicTurtleSerialization() throws SerializationException, IOException { + void testBasicTurtleSerialization() throws SerializationException { Statement mockStatement = factory.createStatement( factory.createIRI("http://example.org/ns/person1"), factory.createIRI("http://example.org/ns/hasName"), @@ -92,10 +87,9 @@ void testBasicTurtleSerialization() throws SerializationException, IOException { * Verifies that `rdf:type` is serialized as `a` when the option is enabled. * * @throws SerializationException if a serialization error occurs. - * @throws IOException if an I/O error occurs during writing. */ @Test - void testRdfTypeShortcut() throws SerializationException, IOException { + void testRdfTypeShortcut() throws SerializationException { Statement mockStatement = factory.createStatement( factory.createIRI("http://example.org/ns/person1"), @@ -137,10 +131,9 @@ void testRdfTypeShortcut() throws SerializationException, IOException { * Verifies that the language tag is appended correctly. * * @throws SerializationException if a serialization error occurs. - * @throws IOException if an I/O error occurs during writing. */ @Test - void testLiteralWithLanguageTag() throws SerializationException, IOException { + void testLiteralWithLanguageTag() throws SerializationException { Statement mockStatement = factory.createStatement( factory.createIRI("http://example.org/data/book1"), @@ -195,11 +188,10 @@ void testLiteralWithLanguageTag() throws SerializationException, IOException { * Verifies that the datatype is printed when `ALWAYS_TYPED` policy is used. * * @throws SerializationException if a serialization error occurs. - * @throws IOException if an I/O error occurs during writing. */ @Test @DisplayName("Should serialize literal with xsd:string datatype (minimal policy)") - void testLiteralWithExplicitXsdStringType() throws SerializationException, IOException { + void testLiteralWithExplicitXsdStringType() throws SerializationException { IRI mockDatatype = XSD.STRING.getIRI(); Statement mockStatement = factory.createStatement( @@ -253,10 +245,9 @@ void testLiteralWithExplicitXsdStringType() throws SerializationException, IOExc * Verifies that the blank node is serialized inline with its properties. * * @throws SerializationException if a serialization error occurs. - * @throws IOException if an I/O error occurs during writing. */ @Test - void testBlankNodeSerialization() throws SerializationException, IOException { + void testBlankNodeSerialization() throws SerializationException { Statement mainStatement = factory.createStatement( factory.createIRI("http://example.org/ns/mainSubject"), @@ -296,8 +287,6 @@ void testBlankNodeSerialization() throws SerializationException, IOException { _:b1 ns:hasValue "Value of BNode" . """; - String actual = writer.toString().replace("\r\n", "\n"); - String expected1 = """ _:b1 ns:hasValue "Value of BNode" . ns:mainSubject ns:refersTo _:b1 . @@ -318,15 +307,11 @@ void testBlankNodeSerializarionWithoutId() { Logger logger = LoggerFactory.getLogger(TurtleSerializerTest.class); ValueFactory valueFactory; - fr.inria.corese.core.next.api.io.serialization.SerializerFactory serializerFactory; - ParserFactory parserFactory; TurtleSerializerOptions defaultConfig; String EXAMPLE_NS = "http://example.org/"; String PREDICATE_KNOWS = EXAMPLE_NS + "knows"; valueFactory = new CoreseAdaptedValueFactory(); - serializerFactory = new SerializerFactory(); - parserFactory = new ParserFactory(); defaultConfig = TurtleSerializerOptions.defaultConfig(); Model model = new CoreseModel(); @@ -338,34 +323,34 @@ void testBlankNodeSerializarionWithoutId() { model.add(blankSubject, predicate, blankObject); - model.stream().forEach(stmt -> { - Value obj = stmt.getObject(); - String subjectString = stmt.getSubject().stringValue(); - String predicateString = stmt.getPredicate().stringValue(); - - if (obj instanceof Literal literal) { - String label = String.valueOf(literal.getLabel()); - String languageTag = literal.getLanguage().orElse(null); - - if (languageTag != null) { - logger.debug("({}, {}, \"{}\"@{})", - subjectString, - predicateString, - label, - languageTag); - } else { - logger.debug("({}, {}, \"{}\")", - subjectString, - predicateString, - label); - } + for (Statement stmt : model) { + Value obj = stmt.getObject(); + String subjectString = stmt.getSubject().stringValue(); + String predicateString = stmt.getPredicate().stringValue(); + + if (obj instanceof Literal literal) { + String label = String.valueOf(literal.getLabel()); + String languageTag = literal.getLanguage().orElse(null); + + if (languageTag != null) { + logger.debug("({}, {}, \"{}\"@{})", + subjectString, + predicateString, + label, + languageTag); } else { - logger.debug("({}, {}, {})", + logger.debug("({}, {}, \"{}\")", subjectString, predicateString, - obj.stringValue()); + label); } - }); + } else { + logger.debug("({}, {}, {})", + subjectString, + predicateString, + obj.stringValue()); + } + } StringWriter writer = new StringWriter(); TurtleSerializer turtleSerializer = new TurtleSerializer(model, defaultConfig); @@ -380,10 +365,9 @@ void testBlankNodeSerializarionWithoutId() { * Verifies that the `@base` directive is included in the output. * * @throws SerializationException if a serialization error occurs. - * @throws IOException if an I/O error occurs during writing. */ @Test - void testBaseIRI() throws SerializationException, IOException { + void testBaseIRI() throws SerializationException { Statement mockStatement = factory.createStatement( factory.createIRI("http://example.org/base/resource1"), factory.createIRI("http://example.org/base/prop"), @@ -428,13 +412,12 @@ void testBaseIRI() throws SerializationException, IOException { * Verifies that only prefix declarations (if auto-declared) are written, with no statements. * * @throws SerializationException if a serialization error occurs. - * @throws IOException if an I/O error occurs during writing. */ @Test - void testEmptyModel() throws SerializationException, IOException { + void testEmptyModel() throws SerializationException { Model emptyModel = mock(Model.class); - when(emptyModel.iterator()).thenAnswer(invocation -> Collections.emptyList().iterator()); + when(emptyModel.iterator()).thenAnswer(invocation -> Collections.emptyIterator()); when(emptyModel.stream()) .thenReturn(Stream.empty()) .thenReturn(Stream.empty()); @@ -487,9 +470,7 @@ void testStrictModeInvalidLiteral() throws SerializationException { TurtleSerializer turtleSerializer = new TurtleSerializer(mockModel, strictConfig); - SerializationException thrown = assertThrows(SerializationException.class, () -> { - turtleSerializer.write(writer); - }); + SerializationException thrown = assertThrows(SerializationException.class, () -> turtleSerializer.write(writer)); assertEquals("Turtle", thrown.getFormatName()); @@ -523,9 +504,7 @@ void testStrictModeInvalidIRICharacters() throws SerializationException { TurtleSerializer turtleSerializer = new TurtleSerializer(mockModel, strictConfig); - SerializationException thrown = assertThrows(SerializationException.class, () -> { - turtleSerializer.write(writer); - }); + SerializationException thrown = assertThrows(SerializationException.class, () -> turtleSerializer.write(writer)); assertEquals("Turtle", thrown.getFormatName()); @@ -537,10 +516,9 @@ void testStrictModeInvalidIRICharacters() throws SerializationException { * Verifies that the literal is wrapped in triple quotes `"""` when `useMultilineLiterals` is true. * * @throws SerializationException if a serialization error occurs. - * @throws IOException if an I/O error occurs during writing. */ @Test - void testMultilineLiteralSerialization() throws SerializationException, IOException { + void testMultilineLiteralSerialization() throws SerializationException { String multilineText = "This is the first line.\nThis is the second line."; Statement mockStatement = factory.createStatement( factory.createIRI("http://example.org/book/1"), @@ -584,10 +562,9 @@ void testMultilineLiteralSerialization() throws SerializationException, IOExcept * Tests serialization of a literal containing escaped characters. * * @throws SerializationException if a serialization error occurs. - * @throws IOException if an I/O error occurs during writing. */ @Test - void testEscapedCharacterLiteralSerialization() throws SerializationException, IOException { + void testEscapedCharacterLiteralSerialization() throws SerializationException { ValueFactory coreseFactory = new CoreseAdaptedValueFactory(); Statement statement = coreseFactory.createStatement( coreseFactory.createIRI("http://example.org/book/1"),