]> source.dussan.org Git - sonarqube.git/commitdiff
Improve Java WS Client of /api/sources/show and /api/source/show
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 19 May 2014 09:04:39 +0000 (11:04 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 19 May 2014 09:04:39 +0000 (11:04 +0200)
sonar-ws-client/src/main/java/org/sonar/wsclient/source/Scm.java
sonar-ws-client/src/main/java/org/sonar/wsclient/source/Source.java
sonar-ws-client/src/main/java/org/sonar/wsclient/source/SourceClient.java
sonar-ws-client/src/main/java/org/sonar/wsclient/source/SourceScmQuery.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/source/SourceShowQuery.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/source/internal/DefaultScm.java
sonar-ws-client/src/main/java/org/sonar/wsclient/source/internal/DefaultSource.java
sonar-ws-client/src/main/java/org/sonar/wsclient/source/internal/DefaultSourceClient.java
sonar-ws-client/src/test/java/org/sonar/wsclient/source/internal/DefaultSourceClientTest.java

index cf420820659059596bdfa0d60d3c761e5814ba82..27a2bb6b2d85b4f12286702525b780132ff3c623 100644 (file)
@@ -26,7 +26,8 @@ import java.util.Date;
 
 public interface Scm {
 
-  long index();
+  @CheckForNull
+  Integer lineIndex();
 
   @CheckForNull
   String author();
index f1d1cf7b032d44dc9ce1e30413bc9984e0914171..2d5b807fce1b10c2bf2b4fc90a2e7ea03a5f999c 100644 (file)
 
 package org.sonar.wsclient.source;
 
+import javax.annotation.CheckForNull;
+
 /**
  * @since 4.4
  */
 public interface Source {
 
-  long index();
+  @CheckForNull
+  Integer lineIndex();
 
-  String line();
+  @CheckForNull
+  String lineAsHtml();
 
 }
index 36b65251bd4f7a00e17e7505d61fe7f8928c9b65..6f5bb490def084e130d9745b7fdd8d109ef67882 100644 (file)
 
 package org.sonar.wsclient.source;
 
-import javax.annotation.Nullable;
-
 import java.util.List;
 
 /**
  * Display sources information
+ *
  * @since 4.4
  */
 public interface SourceClient {
 
-  List<Source> show(String key, @Nullable String from, @Nullable String to);
+  List<Source> show(SourceShowQuery query);
 
-  List<Scm> scm(String key, @Nullable String from, @Nullable String to, @Nullable Boolean groupCommits);
+  List<Scm> scm(SourceScmQuery query);
 
 }
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/source/SourceScmQuery.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/source/SourceScmQuery.java
new file mode 100644 (file)
index 0000000..70d5124
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.sonar.wsclient.source;
+
+import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
+
+public class SourceScmQuery {
+
+  private String key;
+  private String from;
+  private String to;
+  private Boolean groupCommits;
+
+  private SourceScmQuery() {
+    // Nothing here
+  }
+
+  public String key() {
+    return key;
+  }
+
+  public SourceScmQuery setKey(String key) {
+    this.key = key;
+    return this;
+  }
+
+  @CheckForNull
+  public String from() {
+    return from;
+  }
+
+  public SourceScmQuery setFrom(@Nullable String from) {
+    this.from = from;
+    return this;
+  }
+
+  @CheckForNull
+  public String to() {
+    return to;
+  }
+
+  public SourceScmQuery setTo(@Nullable String to) {
+    this.to = to;
+    return this;
+  }
+
+  @CheckForNull
+  public Boolean groupCommits() {
+    return groupCommits;
+  }
+
+  public SourceScmQuery setGroupCommits(@Nullable Boolean groupCommits) {
+    this.groupCommits = groupCommits;
+    return this;
+  }
+
+  public static SourceScmQuery create(){
+    return new SourceScmQuery();
+  }
+}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/source/SourceShowQuery.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/source/SourceShowQuery.java
new file mode 100644 (file)
index 0000000..d56241a
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.sonar.wsclient.source;
+
+import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
+
+public class SourceShowQuery {
+
+  private String key;
+  private String from;
+  private String to;
+
+  private SourceShowQuery() {
+    // Nothing here
+  }
+
+  public String key() {
+    return key;
+  }
+
+  public SourceShowQuery setKey(String key) {
+    this.key = key;
+    return this;
+  }
+
+  @CheckForNull
+  public String from() {
+    return from;
+  }
+
+  public SourceShowQuery setFrom(@Nullable String from) {
+    this.from = from;
+    return this;
+  }
+
+  @CheckForNull
+  public String to() {
+    return to;
+  }
+
+  public SourceShowQuery setTo(@Nullable String to) {
+    this.to = to;
+    return this;
+  }
+
+  public static SourceShowQuery create(){
+    return new SourceShowQuery();
+  }
+
+}
index b563f5ec39a003531d1d59e45e0b0d25ffdc2795..e6cfc870a95b0a6e83cfb90331b85487d6c45cf1 100644 (file)
@@ -37,10 +37,16 @@ public class DefaultScm implements Scm {
   }
 
   @Override
-  public long index() {
-    return (Long) json.get(0);
+  @CheckForNull
+  public Integer lineIndex() {
+    Object value = json.get(0);
+    if (value != null) {
+      return ((Long) value).intValue();
+    }
+    return null;
   }
 
+
   @Override
   @CheckForNull
   public String author() {
index 8794a5f8bff36a9974d07c08a721d2a3699064da..37a4f7db6467ece87ad244ab4214b185386593a5 100644 (file)
@@ -22,6 +22,8 @@ package org.sonar.wsclient.source.internal;
 
 import org.sonar.wsclient.source.Source;
 
+import javax.annotation.CheckForNull;
+
 import java.util.List;
 
 public class DefaultSource implements Source {
@@ -33,12 +35,18 @@ public class DefaultSource implements Source {
   }
 
   @Override
-  public long index() {
-    return (Long) json.get(0);
+  @CheckForNull
+  public Integer lineIndex() {
+    Object value = json.get(0);
+    if (value != null) {
+      return ((Long) value).intValue();
+    }
+    return null;
   }
 
   @Override
-  public String line() {
+  @CheckForNull
+  public String lineAsHtml() {
     return (String) json.get(1);
   }
 }
index d5e0acffe2684bdfa82546b79b923b47064d4358..db20901c1b1952e04e6121abeb550a189e58cb6c 100644 (file)
@@ -23,11 +23,7 @@ package org.sonar.wsclient.source.internal;
 import org.json.simple.JSONValue;
 import org.sonar.wsclient.internal.EncodingUtils;
 import org.sonar.wsclient.internal.HttpRequestFactory;
-import org.sonar.wsclient.source.Scm;
-import org.sonar.wsclient.source.Source;
-import org.sonar.wsclient.source.SourceClient;
-
-import javax.annotation.Nullable;
+import org.sonar.wsclient.source.*;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -42,8 +38,8 @@ public class DefaultSourceClient implements SourceClient {
   }
 
   @Override
-  public List<Source> show(String key, @Nullable String from, @Nullable String to) {
-    Map<String, Object> params = EncodingUtils.toMap("key", key, "from", from, "to", to);
+  public List<Source> show(SourceShowQuery query) {
+    Map<String, Object> params = EncodingUtils.toMap("key", query.key(), "from", query.from(), "to", query.to());
     String json = requestFactory.get("/api/sources/show", params);
 
     List<Source> sources = new ArrayList<Source>();
@@ -58,9 +54,9 @@ public class DefaultSourceClient implements SourceClient {
   }
 
   @Override
-  public List<Scm> scm(String key, @Nullable String from, @Nullable String to, @Nullable Boolean groupCommits) {
-    Map<String, Object> params = EncodingUtils.toMap("key", key, "from", from, "to", to);
-    params.put("group_commits", groupCommits);
+  public List<Scm> scm(SourceScmQuery query) {
+    Map<String, Object> params = EncodingUtils.toMap("key", query.key(), "from", query.from(), "to", query.to());
+    params.put("group_commits", query.groupCommits());
     String json = requestFactory.get("/api/sources/scm", params);
 
     List<Scm> result = new ArrayList<Scm>();
index 4abb642671cd355988db607fc7fcc535c80e9e59..b9019f6a03c85e693effe559aefce3a31bf8bc8c 100644 (file)
@@ -26,9 +26,7 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.sonar.wsclient.MockHttpServerInterceptor;
 import org.sonar.wsclient.internal.HttpRequestFactory;
-import org.sonar.wsclient.source.Scm;
-import org.sonar.wsclient.source.Source;
-import org.sonar.wsclient.source.SourceClient;
+import org.sonar.wsclient.source.*;
 
 import java.io.IOException;
 import java.util.List;
@@ -46,12 +44,12 @@ public class DefaultSourceClientTest {
     httpServer.stubResponseBody(Resources.toString(Resources.getResource(this.getClass(), "DefaultSourceClientTest/show_source.json"), Charsets.UTF_8));
 
     SourceClient client = new DefaultSourceClient(requestFactory);
-    List<Source> sources = client.show("MyFile", "20", "25");
+    List<Source> sources = client.show(SourceShowQuery.create().setKey("MyFile").setFrom("20").setTo("25"));
 
     assertThat(httpServer.requestedPath()).isEqualTo("/api/sources/show?key=MyFile&from=20&to=25");
     assertThat(sources).hasSize(6);
-    assertThat(sources.get(0).index()).isEqualTo(20);
-    assertThat(sources.get(0).line()).isEqualTo("<span class=\"k\">package </span>org.sonar.check;");
+    assertThat(sources.get(0).lineIndex()).isEqualTo(20);
+    assertThat(sources.get(0).lineAsHtml()).isEqualTo("<span class=\"k\">package </span>org.sonar.check;");
   }
 
   @Test
@@ -60,11 +58,11 @@ public class DefaultSourceClientTest {
     httpServer.stubResponseBody(Resources.toString(Resources.getResource(this.getClass(), "DefaultSourceClientTest/return_scm.json"), Charsets.UTF_8));
 
     SourceClient client = new DefaultSourceClient(requestFactory);
-    List<Scm> result = client.scm("MyFile", "1", "3", true);
+    List<Scm> result = client.scm(SourceScmQuery.create().setKey("MyFile").setFrom("1").setTo("3").setGroupCommits(true));
 
     assertThat(httpServer.requestedPath()).isEqualTo("/api/sources/scm?key=MyFile&from=1&to=3&group_commits=true");
     assertThat(result).hasSize(3);
-    assertThat(result.get(0).index()).isEqualTo(1);
+    assertThat(result.get(0).lineIndex()).isEqualTo(1);
     assertThat(result.get(0).author()).isEqualTo("julien");
     assertThat(result.get(0).date().getTime()).isEqualTo(1363129200000L);
   }