]> source.dussan.org Git - sonar-scanner-cli.git/commitdiff
SQSCANNER-17 Remove unused interactive mode 7/head
authorJulien HENRY <julien.henry@sonarsource.com>
Mon, 11 Apr 2016 11:23:23 +0000 (13:23 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Mon, 11 Apr 2016 11:37:27 +0000 (13:37 +0200)
pom.xml
src/main/java/org/sonarsource/scanner/cli/Cli.java
src/main/java/org/sonarsource/scanner/cli/Main.java
src/main/java/org/sonarsource/scanner/cli/Shutdown.java [deleted file]
src/test/java/org/sonarsource/scanner/cli/CliTest.java
src/test/java/org/sonarsource/scanner/cli/MainTest.java
src/test/java/org/sonarsource/scanner/cli/ShutdownTest.java [deleted file]

diff --git a/pom.xml b/pom.xml
index a8c73cedb597fa2a824dacf056fbf4c45335ba8b..d490039c7fb1aa63df2206df8b7835dade771713 100644 (file)
--- a/pom.xml
+++ b/pom.xml
             <configuration>
               <rules>
                 <requireFilesSize>
-                  <minsize>500000</minsize>
-                  <maxsize>510000</maxsize>
+                  <minsize>490000</minsize>
+                  <maxsize>500000</maxsize>
                   <files>
                     <file>${project.build.directory}/sonar-scanner-${project.version}.zip</file>
                   </files>
index 6544cc2c69beb4abb1ec206e484c49f76eaba8ce..3b110eff413f42d01722b568aec28d2269393668 100644 (file)
@@ -27,7 +27,6 @@ class Cli {
   private boolean debugEnabled = false;
   private boolean displayVersionOnly = false;
   private boolean displayStackTrace = false;
-  private boolean interactive = false;
   private final Properties props = new Properties();
   private final Exit exit;
   private final Logs logger;
@@ -49,10 +48,6 @@ class Cli {
     return displayStackTrace;
   }
 
-  boolean isInteractive() {
-    return interactive;
-  }
-
   Properties properties() {
     return props;
   }
@@ -93,9 +88,6 @@ class Cli {
         arg = arg.substring(2);
         appendPropertyTo(arg, props);
 
-      } else if ("-i".equals(arg) || "--interactive".equals(arg)) {
-        interactive = true;
-
       } else {
         printError("Unrecognized option: " + arg);
       }
@@ -104,12 +96,6 @@ class Cli {
     return this;
   }
 
-  public void verify() {
-    if ("fork".equals(props.getProperty("sonarRunner.mode")) && isInteractive()) {
-      printError("Cannot run interactively in fork mode.");
-    }
-  }
-
   private void reset() {
     props.clear();
     debugEnabled = false;
index 0f6c1ebde19c059aac05b12e02b9c7d7b2cf8192..fc7be85af156b291a21d8335de474efdf04e57c8 100644 (file)
  */
 package org.sonarsource.scanner.cli;
 
-import java.io.BufferedReader;
 import java.io.IOException;
-import java.io.InputStreamReader;
-import java.nio.charset.StandardCharsets;
 import java.util.Properties;
 import org.sonarsource.scanner.api.EmbeddedScanner;
 
@@ -40,16 +37,15 @@ import org.sonarsource.scanner.api.EmbeddedScanner;
  */
 public class Main {
 
-  private final Shutdown shutdown;
+  private final Exit exit;
   private final Cli cli;
   private final Conf conf;
   private EmbeddedScanner runner;
-  private BufferedReader inputReader;
   private ScannerFactory runnerFactory;
   private Logs logger;
 
-  Main(Shutdown shutdown, Cli cli, Conf conf, ScannerFactory runnerFactory, Logs logger) {
-    this.shutdown = shutdown;
+  Main(Exit exit, Cli cli, Conf conf, ScannerFactory runnerFactory, Logs logger) {
+    this.exit = exit;
     this.cli = cli;
     this.conf = conf;
     this.runnerFactory = runnerFactory;
@@ -57,56 +53,37 @@ public class Main {
   }
 
   public static void main(String[] args) {
-    Exit exit = new Exit();
     Logs logs = new Logs(System.out, System.err);
+    Exit exit = new Exit();
     Cli cli = new Cli(exit, logs).parse(args);
-    cli.verify();
-    Shutdown shutdown = new Shutdown(exit, cli.isInteractive());
-    Main main = new Main(shutdown, cli, new Conf(cli, logs), new ScannerFactory(logs), logs);
+    Main main = new Main(exit, cli, new Conf(cli, logs), new ScannerFactory(logs), logs);
     main.execute();
   }
 
   void execute() {
     Stats stats = new Stats(logger).start();
-    
+
     try {
       Properties p = conf.properties();
       configureLogging(p);
       init(p);
       runner.start();
       logger.info("SonarQube server " + runner.serverVersion());
-
-      if (cli.isInteractive()) {
-        interactiveLoop(p);
-      } else {
-        runAnalysis(stats, p);
-      }
+      runAnalysis(stats, p);
     } catch (Exception e) {
       displayExecutionResult(stats, "FAILURE");
       showError("Error during SonarQube Scanner execution", e, cli.isDisplayStackTrace() || cli.isDebugEnabled());
-      shutdown.exit(Exit.ERROR);
+      exit.exit(Exit.ERROR);
     }
 
     runner.stop();
-    shutdown.exit(Exit.SUCCESS);
-  }
-
-  private void interactiveLoop(Properties p) throws IOException {
-    do {
-      Stats stats = new Stats(logger).start();
-      try {
-        runAnalysis(stats, p);
-      } catch (Exception e) {
-        displayExecutionResult(stats, "FAILURE");
-        showError("Error during SonarQube Scanner execution", e, cli.isDisplayStackTrace() || cli.isDebugEnabled());
-      }
-    } while (waitForUser());
+    exit.exit(Exit.SUCCESS);
   }
 
   private void init(Properties p) throws IOException {
     SystemInfo.print(logger);
     if (cli.isDisplayVersionOnly()) {
-      shutdown.exit(Exit.SUCCESS);
+      exit.exit(Exit.SUCCESS);
     }
 
     if (cli.isDisplayStackTrace()) {
@@ -117,14 +94,14 @@ public class Main {
   }
 
   private void configureLogging(Properties props) throws IOException {
-    if("true".equals(props.getProperty("sonar.verbose"))
-      || "DEBUG".equalsIgnoreCase(props.getProperty("sonar.log.level")) 
-      || "TRACE".equalsIgnoreCase(props.getProperty("sonar.log.level")) ) {
+    if ("true".equals(props.getProperty("sonar.verbose"))
+      || "DEBUG".equalsIgnoreCase(props.getProperty("sonar.log.level"))
+      || "TRACE".equalsIgnoreCase(props.getProperty("sonar.log.level"))) {
       logger.setDebugEnabled(true);
       logger.setDisplayStackTrace(true);
     }
-    
-    if(cli.isDisplayStackTrace()) {
+
+    if (cli.isDisplayStackTrace()) {
       logger.setDisplayStackTrace(true);
     }
   }
@@ -134,30 +111,6 @@ public class Main {
     displayExecutionResult(stats, "SUCCESS");
   }
 
-  private boolean waitForUser() throws IOException {
-    if (inputReader == null) {
-      inputReader = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
-    }
-
-    shutdown.signalReady(true);
-    if (shutdown.shouldExit()) {
-      // exit before displaying message
-      return false;
-    }
-
-    System.out.println("");
-    System.out.println("<Press enter to restart analysis or Ctrl+C to exit the interactive mode>");
-    String line = inputReader.readLine();
-    shutdown.signalReady(false);
-
-    return line != null;
-  }
-
-  // Visible for testing
-  void setInputReader(BufferedReader inputReader) {
-    this.inputReader = inputReader;
-  }
-
   private void displayExecutionResult(Stats stats, String resultMsg) {
     logger.info("------------------------------------------------------------------------");
     logger.info("EXECUTION " + resultMsg);
diff --git a/src/main/java/org/sonarsource/scanner/cli/Shutdown.java b/src/main/java/org/sonarsource/scanner/cli/Shutdown.java
deleted file mode 100644 (file)
index d2746b1..0000000
+++ /dev/null
@@ -1,92 +0,0 @@
-/*
- * SonarQube Scanner
- * Copyright (C) 2011-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-package org.sonarsource.scanner.cli;
-
-class Shutdown {
-  static final int SUCCESS = 0;
-  static final int ERROR = 1;
-  private static final long DEFAULT_MAX_WAIT = 10_000;
-
-  private long maxWait;
-  ShutdownHook hook = new ShutdownHook();
-  private boolean isReady = false;
-  private boolean exiting = false;
-  private Object lock = new Object();
-  private Exit exit;
-
-  Shutdown(Exit exit, boolean isInteractive) {
-    this(exit, isInteractive, DEFAULT_MAX_WAIT);
-  }
-
-  Shutdown(Exit exit, boolean isInteractive, long maxWait) {
-    this.maxWait = maxWait;
-    this.exit = exit;
-    if (isInteractive) {
-      Runtime.getRuntime().addShutdownHook(hook);
-    }
-  }
-
-  void exit(int status) {
-    synchronized (lock) {
-      signalReady(true);
-    }
-    exit.exit(status);
-  }
-
-  void signalReady(boolean ready) {
-    synchronized (lock) {
-      this.isReady = ready;
-      lock.notifyAll();
-    }
-  }
-
-  boolean shouldExit() {
-    synchronized (lock) {
-      return exiting;
-    }
-  }
-
-  class ShutdownHook extends Thread {
-    private ShutdownHook() {
-      this.setName("shutdown-hook");
-    }
-
-    @Override
-    public void run() {
-      long startTime = System.currentTimeMillis();
-      synchronized (lock) {
-        exiting = true;
-
-        while (!isReady) {
-          long waitTime = startTime + maxWait - System.currentTimeMillis();
-          if (waitTime <= 0) {
-            break;
-          }
-
-          try {
-            lock.wait(waitTime);
-          } catch (InterruptedException e) {
-            // continue
-          }
-        }
-      }
-    }
-  }
-}
index 8e9a4ed256483fb285ef272c74ec22c7a69d6624..6bac2d0a62514a0336cade45346a06b900eebdac 100644 (file)
 package org.sonarsource.scanner.cli;
 
 import org.junit.Test;
-import org.sonarsource.scanner.cli.Cli;
-import org.sonarsource.scanner.cli.Exit;
-import org.sonarsource.scanner.cli.Logs;
 
 import static org.fest.assertions.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
 
 public class CliTest {
   Exit exit = mock(Exit.class);
@@ -50,13 +46,6 @@ public class CliTest {
     assertThat(cli.properties().get("boolean")).isEqualTo("true");
   }
 
-  @Test
-  public void dont_allow_interactive_fork() {
-    cli.parse(new String[] {"-i", "-DsonarRunner.mode=fork"});
-    cli.verify();
-    verify(exit).exit(Exit.SUCCESS);
-  }
-
   @Test
   public void should_parse_optional_task() {
     cli.parse(new String[] {"-D", "foo=bar"});
index 4e5da4e0a2d48c6a724fe7c295b8595668372b79..06adc9d588c34405b970cda9b918cd4dd8350ab2 100644 (file)
  */
 package org.sonarsource.scanner.cli;
 
-import java.io.BufferedReader;
-import java.io.ByteArrayInputStream;
 import java.io.IOException;
-import java.io.InputStream;
-import java.nio.charset.StandardCharsets;
 import java.util.Properties;
 import org.junit.Before;
 import org.junit.Test;
@@ -46,7 +42,7 @@ import static org.mockito.Mockito.when;
 public class MainTest {
 
   @Mock
-  private Shutdown shutdown;
+  private Exit exit;
   @Mock
   private Cli cli;
   @Mock
@@ -69,10 +65,10 @@ public class MainTest {
 
   @Test
   public void should_execute_runner() {
-    Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
+    Main main = new Main(exit, cli, conf, runnerFactory, logs);
     main.execute();
 
-    verify(shutdown).exit(Exit.SUCCESS);
+    verify(exit).exit(Exit.SUCCESS);
     verify(runnerFactory).create(properties);
 
     verify(runner, times(1)).start();
@@ -88,11 +84,11 @@ public class MainTest {
     doThrow(e).when(runner).runAnalysis(any(Properties.class));
     when(runnerFactory.create(any(Properties.class))).thenReturn(runner);
 
-    Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
+    Main main = new Main(exit, cli, conf, runnerFactory, logs);
     main.execute();
 
     verify(runner).stop();
-    verify(shutdown).exit(Exit.ERROR);
+    verify(exit).exit(Exit.ERROR);
     verify(logs).error("Caused by: NPE");
 
   }
@@ -130,33 +126,14 @@ public class MainTest {
     doThrow(e).when(runner).runAnalysis(any(Properties.class));
     when(runnerFactory.create(any(Properties.class))).thenReturn(runner);
 
-    Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
+    Main main = new Main(exit, cli, conf, runnerFactory, logs);
     main.execute();
 
     verify(runner).stop();
-    verify(shutdown).exit(Exit.ERROR);
+    verify(exit).exit(Exit.ERROR);
     return e;
   }
 
-  @Test
-  public void should_not_stop_on_error_in_interactive_mode() throws Exception {
-    EmbeddedScanner runner = mock(EmbeddedScanner.class);
-    doThrow(new IllegalStateException("Error")).when(runner).runAnalysis(any(Properties.class));
-    when(runnerFactory.create(any(Properties.class))).thenReturn(runner);
-    when(cli.isInteractive()).thenReturn(true);
-
-    Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
-    BufferedReader inputReader = mock(BufferedReader.class);
-    when(inputReader.readLine()).thenReturn("");
-    when(shutdown.shouldExit()).thenReturn(false).thenReturn(true);
-    main.setInputReader(inputReader);
-    main.execute();
-
-    verify(runner, times(2)).runAnalysis(any(Properties.class));
-    verify(runner).stop();
-    verify(shutdown).exit(Exit.SUCCESS);
-  }
-
   @Test
   public void should_only_display_version() throws IOException {
 
@@ -164,14 +141,14 @@ public class MainTest {
     when(cli.isDisplayVersionOnly()).thenReturn(true);
     when(conf.properties()).thenReturn(p);
 
-    Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
+    Main main = new Main(exit, cli, conf, runnerFactory, logs);
     main.execute();
 
-    InOrder inOrder = Mockito.inOrder(shutdown, runnerFactory);
+    InOrder inOrder = Mockito.inOrder(exit, runnerFactory);
 
-    inOrder.verify(shutdown, times(1)).exit(Exit.SUCCESS);
+    inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
     inOrder.verify(runnerFactory, times(1)).create(p);
-    inOrder.verify(shutdown, times(1)).exit(Exit.SUCCESS);
+    inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
   }
 
   @Test
@@ -181,7 +158,7 @@ public class MainTest {
     when(cli.isDisplayVersionOnly()).thenReturn(true);
     when(conf.properties()).thenReturn(p);
 
-    Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
+    Main main = new Main(exit, cli, conf, runnerFactory, logs);
     main.execute();
     verify(logs).info("SonarQube server 5.5");
   }
@@ -209,7 +186,7 @@ public class MainTest {
     p.put(propKey, propValue);
     when(conf.properties()).thenReturn(p);
 
-    Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
+    Main main = new Main(exit, cli, conf, runnerFactory, logs);
     main.execute();
 
     // Logger used for callback should have debug enabled
@@ -222,22 +199,4 @@ public class MainTest {
     return propertiesCapture.getValue();
   }
 
-  @Test(timeout = 30000)
-  public void test_interactive_mode() throws IOException {
-    String inputStr = "qwe" + System.lineSeparator() + "qwe" + System.lineSeparator();
-    InputStream input = new ByteArrayInputStream(inputStr.getBytes(StandardCharsets.UTF_8));
-    System.setIn(input);
-    input.close();
-
-    when(cli.isInteractive()).thenReturn(true);
-    when(cli.isDebugEnabled()).thenReturn(true);
-    when(cli.isDisplayStackTrace()).thenReturn(true);
-
-    Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
-    main.execute();
-
-    verify(runner, times(1)).start();
-    verify(runner, times(3)).runAnalysis(any(Properties.class));
-    verify(runner, times(1)).stop();
-  }
 }
diff --git a/src/test/java/org/sonarsource/scanner/cli/ShutdownTest.java b/src/test/java/org/sonarsource/scanner/cli/ShutdownTest.java
deleted file mode 100644 (file)
index c9a0d52..0000000
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
- * SonarQube Scanner
- * Copyright (C) 2011-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-package org.sonarsource.scanner.cli;
-
-import com.jayway.awaitility.Duration;
-import java.util.concurrent.Callable;
-import java.util.concurrent.TimeUnit;
-import org.junit.Before;
-import org.junit.Test;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-import org.sonarsource.scanner.cli.Exit;
-import org.sonarsource.scanner.cli.Shutdown;
-
-import static com.jayway.awaitility.Awaitility.await;
-import static org.fest.assertions.Assertions.assertThat;
-import static org.mockito.Mockito.verify;
-
-public class ShutdownTest {
-  @Mock
-  private Exit exit;
-  private Shutdown shutdown;
-
-  @Before
-  public void setUp() {
-    MockitoAnnotations.initMocks(this);
-    shutdown = new Shutdown(exit, true);
-  }
-
-  @Test
-  public void testShutdown() {
-    shutdown.exit(3);
-    verify(exit).exit(3);
-  }
-
-  @Test(timeout = 60_000)
-  public void testWaitReady() throws InterruptedException {
-    shutdown = new Shutdown(exit, true, 100_000);
-    shutdown.signalReady(false);
-    assertThat(shutdown.shouldExit()).isFalse();
-
-    final Thread t = new HookCaller();
-    t.start();
-
-    await().atMost(Duration.TWO_SECONDS).pollDelay(50, TimeUnit.MILLISECONDS).until(new Callable<Boolean>() {
-      @Override
-      public Boolean call() throws Exception {
-        return t.isAlive();
-      }
-    });
-
-    assertThat(shutdown.shouldExit()).isTrue();
-
-    shutdown.signalReady(true);
-    t.join();
-  }
-
-  @Test(timeout = 60_000)
-  public void testTimeout() throws InterruptedException {
-    shutdown = new Shutdown(exit, true, 0);
-
-    Thread t = new HookCaller();
-    t.start();
-    t.join();
-  }
-
-  private class HookCaller extends Thread {
-    @Override
-    public void run() {
-      shutdown.hook.run();
-    }
-  }
-}