AnalyzerGuruTest.java revision 670
150N/Apackage org.opensolaris.opengrok.analysis;
150N/A
150N/Aimport java.io.ByteArrayInputStream;
257N/Aimport java.io.ByteArrayOutputStream;
257N/Aimport java.io.IOException;
257N/Aimport java.io.InputStream;
257N/Aimport java.util.jar.JarEntry;
257N/Aimport java.util.jar.JarOutputStream;
257N/Aimport java.util.zip.ZipEntry;
257N/Aimport java.util.zip.ZipOutputStream;
150N/Aimport org.junit.Test;
257N/Aimport org.opensolaris.opengrok.analysis.archive.ZipAnalyzer;
450N/Aimport org.opensolaris.opengrok.analysis.c.CAnalyzerFactory;
670N/Aimport org.opensolaris.opengrok.analysis.c.CxxAnalyzerFactory;
257N/Aimport org.opensolaris.opengrok.analysis.executables.JarAnalyzer;
257N/Aimport org.opensolaris.opengrok.analysis.plain.PlainAnalyzer;
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 }
257N/A
257N/A @Test
257N/A public void testZip() throws IOException {
257N/A ByteArrayOutputStream baos = new ByteArrayOutputStream();
257N/A ZipOutputStream zos = new ZipOutputStream(baos);
257N/A zos.putNextEntry(new ZipEntry("dummy"));
257N/A zos.closeEntry();
257N/A zos.close();
257N/A InputStream in = new ByteArrayInputStream(baos.toByteArray());
257N/A FileAnalyzer fa = AnalyzerGuru.getAnalyzer(in, "dummy");
257N/A assertSame(ZipAnalyzer.class, fa.getClass());
257N/A }
257N/A
257N/A @Test
257N/A public void testJar() throws IOException {
257N/A ByteArrayOutputStream baos = new ByteArrayOutputStream();
257N/A JarOutputStream jos = new JarOutputStream(baos);
257N/A jos.putNextEntry(new JarEntry("dummy"));
257N/A jos.closeEntry();
257N/A jos.close();
257N/A InputStream in = new ByteArrayInputStream(baos.toByteArray());
257N/A FileAnalyzer fa = AnalyzerGuru.getAnalyzer(in, "dummy");
257N/A assertSame(JarAnalyzer.class, fa.getClass());
257N/A }
257N/A
257N/A @Test
257N/A public void testPlainText() throws IOException {
257N/A ByteArrayInputStream in = new ByteArrayInputStream(
257N/A "This is a plain text file.".getBytes("US-ASCII"));
257N/A assertSame(PlainAnalyzer.class,
257N/A AnalyzerGuru.getAnalyzer(in, "dummy").getClass());
257N/A }
450N/A
450N/A @Test
450N/A public void rfe2969() {
450N/A FileAnalyzerFactory faf = AnalyzerGuru.find("foo.hxx");
450N/A assertNotNull(faf);
670N/A assertSame(CxxAnalyzerFactory.class, faf.getClass());
670N/A }
670N/A
670N/A @Test
670N/A public void rfe3401() {
670N/A FileAnalyzerFactory f1 = AnalyzerGuru.find("main.c");
670N/A assertNotNull(f1);
670N/A FileAnalyzerFactory f2 = AnalyzerGuru.find("main.cc");
670N/A assertNotNull(f2);
670N/A assertNotSame(f1.getClass(), f2.getClass());
670N/A
450N/A }
483N/A
483N/A /**
483N/A * Test that matching of full names works. Bug #859.
483N/A */
483N/A @Test
483N/A public void matchesFullName() {
483N/A FileAnalyzerFactory faf = AnalyzerGuru.find("/path/to/Makefile");
483N/A assertSame(ShAnalyzerFactory.class, faf.getClass());
483N/A faf = AnalyzerGuru.find("GNUMakefile");
483N/A assertSame(ShAnalyzerFactory.class, faf.getClass());
483N/A }
150N/A}