RuntimeEnvironmentTest.java revision 1470
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* See LICENSE.txt included in this distribution for the specific
* language governing permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at LICENSE.txt.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
*/
package org.opensolaris.opengrok.configuration;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.List;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.opensolaris.opengrok.analysis.JFlexXrefTest;
import org.opensolaris.opengrok.analysis.XrefWriter;
import org.opensolaris.opengrok.analysis.plain.PlainXref;
import org.opensolaris.opengrok.history.RepositoryInfo;
import org.opensolaris.opengrok.web.Prefix;
/**
* Test the RuntimeEnvironment class
*
* @author Trond Norbye
*/
public class RuntimeEnvironmentTest {
private static File orgiginalConfig;
@SuppressWarnings("javadoc")
@BeforeClass
public static void setUpClass() throws Exception {
// preserve the original
orgiginalConfig = File.createTempFile("config", ".xml");
RuntimeEnvironment.writeConfig(orgiginalConfig);
// Create a default configuration
Configuration config = new Configuration();
RuntimeEnvironment.setConfig(config);
}
@SuppressWarnings("javadoc")
@AfterClass
public static void tearDownClass() throws Exception {
// restore the configuration
RuntimeEnvironment.readConfig(orgiginalConfig);
RuntimeEnvironment.register();
orgiginalConfig.delete();
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testDataRoot() throws IOException {
Configuration cfg = RuntimeEnvironment.getConfig();
assertNull(cfg.getDataRootFile());
assertNull(cfg.getDataRoot());
File f = File.createTempFile("dataroot", null);
String path = f.getCanonicalPath();
assertTrue(f.delete());
assertFalse(f.exists());
cfg.setDataRoot(path);
// setDataRoot() used to create path if it didn't exist, but that
// logic has been moved. Verify that it is so.
assertFalse(f.exists());
assertTrue(f.mkdirs());
assertEquals(path, cfg.getDataRoot());
assertEquals(path, cfg.getDataRootFile().getCanonicalPath());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testSourceRoot() throws IOException, InterruptedException {
final File f = File.createTempFile("sourceroot", null);
final String path = f.getCanonicalPath();
Runnable runnable = new Runnable() {
@Override
public void run() {
Configuration cfg = RuntimeEnvironment.getConfig();
assertNull("" + cfg.getSourceRootFile(), cfg.getSourceRootFile());
assertNull(cfg.getSourceRoot(), cfg.getSourceRoot());
assertTrue(f.delete());
cfg.setSourceRoot(path);
assertEquals(path, cfg.getSourceRoot());
try {
assertEquals(path, cfg.getSourceRootFile().getCanonicalPath());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
};
Thread thread = new Thread(runnable);
thread.start();
thread.join(1000);
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testProjects() throws InterruptedException {
// actually all tests should use this pattern to make sure to really
// have a virgin RE
Runnable runnable = new Runnable() {
@Override
public void run() {
Configuration cfg = RuntimeEnvironment.getConfig();
assertFalse(cfg.hasProjects());
assertNotNull(cfg.getProjects());
assertEquals(0, cfg.getProjects().size());
assertNull(cfg.getDefaultProject());
File file = new File("/opengrok_automatic_test/foo/bar");
cfg.setSourceRoot("/opengrok_automatic_test/foo");
Project p = new Project();
p.setPath("/bar");
assertEquals("/bar", p.getId());
cfg.getProjects().add(p);
assertEquals(p, Project.getProject(file));
cfg.setProjects(null);
assertNull(cfg.getProjects());
}
};
Thread thread = new Thread(runnable);
thread.start();
thread.join(1000);
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testRegister() throws InterruptedException, IOException {
Configuration cfg = RuntimeEnvironment.getConfig();
String path = "/tmp/dataroot";
cfg.setDataRoot(path);
RuntimeEnvironment.register();
Thread t = new Thread(new Runnable() {
@Override
public void run() {
Configuration c = new Configuration();
RuntimeEnvironment.setConfig(c);
}
});
t.start();
t.join(1000);
assertEquals(new File(path).getCanonicalPath(), cfg.getDataRoot());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testUrlPrefix() {
Configuration cfg = RuntimeEnvironment.getConfig();
assertEquals("/source" + Prefix.SEARCH_R + '?', cfg.getUrlPrefix());
String prefix = "/opengrok" + Prefix.SEARCH_R + '?';
cfg.setUrlPrefix(prefix);
assertEquals(prefix, cfg.getUrlPrefix());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testCtags() {
Configuration cfg = RuntimeEnvironment.getConfig();
assertEquals("ctags", cfg.getCtags());
String path = "/usr/bin/ctags";
cfg.setCtags(path);
assertEquals(path, cfg.getCtags());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testHistoryReaderTimeLimit() {
Configuration cfg = RuntimeEnvironment.getConfig();
assertEquals(30, cfg.getHistoryCacheTime());
cfg.setHistoryCacheTime(50);
assertEquals(50, cfg.getHistoryCacheTime());
}
@SuppressWarnings({ "javadoc", "static-method" })
@Test
public void testUseHistoryCache() {
Configuration cfg = RuntimeEnvironment.getConfig();
assertTrue(cfg.isHistoryCache());
cfg.setHistoryCache(false);
assertFalse(cfg.isHistoryCache());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testStoreHistoryCacheInDB() {
Configuration cfg = RuntimeEnvironment.getConfig();
assertFalse(cfg.isHistoryCacheInDB());
cfg.setHistoryCacheInDB(true);
assertTrue(cfg.isHistoryCacheInDB());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testGenerateHtml() {
Configuration cfg = RuntimeEnvironment.getConfig();
assertTrue(cfg.isGenerateHtml());
cfg.setGenerateHtml(false);
assertFalse(cfg.isGenerateHtml());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testCompressXref() {
Configuration cfg = RuntimeEnvironment.getConfig();
assertTrue(cfg.isCompressXref());
cfg.setCompressXref(false);
assertFalse(cfg.isCompressXref());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testQuickContextScan() {
Configuration cfg = RuntimeEnvironment.getConfig();
assertTrue(cfg.isQuickContextScan());
cfg.setQuickContextScan(false);
assertFalse(cfg.isQuickContextScan());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testRepositories() {
Configuration cfg = RuntimeEnvironment.getConfig();
assertNotNull(cfg.getRepositories());
cfg.setRepositories(null);
assertNull(cfg.getRepositories());
List<RepositoryInfo> reps = new ArrayList<RepositoryInfo>();
cfg.setRepositories(reps);
assertSame(reps, cfg.getRepositories());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testIndexWordLimit() {
Configuration cfg = RuntimeEnvironment.getConfig();
//default is unlimited
assertEquals(Integer.MAX_VALUE, cfg.getIndexWordLimit());
cfg.setIndexWordLimit(100000);
assertEquals(100000, cfg.getIndexWordLimit());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testVerbose() {
Configuration cfg = RuntimeEnvironment.getConfig();
assertFalse(cfg.isVerbose());
cfg.setVerbose(true);
assertTrue(cfg.isVerbose());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testAllowLeadingWildcard() {
Configuration cfg = RuntimeEnvironment.getConfig();
assertFalse(cfg.isAllowLeadingWildcard());
cfg.setAllowLeadingWildcard(true);
assertTrue(cfg.isAllowLeadingWildcard());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testIgnoredNames() {
Configuration cfg = RuntimeEnvironment.getConfig();
assertNotNull(cfg.getIgnoredNames());
cfg.setIgnoredNames(null);
assertNotNull(cfg.getIgnoredNames());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testUserPage() {
Configuration cfg = RuntimeEnvironment.getConfig();
String page = "http://www.opensolaris.org/viewProfile.jspa?username=";
assertEquals(page, cfg.getUserPage());
cfg.setUserPage(page.substring(5));
assertEquals(page.substring(5), cfg.getUserPage());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testBugPage() {
Configuration cfg = RuntimeEnvironment.getConfig();
String page = "http://bugs.opensolaris.org/bugdatabase/view_bug.do?bug_id=";
assertEquals(page, cfg.getBugPage());
cfg.setBugPage(page.substring(5));
assertEquals(page.substring(5), cfg.getBugPage());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testBugPattern() {
Configuration cfg = RuntimeEnvironment.getConfig();
String page = "\\b([12456789][0-9]{6})\\b";
assertEquals(page, cfg.getBugPattern());
cfg.setBugPattern(page.substring(5));
assertEquals(page.substring(5), cfg.getBugPattern());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testReviewPage() {
Configuration cfg = RuntimeEnvironment.getConfig();
String page = "http://arc.opensolaris.org/caselog/PSARC/";
assertEquals(page, cfg.getReviewPage());
cfg.setReviewPage(page.substring(5));
assertEquals(page.substring(5), cfg.getReviewPage());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testReviewPattern() {
Configuration cfg = RuntimeEnvironment.getConfig();
String page = "\\b(\\d{4}/\\d{3})\\b";
assertEquals(page, cfg.getReviewPattern());
cfg.setReviewPattern(page.substring(5));
assertEquals(page.substring(5), cfg.getReviewPattern());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testWebappLAF() {
Configuration cfg = RuntimeEnvironment.getConfig();
assertEquals(Configuration.DEFAULT_STYLE, cfg.getWebappLAF());
cfg.setWebappLAF("foo");
assertEquals("foo", cfg.getWebappLAF());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testRemoteScmSupported() {
Configuration cfg = RuntimeEnvironment.getConfig();
assertFalse(cfg.isRemoteScmSupported());
cfg.setRemoteScmSupported(true);
assertTrue(cfg.isRemoteScmSupported());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testOptimizeDatabase() {
Configuration cfg = RuntimeEnvironment.getConfig();
assertTrue(cfg.isOptimizeDatabase());
cfg.setOptimizeDatabase(false);
assertFalse(cfg.isOptimizeDatabase());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testUsingLuceneLocking() {
Configuration cfg = RuntimeEnvironment.getConfig();
assertFalse(cfg.isUsingLuceneLocking());
cfg.setUsingLuceneLocking(true);
assertTrue(cfg.isUsingLuceneLocking());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testIndexVersionedFilesOnly() {
Configuration cfg = RuntimeEnvironment.getConfig();
assertFalse(cfg.isIndexVersionedFilesOnly());
cfg.setIndexVersionedFilesOnly(true);
assertTrue(cfg.isIndexVersionedFilesOnly());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testConfigListenerThread() throws IOException {
SocketAddress addr = new InetSocketAddress(0);
assertTrue(RuntimeEnvironment.startConfigListenerThread(addr));
try {
Thread.sleep(1000);
} catch (InterruptedException exp) {
// do nothing
}
RuntimeEnvironment.getInstance().writeConfig();
RuntimeEnvironment.stopConfigListenerThread();
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test(expected=ArrayIndexOutOfBoundsException.class)
public void testXMLencdec() throws IOException {
Configuration c = new Configuration();
String m = c.getXMLRepresentationAsString();
Configuration o = Configuration.makeXMLStringAsConfiguration(m);
assertNotNull(o);
m = m.replace('a', 'm');
o = Configuration.makeXMLStringAsConfiguration(m);
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testBug3095() throws IOException {
Configuration cfg = RuntimeEnvironment.getConfig();
File file = new File("foobar");
assertTrue(file.createNewFile());
assertFalse(file.isAbsolute());
cfg.setDataRoot(file.getName());
File f = cfg.getDataRootFile();
assertNotNull(f);
assertEquals("foobar", f.getName());
assertTrue(f.isAbsolute());
assertTrue(file.delete());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testBug3154() throws IOException {
Configuration cfg = RuntimeEnvironment.getConfig();
File file = File.createTempFile("dataroot", null);
assertTrue(file.delete());
assertFalse(file.exists());
cfg.setDataRoot(file.getAbsolutePath());
// The point of this test was to verify that setDataRoot() created
// the directory, but that logic has been moved as of bug 16986, so
// expect that the file does not exist.
assertFalse(file.exists());
}
@SuppressWarnings({ "javadoc", "static-method" })
@Test
public void testObfuscateEMail() throws IOException {
Configuration cfg = RuntimeEnvironment.getConfig();
// By default, don't obfuscate.
assertObfuscated(Boolean.FALSE, cfg);
cfg.setObfuscatingEMailAddresses(true);
assertObfuscated(Boolean.TRUE, cfg);
cfg.setObfuscatingEMailAddresses(false);
assertObfuscated(Boolean.FALSE, cfg);
}
@SuppressWarnings("boxing")
private static void assertObfuscated(Boolean expected, Configuration cfg)
throws IOException
{
assertEquals(expected, cfg.isObfuscatingEMailAddresses());
String address = "opengrok-discuss@opensolaris.org";
PlainXref xref = new PlainXref(new StringReader(address));
StringWriter out = new StringWriter();
XrefWriter w = new XrefWriter(out);
xref.write(w);
w.close();
String expectedAddress = expected.booleanValue()
? address.replace("@", " (at) ")
: address;
String expectedOutput = JFlexXrefTest.FIRST_LINE_PREAMBLE
+ expectedAddress + JFlexXrefTest.LINE_SUFFIX + JFlexXrefTest.EOS;
assertEquals(expectedOutput, out.toString());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void isChattyStatusPage() {
Configuration cfg = RuntimeEnvironment.getConfig();
// By default, status page should not be chatty.
assertFalse(cfg.isChattyStatusPage());
cfg.setChattyStatusPage(true);
assertTrue(cfg.isChattyStatusPage());
cfg.setChattyStatusPage(false);
assertFalse(cfg.isChattyStatusPage());
}
}