|
| 1 | +/* |
| 2 | + * CDDL HEADER START |
| 3 | + * |
| 4 | + * The contents of this file are subject to the terms of the |
| 5 | + * Common Development and Distribution License (the "License"). |
| 6 | + * You may not use this file except in compliance with the License. |
| 7 | + * |
| 8 | + * See LICENSE.txt included in this distribution for the specific |
| 9 | + * language governing permissions and limitations under the License. |
| 10 | + * |
| 11 | + * When distributing Covered Code, include this CDDL HEADER in each |
| 12 | + * file and include the License file at LICENSE.txt. |
| 13 | + * If applicable, add the following below this CDDL HEADER, with the |
| 14 | + * fields enclosed by brackets "[]" replaced with your own identifying |
| 15 | + * information: Portions Copyright [yyyy] [name of copyright owner] |
| 16 | + * |
| 17 | + * CDDL HEADER END |
| 18 | + */ |
| 19 | + |
| 20 | +/* |
| 21 | + * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
| 22 | + */ |
| 23 | +package org.opensolaris.opengrok.configuration.messages; |
| 24 | + |
| 25 | +import java.io.File; |
| 26 | +import java.io.IOException; |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.concurrent.ConcurrentHashMap; |
| 29 | +import org.junit.After; |
| 30 | +import org.junit.Assert; |
| 31 | +import org.junit.Assume; |
| 32 | +import org.junit.Before; |
| 33 | +import org.junit.Test; |
| 34 | +import org.opensolaris.opengrok.condition.ConditionalRun; |
| 35 | +import org.opensolaris.opengrok.condition.RepositoryInstalled; |
| 36 | +import org.opensolaris.opengrok.configuration.RuntimeEnvironment; |
| 37 | +import org.opensolaris.opengrok.history.GitRepository; |
| 38 | +import org.opensolaris.opengrok.history.HistoryGuru; |
| 39 | +import org.opensolaris.opengrok.history.MercurialRepository; |
| 40 | +import org.opensolaris.opengrok.history.MercurialRepositoryTest; |
| 41 | +import org.opensolaris.opengrok.history.RepositoryFactory; |
| 42 | +import org.opensolaris.opengrok.history.RepositoryInfo; |
| 43 | +import org.opensolaris.opengrok.history.SubversionRepository; |
| 44 | +import org.opensolaris.opengrok.index.Indexer; |
| 45 | +import org.opensolaris.opengrok.util.TestRepository; |
| 46 | + |
| 47 | +/** |
| 48 | + * Test repository message handling. |
| 49 | + * |
| 50 | + * @author Vladimir Kotal |
| 51 | + */ |
| 52 | +public class RepositoryMessageTest { |
| 53 | + |
| 54 | + RuntimeEnvironment env; |
| 55 | + |
| 56 | + private static TestRepository repository = new TestRepository(); |
| 57 | + |
| 58 | + @Before |
| 59 | + public void setUp() throws IOException { |
| 60 | + Assume.assumeTrue(new MercurialRepository().isWorking()); |
| 61 | + Assume.assumeTrue(new SubversionRepository().isWorking()); |
| 62 | + Assume.assumeTrue(new GitRepository().isWorking()); |
| 63 | + |
| 64 | + repository = new TestRepository(); |
| 65 | + repository.create(HistoryGuru.class.getResourceAsStream( |
| 66 | + "repositories.zip")); |
| 67 | + |
| 68 | + env = RuntimeEnvironment.getInstance(); |
| 69 | + env.removeAllMessages(); |
| 70 | + env.setSourceRoot(repository.getSourceRoot()); |
| 71 | + env.setDataRoot(repository.getDataRoot()); |
| 72 | + env.setProjectsEnabled(true); |
| 73 | + RepositoryFactory.initializeIgnoredNames(env); |
| 74 | + } |
| 75 | + |
| 76 | + @After |
| 77 | + public void tearDown() { |
| 78 | + if (env != null) { |
| 79 | + env.removeAllMessages(); |
| 80 | + |
| 81 | + // This should match Configuration constructor. |
| 82 | + env.setProjects(new ConcurrentHashMap<>()); |
| 83 | + env.setRepositories(new ArrayList<RepositoryInfo>()); |
| 84 | + env.getProjectRepositoriesMap().clear(); |
| 85 | + } |
| 86 | + |
| 87 | + repository.destroy(); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testValidate() { |
| 92 | + Message m = new RepositoryMessage(); |
| 93 | + Assert.assertFalse(MessageTest.assertValid(m)); |
| 94 | + m.addTag("foo"); |
| 95 | + Assert.assertFalse(MessageTest.assertValid(m)); |
| 96 | + m.setText("text"); |
| 97 | + Assert.assertFalse(MessageTest.assertValid(m)); |
| 98 | + m.setText(null); |
| 99 | + Assert.assertFalse(MessageTest.assertValid(m)); |
| 100 | + m.setText("get-repo-type"); |
| 101 | + Assert.assertTrue(MessageTest.assertValid(m)); |
| 102 | + } |
| 103 | + |
| 104 | + @ConditionalRun(condition = RepositoryInstalled.MercurialInstalled.class) |
| 105 | + @Test |
| 106 | + public void testGetRepositoryType() throws Exception { |
| 107 | + Message m = new RepositoryMessage(); |
| 108 | + m.setText("get-repo-type"); |
| 109 | + m.addTag("/totally-nonexistent-repository"); |
| 110 | + String out = new String(m.apply(env)); |
| 111 | + Assert.assertEquals("N/A", out); |
| 112 | + |
| 113 | + // Create subrepository. |
| 114 | + File mercurialRoot = new File(repository.getSourceRoot() + File.separator + "mercurial"); |
| 115 | + MercurialRepositoryTest.runHgCommand(mercurialRoot, |
| 116 | + "clone", mercurialRoot.getAbsolutePath(), |
| 117 | + mercurialRoot.getAbsolutePath() + File.separator + "closed"); |
| 118 | + |
| 119 | + env.setHistoryEnabled(true); |
| 120 | + Indexer.getInstance().prepareIndexer( |
| 121 | + env, |
| 122 | + true, // search for repositories |
| 123 | + true, // scan and add projects |
| 124 | + null, // no default project |
| 125 | + false, // don't list files |
| 126 | + false, // don't create dictionary |
| 127 | + null, // subFiles - needed when refreshing history partially |
| 128 | + null, // repositories - needed when refreshing history partially |
| 129 | + new ArrayList<>(), // don't zap cache |
| 130 | + false); // don't list repos |
| 131 | + |
| 132 | + m = new RepositoryMessage(); |
| 133 | + m.setText("get-repo-type"); |
| 134 | + m.addTag("/mercurial"); |
| 135 | + out = new String(m.apply(env)); |
| 136 | + Assert.assertEquals("Mercurial", out); |
| 137 | + |
| 138 | + m = new RepositoryMessage(); |
| 139 | + m.setText("get-repo-type"); |
| 140 | + m.addTag("/mercurial/closed"); |
| 141 | + out = new String(m.apply(env)); |
| 142 | + Assert.assertEquals("Mercurial", out); |
| 143 | + } |
| 144 | +} |
0 commit comments