RuntimeEnvironmentTest.java revision 1461
/*
* 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.getInstance().writeConfiguration(orgiginalConfig);
// Create a default configuration
Configuration config = new Configuration();
RuntimeEnvironment.getInstance().setConfiguration(config);
}
@SuppressWarnings("javadoc")
@AfterClass
public static void tearDownClass() throws Exception {
// restore the configuration
RuntimeEnvironment.getInstance().readConfiguration(orgiginalConfig);
RuntimeEnvironment.getInstance().register();
orgiginalConfig.delete();
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testDataRoot() throws IOException {
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
assertNull(instance.getDataRootFile());
assertNull(instance.getDataRootPath());
File f = File.createTempFile("dataroot", null);
String path = f.getCanonicalPath();
assertTrue(f.delete());
assertFalse(f.exists());
instance.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, instance.getDataRootPath());
assertEquals(path, instance.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() {
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
assertNull("" + instance.getSourceRootFile(), instance.getSourceRootFile());
assertNull(instance.getSourceRootPath(), instance.getSourceRootPath());
assertTrue(f.delete());
instance.setSourceRoot(path);
assertEquals(path, instance.getSourceRootPath());
try {
assertEquals(path, instance.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() {
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
assertFalse(instance.hasProjects());
assertNotNull(instance.getProjects());
assertEquals(0, instance.getProjects().size());
assertNull(instance.getDefaultProject());
File file = new File("/opengrok_automatic_test/foo/bar");
instance.setSourceRoot("/opengrok_automatic_test/foo");
Project p = new Project();
p.setPath("/bar");
assertEquals("/bar", p.getId());
instance.getProjects().add(p);
assertEquals(p, Project.getProject(file));
instance.setProjects(null);
assertNull(instance.getProjects());
}
};
Thread thread = new Thread(runnable);
thread.start();
thread.join(1000);
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testRegister() throws InterruptedException, IOException {
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
String path = "/tmp/dataroot";
instance.setDataRoot(path);
instance.register();
Thread t = new Thread(new Runnable() {
@Override
public void run() {
Configuration c = new Configuration();
RuntimeEnvironment.getInstance().setConfiguration(c);
}
});
t.start();
t.join(1000);
assertEquals(new File(path).getCanonicalPath(), instance.getDataRootPath());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testUrlPrefix() {
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
assertEquals("/source" + Prefix.SEARCH_R + '?', instance.getUrlPrefix());
String prefix = "/opengrok" + Prefix.SEARCH_R + '?';
instance.setUrlPrefix(prefix);
assertEquals(prefix, instance.getUrlPrefix());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testCtags() {
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
assertEquals("ctags", instance.getCtags());
String path = "/usr/bin/ctags";
instance.setCtags(path);
assertEquals(path, instance.getCtags());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testHistoryReaderTimeLimit() {
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
assertEquals(30, instance.getHistoryReaderTimeLimit());
instance.setHistoryReaderTimeLimit(50);
assertEquals(50, instance.getHistoryReaderTimeLimit());
}
@SuppressWarnings({ "javadoc", "static-method" })
@Test
public void testUseHistoryCache() {
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
assertTrue(instance.useHistoryCache());
instance.setUseHistoryCache(false);
assertFalse(instance.useHistoryCache());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testStoreHistoryCacheInDB() {
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
assertFalse(env.storeHistoryCacheInDB());
env.setStoreHistoryCacheInDB(true);
assertTrue(env.storeHistoryCacheInDB());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testGenerateHtml() {
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
assertTrue(instance.isGenerateHtml());
instance.setGenerateHtml(false);
assertFalse(instance.isGenerateHtml());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testCompressXref() {
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
assertTrue(instance.isCompressXref());
instance.setCompressXref(false);
assertFalse(instance.isCompressXref());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testQuickContextScan() {
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
assertTrue(instance.isQuickContextScan());
instance.setQuickContextScan(false);
assertFalse(instance.isQuickContextScan());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testRepositories() {
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
assertNotNull(instance.getRepositories());
instance.setRepositories(null);
assertNull(instance.getRepositories());
List<RepositoryInfo> reps = new ArrayList<RepositoryInfo>();
instance.setRepositories(reps);
assertSame(reps, instance.getRepositories());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testIndexWordLimit() {
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
//default is unlimited
assertEquals(Integer.MAX_VALUE, instance.getIndexWordLimit());
instance.setIndexWordLimit(100000);
assertEquals(100000, instance.getIndexWordLimit());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testVerbose() {
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
assertFalse(instance.isVerbose());
instance.setVerbose(true);
assertTrue(instance.isVerbose());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testAllowLeadingWildcard() {
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
assertFalse(instance.isAllowLeadingWildcard());
instance.setAllowLeadingWildcard(true);
assertTrue(instance.isAllowLeadingWildcard());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testIgnoredNames() {
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
assertNotNull(instance.getIgnoredNames());
instance.setIgnoredNames(null);
assertNotNull(instance.getIgnoredNames());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testUserPage() {
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
String page = "http://www.opensolaris.org/viewProfile.jspa?username=";
assertEquals(page, instance.getUserPage());
instance.setUserPage(page.substring(5));
assertEquals(page.substring(5), instance.getUserPage());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testBugPage() {
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
String page = "http://bugs.opensolaris.org/bugdatabase/view_bug.do?bug_id=";
assertEquals(page, instance.getBugPage());
instance.setBugPage(page.substring(5));
assertEquals(page.substring(5), instance.getBugPage());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testBugPattern() {
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
String page = "\\b([12456789][0-9]{6})\\b";
assertEquals(page, instance.getBugPattern());
instance.setBugPattern(page.substring(5));
assertEquals(page.substring(5), instance.getBugPattern());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testReviewPage() {
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
String page = "http://arc.opensolaris.org/caselog/PSARC/";
assertEquals(page, instance.getReviewPage());
instance.setReviewPage(page.substring(5));
assertEquals(page.substring(5), instance.getReviewPage());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testReviewPattern() {
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
String page = "\\b(\\d{4}/\\d{3})\\b";
assertEquals(page, instance.getReviewPattern());
instance.setReviewPattern(page.substring(5));
assertEquals(page.substring(5), instance.getReviewPattern());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testWebappLAF() {
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
assertEquals(Configuration.DEFAULT_STYLE, instance.getWebappLAF());
instance.setWebappLAF("foo");
assertEquals("foo", instance.getWebappLAF());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testRemoteScmSupported() {
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
assertFalse(instance.isRemoteScmSupported());
instance.setRemoteScmSupported(true);
assertTrue(instance.isRemoteScmSupported());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testOptimizeDatabase() {
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
assertTrue(instance.isOptimizeDatabase());
instance.setOptimizeDatabase(false);
assertFalse(instance.isOptimizeDatabase());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testUsingLuceneLocking() {
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
assertFalse(instance.isUsingLuceneLocking());
instance.setUsingLuceneLocking(true);
assertTrue(instance.isUsingLuceneLocking());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testIndexVersionedFilesOnly() {
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
assertFalse(instance.isIndexVersionedFilesOnly());
instance.setIndexVersionedFilesOnly(true);
assertTrue(instance.isIndexVersionedFilesOnly());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testConfigListenerThread() throws IOException {
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
SocketAddress addr = new InetSocketAddress(0);
assertTrue(instance.startConfigurationListenerThread(addr));
try {
Thread.sleep(1000);
} catch (InterruptedException exp) {
// do nothing
}
instance.writeConfiguration();
instance.stopConfigurationListenerThread();
}
@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 {
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
File file = new File("foobar");
assertTrue(file.createNewFile());
assertFalse(file.isAbsolute());
instance.setDataRoot(file.getName());
File f = instance.getDataRootFile();
assertNotNull(f);
assertEquals("foobar", f.getName());
assertTrue(f.isAbsolute());
assertTrue(file.delete());
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void testBug3154() throws IOException {
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
File file = File.createTempFile("dataroot", null);
assertTrue(file.delete());
assertFalse(file.exists());
instance.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 {
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
// By default, don't obfuscate.
assertObfuscated(Boolean.FALSE, env);
env.setObfuscatingEMailAddresses(true);
assertObfuscated(Boolean.TRUE, env);
env.setObfuscatingEMailAddresses(false);
assertObfuscated(Boolean.FALSE, env);
}
@SuppressWarnings("boxing")
private static void assertObfuscated(Boolean expected, RuntimeEnvironment env)
throws IOException
{
assertEquals(expected, env.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() {
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
// By default, status page should not be chatty.
assertFalse(env.isChattyStatusPage());
env.setChattyStatusPage(true);
assertTrue(env.isChattyStatusPage());
env.setChattyStatusPage(false);
assertFalse(env.isChattyStatusPage());
}
}