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