Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

WsTestUtil.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  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 License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.scanner;
  21. import java.io.InputStream;
  22. import java.io.Reader;
  23. import javax.annotation.Nullable;
  24. import org.apache.commons.lang.StringUtils;
  25. import org.mockito.ArgumentMatcher;
  26. import org.sonar.scanner.bootstrap.DefaultScannerWsClient;
  27. import org.sonarqube.ws.client.WsRequest;
  28. import org.sonarqube.ws.client.WsResponse;
  29. import static org.mockito.ArgumentMatchers.any;
  30. import static org.mockito.ArgumentMatchers.argThat;
  31. import static org.mockito.Mockito.mock;
  32. import static org.mockito.Mockito.verify;
  33. import static org.mockito.Mockito.when;
  34. public class WsTestUtil {
  35. public static void mockStream(DefaultScannerWsClient mock, String path, InputStream is) {
  36. WsResponse response = mock(WsResponse.class);
  37. when(response.contentStream()).thenReturn(is);
  38. when(mock.call(argThat(new RequestMatcher(path)))).thenReturn(response);
  39. }
  40. public static void mockStream(DefaultScannerWsClient mock, InputStream is) {
  41. WsResponse response = mock(WsResponse.class);
  42. when(response.contentStream()).thenReturn(is);
  43. when(mock.call(any(WsRequest.class))).thenReturn(response);
  44. }
  45. public static void mockReader(DefaultScannerWsClient mock, Reader reader) {
  46. WsResponse response = mock(WsResponse.class);
  47. when(response.contentReader()).thenReturn(reader);
  48. when(mock.call(any(WsRequest.class))).thenReturn(response);
  49. }
  50. public static void mockReader(DefaultScannerWsClient mock, String path, Reader reader, Reader... others) {
  51. WsResponse response = mock(WsResponse.class);
  52. when(response.contentReader()).thenReturn(reader);
  53. WsResponse[] otherResponses = new WsResponse[others.length];
  54. for (int i = 0; i < others.length; i++) {
  55. WsResponse otherResponse = mock(WsResponse.class);
  56. when(otherResponse.contentReader()).thenReturn(others[i]);
  57. otherResponses[i] = otherResponse;
  58. }
  59. when(mock.call(argThat(new RequestMatcher(path)))).thenReturn(response, otherResponses);
  60. }
  61. public static void mockException(DefaultScannerWsClient mock, Exception e) {
  62. when(mock.call(any(WsRequest.class))).thenThrow(e);
  63. }
  64. public static void mockException(DefaultScannerWsClient mock, String path, Exception e) {
  65. when(mock.call(argThat(new RequestMatcher(path)))).thenThrow(e);
  66. }
  67. public static void verifyCall(DefaultScannerWsClient mock, String path) {
  68. verify(mock).call(argThat(new RequestMatcher(path)));
  69. }
  70. private static class RequestMatcher implements ArgumentMatcher<WsRequest> {
  71. private String path;
  72. public RequestMatcher(String path) {
  73. this.path = path;
  74. }
  75. @Override
  76. public boolean matches(@Nullable WsRequest item) {
  77. if (item == null) {
  78. return false;
  79. }
  80. return StringUtils.equals(item.getPath(), path);
  81. }
  82. }
  83. }