3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.qualityprofile;
22 import com.google.common.collect.ImmutableList;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.List;
26 import org.junit.rules.ExternalResource;
27 import org.sonar.api.profiles.RulesProfile;
28 import org.sonar.api.resources.Language;
30 import static com.google.common.base.Preconditions.checkState;
32 public class BuiltInQProfileRepositoryRule extends ExternalResource implements BuiltInQProfileRepository {
33 private boolean initializeCalled = false;
34 private List<BuiltInQProfile> profiles = new ArrayList<>();
37 protected void before() throws Throwable {
38 this.initializeCalled = false;
39 this.profiles.clear();
43 public void initialize() {
44 checkState(!initializeCalled, "initialize must be called only once");
45 this.initializeCalled = true;
49 public List<BuiltInQProfile> get() {
50 checkState(initializeCalled, "initialize must be called first");
52 return ImmutableList.copyOf(profiles);
55 public boolean isInitialized() {
56 return initializeCalled;
59 public BuiltInQProfile add(Language language, String profileName) {
60 return add(language, profileName, false);
63 public BuiltInQProfile add(Language language, String profileName, boolean isDefault) {
64 BuiltInQProfile builtIn = create(language, profileName, isDefault);
65 profiles.add(builtIn);
69 public BuiltInQProfile create(Language language, String profileName, boolean isDefault, org.sonar.api.rules.ActiveRule... rules) {
70 return new BuiltInQProfile.Builder()
71 .setLanguage(language.getKey())
73 .setDeclaredDefault(isDefault)
74 .addRules(Arrays.asList(rules))
78 public BuiltInQProfile create(RulesProfile api) {
79 return new BuiltInQProfile.Builder()
80 .setLanguage(api.getLanguage())
81 .setName(api.getName())
82 .setDeclaredDefault(api.getDefaultProfile())
83 .addRules(new ArrayList<>(api.getActiveRules()))