Преглед на файлове

SQSCANNER-42 Stop support of SQ < 5.2

tags/3.1.0.1141
Julien HENRY преди 6 години
родител
ревизия
265b9abd6d

+ 7
- 7
pom.xml Целия файл

<dependency> <dependency>
<groupId>org.sonarsource.scanner.api</groupId> <groupId>org.sonarsource.scanner.api</groupId>
<artifactId>sonar-scanner-api</artifactId> <artifactId>sonar-scanner-api</artifactId>
<version>2.9.0.887</version>
<version>2.10.0.1049</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.google.code.findbugs</groupId> <groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId> <artifactId>jsr305</artifactId>
<version>2.0.1</version>
<version>2.0.3</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>


<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>4.11</version>
<version>4.12</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.mockito</groupId> <groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<artifactId>mockito-core</artifactId>
<version>2.8.47</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<configuration> <configuration>
<rules> <rules>
<requireFilesSize> <requireFilesSize>
<minsize>490000</minsize>
<maxsize>510000</maxsize>
<minsize>530000</minsize>
<maxsize>550000</maxsize>
<files> <files>
<file>${project.build.directory}/sonar-scanner-${project.version}.zip</file> <file>${project.build.directory}/sonar-scanner-${project.version}.zip</file>
</files> </files>

+ 5
- 16
src/main/java/org/sonarsource/scanner/cli/Main.java Целия файл

*/ */
package org.sonarsource.scanner.cli; package org.sonarsource.scanner.cli;


import java.util.Map;
import java.util.Properties; import java.util.Properties;
import org.sonarsource.scanner.api.EmbeddedScanner; import org.sonarsource.scanner.api.EmbeddedScanner;
import org.sonarsource.scanner.api.ScanProperties; import org.sonarsource.scanner.api.ScanProperties;
Stats stats = new Stats(logger).start(); Stats stats = new Stats(logger).start();


int status = Exit.ERROR; int status = Exit.ERROR;
boolean started = false;
try { try {
Properties p = conf.properties(); Properties p = conf.properties();
checkSkip(p); checkSkip(p);
configureLogging(p); configureLogging(p);
init(p); init(p);
runner.start(); runner.start();
started = true;
logger.info("SonarQube server " + runner.serverVersion()); logger.info("SonarQube server " + runner.serverVersion());
runAnalysis(stats, p);
execute(stats, p);
status = Exit.SUCCESS; status = Exit.SUCCESS;
} catch (Throwable e) { } catch (Throwable e) {
status = Exit.ERROR;
displayExecutionResult(stats, "FAILURE"); displayExecutionResult(stats, "FAILURE");
showError("Error during SonarQube Scanner execution", e, cli.isDebugEnabled()); showError("Error during SonarQube Scanner execution", e, cli.isDebugEnabled());
} finally { } finally {
try {
if (started) {
runner.stop();
}
} catch (Throwable e) {
status = Exit.ERROR;
logger.error("Unable to properly stop the scanner", e);
} finally {
exit.exit(status);
}
exit.exit(status);
} }


} }
} }
} }


private void runAnalysis(Stats stats, Properties p) {
runner.runAnalysis(p);
private void execute(Stats stats, Properties p) {
runner.execute((Map) p);
displayExecutionResult(stats, "SUCCESS"); displayExecutionResult(stats, "SUCCESS");
} }



+ 3
- 3
src/main/java/org/sonarsource/scanner/cli/ScannerFactory.java Целия файл

*/ */
package org.sonarsource.scanner.cli; package org.sonarsource.scanner.cli;


import java.util.Map;
import java.util.Properties; import java.util.Properties;
import org.sonarsource.scanner.api.EmbeddedScanner; import org.sonarsource.scanner.api.EmbeddedScanner;
import org.sonarsource.scanner.api.LogOutput; import org.sonarsource.scanner.api.LogOutput;
} }


EmbeddedScanner create(Properties props) { EmbeddedScanner create(Properties props) {
return EmbeddedScanner.create(new DefaultLogOutput())
.addGlobalProperties(props)
.setApp("ScannerCli", ScannerVersion.version());
return EmbeddedScanner.create("ScannerCli", ScannerVersion.version(), new DefaultLogOutput())
.addGlobalProperties((Map) props);
} }


class DefaultLogOutput implements LogOutput { class DefaultLogOutput implements LogOutput {

+ 30
- 49
src/test/java/org/sonarsource/scanner/cli/MainTest.java Целия файл

package org.sonarsource.scanner.cli; package org.sonarsource.scanner.cli;


import java.io.IOException; import java.io.IOException;
import java.util.Map;
import java.util.Properties; import java.util.Properties;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.sonarsource.scanner.api.ScanProperties; import org.sonarsource.scanner.api.ScanProperties;


import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
@Mock @Mock
private Properties properties; private Properties properties;
@Mock @Mock
private ScannerFactory runnerFactory;
private ScannerFactory scannerFactory;
@Mock @Mock
private EmbeddedScanner runner;
private EmbeddedScanner scanner;
@Mock @Mock
private Logs logs; private Logs logs;


@Before @Before
public void setUp() throws IOException { public void setUp() throws IOException {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
when(runnerFactory.create(any(Properties.class))).thenReturn(runner);
when(scannerFactory.create(any(Properties.class))).thenReturn(scanner);
when(conf.properties()).thenReturn(properties); when(conf.properties()).thenReturn(properties);
} }


@Test @Test
public void should_execute_runner() { public void should_execute_runner() {
Main main = new Main(exit, cli, conf, runnerFactory, logs);
Main main = new Main(exit, cli, conf, scannerFactory, logs);
main.execute(); main.execute();


verify(exit).exit(Exit.SUCCESS); verify(exit).exit(Exit.SUCCESS);
verify(runnerFactory).create(properties);
verify(scannerFactory).create(properties);


verify(runner, times(1)).start();
verify(runner, times(1)).runAnalysis(properties);
verify(runner, times(1)).stop();
verify(scanner, times(1)).start();
verify(scanner, times(1)).execute((Map) properties);
} }


@Test @Test
public void should_call_stop_on_error_during_analysis() {
public void should_exit_with_error_on_error_during_analysis() {
EmbeddedScanner runner = mock(EmbeddedScanner.class); EmbeddedScanner runner = mock(EmbeddedScanner.class);
Exception e = new NullPointerException("NPE"); Exception e = new NullPointerException("NPE");
e = new IllegalStateException("Error", e); e = new IllegalStateException("Error", e);
doThrow(e).when(runner).runAnalysis(any(Properties.class));
when(runnerFactory.create(any(Properties.class))).thenReturn(runner);
doThrow(e).when(runner).execute(any(Map.class));
when(scannerFactory.create(any(Properties.class))).thenReturn(runner);


Main main = new Main(exit, cli, conf, runnerFactory, logs);
Main main = new Main(exit, cli, conf, scannerFactory, logs);
main.execute(); main.execute();


verify(runner).stop();
verify(exit).exit(Exit.ERROR); verify(exit).exit(Exit.ERROR);
verify(logs).error("Error during SonarQube Scanner execution", e); verify(logs).error("Error during SonarQube Scanner execution", e);
} }


@Test @Test
public void should_not_call_stop_on_error_during_start() {
public void should_exit_with_error_on_error_during_start() {
EmbeddedScanner runner = mock(EmbeddedScanner.class); EmbeddedScanner runner = mock(EmbeddedScanner.class);
Exception e = new NullPointerException("NPE"); Exception e = new NullPointerException("NPE");
e = new IllegalStateException("Error", e); e = new IllegalStateException("Error", e);
doThrow(e).when(runner).start(); doThrow(e).when(runner).start();
when(runnerFactory.create(any(Properties.class))).thenReturn(runner);
when(scannerFactory.create(any(Properties.class))).thenReturn(runner);


Main main = new Main(exit, cli, conf, runnerFactory, logs);
Main main = new Main(exit, cli, conf, scannerFactory, logs);
main.execute(); main.execute();


verify(runner).start(); verify(runner).start();
verify(runner, never()).runAnalysis(any());
verify(runner, never()).stop();
verify(runner, never()).execute(any(Map.class));
verify(exit).exit(Exit.ERROR); verify(exit).exit(Exit.ERROR);
verify(logs).error("Error during SonarQube Scanner execution", e); verify(logs).error("Error during SonarQube Scanner execution", e);
} }


@Test
public void should_exit_on_error() {
EmbeddedScanner runner = mock(EmbeddedScanner.class);
Exception e = new NullPointerException("NPE");
e = new IllegalStateException("Error", e);
doThrow(e).when(runner).stop();
when(runnerFactory.create(any(Properties.class))).thenReturn(runner);

Main main = new Main(exit, cli, conf, runnerFactory, logs);
main.execute();

verify(runner).stop();
verify(exit).exit(Exit.ERROR);
verify(logs).error("Unable to properly stop the scanner", e);
}

@Test @Test
public void show_error_with_stacktrace() { public void show_error_with_stacktrace() {
Exception e = createException(false); Exception e = createException(false);
when(cli.isDebugEnabled()).thenReturn(debugEnabled); when(cli.isDebugEnabled()).thenReturn(debugEnabled);


EmbeddedScanner runner = mock(EmbeddedScanner.class); EmbeddedScanner runner = mock(EmbeddedScanner.class);
doThrow(e).when(runner).runAnalysis(any(Properties.class));
when(runnerFactory.create(any(Properties.class))).thenReturn(runner);
doThrow(e).when(runner).execute(any(Map.class));
when(scannerFactory.create(any(Properties.class))).thenReturn(runner);


Main main = new Main(exit, cli, conf, runnerFactory, logs);
Main main = new Main(exit, cli, conf, scannerFactory, logs);
main.execute(); main.execute();


verify(runner).stop();
verify(exit).exit(Exit.ERROR); verify(exit).exit(Exit.ERROR);
} }


when(cli.isDisplayVersionOnly()).thenReturn(true); when(cli.isDisplayVersionOnly()).thenReturn(true);
when(conf.properties()).thenReturn(p); when(conf.properties()).thenReturn(p);


Main main = new Main(exit, cli, conf, runnerFactory, logs);
Main main = new Main(exit, cli, conf, scannerFactory, logs);
main.execute(); main.execute();


InOrder inOrder = Mockito.inOrder(exit, runnerFactory);
InOrder inOrder = Mockito.inOrder(exit, scannerFactory);


inOrder.verify(exit, times(1)).exit(Exit.SUCCESS); inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
inOrder.verify(runnerFactory, times(1)).create(p);
inOrder.verify(scannerFactory, times(1)).create(p);
inOrder.verify(exit, times(1)).exit(Exit.SUCCESS); inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
} }


p.setProperty(ScanProperties.SKIP, "true"); p.setProperty(ScanProperties.SKIP, "true");
when(conf.properties()).thenReturn(p); when(conf.properties()).thenReturn(p);


Main main = new Main(exit, cli, conf, runnerFactory, logs);
Main main = new Main(exit, cli, conf, scannerFactory, logs);
main.execute(); main.execute();


verify(logs).info("SonarQube Scanner analysis skipped"); verify(logs).info("SonarQube Scanner analysis skipped");
InOrder inOrder = Mockito.inOrder(exit, runnerFactory);
InOrder inOrder = Mockito.inOrder(exit, scannerFactory);


inOrder.verify(exit, times(1)).exit(Exit.SUCCESS); inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
inOrder.verify(runnerFactory, times(1)).create(p);
inOrder.verify(scannerFactory, times(1)).create(p);
inOrder.verify(exit, times(1)).exit(Exit.SUCCESS); inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
} }


@Test @Test
public void shouldLogServerVersion() throws IOException { public void shouldLogServerVersion() throws IOException {
when(runner.serverVersion()).thenReturn("5.5");
when(scanner.serverVersion()).thenReturn("5.5");
Properties p = new Properties(); Properties p = new Properties();
when(cli.isDisplayVersionOnly()).thenReturn(true); when(cli.isDisplayVersionOnly()).thenReturn(true);
when(conf.properties()).thenReturn(p); when(conf.properties()).thenReturn(p);


Main main = new Main(exit, cli, conf, runnerFactory, logs);
Main main = new Main(exit, cli, conf, scannerFactory, logs);
main.execute(); main.execute();
verify(logs).info("SonarQube server 5.5"); verify(logs).info("SonarQube server 5.5");
} }
p.put(propKey, propValue); p.put(propKey, propValue);
when(conf.properties()).thenReturn(p); when(conf.properties()).thenReturn(p);


Main main = new Main(exit, cli, conf, runnerFactory, logs);
Main main = new Main(exit, cli, conf, scannerFactory, logs);
main.execute(); main.execute();


// Logger used for callback should have debug enabled // Logger used for callback should have debug enabled
verify(logs).setDebugEnabled(true); verify(logs).setDebugEnabled(true);


ArgumentCaptor<Properties> propertiesCapture = ArgumentCaptor.forClass(Properties.class); ArgumentCaptor<Properties> propertiesCapture = ArgumentCaptor.forClass(Properties.class);
verify(runner).runAnalysis(propertiesCapture.capture());
verify(scanner).execute((Map) propertiesCapture.capture());


return propertiesCapture.getValue(); return propertiesCapture.getValue();
} }

Loading…
Отказ
Запис