3 * Copyright (C) 2009-2023 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.platform.platformlevel;
22 import java.util.Optional;
23 import java.util.Properties;
24 import org.junit.Before;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.TemporaryFolder;
28 import org.sonar.core.platform.SpringComponentContainer;
29 import org.sonar.server.platform.NodeInformation;
30 import org.sonar.server.platform.db.migration.charset.DatabaseCharsetChecker;
31 import org.sonar.server.plugins.ServerPluginRepository;
33 import static org.mockito.ArgumentMatchers.any;
34 import static org.mockito.Mockito.mock;
35 import static org.mockito.Mockito.never;
36 import static org.mockito.Mockito.times;
37 import static org.mockito.Mockito.verify;
38 import static org.mockito.Mockito.when;
39 import static org.sonar.process.ProcessProperties.Property.PATH_DATA;
40 import static org.sonar.process.ProcessProperties.Property.PATH_HOME;
41 import static org.sonar.process.ProcessProperties.Property.PATH_TEMP;
43 public class PlatformLevel2Test {
46 public TemporaryFolder tempFolder = new TemporaryFolder();
48 private Properties props = new Properties();
51 public void setUp() throws Exception {
52 // these are mandatory settings declared by bootstrap process
53 props.setProperty(PATH_HOME.getKey(), tempFolder.newFolder().getAbsolutePath());
54 props.setProperty(PATH_DATA.getKey(), tempFolder.newFolder().getAbsolutePath());
55 props.setProperty(PATH_TEMP.getKey(), tempFolder.newFolder().getAbsolutePath());
59 public void add_all_components_by_default() {
60 var parentContainer = mock(SpringComponentContainer.class);
61 var container = mock(SpringComponentContainer.class);
62 var platform = mock(PlatformLevel.class);
63 var webserver = mock(NodeInformation.class);
65 when(parentContainer.createChild()).thenReturn(container);
66 when(platform.getContainer()).thenReturn(parentContainer);
67 when(parentContainer.getOptionalComponentByType(any())).thenReturn(Optional.empty());
68 when(container.getOptionalComponentByType(NodeInformation.class)).thenReturn(Optional.of(webserver));
69 when(webserver.isStartupLeader()).thenReturn(true);
71 PlatformLevel2 underTest = new PlatformLevel2(platform);
72 underTest.configure();
74 verify(container).add(ServerPluginRepository.class);
75 verify(container).add(DatabaseCharsetChecker.class);
76 verify(container, times(22)).add(any());
80 public void do_not_add_all_components_when_startup_follower() {
81 var parentContainer = mock(SpringComponentContainer.class);
82 var container = mock(SpringComponentContainer.class);
83 var platform = mock(PlatformLevel.class);
84 var webserver = mock(NodeInformation.class);
86 when(parentContainer.createChild()).thenReturn(container);
87 when(platform.getContainer()).thenReturn(parentContainer);
88 when(parentContainer.getOptionalComponentByType(any())).thenReturn(Optional.empty());
89 when(container.getOptionalComponentByType(NodeInformation.class)).thenReturn(Optional.of(webserver));
90 when(webserver.isStartupLeader()).thenReturn(false);
92 PlatformLevel2 underTest = new PlatformLevel2(platform);
93 underTest.configure();
95 verify(container).add(ServerPluginRepository.class);
96 verify(container, never()).add(DatabaseCharsetChecker.class);
97 verify(container, times(20)).add(any());