Skip to content

Commit 7fb6ef0

Browse files
committed
adding a test case for analyzer framework
1 parent 6dd520c commit 7fb6ef0

File tree

3 files changed

+117
-2
lines changed

3 files changed

+117
-2
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/analysis/AnalyzerFramework.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ protected void classLoaded(AnalyzersInfo localInfo, IAnalyzerPlugin plugin) {
190190
localInfo.fileTypeDescriptions.put(analyzer.getAnalyzer().getFileTypeName(), analyzer.getName());
191191
}
192192

193-
LOGGER.log(Level.FINER, "An analyzer factory {0} has been loaded.", analyzer.getClass().getCanonicalName());
193+
LOGGER.log(Level.INFO, "An analyzer factory {0} has been loaded.", analyzer.getClass().getCanonicalName());
194194
}
195195

196196
/**
Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,127 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
22+
*/
123
package org.opengrok.indexer.analysis;
224

25+
import static org.junit.Assert.assertEquals;
26+
import static org.junit.Assert.assertNotNull;
327

28+
import java.io.File;
29+
import java.io.IOException;
30+
import java.io.Reader;
31+
import java.io.StringWriter;
32+
import java.net.URISyntaxException;
33+
import java.nio.file.Files;
34+
import java.nio.file.Path;
35+
import java.nio.file.Paths;
36+
import java.util.Arrays;
37+
import java.util.List;
38+
import java.util.stream.Collectors;
39+
import org.junit.Assert;
440
import org.junit.Test;
41+
import org.opengrok.indexer.configuration.RuntimeEnvironment;
542

643
public class AnalyzerFrameworkTest {
744

45+
private final File pluginDirectory;
46+
47+
public AnalyzerFrameworkTest() throws URISyntaxException {
48+
pluginDirectory = Paths.get(getClass().getResource("/analysis/plugins/testplugins.jar").toURI()).toFile().getParentFile();
49+
}
50+
51+
/**
52+
* Analyzer framework should be started reloaded from the plugin directory.
53+
*/
54+
@Test
55+
public void testReloadDefault() {
56+
AnalyzerFramework framework = new AnalyzerFramework(null);
57+
58+
Assert.assertFalse("Analyzer framework should load the default analyzers", framework.getAnalyzersInfo().factories.isEmpty());
59+
Assert.assertFalse("Analyzer framework should load the default analyzers", framework.getAnalyzersInfo().extensions.isEmpty());
60+
Assert.assertFalse("Analyzer framework should load the default analyzers", framework.getAnalyzersInfo().magics.isEmpty());
61+
Assert.assertFalse("Analyzer framework should load the default analyzers", framework.getAnalyzersInfo().prefixes.isEmpty());
62+
Assert.assertFalse("Analyzer framework should load the default analyzers", framework.getAnalyzersInfo().matchers.isEmpty());
63+
}
64+
65+
/**
66+
* Analyzer framework should be started reloaded from the plugin directory.
67+
*/
868
@Test
9-
public void test() {
69+
public void testReloadSimple() {
70+
AnalyzerFramework framework = new AnalyzerFramework(pluginDirectory.getPath());
71+
framework.setLoadClasses(false); // to avoid noise when loading classes of other tests
72+
73+
// Ensure the framework was setup correctly.
74+
assertNotNull(framework.getPluginDirectory());
75+
assertEquals(pluginDirectory, framework.getPluginDirectory());
76+
77+
final List<AnalyzerFactory> factories = framework.getAnalyzersInfo().factories;
78+
Assert.assertTrue(factories.stream().map(f -> f.getClass().getSimpleName()).anyMatch(name -> "DummyAnalyzerFactory".equals(name)));
79+
Assert.assertTrue(factories.stream().map(AnalyzerFactory::getName).anyMatch(name -> "Dummy".equals(name)));
80+
Assert.assertTrue(framework.getAnalyzersInfo().extensions.containsKey("DUMMY"));
1081
}
1182

83+
@Test
84+
public void testGuruRegisteredExtension() {
85+
final RuntimeEnvironment env = RuntimeEnvironment.getInstance();
86+
env.setPluginDirectory(pluginDirectory.getAbsolutePath());
87+
final AnalyzerGuru guru = env.getAnalyzerGuru();
88+
89+
Assert.assertNotNull(guru.find("file.dummy"));
90+
Assert.assertEquals("DummyAnalyzerFactory", guru.find("file.dummy").getClass().getSimpleName());
91+
Assert.assertEquals("Dummy", guru.find("file.dummy").getName());
92+
}
93+
94+
@Test
95+
public void testAnalyzeStream() throws IOException {
96+
final Path file = Files.createTempFile("opengrok", "analyzer-framework-test.dummy");
97+
final List<String> lines = Arrays.asList(new String[]{
98+
"Some random content",
99+
"\tsecond line is padded",
100+
"james.bond@italy.com",
101+
"opengrok is the best"
102+
});
103+
104+
Files.write(file, lines);
105+
106+
try (Reader reader = Files.newBufferedReader(file); StringWriter out = new StringWriter()) {
107+
final RuntimeEnvironment env = RuntimeEnvironment.getInstance();
108+
env.setPluginDirectory(pluginDirectory.getAbsolutePath());
109+
final AnalyzerGuru guru = env.getAnalyzerGuru();
110+
111+
final AnalyzerFactory factory = guru.find(file.toString());
112+
final AbstractAnalyzer analyzer = factory.getAnalyzer();
113+
114+
Assert.assertEquals(AbstractAnalyzer.Genre.PLAIN, analyzer.getGenre());
115+
116+
final Xrefer xrefer = analyzer.writeXref(new WriteXrefArgs(reader, out));
117+
118+
Assert.assertEquals(4, xrefer.getLineNumber());
119+
Assert.assertEquals(4, xrefer.getLOC());
120+
121+
// assert content
122+
Assert.assertEquals(lines.stream().collect(Collectors.joining("\n")), out.toString());
123+
} finally {
124+
Files.deleteIfExists(file);
125+
}
126+
}
12127
}
Binary file not shown.

0 commit comments

Comments
 (0)