IndexerTest.java revision 365
352N/A/*
352N/A * CDDL HEADER START
352N/A *
352N/A * The contents of this file are subject to the terms of the
352N/A * Common Development and Distribution License (the "License").
352N/A * You may not use this file except in compliance with the License.
352N/A *
352N/A * See LICENSE.txt included in this distribution for the specific
352N/A * language governing permissions and limitations under the License.
352N/A *
352N/A * When distributing Covered Code, include this CDDL HEADER in each
352N/A * file and include the License file at LICENSE.txt.
352N/A * If applicable, add the following below this CDDL HEADER, with the
352N/A * fields enclosed by brackets "[]" replaced with your own identifying
352N/A * information: Portions Copyright [yyyy] [name of copyright owner]
352N/A *
352N/A * CDDL HEADER END
352N/A */
352N/A
352N/A/*
352N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
352N/A * Use is subject to license terms.
352N/A */
352N/Apackage org.opensolaris.opengrok.index;
352N/A
352N/Aimport java.io.File;
352N/Aimport java.io.FileOutputStream;
352N/Aimport java.io.IOException;
352N/Aimport java.io.InputStream;
352N/Aimport java.io.OutputStream;
352N/Aimport java.util.Enumeration;
352N/Aimport java.util.zip.ZipEntry;
352N/Aimport java.util.zip.ZipFile;
352N/Aimport org.junit.After;
352N/Aimport org.junit.AfterClass;
352N/Aimport org.junit.Before;
352N/Aimport org.junit.BeforeClass;
352N/Aimport org.junit.Test;
352N/Aimport static org.junit.Assert.*;
352N/Aimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
352N/A
352N/A/**
352N/A *
352N/A * @author Trond Norbye
352N/A */
352N/Apublic class IndexerTest {
352N/A
352N/A private File sourceRoot;
352N/A private File dataRoot;
352N/A
352N/A public IndexerTest() {
352N/A }
352N/A
352N/A @BeforeClass
352N/A public static void setUpClass() throws Exception {
352N/A }
352N/A
352N/A @AfterClass
352N/A public static void tearDownClass() throws Exception {
352N/A }
352N/A
352N/A @Before
352N/A public void setUp() {
352N/A File sourceBundle = null;
352N/A try {
352N/A sourceRoot = File.createTempFile("source", "opengrok");
352N/A dataRoot = File.createTempFile("data", "opengrok");
352N/A sourceBundle = File.createTempFile("srcbundle", ".zip");
352N/A
352N/A if (sourceRoot.exists()) {
352N/A assertTrue(sourceRoot.delete());
352N/A }
352N/A
352N/A if (dataRoot.exists()) {
352N/A assertTrue(dataRoot.delete());
352N/A }
352N/A
352N/A if (sourceBundle.exists()) {
352N/A assertTrue(sourceBundle.delete());
352N/A }
352N/A
352N/A assertTrue(sourceRoot.mkdirs());
352N/A assertTrue(dataRoot.mkdirs());
352N/A
352N/A // unzip source-root
352N/A InputStream in = IndexerTest.class.getResourceAsStream("source.zip");
352N/A assertNotNull(in);
352N/A FileOutputStream out = new FileOutputStream(sourceBundle);
352N/A copyFile(in, out);
352N/A out.close();
352N/A extractArchive(sourceBundle);
352N/A } catch (IOException ex) {
352N/A fail("Failed to up the test-file");
352N/A } finally {
352N/A if (sourceBundle != null) {
352N/A sourceBundle.delete();
352N/A }
352N/A }
352N/A }
352N/A
352N/A @After
352N/A public void tearDown() {
352N/A if (sourceRoot != null) {
352N/A removeDirs(sourceRoot);
352N/A }
352N/A if (dataRoot != null) {
352N/A removeDirs(dataRoot);
352N/A }
352N/A }
352N/A
352N/A /**
352N/A * Test of doIndexerExecution method, of class Indexer.
352N/A */
352N/A @Test
352N/A public void testIndexGeneration() throws Exception {
365N/A System.out.println("Generating index by using the class methods");
352N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
352N/A env.setCtags(System.getProperty("org.opensolaris.opengrok.configuration.ctags", "ctags"));
352N/A if (env.validateExuberantCtags()) {
352N/A env.setSourceRootFile(sourceRoot);
352N/A env.setDataRoot(dataRoot);
352N/A env.setVerbose(true);
352N/A Indexer.getInstance().prepareIndexer(env, true, true, "/c", null, false, false, false, null, null);
352N/A Indexer.getInstance().doIndexerExecution(true, 1, null, null);
352N/A } else {
352N/A System.out.println("Skipping test. Could not find a ctags I could use in path.");
352N/A }
352N/A }
352N/A
365N/A /**
365N/A * Test of doIndexerExecution method, of class Indexer.
365N/A */
365N/A @Test
365N/A public void testMain() throws IOException {
365N/A System.out.println("Generate index by using command line options");
365N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
365N/A env.setCtags(System.getProperty("org.opensolaris.opengrok.configuration.ctags", "ctags"));
365N/A if (env.validateExuberantCtags()) {
365N/A String[] argv = { "-S", "-P", "-p", "/c", "-H", "-Q", "off", "-s", sourceRoot.getAbsolutePath(), "-d", dataRoot.getAbsolutePath(), "-v"};
365N/A Indexer.main(argv);
365N/A } else {
365N/A System.out.println("Skipping test. Could not find a ctags I could use in path.");
365N/A }
365N/A }
365N/A
352N/A private void extractArchive(File sourceBundle) throws IOException {
352N/A ZipFile zipfile = new ZipFile(sourceBundle);
352N/A
352N/A Enumeration<? extends ZipEntry> e = zipfile.entries();
352N/A
352N/A while (e.hasMoreElements()) {
352N/A ZipEntry ze = e.nextElement();
352N/A File file = new File(sourceRoot, ze.getName());
352N/A if (ze.isDirectory()) {
352N/A file.mkdirs();
352N/A } else {
352N/A InputStream in = zipfile.getInputStream(ze);
352N/A assertNotNull(in);
352N/A FileOutputStream out = new FileOutputStream(file);
352N/A assertNotNull(out);
352N/A copyFile(in, out);
352N/A }
352N/A }
352N/A }
352N/A
352N/A private void removeDirs(File root) {
352N/A for (File f : root.listFiles()) {
352N/A if (f.isDirectory()) {
352N/A removeDirs(f);
352N/A } else {
352N/A f.delete();
352N/A }
352N/A }
352N/A root.delete();
352N/A }
352N/A
352N/A private void copyFile(InputStream in, OutputStream out) throws IOException {
352N/A byte[] array = new byte[8192];
352N/A int nr;
352N/A
352N/A while ((nr = in.read(array)) > 0) {
352N/A out.write(array, 0, nr);
352N/A }
352N/A out.flush();
352N/A }
352N/A}