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

/etlunit-core/src/test/java/org/bitbucket/bradleysmithllc/etlunit/test/ClassBroadcasterimplTest.java

https://bitbucket.org/bradleysmithllc/etl-unit
Java | 321 lines | 252 code | 47 blank | 22 comment | 7 complexity | 8f56d179b6da8cc1d123f7a378f08207 MD5 | raw file
  1. package org.bitbucket.bradleysmithllc.etlunit.test;
  2. /*
  3. * #%L
  4. * etlunit-core
  5. * %%
  6. * Copyright (C) 2010 - 2014 bradleysmithllc
  7. * %%
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. * #L%
  20. */
  21. import org.bitbucket.bradleysmithllc.etlunit.*;
  22. import org.bitbucket.bradleysmithllc.etlunit.listener.ClassBroadcaster;
  23. import org.bitbucket.bradleysmithllc.etlunit.listener.ClassBroadcasterImpl;
  24. import org.bitbucket.bradleysmithllc.etlunit.listener.ClassListener;
  25. import org.bitbucket.bradleysmithllc.etlunit.context.VariableContext;
  26. import org.bitbucket.bradleysmithllc.etlunit.parser.*;
  27. import org.bitbucket.bradleysmithllc.etlunit.util.IOUtils;
  28. import org.bitbucket.bradleysmithllc.etlunit.util.EtlUnitStringUtils;
  29. import org.junit.Assert;
  30. import org.junit.Test;
  31. import java.io.File;
  32. import java.io.FileFilter;
  33. import java.util.List;
  34. public class ClassBroadcasterimplTest
  35. {
  36. int testCount = 0;
  37. @Test
  38. public void broadcasting()
  39. {
  40. String file = getClass().getResource("/broadcastTests").getFile();
  41. File source = new File(file);
  42. source.listFiles(new FileFilter()
  43. {
  44. @Override
  45. public boolean accept(File file)
  46. {
  47. if (file.getName().endsWith(".etlunit"))
  48. {
  49. test(
  50. file,
  51. new File(file.getParentFile(), file.getName().substring(0, file.getName().length() - 7) + "reporter"),
  52. new File(file.getParentFile(), file.getName().substring(0, file.getName().length() - 7) + "events"),
  53. new File(file.getParentFile(), file.getName().substring(0, file.getName().length() - 7) + "scan_events"),
  54. new File(file.getParentFile(), file.getName().substring(0, file.getName().length() - 7) + "scan_reporter")
  55. );
  56. }
  57. return false;
  58. }
  59. private void test(File file, File reporter, File full_events, File scan_events, File scan_reporter)
  60. {
  61. Assert.assertTrue(file.getAbsolutePath(), file.exists());
  62. Assert.assertTrue(reporter.getAbsolutePath(), reporter.exists());
  63. Assert.assertTrue(full_events.getAbsolutePath(), full_events.exists());
  64. Assert.assertTrue(scan_events.getAbsolutePath(), scan_events.exists());
  65. Assert.assertTrue(scan_reporter.getAbsolutePath(), scan_reporter.exists());
  66. final StringBuilder buffer = new StringBuilder();
  67. final StringBuilder reporterBuffer = new StringBuilder();
  68. try
  69. {
  70. List<ETLTestClass> list = ETLTestParser.load(IOUtils.readFileToString(file));
  71. ClassBroadcaster broadcaster = new ClassBroadcasterImpl(new PassThroughClassLocatorImpl(list),
  72. new ClassDirector()
  73. {
  74. @Override
  75. public void beginBroadcast()
  76. {
  77. testCount = 0;
  78. buffer.append("classDirector.beginBroadcast\n");
  79. }
  80. @Override
  81. public response_code accept(ETLTestClass cl)
  82. {
  83. buffer.append("classDirector.accept(class): " + cl.getName() + "\n");
  84. return cl.getName().contains("skip") ? response_code.reject : response_code.accept;
  85. }
  86. @Override
  87. public response_code accept(ETLTestMethod mt)
  88. {
  89. buffer.append("classDirector.accept(method): " + mt.getName() + "\n");
  90. if (!mt.getName().contains("skip"))
  91. {
  92. testCount++;
  93. return response_code.accept;
  94. }
  95. else
  96. {
  97. return response_code.reject;
  98. }
  99. }
  100. @Override
  101. public response_code accept(ETLTestOperation op)
  102. {
  103. buffer.append("classDirector.accept(operation): " + op.getOperationName() + "\n");
  104. return op.getOperationName().contains("skip") ? response_code.reject : response_code.accept;
  105. }
  106. @Override
  107. public void endBroadcast()
  108. {
  109. buffer.append("classDirector.endBroadcast\n");
  110. }
  111. },
  112. new ClassListener()
  113. {
  114. @Override
  115. public void beginTests(VariableContext context, int executorId) {
  116. buffer.append("classListener.beginTests(exe)\n");
  117. }
  118. @Override
  119. public void beginTests(VariableContext context) throws TestExecutionError {
  120. buffer.append("classListener.beginTests()\n");
  121. }
  122. @Override
  123. public void beginPackage(ETLTestPackage name, VariableContext context, final int executor) throws TestAssertionFailure, TestExecutionError, TestWarning {
  124. buffer.append("classListener.beginPackage: " + name.getPackageName() + "\n");
  125. }
  126. @Override
  127. public void endPackage(ETLTestPackage name, VariableContext context, final int executor) throws TestAssertionFailure, TestExecutionError, TestWarning {
  128. buffer.append("classListener.endPackage: " + name.getPackageName() + "\n");
  129. }
  130. @Override
  131. public void endTests(VariableContext context, int executorId) {
  132. buffer.append("classListener.endTests(exe)\n");
  133. }
  134. @Override
  135. public void endTests(VariableContext context) {
  136. buffer.append("classListener.endTests()\n");
  137. }
  138. @Override
  139. public void begin(ETLTestClass cl, final VariableContext context, final int executor)
  140. {
  141. buffer.append("classListener.begin(class): " + cl.getName() + "\n");
  142. }
  143. @Override
  144. public void declare(ETLTestVariable var, final VariableContext context, final int executor)
  145. {
  146. buffer.append("classListener.declare: " + var.getName() + "\n");
  147. }
  148. @Override
  149. public void begin(ETLTestMethod mt, final VariableContext context, final int executor)
  150. {
  151. buffer.append("classListener.begin(method): " + mt.getName() + "\n");
  152. }
  153. @Override
  154. public void begin(ETLTestMethod mt, ETLTestOperation op, ETLTestValueObject parameters, VariableContext vcontext, ExecutionContext econtext, final int executor)
  155. throws TestAssertionFailure, TestExecutionError, TestWarning
  156. {
  157. buffer.append("classListener.begin(operation): " + op.getOperationName() + "\n");
  158. }
  159. @Override
  160. public action_code process(ETLTestMethod mt, ETLTestOperation op, ETLTestValueObject obj, final VariableContext context, ExecutionContext econtext, final int executor)
  161. throws TestAssertionFailure, TestExecutionError, TestWarning
  162. {
  163. String operationName = op.getOperationName();
  164. buffer.append("classListener.process(operation): " + operationName + "\n");
  165. if (operationName.equals("testAssertionFailure"))
  166. {
  167. throw new TestAssertionFailure(op.getOperands().getValueAsMap().get("message").getValueAsString());
  168. }
  169. else if (operationName.equals("testExecutionError"))
  170. {
  171. throw new TestExecutionError(op.getOperands().getValueAsMap().get("message").getValueAsString());
  172. }
  173. else if (operationName.equals("testWarning"))
  174. {
  175. throw new TestWarning(op.getOperands().getValueAsMap().get("message").getValueAsString());
  176. }
  177. return action_code.handled;
  178. }
  179. @Override
  180. public void end(ETLTestMethod mt, ETLTestOperation op, ETLTestValueObject parameters, VariableContext vcontext, ExecutionContext econtext, final int executor)
  181. throws TestAssertionFailure, TestExecutionError, TestWarning
  182. {
  183. buffer.append("classListener.end(operation): " + op.getOperationName() + "\n");
  184. }
  185. @Override
  186. public void end(ETLTestMethod mt, final VariableContext context, final int executor)
  187. {
  188. buffer.append("classListener.end(method): " + mt.getName() + "\n");
  189. }
  190. @Override
  191. public void end(ETLTestClass cl, final VariableContext context, final int executor)
  192. {
  193. buffer.append("classListener.end(class): " + cl.getName() + "\n");
  194. }
  195. },
  196. new MapLocal(),
  197. new PrintWriterLog()
  198. );
  199. MyStatusReporter statusReporter = new MyStatusReporter(reporterBuffer);
  200. ETLTestCases etlCases = broadcaster.preScan(statusReporter);
  201. // assert on the pre scan events
  202. Assert.assertEquals(scan_events.getName(),
  203. EtlUnitStringUtils.convertEol(IOUtils.readFileToString(scan_events)),
  204. buffer.toString());
  205. Assert.assertEquals(scan_reporter.getName(),
  206. EtlUnitStringUtils.convertEol(IOUtils.readFileToString(scan_reporter)),
  207. reporterBuffer.toString());
  208. Assert.assertEquals("Test Count failed: " + file, testCount, etlCases.getTestCount());
  209. // clear the buffers for the actual test
  210. reporterBuffer.setLength(0);
  211. buffer.setLength(0);
  212. // broadcast for real
  213. broadcaster.broadcast(statusReporter, etlCases);
  214. Assert.assertEquals(full_events.getName(),
  215. EtlUnitStringUtils.convertEol(IOUtils.readFileToString(full_events)),
  216. buffer.toString());
  217. Assert.assertEquals(reporter.getName(),
  218. EtlUnitStringUtils.convertEol(IOUtils.readFileToString(reporter)),
  219. reporterBuffer.toString());
  220. }
  221. catch (Exception e)
  222. {
  223. throw new RuntimeException(e);
  224. }
  225. }
  226. });
  227. }
  228. private static class MyStatusReporter implements StatusReporter {
  229. private final StringBuilder reporterBuffer;
  230. public MyStatusReporter(StringBuilder reporterBuffer) {
  231. this.reporterBuffer = reporterBuffer;
  232. }
  233. @Override
  234. public void scanStarted()
  235. {
  236. reporterBuffer.append("statusReporter.scanStarted()\n");
  237. }
  238. @Override
  239. public void scanCompleted()
  240. {
  241. reporterBuffer.append("statusReporter.scanCompleted()\n");
  242. }
  243. @Override
  244. public void testsStarted(int numTestsSelected)
  245. {
  246. reporterBuffer.append("statusReporter.testsStarted(" + numTestsSelected + ")\n");
  247. }
  248. @Override
  249. public void testClassAccepted(ETLTestClass method)
  250. {
  251. reporterBuffer.append("statusReporter.testClassAccepted(" + method.getName() + ")\n");
  252. }
  253. @Override
  254. public void testMethodAccepted(ETLTestMethod method)
  255. {
  256. reporterBuffer.append("statusReporter.testMethodAccepted(" + method.getName() + ")\n");
  257. }
  258. @Override
  259. public void testBeginning(ETLTestMethod method)
  260. {
  261. reporterBuffer.append("statusReporter.testBeginning(" + method.getName() + ")\n");
  262. }
  263. @Override
  264. public void testCompleted(ETLTestMethod method, CompletionStatus status)
  265. {
  266. reporterBuffer.append("statusReporter.testCompleted(" + method.getName() + ", " + status.toString() + ")\n");
  267. }
  268. @Override
  269. public void testsCompleted()
  270. {
  271. reporterBuffer.append("statusReporter.testsCompleted()\n");
  272. }
  273. }
  274. }