This project has retired. For details please refer to its Attic page.
RoverTest xref
View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *  http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.any23.cli;
19  
20  import org.apache.any23.rdf.RDFUtils;
21  import org.apache.any23.util.FileUtils;
22  import org.apache.any23.util.StringUtils;
23  import org.apache.any23.util.URLUtils;
24  import org.junit.Assert;
25  import org.junit.Assume;
26  import org.junit.Test;
27  import org.eclipse.rdf4j.model.Statement;
28  import org.eclipse.rdf4j.rio.RDFFormat;
29  
30  import java.io.File;
31  import java.util.Locale;
32  
33  /**
34   * Test case for {@link Rover}.
35   *
36   * @author Michele Mostarda (mostarda@fbk.eu)
37   */
38  public class RoverTest extends ToolTestBase {
39  
40      private static final String[] TARGET_FILES = { "/microdata/microdata-nested.html",
41              "/org/apache/any23/extractor/csv/test-semicolon.csv" };
42  
43      private static final String[] TARGET_URLS = { "http://twitter.com/micmos", "http://twitter.com/dpalmisano" };
44  
45      public RoverTest() {
46          super(Rover.class);
47      }
48  
49      @Test
50      public void testRunMultiFiles() throws Exception {
51  
52          String[] copiedTargets = new String[TARGET_FILES.length];
53          for (int i = 0; i < TARGET_FILES.length; i++) {
54              File tempFile = copyResourceToTempFile(TARGET_FILES[i]);
55  
56              copiedTargets[i] = tempFile.getAbsolutePath();
57          }
58  
59          runWithMultiSourcesAndVerify(copiedTargets, 0);
60      }
61  
62      @Test
63      public void testRunWithDefaultNS() throws Exception {
64          final String DEFAULT_GRAPH = "http://test/default/ns";
65          final File outFile = File.createTempFile("rover-test", "out", tempDirectory);
66          final int exitCode = runTool(
67                  String.format(Locale.ROOT, "-o %s -f nquads -p -n %s -d %s", outFile.getAbsolutePath(),
68                          copyResourceToTempFile("/cli/rover-test1.nq").getAbsolutePath(), DEFAULT_GRAPH));
69  
70          Assert.assertEquals("Unexpected exit code.", 0, exitCode);
71          Assert.assertTrue(outFile.exists());
72          final String fileContent = FileUtils.readFileContent(outFile);
73          final String[] lines = fileContent.split("\\n");
74          int graphCounter = 0;
75          for (String line : lines) {
76              if (line.contains(DEFAULT_GRAPH)) {
77                  graphCounter++;
78              }
79          }
80          Assert.assertEquals(0, graphCounter);
81      }
82  
83      @Test
84      public void testDelegatingWriterFactory() throws Exception {
85          final File outFile = File.createTempFile("rover-test", "out", tempDirectory);
86          final String DEFAULT_GRAPH = "http://test/default/ns";
87          final String stylesheet = "http://www.w3.org/1999/xhtml/vocab#stylesheet";
88  
89          Assert.assertEquals("Unexpected exit code.", 0,
90                  runTool(String.format(Locale.ROOT, "-o %s -f nquads %s -d %s", outFile.getAbsolutePath(),
91                          copyResourceToTempFile("/cli/basic-with-stylesheet.html").getAbsolutePath(), DEFAULT_GRAPH)));
92  
93          String content = FileUtils.readFileContent(outFile);
94  
95          Assert.assertTrue(content.contains(stylesheet));
96  
97          final int lineCountWithStylesheet = content.split("\\n").length;
98  
99          Assert.assertEquals("Unexpected exit code.", 0,
100                 runTool(String.format(Locale.ROOT, "-o %s -f notrivial,nquads %s -d %s", outFile.getAbsolutePath(),
101                         copyResourceToTempFile("/cli/basic-with-stylesheet.html").getAbsolutePath(), DEFAULT_GRAPH)));
102 
103         content = FileUtils.readFileContent(outFile);
104 
105         Assert.assertTrue(!content.contains(stylesheet));
106 
107         final int lineCountWithoutStylesheet = content.split("\\n").length;
108 
109         Assert.assertEquals(lineCountWithStylesheet - 1, lineCountWithoutStylesheet);
110     }
111 
112     /* BEGIN: online tests. */
113 
114     @Test
115     public void testRunMultiURLs() throws Exception {
116         // Assuming first accessibility to remote resources.
117         assumeOnlineAllowed();
118         for (String targetURL : TARGET_URLS) {
119             Assume.assumeTrue(URLUtils.isOnline(targetURL));
120         }
121 
122         runWithMultiSourcesAndVerify(TARGET_URLS, 0);
123     }
124 
125     private void runWithMultiSourcesAndVerify(String[] targets, int expectedExit) throws Exception {
126         final File outFile = File.createTempFile("rover-test", "out", tempDirectory);
127         final File logFile = File.createTempFile("rover-test", "log", tempDirectory);
128 
129         final int exitCode = runTool(String.format(Locale.ROOT, "-o %s -f nquads -l %s -p -n %s",
130                 outFile.getAbsolutePath(), logFile.getAbsolutePath(), StringUtils.join(" ", targets)));
131         Assert.assertEquals("Unexpected exit code.", expectedExit, exitCode);
132 
133         Assert.assertTrue(outFile.exists());
134         Assert.assertTrue(logFile.exists());
135 
136         final String logFileContent = FileUtils.readFileContent(logFile);
137         Assert.assertEquals("Unexpected number of log lines.", targets.length + 1, // Header line.
138                 StringUtils.countNL(logFileContent));
139 
140         final String outNQuads = FileUtils.readFileContent(outFile);
141         final Statement[] statements = RDFUtils.parseRDF(RDFFormat.NQUADS, outNQuads);
142         Assert.assertTrue("Unexpected number of statements.", statements.length > 9);
143     }
144 
145 }