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
1384N/Aimport static org.junit.Assert.assertEquals;
1384N/Aimport static org.junit.Assert.assertFalse;
1384N/Aimport static org.junit.Assert.assertNotNull;
1384N/Aimport static org.junit.Assert.assertNull;
1384N/Aimport static org.junit.Assert.assertSame;
1384N/Aimport static org.junit.Assert.assertTrue;
1384N/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;
1384N/A
431N/Aimport org.junit.AfterClass;
431N/Aimport org.junit.BeforeClass;
431N/Aimport org.junit.Test;
1386N/Aimport org.opensolaris.opengrok.analysis.JFlexXrefTest;
1384N/Aimport org.opensolaris.opengrok.analysis.XrefWriter;
1123N/Aimport org.opensolaris.opengrok.analysis.plain.PlainXref;
693N/Aimport org.opensolaris.opengrok.history.RepositoryInfo;
1419N/Aimport org.opensolaris.opengrok.web.Prefix;
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
1461N/A @SuppressWarnings("javadoc")
431N/A @BeforeClass
431N/A public static void setUpClass() throws Exception {
431N/A // preserve the original
431N/A orgiginalConfig = File.createTempFile("config", ".xml");
1470N/A RuntimeEnvironment.writeConfig(orgiginalConfig);
431N/A
431N/A // Create a default configuration
431N/A Configuration config = new Configuration();
1470N/A RuntimeEnvironment.setConfig(config);
431N/A }
431N/A
1461N/A @SuppressWarnings("javadoc")
431N/A @AfterClass
431N/A public static void tearDownClass() throws Exception {
431N/A // restore the configuration
1470N/A RuntimeEnvironment.readConfig(orgiginalConfig);
1470N/A RuntimeEnvironment.register();
431N/A orgiginalConfig.delete();
431N/A }
431N/A
1461N/A @SuppressWarnings({ "static-method", "javadoc" })
431N/A @Test
431N/A public void testDataRoot() throws IOException {
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
1470N/A assertNull(cfg.getDataRootFile());
1470N/A assertNull(cfg.getDataRoot());
431N/A File f = File.createTempFile("dataroot", null);
431N/A String path = f.getCanonicalPath();
431N/A assertTrue(f.delete());
1106N/A assertFalse(f.exists());
1470N/A cfg.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());
1470N/A assertEquals(path, cfg.getDataRoot());
1470N/A assertEquals(path, cfg.getDataRootFile().getCanonicalPath());
431N/A }
431N/A
1461N/A @SuppressWarnings({ "static-method", "javadoc" })
431N/A @Test
1461N/A public void testSourceRoot() throws IOException, InterruptedException {
1461N/A final File f = File.createTempFile("sourceroot", null);
1461N/A final String path = f.getCanonicalPath();
1461N/A Runnable runnable = new Runnable() {
1461N/A @Override
1461N/A public void run() {
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
1470N/A assertNull("" + cfg.getSourceRootFile(), cfg.getSourceRootFile());
1470N/A assertNull(cfg.getSourceRoot(), cfg.getSourceRoot());
1461N/A assertTrue(f.delete());
1470N/A cfg.setSourceRoot(path);
1470N/A assertEquals(path, cfg.getSourceRoot());
1461N/A try {
1470N/A assertEquals(path, cfg.getSourceRootFile().getCanonicalPath());
1461N/A } catch (IOException e) {
1461N/A throw new RuntimeException(e);
1461N/A }
1461N/A }
1461N/A };
1461N/A Thread thread = new Thread(runnable);
1461N/A thread.start();
1461N/A thread.join(1000);
431N/A }
431N/A
1461N/A @SuppressWarnings({ "static-method", "javadoc" })
431N/A @Test
1461N/A public void testProjects() throws InterruptedException {
1461N/A // actually all tests should use this pattern to make sure to really
1461N/A // have a virgin RE
1461N/A Runnable runnable = new Runnable() {
1461N/A @Override
1461N/A public void run() {
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
1470N/A assertFalse(cfg.hasProjects());
1470N/A assertNotNull(cfg.getProjects());
1470N/A assertEquals(0, cfg.getProjects().size());
1470N/A assertNull(cfg.getDefaultProject());
1461N/A
1461N/A File file = new File("/opengrok_automatic_test/foo/bar");
1470N/A cfg.setSourceRoot("/opengrok_automatic_test/foo");
1461N/A Project p = new Project();
1461N/A p.setPath("/bar");
1461N/A assertEquals("/bar", p.getId());
1470N/A cfg.getProjects().add(p);
1461N/A assertEquals(p, Project.getProject(file));
1470N/A cfg.setProjects(null);
1470N/A assertNull(cfg.getProjects());
1461N/A }
1461N/A };
1461N/A Thread thread = new Thread(runnable);
1461N/A thread.start();
1461N/A thread.join(1000);
431N/A }
431N/A
1461N/A @SuppressWarnings({ "static-method", "javadoc" })
431N/A @Test
610N/A public void testRegister() throws InterruptedException, IOException {
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
431N/A String path = "/tmp/dataroot";
1470N/A cfg.setDataRoot(path);
1470N/A RuntimeEnvironment.register();
431N/A Thread t = new Thread(new Runnable() {
1461N/A @Override
431N/A public void run() {
431N/A Configuration c = new Configuration();
1470N/A RuntimeEnvironment.setConfig(c);
431N/A
431N/A }
431N/A });
431N/A t.start();
1461N/A t.join(1000);
1470N/A assertEquals(new File(path).getCanonicalPath(), cfg.getDataRoot());
431N/A }
431N/A
1461N/A @SuppressWarnings({ "static-method", "javadoc" })
431N/A @Test
431N/A public void testUrlPrefix() {
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
1470N/A assertEquals("/source" + Prefix.SEARCH_R + '?', cfg.getUrlPrefix());
1419N/A String prefix = "/opengrok" + Prefix.SEARCH_R + '?';
1470N/A cfg.setUrlPrefix(prefix);
1470N/A assertEquals(prefix, cfg.getUrlPrefix());
431N/A }
431N/A
1461N/A @SuppressWarnings({ "static-method", "javadoc" })
431N/A @Test
431N/A public void testCtags() {
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
1470N/A assertEquals("ctags", cfg.getCtags());
431N/A String path = "/usr/bin/ctags";
1470N/A cfg.setCtags(path);
1470N/A assertEquals(path, cfg.getCtags());
431N/A }
431N/A
1461N/A @SuppressWarnings({ "static-method", "javadoc" })
431N/A @Test
431N/A public void testHistoryReaderTimeLimit() {
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
1470N/A assertEquals(30, cfg.getHistoryCacheTime());
1470N/A cfg.setHistoryCacheTime(50);
1470N/A assertEquals(50, cfg.getHistoryCacheTime());
431N/A }
431N/A
1461N/A @SuppressWarnings({ "javadoc", "static-method" })
431N/A @Test
431N/A public void testUseHistoryCache() {
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
1470N/A assertTrue(cfg.isHistoryCache());
1470N/A cfg.setHistoryCache(false);
1470N/A assertFalse(cfg.isHistoryCache());
431N/A }
431N/A
1461N/A @SuppressWarnings({ "static-method", "javadoc" })
431N/A @Test
773N/A public void testStoreHistoryCacheInDB() {
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
1470N/A assertFalse(cfg.isHistoryCacheInDB());
1470N/A cfg.setHistoryCacheInDB(true);
1470N/A assertTrue(cfg.isHistoryCacheInDB());
773N/A }
773N/A
1461N/A @SuppressWarnings({ "static-method", "javadoc" })
773N/A @Test
431N/A public void testGenerateHtml() {
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
1470N/A assertTrue(cfg.isGenerateHtml());
1470N/A cfg.setGenerateHtml(false);
1470N/A assertFalse(cfg.isGenerateHtml());
431N/A }
431N/A
1461N/A @SuppressWarnings({ "static-method", "javadoc" })
431N/A @Test
431N/A public void testCompressXref() {
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
1470N/A assertTrue(cfg.isCompressXref());
1470N/A cfg.setCompressXref(false);
1470N/A assertFalse(cfg.isCompressXref());
431N/A }
431N/A
1461N/A @SuppressWarnings({ "static-method", "javadoc" })
431N/A @Test
431N/A public void testQuickContextScan() {
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
1470N/A assertTrue(cfg.isQuickContextScan());
1470N/A cfg.setQuickContextScan(false);
1470N/A assertFalse(cfg.isQuickContextScan());
431N/A }
431N/A
1461N/A @SuppressWarnings({ "static-method", "javadoc" })
431N/A @Test
431N/A public void testRepositories() {
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
1470N/A assertNotNull(cfg.getRepositories());
1470N/A cfg.setRepositories(null);
1470N/A assertNull(cfg.getRepositories());
693N/A List<RepositoryInfo> reps = new ArrayList<RepositoryInfo>();
1470N/A cfg.setRepositories(reps);
1470N/A assertSame(reps, cfg.getRepositories());
431N/A }
431N/A
1461N/A @SuppressWarnings({ "static-method", "javadoc" })
431N/A @Test
431N/A public void testIndexWordLimit() {
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
1461N/A //default is unlimited
1470N/A assertEquals(Integer.MAX_VALUE, cfg.getIndexWordLimit());
1470N/A cfg.setIndexWordLimit(100000);
1470N/A assertEquals(100000, cfg.getIndexWordLimit());
431N/A }
431N/A
1461N/A @SuppressWarnings({ "static-method", "javadoc" })
431N/A @Test
431N/A public void testVerbose() {
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
1470N/A assertFalse(cfg.isVerbose());
1470N/A cfg.setVerbose(true);
1470N/A assertTrue(cfg.isVerbose());
431N/A }
431N/A
1461N/A @SuppressWarnings({ "static-method", "javadoc" })
431N/A @Test
431N/A public void testAllowLeadingWildcard() {
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
1470N/A assertFalse(cfg.isAllowLeadingWildcard());
1470N/A cfg.setAllowLeadingWildcard(true);
1470N/A assertTrue(cfg.isAllowLeadingWildcard());
431N/A }
431N/A
1461N/A @SuppressWarnings({ "static-method", "javadoc" })
431N/A @Test
431N/A public void testIgnoredNames() {
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
1470N/A assertNotNull(cfg.getIgnoredNames());
1470N/A cfg.setIgnoredNames(null);
1470N/A assertNotNull(cfg.getIgnoredNames());
431N/A }
431N/A
1461N/A @SuppressWarnings({ "static-method", "javadoc" })
431N/A @Test
431N/A public void testUserPage() {
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
431N/A String page = "http://www.opensolaris.org/viewProfile.jspa?username=";
1470N/A assertEquals(page, cfg.getUserPage());
1470N/A cfg.setUserPage(page.substring(5));
1470N/A assertEquals(page.substring(5), cfg.getUserPage());
431N/A }
431N/A
1461N/A @SuppressWarnings({ "static-method", "javadoc" })
431N/A @Test
431N/A public void testBugPage() {
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
431N/A String page = "http://bugs.opensolaris.org/bugdatabase/view_bug.do?bug_id=";
1470N/A assertEquals(page, cfg.getBugPage());
1470N/A cfg.setBugPage(page.substring(5));
1470N/A assertEquals(page.substring(5), cfg.getBugPage());
431N/A }
431N/A
1461N/A @SuppressWarnings({ "static-method", "javadoc" })
431N/A @Test
431N/A public void testBugPattern() {
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
431N/A String page = "\\b([12456789][0-9]{6})\\b";
1470N/A assertEquals(page, cfg.getBugPattern());
1470N/A cfg.setBugPattern(page.substring(5));
1470N/A assertEquals(page.substring(5), cfg.getBugPattern());
431N/A }
431N/A
1461N/A @SuppressWarnings({ "static-method", "javadoc" })
431N/A @Test
431N/A public void testReviewPage() {
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
876N/A String page = "http://arc.opensolaris.org/caselog/PSARC/";
1470N/A assertEquals(page, cfg.getReviewPage());
1470N/A cfg.setReviewPage(page.substring(5));
1470N/A assertEquals(page.substring(5), cfg.getReviewPage());
431N/A }
431N/A
1461N/A @SuppressWarnings({ "static-method", "javadoc" })
431N/A @Test
431N/A public void testReviewPattern() {
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
431N/A String page = "\\b(\\d{4}/\\d{3})\\b";
1470N/A assertEquals(page, cfg.getReviewPattern());
1470N/A cfg.setReviewPattern(page.substring(5));
1470N/A assertEquals(page.substring(5), cfg.getReviewPattern());
431N/A }
431N/A
1461N/A @SuppressWarnings({ "static-method", "javadoc" })
431N/A @Test
431N/A public void testWebappLAF() {
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
1470N/A assertEquals(Configuration.DEFAULT_STYLE, cfg.getWebappLAF());
1470N/A cfg.setWebappLAF("foo");
1470N/A assertEquals("foo", cfg.getWebappLAF());
431N/A }
431N/A
1461N/A @SuppressWarnings({ "static-method", "javadoc" })
431N/A @Test
431N/A public void testRemoteScmSupported() {
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
1470N/A assertFalse(cfg.isRemoteScmSupported());
1470N/A cfg.setRemoteScmSupported(true);
1470N/A assertTrue(cfg.isRemoteScmSupported());
431N/A }
431N/A
1461N/A @SuppressWarnings({ "static-method", "javadoc" })
431N/A @Test
431N/A public void testOptimizeDatabase() {
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
1470N/A assertTrue(cfg.isOptimizeDatabase());
1470N/A cfg.setOptimizeDatabase(false);
1470N/A assertFalse(cfg.isOptimizeDatabase());
431N/A }
431N/A
1461N/A @SuppressWarnings({ "static-method", "javadoc" })
431N/A @Test
431N/A public void testUsingLuceneLocking() {
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
1470N/A assertFalse(cfg.isUsingLuceneLocking());
1470N/A cfg.setUsingLuceneLocking(true);
1470N/A assertTrue(cfg.isUsingLuceneLocking());
431N/A }
471N/A
1461N/A @SuppressWarnings({ "static-method", "javadoc" })
471N/A @Test
480N/A public void testIndexVersionedFilesOnly() {
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
1470N/A assertFalse(cfg.isIndexVersionedFilesOnly());
1470N/A cfg.setIndexVersionedFilesOnly(true);
1470N/A assertTrue(cfg.isIndexVersionedFilesOnly());
480N/A }
480N/A
1461N/A @SuppressWarnings({ "static-method", "javadoc" })
480N/A @Test
471N/A public void testConfigListenerThread() throws IOException {
471N/A SocketAddress addr = new InetSocketAddress(0);
1470N/A assertTrue(RuntimeEnvironment.startConfigListenerThread(addr));
471N/A try {
471N/A Thread.sleep(1000);
471N/A } catch (InterruptedException exp) {
471N/A // do nothing
471N/A }
1470N/A RuntimeEnvironment.getInstance().writeConfig();
1470N/A RuntimeEnvironment.stopConfigListenerThread();
471N/A }
490N/A
1461N/A @SuppressWarnings({ "static-method", "javadoc" })
1461N/A @Test(expected=ArrayIndexOutOfBoundsException.class)
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');
1461N/A o = Configuration.makeXMLStringAsConfiguration(m);
690N/A }
690N/A
1461N/A @SuppressWarnings({ "static-method", "javadoc" })
690N/A @Test
490N/A public void testBug3095() throws IOException {
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
490N/A File file = new File("foobar");
490N/A assertTrue(file.createNewFile());
490N/A assertFalse(file.isAbsolute());
1470N/A cfg.setDataRoot(file.getName());
1470N/A File f = cfg.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
1461N/A @SuppressWarnings({ "static-method", "javadoc" })
492N/A @Test
492N/A public void testBug3154() throws IOException {
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
492N/A File file = File.createTempFile("dataroot", null);
492N/A assertTrue(file.delete());
492N/A assertFalse(file.exists());
1470N/A cfg.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
1461N/A @SuppressWarnings({ "javadoc", "static-method" })
1123N/A @Test
1123N/A public void testObfuscateEMail() throws IOException {
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
1123N/A // By default, don't obfuscate.
1470N/A assertObfuscated(Boolean.FALSE, cfg);
1123N/A
1470N/A cfg.setObfuscatingEMailAddresses(true);
1470N/A assertObfuscated(Boolean.TRUE, cfg);
1123N/A
1470N/A cfg.setObfuscatingEMailAddresses(false);
1470N/A assertObfuscated(Boolean.FALSE, cfg);
1123N/A }
1123N/A
1461N/A @SuppressWarnings("boxing")
1470N/A private static void assertObfuscated(Boolean expected, Configuration cfg)
1461N/A throws IOException
1461N/A {
1470N/A assertEquals(expected, cfg.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();
1461N/A XrefWriter w = new XrefWriter(out);
1461N/A xref.write(w);
1461N/A w.close();
1461N/A String expectedAddress = expected.booleanValue()
1461N/A ? address.replace("@", " (at) ")
1461N/A : address;
1123N/A
1386N/A String expectedOutput = JFlexXrefTest.FIRST_LINE_PREAMBLE
1386N/A + expectedAddress + JFlexXrefTest.LINE_SUFFIX + JFlexXrefTest.EOS;
1123N/A
1123N/A assertEquals(expectedOutput, out.toString());
1123N/A }
1125N/A
1461N/A @SuppressWarnings({ "static-method", "javadoc" })
1125N/A @Test
1125N/A public void isChattyStatusPage() {
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
1125N/A
1125N/A // By default, status page should not be chatty.
1470N/A assertFalse(cfg.isChattyStatusPage());
1125N/A
1470N/A cfg.setChattyStatusPage(true);
1470N/A assertTrue(cfg.isChattyStatusPage());
1125N/A
1470N/A cfg.setChattyStatusPage(false);
1470N/A assertFalse(cfg.isChattyStatusPage());
1125N/A }
490N/A}