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