PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/test/com/facebook/buck/apple/XctoolOutputParsingTest.java

https://gitlab.com/smartether/buck
Java | 320 lines | 263 code | 42 blank | 15 comment | 0 complexity | 386a16c06f7ada31fe59553b2d4102a9 MD5 | raw file
  1. /*
  2. * Copyright 2015-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. * not use this file except in compliance with the License. You may obtain
  6. * a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations
  14. * under the License.
  15. */
  16. package com.facebook.buck.apple;
  17. import static org.hamcrest.Matchers.closeTo;
  18. import static org.hamcrest.Matchers.empty;
  19. import static org.hamcrest.Matchers.equalTo;
  20. import static org.hamcrest.Matchers.hasSize;
  21. import static org.hamcrest.Matchers.instanceOf;
  22. import static org.hamcrest.Matchers.is;
  23. import static org.hamcrest.Matchers.nullValue;
  24. import static org.junit.Assert.assertThat;
  25. import com.facebook.buck.test.TestStatusMessage;
  26. import com.facebook.buck.testutil.integration.TestDataHelper;
  27. import org.junit.Test;
  28. import java.io.Reader;
  29. import java.io.StringReader;
  30. import java.nio.charset.StandardCharsets;
  31. import java.nio.file.Files;
  32. import java.nio.file.Path;
  33. import java.util.ArrayList;
  34. import java.util.Iterator;
  35. import java.util.List;
  36. import java.util.Optional;
  37. import java.util.logging.Level;
  38. public class XctoolOutputParsingTest {
  39. private static final double EPSILON = 1e-6;
  40. private static XctoolOutputParsing.XctoolEventCallback eventCallbackAddingEventsToList(
  41. final List<Object> streamedObjects) {
  42. return new XctoolOutputParsing.XctoolEventCallback() {
  43. @Override
  44. public void handleBeginOcunitEvent(XctoolOutputParsing.BeginOcunitEvent event) {
  45. streamedObjects.add(event);
  46. }
  47. @Override
  48. public void handleEndOcunitEvent(XctoolOutputParsing.EndOcunitEvent event) {
  49. streamedObjects.add(event);
  50. }
  51. @Override
  52. public void handleBeginTestSuiteEvent(XctoolOutputParsing.BeginTestSuiteEvent event) {
  53. streamedObjects.add(event);
  54. }
  55. @Override
  56. public void handleEndTestSuiteEvent(XctoolOutputParsing.EndTestSuiteEvent event) {
  57. streamedObjects.add(event);
  58. }
  59. @Override
  60. public void handleBeginStatusEvent(XctoolOutputParsing.StatusEvent event) {
  61. streamedObjects.add(event);
  62. }
  63. @Override
  64. public void handleEndStatusEvent(XctoolOutputParsing.StatusEvent event) {
  65. streamedObjects.add(event);
  66. }
  67. @Override
  68. public void handleBeginTestEvent(XctoolOutputParsing.BeginTestEvent event) {
  69. streamedObjects.add(event);
  70. }
  71. @Override
  72. public void handleEndTestEvent(XctoolOutputParsing.EndTestEvent event) {
  73. streamedObjects.add(event);
  74. }};
  75. }
  76. @Test
  77. public void streamingSimpleSuccess() throws Exception {
  78. Path jsonPath = TestDataHelper.getTestDataDirectory(this)
  79. .resolve("xctool-output/simple-success.json");
  80. final List<Object> streamedObjects = new ArrayList<>();
  81. try (Reader jsonReader = Files.newBufferedReader(jsonPath, StandardCharsets.UTF_8)) {
  82. XctoolOutputParsing.streamOutputFromReader(
  83. jsonReader,
  84. eventCallbackAddingEventsToList(streamedObjects));
  85. }
  86. assertThat(streamedObjects, hasSize(8));
  87. Iterator<Object> iter = streamedObjects.iterator();
  88. Object nextStreamedObject;
  89. nextStreamedObject = iter.next();
  90. assertThat(nextStreamedObject, instanceOf(XctoolOutputParsing.StatusEvent.class));
  91. XctoolOutputParsing.StatusEvent beginStatusEvent = (XctoolOutputParsing.StatusEvent)
  92. nextStreamedObject;
  93. assertThat(beginStatusEvent.timestamp, closeTo(1432065853.406129, EPSILON));
  94. assertThat(beginStatusEvent.message, equalTo("Collecting info for testables..."));
  95. assertThat(beginStatusEvent.level, equalTo("Info"));
  96. nextStreamedObject = iter.next();
  97. assertThat(nextStreamedObject, instanceOf(XctoolOutputParsing.StatusEvent.class));
  98. XctoolOutputParsing.StatusEvent endStatusEvent = (XctoolOutputParsing.StatusEvent)
  99. nextStreamedObject;
  100. assertThat(endStatusEvent.timestamp, closeTo(1432065854.077704, EPSILON));
  101. assertThat(endStatusEvent.message, equalTo("Collecting info for testables..."));
  102. assertThat(endStatusEvent.level, equalTo("Info"));
  103. nextStreamedObject = iter.next();
  104. assertThat(nextStreamedObject, instanceOf(XctoolOutputParsing.BeginOcunitEvent.class));
  105. XctoolOutputParsing.BeginOcunitEvent beginOcunitEvent = (XctoolOutputParsing.BeginOcunitEvent)
  106. nextStreamedObject;
  107. assertThat(beginOcunitEvent.timestamp, closeTo(1432065854.07812, EPSILON));
  108. nextStreamedObject = iter.next();
  109. assertThat(nextStreamedObject, instanceOf(XctoolOutputParsing.BeginTestSuiteEvent.class));
  110. XctoolOutputParsing.BeginTestSuiteEvent beginTestSuiteEvent =
  111. (XctoolOutputParsing.BeginTestSuiteEvent) nextStreamedObject;
  112. assertThat(beginTestSuiteEvent.timestamp, closeTo(1432065854.736793, EPSILON));
  113. assertThat(beginTestSuiteEvent.suite, equalTo("Toplevel Test Suite"));
  114. nextStreamedObject = iter.next();
  115. assertThat(nextStreamedObject, instanceOf(XctoolOutputParsing.BeginTestEvent.class));
  116. XctoolOutputParsing.BeginTestEvent beginTestEvent =
  117. (XctoolOutputParsing.BeginTestEvent) nextStreamedObject;
  118. assertThat(beginTestEvent.timestamp, closeTo(1432065854.739917, EPSILON));
  119. assertThat(beginTestEvent.test, equalTo("-[FooXCTest testTwoPlusTwoEqualsFour]"));
  120. assertThat(beginTestEvent.className, equalTo("FooXCTest"));
  121. assertThat(beginTestEvent.methodName, equalTo("testTwoPlusTwoEqualsFour"));
  122. nextStreamedObject = iter.next();
  123. assertThat(nextStreamedObject, instanceOf(XctoolOutputParsing.EndTestEvent.class));
  124. XctoolOutputParsing.EndTestEvent endTestEvent =
  125. (XctoolOutputParsing.EndTestEvent) nextStreamedObject;
  126. assertThat(endTestEvent.timestamp, closeTo(1432065854.740184, EPSILON));
  127. assertThat(endTestEvent.test, equalTo("-[FooXCTest testTwoPlusTwoEqualsFour]"));
  128. assertThat(endTestEvent.className, equalTo("FooXCTest"));
  129. assertThat(endTestEvent.methodName, equalTo("testTwoPlusTwoEqualsFour"));
  130. assertThat(endTestEvent.succeeded, is(true));
  131. assertThat(endTestEvent.exceptions, empty());
  132. assertThat(endTestEvent.totalDuration, closeTo(0.003052949905395508, EPSILON));
  133. nextStreamedObject = iter.next();
  134. assertThat(nextStreamedObject, instanceOf(XctoolOutputParsing.EndTestSuiteEvent.class));
  135. XctoolOutputParsing.EndTestSuiteEvent endTestSuiteEvent =
  136. (XctoolOutputParsing.EndTestSuiteEvent) nextStreamedObject;
  137. assertThat(endTestSuiteEvent.timestamp, closeTo(1432065854.740343, EPSILON));
  138. assertThat(endTestSuiteEvent.suite, equalTo("Toplevel Test Suite"));
  139. assertThat(endTestSuiteEvent.testCaseCount, equalTo(1));
  140. assertThat(endTestSuiteEvent.totalFailureCount, equalTo(0));
  141. assertThat(endTestSuiteEvent.unexpectedExceptionCount, equalTo(0));
  142. assertThat(endTestSuiteEvent.totalDuration, closeTo(0.003550052642822266, EPSILON));
  143. nextStreamedObject = iter.next();
  144. assertThat(nextStreamedObject, instanceOf(XctoolOutputParsing.EndOcunitEvent.class));
  145. XctoolOutputParsing.EndOcunitEvent endOcunitEvent =
  146. (XctoolOutputParsing.EndOcunitEvent) nextStreamedObject;
  147. assertThat(endOcunitEvent.timestamp, closeTo(1432065854.806839, EPSILON));
  148. assertThat(endOcunitEvent.message, nullValue(String.class));
  149. assertThat(endOcunitEvent.succeeded, is(true));
  150. }
  151. @Test
  152. public void streamingSimpleFailure() throws Exception {
  153. Path jsonPath = TestDataHelper.getTestDataDirectory(this)
  154. .resolve("xctool-output/simple-failure.json");
  155. final List<Object> streamedObjects = new ArrayList<>();
  156. try (Reader jsonReader = Files.newBufferedReader(jsonPath, StandardCharsets.UTF_8)) {
  157. XctoolOutputParsing.streamOutputFromReader(
  158. jsonReader,
  159. eventCallbackAddingEventsToList(streamedObjects));
  160. }
  161. assertThat(streamedObjects, hasSize(8));
  162. Iterator<Object> iter = streamedObjects.iterator();
  163. Object nextStreamedObject;
  164. nextStreamedObject = iter.next();
  165. assertThat(nextStreamedObject, instanceOf(XctoolOutputParsing.StatusEvent.class));
  166. XctoolOutputParsing.StatusEvent beginStatusEvent = (XctoolOutputParsing.StatusEvent)
  167. nextStreamedObject;
  168. assertThat(beginStatusEvent.timestamp, closeTo(1432065858.258645, EPSILON));
  169. assertThat(beginStatusEvent.message, equalTo("Collecting info for testables..."));
  170. assertThat(beginStatusEvent.level, equalTo("Info"));
  171. nextStreamedObject = iter.next();
  172. assertThat(nextStreamedObject, instanceOf(XctoolOutputParsing.StatusEvent.class));
  173. XctoolOutputParsing.StatusEvent endStatusEvent = (XctoolOutputParsing.StatusEvent)
  174. nextStreamedObject;
  175. assertThat(endStatusEvent.timestamp, closeTo(1432065859.00568, EPSILON));
  176. assertThat(endStatusEvent.message, equalTo("Collecting info for testables..."));
  177. assertThat(endStatusEvent.level, equalTo("Info"));
  178. nextStreamedObject = iter.next();
  179. assertThat(nextStreamedObject, instanceOf(XctoolOutputParsing.BeginOcunitEvent.class));
  180. XctoolOutputParsing.BeginOcunitEvent beginOcunitEvent = (XctoolOutputParsing.BeginOcunitEvent)
  181. nextStreamedObject;
  182. assertThat(beginOcunitEvent.timestamp, closeTo(1432065859.006029, EPSILON));
  183. nextStreamedObject = iter.next();
  184. assertThat(nextStreamedObject, instanceOf(XctoolOutputParsing.BeginTestSuiteEvent.class));
  185. XctoolOutputParsing.BeginTestSuiteEvent beginTestSuiteEvent =
  186. (XctoolOutputParsing.BeginTestSuiteEvent) nextStreamedObject;
  187. assertThat(beginTestSuiteEvent.timestamp, closeTo(1432065859.681727, EPSILON));
  188. assertThat(beginTestSuiteEvent.suite, equalTo("Toplevel Test Suite"));
  189. nextStreamedObject = iter.next();
  190. assertThat(nextStreamedObject, instanceOf(XctoolOutputParsing.BeginTestEvent.class));
  191. XctoolOutputParsing.BeginTestEvent beginTestEvent =
  192. (XctoolOutputParsing.BeginTestEvent) nextStreamedObject;
  193. assertThat(beginTestEvent.timestamp, closeTo(1432065859.684965, EPSILON));
  194. assertThat(beginTestEvent.test, equalTo("-[FooXCTest testTwoPlusTwoEqualsFive]"));
  195. assertThat(beginTestEvent.className, equalTo("FooXCTest"));
  196. assertThat(beginTestEvent.methodName, equalTo("testTwoPlusTwoEqualsFive"));
  197. nextStreamedObject = iter.next();
  198. assertThat(nextStreamedObject, instanceOf(XctoolOutputParsing.EndTestEvent.class));
  199. XctoolOutputParsing.EndTestEvent endTestEvent =
  200. (XctoolOutputParsing.EndTestEvent) nextStreamedObject;
  201. assertThat(endTestEvent.timestamp, closeTo(1432065859.685524, EPSILON));
  202. assertThat(endTestEvent.totalDuration, closeTo(0.003522038459777832, EPSILON));
  203. assertThat(endTestEvent.test, equalTo("-[FooXCTest testTwoPlusTwoEqualsFive]"));
  204. assertThat(endTestEvent.className, equalTo("FooXCTest"));
  205. assertThat(endTestEvent.methodName, equalTo("testTwoPlusTwoEqualsFive"));
  206. assertThat(endTestEvent.succeeded, is(false));
  207. assertThat(endTestEvent.exceptions, hasSize(1));
  208. nextStreamedObject = iter.next();
  209. XctoolOutputParsing.TestException testException = endTestEvent.exceptions.get(0);
  210. assertThat(testException.lineNumber, equalTo(9));
  211. assertThat(testException.filePathInProject, equalTo("FooXCTest.m"));
  212. assertThat(
  213. testException.reason,
  214. equalTo(
  215. "((2 + 2) equal to (5)) failed: (\"4\") is not equal to (\"5\") - Two plus two " +
  216. "equals five"));
  217. assertThat(nextStreamedObject, instanceOf(XctoolOutputParsing.EndTestSuiteEvent.class));
  218. XctoolOutputParsing.EndTestSuiteEvent endTestSuiteEvent =
  219. (XctoolOutputParsing.EndTestSuiteEvent) nextStreamedObject;
  220. assertThat(endTestSuiteEvent.timestamp, closeTo(1432065859.685689, EPSILON));
  221. assertThat(endTestSuiteEvent.suite, equalTo("Toplevel Test Suite"));
  222. assertThat(endTestSuiteEvent.testCaseCount, equalTo(1));
  223. assertThat(endTestSuiteEvent.totalFailureCount, equalTo(1));
  224. assertThat(endTestSuiteEvent.unexpectedExceptionCount, equalTo(0));
  225. assertThat(endTestSuiteEvent.totalDuration, closeTo(0.003962039947509766, EPSILON));
  226. nextStreamedObject = iter.next();
  227. assertThat(nextStreamedObject, instanceOf(XctoolOutputParsing.EndOcunitEvent.class));
  228. XctoolOutputParsing.EndOcunitEvent endOcunitEvent =
  229. (XctoolOutputParsing.EndOcunitEvent) nextStreamedObject;
  230. assertThat(endOcunitEvent.timestamp, closeTo(1432065859.751992, EPSILON));
  231. assertThat(endOcunitEvent.message, nullValue(String.class));
  232. assertThat(endOcunitEvent.succeeded, is(false));
  233. }
  234. @Test
  235. public void streamingEmptyReaderDoesNotCauseFailure() {
  236. final List<Object> streamedObjects = new ArrayList<>();
  237. XctoolOutputParsing.streamOutputFromReader(
  238. new StringReader(""),
  239. eventCallbackAddingEventsToList(streamedObjects));
  240. assertThat(streamedObjects, is(empty()));
  241. }
  242. @Test
  243. public void validEventParsesToStatusMessage() {
  244. XctoolOutputParsing.StatusEvent statusEvent = new XctoolOutputParsing.StatusEvent();
  245. statusEvent.message = "Hello world";
  246. statusEvent.level = "Info";
  247. statusEvent.timestamp = 123.456;
  248. Optional<TestStatusMessage> testStatusMessage =
  249. XctoolOutputParsing.testStatusMessageForStatusEvent(statusEvent);
  250. assertThat(
  251. testStatusMessage,
  252. equalTo(Optional.of(TestStatusMessage.of("Hello world", Level.INFO, 123456L))));
  253. }
  254. @Test
  255. public void invalidEventLevelParsesToAbsent() {
  256. XctoolOutputParsing.StatusEvent statusEvent = new XctoolOutputParsing.StatusEvent();
  257. statusEvent.message = "Hello world";
  258. statusEvent.level = "BALEETED";
  259. statusEvent.timestamp = 123.456;
  260. Optional<TestStatusMessage> testStatusMessage =
  261. XctoolOutputParsing.testStatusMessageForStatusEvent(statusEvent);
  262. assertThat(
  263. testStatusMessage,
  264. equalTo(Optional.empty()));
  265. }
  266. @Test
  267. public void invalidEventMessageParsesToAbsent() {
  268. XctoolOutputParsing.StatusEvent statusEvent = new XctoolOutputParsing.StatusEvent();
  269. statusEvent.message = null;
  270. statusEvent.level = "Info";
  271. statusEvent.timestamp = 123.456;
  272. Optional<TestStatusMessage> testStatusMessage =
  273. XctoolOutputParsing.testStatusMessageForStatusEvent(statusEvent);
  274. assertThat(
  275. testStatusMessage,
  276. equalTo(Optional.empty()));
  277. }
  278. }