RuntimeEnvironmentTest.java revision 1253
431N/A/*
431N/A * CDDL HEADER START
431N/A *
431N/A * The contents of this file are subject to the terms of the
431N/A * Common Development and Distribution License (the "License").
431N/A * You may not use this file except in compliance with the License.
431N/A *
431N/A * See LICENSE.txt included in this distribution for the specific
431N/A * language governing permissions and limitations under the License.
431N/A *
431N/A * When distributing Covered Code, include this CDDL HEADER in each
431N/A * file and include the License file at LICENSE.txt.
431N/A * If applicable, add the following below this CDDL HEADER, with the
431N/A * fields enclosed by brackets "[]" replaced with your own identifying
431N/A * information: Portions Copyright [yyyy] [name of copyright owner]
431N/A *
431N/A * CDDL HEADER END
431N/A */
431N/A
431N/A/*
1253N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
431N/A */
431N/Apackage org.opensolaris.opengrok.configuration;
431N/A
431N/Aimport java.io.File;
431N/Aimport java.io.IOException;
1123N/Aimport java.io.StringReader;
1123N/Aimport java.io.StringWriter;
471N/Aimport java.net.InetSocketAddress;
471N/Aimport java.net.SocketAddress;
693N/Aimport java.util.ArrayList;
693N/Aimport java.util.List;
431N/Aimport org.junit.After;
431N/Aimport org.junit.AfterClass;
431N/Aimport org.junit.Before;
431N/Aimport org.junit.BeforeClass;
431N/Aimport org.junit.Test;
1123N/Aimport org.opensolaris.opengrok.analysis.plain.PlainXref;
693N/Aimport org.opensolaris.opengrok.history.RepositoryInfo;
431N/Aimport static org.junit.Assert.*;
431N/A
431N/A/**
431N/A * Test the RuntimeEnvironment class
431N/A *
431N/A * @author Trond Norbye
431N/A */
431N/Apublic class RuntimeEnvironmentTest {
431N/A private static File orgiginalConfig;
431N/A
431N/A public RuntimeEnvironmentTest() {
431N/A }
431N/A
431N/A @BeforeClass
431N/A public static void setUpClass() throws Exception {
431N/A // preserve the original
431N/A orgiginalConfig = File.createTempFile("config", ".xml");
431N/A RuntimeEnvironment.getInstance().writeConfiguration(orgiginalConfig);
431N/A
431N/A // Create a default configuration
431N/A Configuration config = new Configuration();
431N/A RuntimeEnvironment.getInstance().setConfiguration(config);
431N/A }
431N/A
431N/A @AfterClass
431N/A public static void tearDownClass() throws Exception {
431N/A // restore the configuration
431N/A RuntimeEnvironment.getInstance().readConfiguration(orgiginalConfig);
431N/A RuntimeEnvironment.getInstance().register();
431N/A orgiginalConfig.delete();
431N/A }
431N/A
431N/A @Before
431N/A public void setUp() {
431N/A }
431N/A
431N/A @After
431N/A public void tearDown() {
431N/A }
431N/A
431N/A @Test
431N/A public void testDataRoot() throws IOException {
431N/A RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
431N/A assertNull(instance.getDataRootFile());
431N/A assertNull(instance.getDataRootPath());
431N/A File f = File.createTempFile("dataroot", null);
431N/A String path = f.getCanonicalPath();
431N/A assertTrue(f.delete());
1106N/A assertFalse(f.exists());
431N/A instance.setDataRoot(path);
1106N/A // setDataRoot() used to create path if it didn't exist, but that
1106N/A // logic has been moved. Verify that it is so.
1106N/A assertFalse(f.exists());
1106N/A assertTrue(f.mkdirs());
431N/A assertEquals(path, instance.getDataRootPath());
431N/A assertEquals(path, instance.getDataRootFile().getCanonicalPath());
431N/A }
431N/A
431N/A @Test
431N/A public void testSourceRoot() throws IOException {
431N/A RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
431N/A assertNull(instance.getSourceRootFile());
431N/A assertNull(instance.getSourceRootPath());
431N/A File f = File.createTempFile("sourceroot", null);
431N/A String path = f.getCanonicalPath();
431N/A assertTrue(f.delete());
431N/A instance.setSourceRoot(path);
431N/A assertEquals(path, instance.getSourceRootPath());
431N/A assertEquals(path, instance.getSourceRootFile().getCanonicalPath());
431N/A }
431N/A
431N/A @Test
526N/A public void testProjects() throws IOException {
431N/A RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
431N/A assertFalse(instance.hasProjects());
431N/A assertNotNull(instance.getProjects());
526N/A assertEquals(0, instance.getProjects().size());
431N/A assertNull(instance.getDefaultProject());
526N/A
526N/A File file = new File("/opengrok_automatic_test/foo/bar");
526N/A instance.setSourceRoot("/opengrok_automatic_test/foo");
526N/A Project p = new Project();
526N/A p.setPath("/bar");
526N/A assertEquals("/bar", p.getId());
526N/A instance.getProjects().add(p);
526N/A assertEquals(p, Project.getProject(file));
431N/A instance.setProjects(null);
431N/A assertNull(instance.getProjects());
431N/A }
431N/A
431N/A @Test
610N/A public void testRegister() throws InterruptedException, IOException {
431N/A RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
431N/A String path = "/tmp/dataroot";
431N/A instance.setDataRoot(path);
431N/A instance.register();
431N/A Thread t = new Thread(new Runnable() {
431N/A
431N/A public void run() {
431N/A Configuration c = new Configuration();
431N/A RuntimeEnvironment.getInstance().setConfiguration(c);
431N/A
431N/A }
431N/A });
431N/A t.start();
431N/A t.join();
610N/A assertEquals(new File(path).getCanonicalFile().getAbsolutePath(), instance.getDataRootPath());
431N/A }
431N/A
431N/A @Test
431N/A public void testUrlPrefix() {
431N/A RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
431N/A assertEquals("/source/s?", instance.getUrlPrefix());
431N/A String prefix = "/opengrok/s?";
431N/A instance.setUrlPrefix(prefix);
431N/A assertEquals(prefix, instance.getUrlPrefix());
431N/A }
431N/A
431N/A @Test
431N/A public void testCtags() {
431N/A RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
431N/A assertEquals("ctags", instance.getCtags());
431N/A String path = "/usr/bin/ctags";
431N/A instance.setCtags(path);
431N/A assertEquals(path, instance.getCtags());
431N/A }
431N/A
431N/A @Test
431N/A public void testHistoryReaderTimeLimit() {
431N/A RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
431N/A assertEquals(30, instance.getHistoryReaderTimeLimit());
431N/A instance.setHistoryReaderTimeLimit(50);
431N/A assertEquals(50, instance.getHistoryReaderTimeLimit());
431N/A }
431N/A
431N/A @Test
431N/A public void testUseHistoryCache() {
431N/A RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
431N/A assertEquals(true, instance.useHistoryCache());
431N/A instance.setUseHistoryCache(false);
431N/A assertEquals(false, instance.useHistoryCache());
431N/A }
431N/A
431N/A @Test
773N/A public void testStoreHistoryCacheInDB() {
773N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
773N/A assertFalse(env.storeHistoryCacheInDB());
773N/A env.setStoreHistoryCacheInDB(true);
773N/A assertTrue(env.storeHistoryCacheInDB());
773N/A }
773N/A
773N/A @Test
431N/A public void testGenerateHtml() {
431N/A RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
431N/A assertTrue(instance.isGenerateHtml());
431N/A instance.setGenerateHtml(false);
431N/A assertFalse(instance.isGenerateHtml());
431N/A }
431N/A
431N/A @Test
431N/A public void testCompressXref() {
431N/A RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
431N/A assertTrue(instance.isCompressXref());
431N/A instance.setCompressXref(false);
431N/A assertFalse(instance.isCompressXref());
431N/A }
431N/A
431N/A @Test
431N/A public void testQuickContextScan() {
431N/A RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
431N/A assertTrue(instance.isQuickContextScan());
431N/A instance.setQuickContextScan(false);
431N/A assertFalse(instance.isQuickContextScan());
431N/A }
431N/A
431N/A @Test
431N/A public void testRepositories() {
431N/A RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
431N/A assertNotNull(instance.getRepositories());
431N/A instance.setRepositories(null);
431N/A assertNull(instance.getRepositories());
693N/A List<RepositoryInfo> reps = new ArrayList<RepositoryInfo>();
693N/A instance.setRepositories(reps);
693N/A assertSame(reps, instance.getRepositories());
431N/A }
431N/A
431N/A @Test
431N/A public void testIndexWordLimit() {
431N/A RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
871N/A assertEquals(Integer.MAX_VALUE, instance.getIndexWordLimit()); //default is unlimited
431N/A instance.setIndexWordLimit(100000);
431N/A assertEquals(100000, instance.getIndexWordLimit());
431N/A }
431N/A
431N/A @Test
431N/A public void testVerbose() {
431N/A RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
431N/A assertFalse(instance.isVerbose());
431N/A instance.setVerbose(true);
431N/A assertTrue(instance.isVerbose());
431N/A }
431N/A
431N/A @Test
431N/A public void testAllowLeadingWildcard() {
431N/A RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
431N/A assertFalse(instance.isAllowLeadingWildcard());
431N/A instance.setAllowLeadingWildcard(true);
431N/A assertTrue(instance.isAllowLeadingWildcard());
431N/A }
431N/A
431N/A @Test
431N/A public void testIgnoredNames() {
431N/A RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
431N/A assertNotNull(instance.getIgnoredNames());
431N/A instance.setIgnoredNames(null);
431N/A assertNull(instance.getIgnoredNames());
431N/A }
431N/A
431N/A @Test
431N/A public void testUserPage() {
431N/A RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
431N/A String page = "http://www.opensolaris.org/viewProfile.jspa?username=";
431N/A assertEquals(page, instance.getUserPage());
431N/A instance.setUserPage(page.substring(5));
431N/A assertEquals(page.substring(5), instance.getUserPage());
431N/A }
431N/A
431N/A @Test
431N/A public void testBugPage() {
431N/A RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
431N/A String page = "http://bugs.opensolaris.org/bugdatabase/view_bug.do?bug_id=";
431N/A assertEquals(page, instance.getBugPage());
431N/A instance.setBugPage(page.substring(5));
431N/A assertEquals(page.substring(5), instance.getBugPage());
431N/A }
431N/A
431N/A @Test
431N/A public void testBugPattern() {
431N/A RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
431N/A String page = "\\b([12456789][0-9]{6})\\b";
431N/A assertEquals(page, instance.getBugPattern());
431N/A instance.setBugPattern(page.substring(5));
431N/A assertEquals(page.substring(5), instance.getBugPattern());
431N/A }
431N/A
431N/A @Test
431N/A public void testReviewPage() {
431N/A RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
876N/A String page = "http://arc.opensolaris.org/caselog/PSARC/";
431N/A assertEquals(page, instance.getReviewPage());
431N/A instance.setReviewPage(page.substring(5));
431N/A assertEquals(page.substring(5), instance.getReviewPage());
431N/A }
431N/A
431N/A @Test
431N/A public void testReviewPattern() {
431N/A RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
431N/A String page = "\\b(\\d{4}/\\d{3})\\b";
431N/A assertEquals(page, instance.getReviewPattern());
431N/A instance.setReviewPattern(page.substring(5));
431N/A assertEquals(page.substring(5), instance.getReviewPattern());
431N/A }
431N/A
431N/A @Test
431N/A public void testWebappLAF() {
431N/A RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
431N/A assertEquals("default", instance.getWebappLAF());
431N/A instance.setWebappLAF("foo");
431N/A assertEquals("foo", instance.getWebappLAF());
431N/A }
431N/A
431N/A @Test
431N/A public void testRemoteScmSupported() {
431N/A RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
431N/A assertFalse(instance.isRemoteScmSupported());
431N/A instance.setRemoteScmSupported(true);
431N/A assertTrue(instance.isRemoteScmSupported());
431N/A }
431N/A
431N/A @Test
431N/A public void testOptimizeDatabase() {
431N/A RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
431N/A assertTrue(instance.isOptimizeDatabase());
431N/A instance.setOptimizeDatabase(false);
431N/A assertFalse(instance.isOptimizeDatabase());
431N/A }
431N/A
431N/A @Test
431N/A public void testUsingLuceneLocking() {
431N/A RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
431N/A assertFalse(instance.isUsingLuceneLocking());
431N/A instance.setUsingLuceneLocking(true);
431N/A assertTrue(instance.isUsingLuceneLocking());
431N/A }
471N/A
471N/A @Test
480N/A public void testIndexVersionedFilesOnly() {
480N/A RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
480N/A assertFalse(instance.isIndexVersionedFilesOnly());
480N/A instance.setIndexVersionedFilesOnly(true);
480N/A assertTrue(instance.isIndexVersionedFilesOnly());
480N/A }
480N/A
480N/A @Test
471N/A public void testConfigListenerThread() throws IOException {
471N/A RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
471N/A SocketAddress addr = new InetSocketAddress(0);
471N/A assertTrue(instance.startConfigurationListenerThread(addr));
471N/A try {
471N/A Thread.sleep(1000);
471N/A } catch (InterruptedException exp) {
471N/A // do nothing
471N/A }
471N/A instance.writeConfiguration();
471N/A instance.stopConfigurationListenerThread();
471N/A }
490N/A
490N/A @Test
774N/A public void testXMLencdec() throws IOException {
690N/A Configuration c = new Configuration();
690N/A String m = c.getXMLRepresentationAsString();
774N/A Configuration o = Configuration.makeXMLStringAsConfiguration(m);
690N/A assertNotNull(o);
1253N/A m = m.replace('a', 'm');
690N/A try {
690N/A o = Configuration.makeXMLStringAsConfiguration(m);
690N/A fail("makeXmlStringsAsConfiguration should throw exception");
690N/A } catch (Throwable t) {
690N/A }
690N/A }
690N/A
690N/A @Test
490N/A public void testBug3095() throws IOException {
490N/A RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
490N/A File file = new File("foobar");
490N/A assertTrue(file.createNewFile());
490N/A assertFalse(file.isAbsolute());
490N/A instance.setDataRoot(file.getName());
490N/A File f = instance.getDataRootFile();
490N/A assertNotNull(f);
490N/A assertEquals("foobar", f.getName());
490N/A assertTrue(f.isAbsolute());
490N/A assertTrue(file.delete());
490N/A }
492N/A
492N/A @Test
492N/A public void testBug3154() throws IOException {
492N/A RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
492N/A File file = File.createTempFile("dataroot", null);
492N/A assertTrue(file.delete());
492N/A assertFalse(file.exists());
492N/A instance.setDataRoot(file.getAbsolutePath());
1106N/A // The point of this test was to verify that setDataRoot() created
1106N/A // the directory, but that logic has been moved as of bug 16986, so
1106N/A // expect that the file does not exist.
1106N/A assertFalse(file.exists());
492N/A }
1123N/A
1123N/A @Test
1123N/A public void testObfuscateEMail() throws IOException {
1123N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
1123N/A
1123N/A // By default, don't obfuscate.
1123N/A assertObfuscated(false, env);
1123N/A
1123N/A env.setObfuscatingEMailAddresses(true);
1123N/A assertObfuscated(true, env);
1123N/A
1123N/A env.setObfuscatingEMailAddresses(false);
1123N/A assertObfuscated(false, env);
1123N/A }
1123N/A
1123N/A private void assertObfuscated(boolean expected, RuntimeEnvironment env)
1123N/A throws IOException {
1123N/A assertEquals(expected, env.isObfuscatingEMailAddresses());
1123N/A
1123N/A String address = "opengrok-discuss@opensolaris.org";
1123N/A
1123N/A PlainXref xref = new PlainXref(new StringReader(address));
1123N/A StringWriter out = new StringWriter();
1123N/A xref.write(out);
1123N/A
1123N/A String expectedAddress = expected ?
1123N/A address.replace("@", " (at) ") : address;
1123N/A
1123N/A String expectedOutput =
1185N/A "<a class=\"l\" name=\"1\" href=\"#1\">1</a>"
1123N/A + expectedAddress;
1123N/A
1123N/A assertEquals(expectedOutput, out.toString());
1123N/A }
1125N/A
1125N/A @Test
1125N/A public void isChattyStatusPage() {
1125N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
1125N/A
1125N/A // By default, status page should not be chatty.
1125N/A assertFalse(env.isChattyStatusPage());
1125N/A
1125N/A env.setChattyStatusPage(true);
1125N/A assertTrue(env.isChattyStatusPage());
1125N/A
1125N/A env.setChattyStatusPage(false);
1125N/A assertFalse(env.isChattyStatusPage());
1125N/A }
490N/A}