Skip to content

Package: FileComparisonUtilsWithPathNormalizer

FileComparisonUtilsWithPathNormalizer

nameinstructionbranchcomplexitylinemethod
FileComparisonUtilsWithPathNormalizer()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
assertSameContents(Path, Path)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
rewriteN3(Path)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
rewriteN3(Path, boolean)
M: 13 C: 88
87%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 1 C: 19
95%
M: 0 C: 1
100%
static {...}
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * *********************************************************************************************************************
3: *
4: * blueMarine II: Semantic Media Centre
5: * http://tidalwave.it/projects/bluemarine2
6: *
7: * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/bluemarine2-src
23: * git clone https://github.com/tidalwave-it/bluemarine2-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.util.test;
28:
29: import javax.annotation.Nonnull;
30: import java.io.IOException;
31: import java.io.Reader;
32: import java.io.Writer;
33: import java.nio.file.Files;
34: import java.nio.file.Path;
35: import org.eclipse.rdf4j.rio.RDFFormat;
36: import org.eclipse.rdf4j.rio.RDFParser;
37: import org.eclipse.rdf4j.rio.Rio;
38: import org.eclipse.rdf4j.rio.n3.N3Writer;
39: import lombok.extern.slf4j.Slf4j;
40: import static java.nio.charset.StandardCharsets.UTF_8;
41: import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
42: import static it.tidalwave.bluemarine2.util.PathNormalization.*;
43:
44: /***********************************************************************************************************************
45: *
46: * @author Fabrizio Giudici
47: *
48: **********************************************************************************************************************/
49: @Slf4j
50: public class FileComparisonUtilsWithPathNormalizer
51: {
52: public static void assertSameContents (@Nonnull final Path expectedPath, @Nonnull final Path actualPath)
53: throws IOException
54: {
55: FileComparisonUtils.assertSameContents(fixedPath(expectedPath), fixedPath(actualPath));
56: }
57:
58: public static void rewriteN3 (@Nonnull final Path file)
59: throws IOException
60: {
61: rewriteN3(file, true);
62: }
63:
64: /*******************************************************************************************************************
65: *
66: * Rewrites a N3 file with the current RDF4J runtime. This takes care of eventual differences in formatting from
67: * version to version and makes it possible to use in tests 'expected files' generated by a previous version with
68: * a different formatting.
69: *
70: * @param file the file to rewrite
71: * @param removeDoubleBlankLines
72: * @throws IOException in case of error
73: *
74: ******************************************************************************************************************/
75: public static void rewriteN3 (@Nonnull final Path file, boolean removeDoubleBlankLines)
76: throws IOException
77: {
78: log.warn("Rewriting {} ... (original file is preserved with .orig suffix)", file);
79:• assert file.toString().contains("target") : "You must not call rewriteN3() on a file in sources";
80:
81: final Path normalizedFile = fixedPath(file);
82: final Path originalFile = normalizedFile.getParent().resolve(file.getFileName() + ".orig");
83: Files.createDirectories(originalFile.getParent());
84: Files.copy(normalizedFile, originalFile, REPLACE_EXISTING);
85:
86: try (final Writer w = Files.newBufferedWriter(normalizedFile, UTF_8);
87: final Reader r = Files.newBufferedReader(originalFile, UTF_8))
88: {
89: final N3Writer n3Writer = new N3Writer(w);
90: final RDFParser rdfParser = Rio.createParser(RDFFormat.N3);
91: rdfParser.setRDFHandler(n3Writer);
92: rdfParser.setPreserveBNodeIDs(true);
93: rdfParser.parse(r, "");
94: }
95:
96:• if (removeDoubleBlankLines)
97: {
98: // Remove a double blank line after namespaces
99: String contents = Files.readString(normalizedFile, UTF_8);
100:
101:• if (contents.endsWith("\n\n"))
102: {
103: contents = contents.substring(0, contents.length() - 1);
104: }
105:
106: // in case there are only namespaces
107: contents = contents.replaceAll("\n\n\n", "\n\n");
108: Files.writeString(normalizedFile, contents, UTF_8);
109: }
110: }
111: }