3 * Copyright (C) 2009-2024 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.plugins.edition;
22 import java.util.Random;
23 import org.junit.Test;
24 import org.sonar.core.platform.PluginInfo;
25 import org.sonar.updatecenter.common.Plugin;
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;
31 public class EditionBundledPluginsTest {
33 private final Random random = new Random();
36 public void isEditionBundled_on_Plugin_fails_with_NPE_if_arg_is_null() {
37 assertThatThrownBy(() -> EditionBundledPlugins.isEditionBundled((Plugin) null))
38 .isInstanceOf(NullPointerException.class);
42 public void isEditionBundled_on_Plugin_returns_false_for_SonarSource_and_non_commercial_license() {
43 Plugin plugin = newPlugin(randomizeCase("SonarSource"), randomAlphanumeric(3));
45 assertThat(EditionBundledPlugins.isEditionBundled(plugin)).isFalse();
49 public void isEditionBundled_on_Plugin_returns_false_for_license_SonarSource_and_non_SonarSource_organization() {
50 Plugin plugin = newPlugin(randomAlphanumeric(3), randomizeCase("SonarSource"));
52 assertThat(EditionBundledPlugins.isEditionBundled(plugin)).isFalse();
56 public void isEditionBundled_on_Plugin_returns_false_for_license_Commercial_and_non_SonarSource_organization() {
57 Plugin plugin = newPlugin(randomAlphanumeric(3), randomizeCase("Commercial"));
59 assertThat(EditionBundledPlugins.isEditionBundled(plugin)).isFalse();
63 public void isEditionBundled_on_Plugin_returns_true_for_organization_SonarSource_and_license_SonarSource_case_insensitive() {
64 Plugin plugin = newPlugin(randomizeCase("SonarSource"), randomizeCase("SonarSource"));
66 assertThat(EditionBundledPlugins.isEditionBundled(plugin)).isTrue();
70 public void isEditionBundled_on_Plugin_returns_true_for_organization_SonarSource_and_license_Commercial_case_insensitive() {
71 Plugin plugin = newPlugin(randomizeCase("SonarSource"), randomizeCase("Commercial"));
73 assertThat(EditionBundledPlugins.isEditionBundled(plugin)).isTrue();
77 public void isEditionBundled_on_PluginInfo_fails_with_NPE_if_arg_is_null() {
78 assertThatThrownBy(() -> EditionBundledPlugins.isEditionBundled((PluginInfo) null))
79 .isInstanceOf(NullPointerException.class);
83 public void isEditionBundled_on_PluginInfo_returns_false_for_SonarSource_and_non_commercial_license() {
84 PluginInfo pluginInfo = newPluginInfo(randomizeCase("SonarSource"), randomAlphanumeric(3));
86 assertThat(EditionBundledPlugins.isEditionBundled(pluginInfo)).isFalse();
90 public void isEditionBundled_on_PluginInfo_returns_false_for_license_SonarSource_and_non_SonarSource_organization() {
91 PluginInfo pluginInfo = newPluginInfo(randomAlphanumeric(3), randomizeCase("SonarSource"));
93 assertThat(EditionBundledPlugins.isEditionBundled(pluginInfo)).isFalse();
97 public void isEditionBundled_on_PluginInfo_returns_false_for_license_Commercial_and_non_SonarSource_organization() {
98 PluginInfo pluginInfo = newPluginInfo(randomAlphanumeric(3), randomizeCase("Commercial"));
100 assertThat(EditionBundledPlugins.isEditionBundled(pluginInfo)).isFalse();
104 public void isEditionBundled_on_PluginInfo_returns_true_for_organization_SonarSource_and_license_SonarSource_case_insensitive() {
105 PluginInfo pluginInfo = newPluginInfo(randomizeCase("SonarSource"), randomizeCase("SonarSource"));
107 assertThat(EditionBundledPlugins.isEditionBundled(pluginInfo)).isTrue();
111 public void isEditionBundled_on_PluginINfo_returns_true_for_organization_SonarSource_and_license_Commercial_case_insensitive() {
112 PluginInfo pluginInfo = newPluginInfo(randomizeCase("SonarSource"), randomizeCase("Commercial"));
114 assertThat(EditionBundledPlugins.isEditionBundled(pluginInfo)).isTrue();
117 private String randomizeCase(String s) {
119 .map(c -> random.nextBoolean() ? Character.toUpperCase(c) : Character.toLowerCase(c))
120 .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
124 private PluginInfo newPluginInfo(String organization, String license) {
125 PluginInfo pluginInfo = new PluginInfo(randomAlphanumeric(2));
126 if (random.nextBoolean()) {
127 pluginInfo.setName(randomAlphanumeric(3));
129 if (random.nextBoolean()) {
130 pluginInfo.setOrganizationUrl(randomAlphanumeric(4));
132 if (random.nextBoolean()) {
133 pluginInfo.setIssueTrackerUrl(randomAlphanumeric(5));
135 if (random.nextBoolean()) {
136 pluginInfo.setIssueTrackerUrl(randomAlphanumeric(6));
138 if (random.nextBoolean()) {
139 pluginInfo.setBasePlugin(randomAlphanumeric(7));
141 if (random.nextBoolean()) {
142 pluginInfo.setHomepageUrl(randomAlphanumeric(8));
145 .setOrganizationName(organization)
146 .setLicense(license);
149 private Plugin newPlugin(String organization, String license) {
150 Plugin plugin = Plugin.factory(randomAlphanumeric(2));
151 if (random.nextBoolean()) {
152 plugin.setName(randomAlphanumeric(3));
154 if (random.nextBoolean()) {
155 plugin.setOrganizationUrl(randomAlphanumeric(4));
157 if (random.nextBoolean()) {
158 plugin.setTermsConditionsUrl(randomAlphanumeric(5));
160 if (random.nextBoolean()) {
161 plugin.setIssueTrackerUrl(randomAlphanumeric(6));
163 if (random.nextBoolean()) {
164 plugin.setCategory(randomAlphanumeric(7));
166 if (random.nextBoolean()) {
167 plugin.setHomepageUrl(randomAlphanumeric(8));
171 .setOrganization(organization);