]> source.dussan.org Git - sonarqube.git/commitdiff
Remove class TxtWriter
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 7 Nov 2014 08:36:47 +0000 (09:36 +0100)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 7 Nov 2014 08:47:35 +0000 (09:47 +0100)
server/sonar-server/src/main/java/org/sonar/server/source/SourceService.java
server/sonar-server/src/main/java/org/sonar/server/source/ws/RawAction.java
server/sonar-server/src/main/java/org/sonar/server/ws/ServletResponse.java
server/sonar-server/src/test/java/org/sonar/server/source/SourceServiceTest.java
server/sonar-server/src/test/java/org/sonar/server/source/ws/RawActionTest.java
server/sonar-server/src/test/java/org/sonar/server/ws/WsTester.java
sonar-plugin-api/src/main/java/org/sonar/api/server/ws/Response.java
sonar-plugin-api/src/main/java/org/sonar/api/utils/text/TxtWriter.java [deleted file]
sonar-plugin-api/src/test/java/org/sonar/api/utils/text/TxtWriterTest.java [deleted file]

index aeac79a61eb93ac7769a221a2fb15ca329a12ca5..a9f6b926613531a65d0555ff919c872c455538c4 100644 (file)
@@ -74,6 +74,10 @@ public class SourceService implements ServerComponent {
     return deprecatedSourceDecorator.getSourceAsHtml(fileKey, from, to);
   }
 
+  /**
+   * Raw lines of source file. Returns <code>null</code> if the file does not exist
+   */
+  @CheckForNull
   public List<String> getLinesAsTxt(DbSession session, String fileKey) {
     checkPermission(fileKey);
 
@@ -81,7 +85,7 @@ public class SourceService implements ServerComponent {
     if (source != null) {
       return newArrayList(Splitter.onPattern("\r?\n|\r").split(source));
     }
-    return Collections.emptyList();
+    return null;
   }
 
   @CheckForNull
index 4c4565e875b27e2059e326600a01615b1053ad53..17eb32ac7afa3c1d3276fe09744cb0dbcf0be37b 100644 (file)
@@ -21,6 +21,8 @@
 package org.sonar.server.source.ws;
 
 import com.google.common.io.Resources;
+import org.apache.commons.io.Charsets;
+import org.apache.commons.io.IOUtils;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.RequestHandler;
 import org.sonar.api.server.ws.Response;
@@ -30,6 +32,7 @@ import org.sonar.server.db.DbClient;
 import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.source.SourceService;
 
+import java.io.IOException;
 import java.util.List;
 
 public class RawAction implements RequestHandler {
@@ -63,10 +66,12 @@ public class RawAction implements RequestHandler {
     try {
       dbClient.componentDao().getByKey(session, fileKey);
       List<String> lines = sourceService.getLinesAsTxt(session, fileKey);
-      if (lines.isEmpty()) {
-        throw new NotFoundException("File '" + fileKey + "' has no sources");
+      if (lines == null) {
+        throw new NotFoundException("File '" + fileKey + "' does not exist");
       }
-      response.newTxtWriter().values(lines).close();
+      IOUtils.writeLines(lines, "\n", response.stream().output(), Charsets.UTF_8);
+    } catch (IOException e) {
+      throw new IllegalStateException("Fail to write raw source of file " + fileKey, e);
     } finally {
       session.close();
     }
index 161d753c2a59ba89659b64c1405d4acfc81ba067..e1907701971c568f1d34711e0f97ebb3fd419b94 100644 (file)
@@ -21,7 +21,6 @@ package org.sonar.server.ws;
 
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.utils.text.JsonWriter;
-import org.sonar.api.utils.text.TxtWriter;
 import org.sonar.api.utils.text.XmlWriter;
 import org.sonar.server.plugins.MimeTypes;
 
@@ -88,12 +87,6 @@ public class ServletResponse implements Response {
     return XmlWriter.of(new OutputStreamWriter(stream.output()));
   }
 
-  @Override
-  public TxtWriter newTxtWriter() {
-    stream.setMediaType(MimeTypes.TXT);
-    return TxtWriter.of(new OutputStreamWriter(stream.output()));
-  }
-
   @Override
   public ServletStream stream() {
     return stream;
index ef632c3200f6a65c06027186dca67161b8b97c9b..b017e96d336721118652cb78eaade2adafdc5f81 100644 (file)
@@ -140,7 +140,7 @@ public class SourceServiceTest {
   }
 
   @Test
-  public void get_txt_lines() throws Exception {
+  public void getLinesAsTxt() throws Exception {
     MockUserSession.set().addComponentPermission(UserRole.CODEVIEWER, PROJECT_KEY, COMPONENT_KEY);
 
     when(snapshotSourceDao.selectSnapshotSourceByComponentKey(COMPONENT_KEY, session)).thenReturn("line1\nline2");
@@ -150,13 +150,13 @@ public class SourceServiceTest {
   }
 
   @Test
-  public void get_txt_lines_when_no_source() throws Exception {
+  public void getLinesAsTxt_returns_null_when_no_sources() throws Exception {
     MockUserSession.set().addComponentPermission(UserRole.CODEVIEWER, PROJECT_KEY, COMPONENT_KEY);
 
     when(snapshotSourceDao.selectSnapshotSourceByComponentKey(COMPONENT_KEY, session)).thenReturn(null);
 
     List<String> result = service.getLinesAsTxt(session, COMPONENT_KEY);
-    assertThat(result).isEmpty();
+    assertThat(result).isNull();
   }
 
 }
index fcac29ee4d4148af804fcbe50ed1a3870d684d1d..158a4fb5791aafc1225f31d9a99177d791f2796c 100644 (file)
@@ -81,26 +81,21 @@ public class RawActionTest {
 
     WsTester.TestRequest request = tester.newGetRequest("api/sources", "raw").setParam("key", fileKey);
     String result = request.execute().outputAsString();
-    assertThat(result).isEqualTo("public class HelloWorld {\r\n}\r\n");
-  }
-
-  @Test(expected = NotFoundException.class)
-  public void fail_to_get_txt_when_file_does_not_exists() throws Exception {
-    WsTester.TestRequest request = tester.newGetRequest("api/sources", "raw").setParam("key", "unknown");
-    request.execute();
+    assertThat(result).isEqualTo("public class HelloWorld {\n}\n");
   }
 
+  @Test
   public void fail_to_get_txt_when_no_source() throws Exception {
     String fileKey = "src/Foo.java";
     when(componentDao.getByKey(session, fileKey)).thenReturn(file);
-    when(sourceService.getLinesAsTxt(session, fileKey)).thenReturn(Collections.<String>emptyList());
+    when(sourceService.getLinesAsTxt(session, fileKey)).thenReturn(null);
 
     WsTester.TestRequest request = tester.newGetRequest("api/sources", "raw").setParam("key", fileKey);
     try {
       request.execute();
       fail();
     } catch (Exception e) {
-      assertThat(e).isInstanceOf(NotFoundException.class).hasMessage("");
+      assertThat(e).isInstanceOf(NotFoundException.class).hasMessage("File 'src/Foo.java' does not exist");
     }
   }
 }
index 07d9fa3aac964655eb65d3feeacb6a033477bffc..bae37674313a2c7fc1a0d78604b00b3a9acac4d6 100644 (file)
@@ -27,7 +27,6 @@ import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.api.server.ws.internal.ValidatingRequest;
 import org.sonar.api.utils.text.JsonWriter;
-import org.sonar.api.utils.text.TxtWriter;
 import org.sonar.api.utils.text.XmlWriter;
 import org.sonar.server.ws.WsTester.TestResponse.TestStream;
 
@@ -133,11 +132,6 @@ public class WsTester {
       return XmlWriter.of(new OutputStreamWriter(output, Charsets.UTF_8));
     }
 
-    @Override
-    public TxtWriter newTxtWriter() {
-      return TxtWriter.of(new OutputStreamWriter(output, Charsets.UTF_8));
-    }
-
     @Override
     public Stream stream() {
       if (stream == null) {
index b192078a057ec78f5610361076cc24f524a2402c..149ae4178c74b23ab935c927b2169479b6bedae0 100644 (file)
@@ -20,7 +20,6 @@
 package org.sonar.api.server.ws;
 
 import org.sonar.api.utils.text.JsonWriter;
-import org.sonar.api.utils.text.TxtWriter;
 import org.sonar.api.utils.text.XmlWriter;
 
 import java.io.OutputStream;
@@ -42,8 +41,6 @@ public interface Response {
 
   XmlWriter newXmlWriter();
 
-  TxtWriter newTxtWriter();
-
   Response noContent();
 
   Stream stream();
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/text/TxtWriter.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/text/TxtWriter.java
deleted file mode 100644 (file)
index e52f92a..0000000
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * 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.api.utils.text;
-
-import java.io.IOException;
-import java.io.Writer;
-import java.util.List;
-
-import static com.google.common.collect.Lists.newArrayList;
-
-/**
- * @since 5.0
- */
-public class TxtWriter {
-
-  private final Writer writer;
-  private static final String LINE_SEPARATOR = "\r\n";
-
-  private TxtWriter(Writer writer) {
-    this.writer = writer;
-  }
-
-  public static TxtWriter of(Writer writer) {
-    return new TxtWriter(writer);
-  }
-
-  public TxtWriter values(List<String> values) {
-    for (String value : values) {
-      write(value);
-      write(LINE_SEPARATOR);
-    }
-    return this;
-  }
-
-  public TxtWriter values(String... values) {
-    return values(newArrayList(values));
-  }
-
-  public void close() {
-    try {
-      writer.close();
-    } catch (IOException e) {
-      throw new WriterException("Fail to close TXT output", e);
-    }
-  }
-
-  private void write(String s) {
-    try {
-      writer.append(s);
-    } catch (IOException e) {
-      throw new WriterException("Fail to generate TXT with value: " + s, e);
-    }
-  }
-}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/text/TxtWriterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/text/TxtWriterTest.java
deleted file mode 100644 (file)
index dca31b0..0000000
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * 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.api.utils.text;
-
-import org.junit.Test;
-
-import java.io.BufferedReader;
-import java.io.StringReader;
-import java.io.StringWriter;
-
-import static org.fest.assertions.Assertions.assertThat;
-
-public class TxtWriterTest {
-
-  @Test
-  public void write_txt() throws Exception {
-    StringWriter output = new StringWriter();
-    TxtWriter writer = TxtWriter.of(output);
-
-    writer.values("France", "Paris");
-    writer.close();
-
-    BufferedReader reader = new BufferedReader(new StringReader(output.toString()));
-    String line1 = reader.readLine();
-    assertThat(line1).isEqualTo("France");
-
-    String line2 = reader.readLine();
-    assertThat(line2).isEqualTo("Paris");
-
-    assertThat(reader.readLine()).isNull();
-  }
-
-}