]> source.dussan.org Git - sonarqube.git/blob
78a420b9d5f3d2bc24c3373e7cb34bdefed7a538
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 SonarSource SA
4  * mailto:info AT sonarsource DOT 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 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.
19  */
20 package org.sonar.server.plugins.edition;
21
22 import java.util.Random;
23 import org.junit.Test;
24 import org.sonar.core.platform.PluginInfo;
25 import org.sonar.updatecenter.common.Plugin;
26
27 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
28 import static org.assertj.core.api.Assertions.assertThat;
29 import static org.assertj.core.api.Assertions.assertThatThrownBy;
30
31 public class EditionBundledPluginsTest {
32
33   private final Random random = new Random();
34
35   @Test
36   public void isEditionBundled_on_Plugin_fails_with_NPE_if_arg_is_null() {
37     assertThatThrownBy(() -> EditionBundledPlugins.isEditionBundled((Plugin) null))
38       .isInstanceOf(NullPointerException.class);
39   }
40
41   @Test
42   public void isEditionBundled_on_Plugin_returns_false_for_SonarSource_and_non_commercial_license() {
43     Plugin plugin = newPlugin(randomizeCase("SonarSource"), randomAlphanumeric(3));
44
45     assertThat(EditionBundledPlugins.isEditionBundled(plugin)).isFalse();
46   }
47
48   @Test
49   public void isEditionBundled_on_Plugin_returns_false_for_license_SonarSource_and_non_SonarSource_organization() {
50     Plugin plugin = newPlugin(randomAlphanumeric(3), randomizeCase("SonarSource"));
51
52     assertThat(EditionBundledPlugins.isEditionBundled(plugin)).isFalse();
53   }
54
55   @Test
56   public void isEditionBundled_on_Plugin_returns_false_for_license_Commercial_and_non_SonarSource_organization() {
57     Plugin plugin = newPlugin(randomAlphanumeric(3), randomizeCase("Commercial"));
58
59     assertThat(EditionBundledPlugins.isEditionBundled(plugin)).isFalse();
60   }
61
62   @Test
63   public void isEditionBundled_on_Plugin_returns_true_for_organization_SonarSource_and_license_SonarSource_case_insensitive() {
64     Plugin plugin = newPlugin(randomizeCase("SonarSource"), randomizeCase("SonarSource"));
65
66     assertThat(EditionBundledPlugins.isEditionBundled(plugin)).isTrue();
67   }
68
69   @Test
70   public void isEditionBundled_on_Plugin_returns_true_for_organization_SonarSource_and_license_Commercial_case_insensitive() {
71     Plugin plugin = newPlugin(randomizeCase("SonarSource"), randomizeCase("Commercial"));
72
73     assertThat(EditionBundledPlugins.isEditionBundled(plugin)).isTrue();
74   }
75
76   @Test
77   public void isEditionBundled_on_PluginInfo_fails_with_NPE_if_arg_is_null() {
78     assertThatThrownBy(() -> EditionBundledPlugins.isEditionBundled((PluginInfo) null))
79       .isInstanceOf(NullPointerException.class);
80   }
81
82   @Test
83   public void isEditionBundled_on_PluginInfo_returns_false_for_SonarSource_and_non_commercial_license() {
84     PluginInfo pluginInfo = newPluginInfo(randomizeCase("SonarSource"), randomAlphanumeric(3));
85
86     assertThat(EditionBundledPlugins.isEditionBundled(pluginInfo)).isFalse();
87   }
88
89   @Test
90   public void isEditionBundled_on_PluginInfo_returns_false_for_license_SonarSource_and_non_SonarSource_organization() {
91     PluginInfo pluginInfo = newPluginInfo(randomAlphanumeric(3), randomizeCase("SonarSource"));
92
93     assertThat(EditionBundledPlugins.isEditionBundled(pluginInfo)).isFalse();
94   }
95
96   @Test
97   public void isEditionBundled_on_PluginInfo_returns_false_for_license_Commercial_and_non_SonarSource_organization() {
98     PluginInfo pluginInfo = newPluginInfo(randomAlphanumeric(3), randomizeCase("Commercial"));
99
100     assertThat(EditionBundledPlugins.isEditionBundled(pluginInfo)).isFalse();
101   }
102
103   @Test
104   public void isEditionBundled_on_PluginInfo_returns_true_for_organization_SonarSource_and_license_SonarSource_case_insensitive() {
105     PluginInfo pluginInfo = newPluginInfo(randomizeCase("SonarSource"), randomizeCase("SonarSource"));
106
107     assertThat(EditionBundledPlugins.isEditionBundled(pluginInfo)).isTrue();
108   }
109
110   @Test
111   public void isEditionBundled_on_PluginINfo_returns_true_for_organization_SonarSource_and_license_Commercial_case_insensitive() {
112     PluginInfo pluginInfo = newPluginInfo(randomizeCase("SonarSource"), randomizeCase("Commercial"));
113
114     assertThat(EditionBundledPlugins.isEditionBundled(pluginInfo)).isTrue();
115   }
116
117   private String randomizeCase(String s) {
118     return s.chars()
119       .map(c -> random.nextBoolean() ? Character.toUpperCase(c) : Character.toLowerCase(c))
120       .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
121       .toString();
122   }
123
124   private PluginInfo newPluginInfo(String organization, String license) {
125     PluginInfo pluginInfo = new PluginInfo(randomAlphanumeric(2));
126     if (random.nextBoolean()) {
127       pluginInfo.setName(randomAlphanumeric(3));
128     }
129     if (random.nextBoolean()) {
130       pluginInfo.setOrganizationUrl(randomAlphanumeric(4));
131     }
132     if (random.nextBoolean()) {
133       pluginInfo.setIssueTrackerUrl(randomAlphanumeric(5));
134     }
135     if (random.nextBoolean()) {
136       pluginInfo.setIssueTrackerUrl(randomAlphanumeric(6));
137     }
138     if (random.nextBoolean()) {
139       pluginInfo.setBasePlugin(randomAlphanumeric(7));
140     }
141     if (random.nextBoolean()) {
142       pluginInfo.setHomepageUrl(randomAlphanumeric(8));
143     }
144     return pluginInfo
145       .setOrganizationName(organization)
146       .setLicense(license);
147   }
148
149   private Plugin newPlugin(String organization, String license) {
150     Plugin plugin = Plugin.factory(randomAlphanumeric(2));
151     if (random.nextBoolean()) {
152       plugin.setName(randomAlphanumeric(3));
153     }
154     if (random.nextBoolean()) {
155       plugin.setOrganizationUrl(randomAlphanumeric(4));
156     }
157     if (random.nextBoolean()) {
158       plugin.setTermsConditionsUrl(randomAlphanumeric(5));
159     }
160     if (random.nextBoolean()) {
161       plugin.setIssueTrackerUrl(randomAlphanumeric(6));
162     }
163     if (random.nextBoolean()) {
164       plugin.setCategory(randomAlphanumeric(7));
165     }
166     if (random.nextBoolean()) {
167       plugin.setHomepageUrl(randomAlphanumeric(8));
168     }
169     return plugin
170       .setLicense(license)
171       .setOrganization(organization);
172   }
173 }