1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
/*
* Sonar, open source software quality management tool.
* Copyright (C) 2008-2012 SonarSource
* mailto:contact AT sonarsource DOT com
*
* Sonar is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* Sonar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.batch;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.config.Settings;
import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.resources.Java;
import org.sonar.api.resources.Project;
import org.sonar.api.utils.SonarException;
import org.sonar.jpa.dao.ProfilesDao;
import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class DefaultProfileLoaderTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
private ProfilesDao dao;
private Project javaProject = newProject(Java.KEY);
@Before
public void setUp() {
dao = mock(ProfilesDao.class);
}
@Test
public void should_get_configured_profile() {
Settings settings = new Settings();
settings.setProperty("sonar.profile.java", "legacy profile");
when(dao.getProfile(Java.KEY, "legacy profile")).thenReturn(RulesProfile.create("legacy profile", "java"));
RulesProfile profile = new DefaultProfileLoader(dao, settings).load(javaProject);
assertThat(profile.getName()).isEqualTo("legacy profile");
}
@Test
public void should_fail_if_not_found() {
Settings settings = new Settings();
settings.setProperty("sonar.profile.java", "unknown");
thrown.expect(SonarException.class);
thrown.expectMessage("Quality profile not found : unknown, language java");
new DefaultProfileLoader(dao, settings).load(javaProject);
}
/**
* SONAR-3125
*/
@Test
public void should_give_explicit_message_if_default_profile_not_found() {
Project cobolProject = newProject("cobol");
thrown.expect(SonarException.class);
thrown.expectMessage("You must install a plugin that supports the language 'cobol'");
new DefaultProfileLoader(dao, new Settings()).load(cobolProject);
}
private Project newProject(String language) {
PropertiesConfiguration configuration = new PropertiesConfiguration();
configuration.setProperty("sonar.language", language);
return new Project("project").setConfiguration(configuration);
}
}
|