AnalyzerGuruTest.java revision 210
150N/Apackage org.opensolaris.opengrok.analysis;
150N/A
150N/Aimport java.io.ByteArrayInputStream;
150N/Aimport org.junit.Test;
200N/Aimport org.opensolaris.opengrok.analysis.plain.XMLAnalyzer;
150N/Aimport org.opensolaris.opengrok.analysis.sh.ShAnalyzer;
210N/Aimport org.opensolaris.opengrok.analysis.sh.ShAnalyzerFactory;
150N/Aimport static org.junit.Assert.*;
150N/A
150N/A/**
150N/A * Tests for the functionality provided by the AnalyzerGuru class.
150N/A */
150N/Apublic class AnalyzerGuruTest {
150N/A /**
150N/A * Test that we get the correct analyzer if the file name exactly matches a
150N/A * known extension.
150N/A */
150N/A @Test
150N/A public void testFileNameSameAsExtension() throws Exception {
150N/A ByteArrayInputStream in = new ByteArrayInputStream(
150N/A "#!/bin/sh\nexec /usr/bin/zip \"$@\"\n".getBytes("US-ASCII"));
150N/A String file = "/dummy/path/to/source/zip";
150N/A FileAnalyzer fa = AnalyzerGuru.getAnalyzer(in, file);
200N/A assertSame(ShAnalyzer.class, fa.getClass());
200N/A }
200N/A
200N/A @Test
200N/A public void testUTF8ByteOrderMark() throws Exception {
200N/A byte[] xml = { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF, // UTF-8 BOM
200N/A '<', '?', 'x', 'm', 'l', ' ',
200N/A 'v', 'e', 'r', 's', 'i', 'o', 'n', '=',
200N/A '"', '1', '.', '0', '"', '?', '>' };
200N/A ByteArrayInputStream in = new ByteArrayInputStream(xml);
200N/A FileAnalyzer fa = AnalyzerGuru.getAnalyzer(in, "/dummy/file");
200N/A assertSame(XMLAnalyzer.class, fa.getClass());
150N/A }
210N/A
210N/A @Test
210N/A public void addExtension() throws Exception {
210N/A // should not find analyzer for this unlikely extension
210N/A assertNull(AnalyzerGuru.find("file.unlikely_extension"));
210N/A
210N/A FileAnalyzerFactory
210N/A faf = AnalyzerGuru.findFactory(ShAnalyzerFactory.class.getName());
210N/A // should be the same factory as the built-in analyzer for sh scripts
210N/A assertSame(AnalyzerGuru.find("myscript.sh"), faf);
210N/A
210N/A // add an analyzer for the extension and see that it is picked up
210N/A AnalyzerGuru.addExtension("UNLIKELY_EXTENSION", faf);
210N/A assertSame(ShAnalyzerFactory.class,
210N/A AnalyzerGuru.find("file.unlikely_extension").getClass());
210N/A
210N/A // remove the mapping and verify that it is gone
210N/A AnalyzerGuru.addExtension("UNLIKELY_EXTENSION", null);
210N/A assertNull(AnalyzerGuru.find("file.unlikely_extension"));
210N/A }
150N/A}