|
| 1 | +/* |
| 2 | + * SonarQube :: GitHub Plugin |
| 3 | + * Copyright (C) 2015 SonarSource |
| 4 | + * sonarqube@googlegroups.com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public |
| 17 | + * License along with this program; if not, write to the Free Software |
| 18 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 |
| 19 | + */ |
| 20 | +package org.sonar.plugins.github; |
| 21 | + |
| 22 | +import java.io.File; |
| 23 | +import org.junit.Before; |
| 24 | +import org.junit.Rule; |
| 25 | +import org.junit.Test; |
| 26 | +import org.junit.rules.ExpectedException; |
| 27 | +import org.sonar.api.CoreProperties; |
| 28 | +import org.sonar.api.batch.bootstrap.ProjectBuilder; |
| 29 | +import org.sonar.api.config.PropertyDefinitions; |
| 30 | +import org.sonar.api.config.Settings; |
| 31 | +import org.sonar.api.utils.MessageException; |
| 32 | + |
| 33 | +import static org.mockito.Matchers.any; |
| 34 | +import static org.mockito.Matchers.eq; |
| 35 | +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; |
| 36 | +import static org.mockito.Mockito.mock; |
| 37 | +import static org.mockito.Mockito.verify; |
| 38 | +import static org.mockito.Mockito.verifyZeroInteractions; |
| 39 | +import static org.mockito.Mockito.withSettings; |
| 40 | + |
| 41 | +public class PullRequestProjectBuilderTest { |
| 42 | + |
| 43 | + @Rule |
| 44 | + public ExpectedException thrown = ExpectedException.none(); |
| 45 | + |
| 46 | + private PullRequestProjectBuilder pullRequestProjectBuilder; |
| 47 | + private PullRequestFacade facade; |
| 48 | + private Settings settings; |
| 49 | + |
| 50 | + @Before |
| 51 | + public void prepare() { |
| 52 | + settings = new Settings(new PropertyDefinitions(GitHubPlugin.class)); |
| 53 | + facade = mock(PullRequestFacade.class); |
| 54 | + pullRequestProjectBuilder = new PullRequestProjectBuilder(new GitHubPluginConfiguration(settings), facade, settings); |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void shouldDoNothing() { |
| 60 | + pullRequestProjectBuilder.build(null); |
| 61 | + verifyZeroInteractions(facade); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void shouldFailIfNotPreview() { |
| 66 | + settings.setProperty(GitHubPlugin.GITHUB_PULL_REQUEST, "1"); |
| 67 | + |
| 68 | + thrown.expect(MessageException.class); |
| 69 | + thrown.expectMessage("The GitHub plugin is only intended to be used in preview mode. Please set 'sonar.analysis.mode'."); |
| 70 | + |
| 71 | + pullRequestProjectBuilder.build(null); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void shouldNotFailIfPreview() { |
| 76 | + settings.setProperty(GitHubPlugin.GITHUB_PULL_REQUEST, "1"); |
| 77 | + settings.setProperty(CoreProperties.DRY_RUN, "true"); |
| 78 | + |
| 79 | + pullRequestProjectBuilder.build(mock(ProjectBuilder.Context.class, withSettings().defaultAnswer(RETURNS_DEEP_STUBS))); |
| 80 | + |
| 81 | + verify(facade).init(eq(1), any(File.class)); |
| 82 | + |
| 83 | + } |
| 84 | +} |
0 commit comments