|
| 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 | + */ |
1 | 23 | package org.opengrok.indexer.analysis;
|
2 | 24 |
|
| 25 | +import static org.junit.Assert.assertEquals; |
| 26 | +import static org.junit.Assert.assertNotNull; |
3 | 27 |
|
| 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; |
4 | 40 | import org.junit.Test;
|
| 41 | +import org.opengrok.indexer.configuration.RuntimeEnvironment; |
5 | 42 |
|
6 | 43 | public class AnalyzerFrameworkTest {
|
7 | 44 |
|
| 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 | + */ |
8 | 68 | @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")); |
10 | 81 | }
|
11 | 82 |
|
| 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 | + } |
12 | 127 | }
|
0 commit comments