You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Sonar Runner - API
  3. * Copyright (C) 2011 SonarSource
  4. * dev@sonar.codehaus.org
  5. *
  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.
  10. *
  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.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.runner;
  21. import org.junit.After;
  22. import org.junit.Before;
  23. import org.junit.Test;
  24. import java.io.ByteArrayOutputStream;
  25. import java.io.PrintStream;
  26. import static org.fest.assertions.Assertions.assertThat;
  27. public class LogsTest {
  28. private PrintStream oldSysout;
  29. private PrintStream oldSyserr;
  30. private ByteArrayOutputStream baosOut;
  31. private ByteArrayOutputStream baosErr;
  32. @Before
  33. public void prepare() {
  34. oldSysout = System.out;
  35. oldSyserr = System.err;
  36. baosOut = new ByteArrayOutputStream();
  37. System.setOut(new PrintStream(baosOut));
  38. baosErr = new ByteArrayOutputStream();
  39. System.setErr(new PrintStream(baosErr));
  40. }
  41. @After
  42. public void restore() {
  43. System.setOut(oldSysout);
  44. System.setErr(oldSyserr);
  45. }
  46. @Test
  47. public void shouldLogInfo() {
  48. Logs.info("info");
  49. assertThat(baosOut.toString()).contains("INFO: info");
  50. assertThat(baosErr.toString()).isEmpty();
  51. }
  52. @Test
  53. public void shouldLogError() {
  54. Logs.error("error");
  55. assertThat(baosOut.toString()).isEmpty();
  56. assertThat(baosErr.toString()).contains("ERROR: error");
  57. }
  58. @Test
  59. public void shouldLogErrorWithoutThrowable() {
  60. Logs.error("error", null);
  61. assertThat(baosOut.toString()).isEmpty();
  62. assertThat(baosErr.toString()).contains("ERROR: error");
  63. }
  64. @Test
  65. public void shouldLogErrorWithThrowable() {
  66. Logs.error("error", new RuntimeException());
  67. assertThat(baosOut.toString()).isEmpty();
  68. assertThat(baosErr.toString()).contains("ERROR: error").contains("RuntimeException");
  69. }
  70. }