/projects/TropixJobQueue/src/test/edu/umn/msi/tropix/common/jobqueue/test/DisposableResourceTrackerSupplierImplTest.java

https://github.com/jmchilton/TINT · Java · 210 lines · 165 code · 30 blank · 15 comment · 23 complexity · be3a6909af9afe55d35b56cbfc3836e1 MD5 · raw file

  1. /********************************************************************************
  2. * Copyright (c) 2009 Regents of the University of Minnesota
  3. *
  4. * This Software was written at the Minnesota Supercomputing Institute
  5. * http://msi.umn.edu
  6. *
  7. * All rights reserved. The following statement of license applies
  8. * only to this file, and and not to the other files distributed with it
  9. * or derived therefrom. This file is made available under the terms of
  10. * the Eclipse Public License v1.0 which accompanies this distribution,
  11. * and is available at http://www.eclipse.org/legal/epl-v10.html
  12. *
  13. * Contributors:
  14. * Minnesota Supercomputing Institute - initial API and implementation
  15. *******************************************************************************/
  16. package edu.umn.msi.tropix.common.jobqueue.test;
  17. import static org.easymock.EasyMock.createMock;
  18. import static org.easymock.EasyMock.expect;
  19. import java.io.ByteArrayInputStream;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.io.OutputStream;
  23. import org.apache.commons.io.FileUtils;
  24. import org.apache.commons.io.IOUtils;
  25. import org.easymock.EasyMock;
  26. import org.testng.annotations.BeforeMethod;
  27. import org.testng.annotations.Test;
  28. import com.google.common.base.Function;
  29. import com.google.common.base.Supplier;
  30. import edu.umn.msi.tropix.common.io.DisposableResource;
  31. import edu.umn.msi.tropix.common.jobqueue.jobprocessors.DisposableResourceTracker;
  32. import edu.umn.msi.tropix.common.jobqueue.jobprocessors.DisposableResourceTrackerSupplierImpl;
  33. import edu.umn.msi.tropix.common.test.EasyMockUtils;
  34. public class DisposableResourceTrackerSupplierImplTest {
  35. private DisposableResourceTrackerSupplierImpl supplier = null;
  36. private Function<File, DisposableResource> factory = null;
  37. private Supplier<File> fileSupplier = null;
  38. private DisposableResourceTracker tracker = null;
  39. @BeforeMethod(groups = "unit")
  40. public void init() {
  41. supplier = new DisposableResourceTrackerSupplierImpl();
  42. factory = EasyMockUtils.createMockFunction();
  43. fileSupplier = EasyMockUtils.createMockSupplier();
  44. supplier.setDisposableResourceFactory(factory);
  45. supplier.setTempFileSupplier(fileSupplier);
  46. tracker = supplier.get();
  47. }
  48. public void replay() {
  49. EasyMock.replay(factory, fileSupplier);
  50. }
  51. public void reset() {
  52. EasyMockUtils.verifyAndReset(factory, fileSupplier);
  53. }
  54. @Test(groups = "unit")
  55. public void addStream() throws IOException {
  56. add(AddType.STREAM);
  57. }
  58. @Test(groups = "unit")
  59. public void addBytes() throws IOException {
  60. add(AddType.BYTES);
  61. }
  62. @Test(groups = "unit")
  63. public void addNewStream() throws IOException {
  64. add(AddType.NEW_STREAM);
  65. }
  66. @Test(groups = "unit")
  67. public void addCopy() throws IOException {
  68. add(AddType.COPY);
  69. }
  70. @Test(groups = "unit", expectedExceptions = IllegalStateException.class)
  71. public void invalidAddStream() throws IOException {
  72. invalidAdd(AddType.STREAM, ExceptionType.CREATE_FILE);
  73. }
  74. @Test(groups = "unit", expectedExceptions = IllegalStateException.class)
  75. public void invalidAddBytes() throws IOException {
  76. invalidAdd(AddType.BYTES, ExceptionType.CREATE_FILE);
  77. }
  78. @Test(groups = "unit", expectedExceptions = IllegalStateException.class)
  79. public void invalidAddNewStream() throws IOException {
  80. invalidAdd(AddType.NEW_STREAM, ExceptionType.CREATE_FILE);
  81. }
  82. @Test(groups = "unit", expectedExceptions = IllegalStateException.class)
  83. public void invalidAddCopy() throws IOException {
  84. invalidAdd(AddType.COPY, ExceptionType.CREATE_FILE);
  85. }
  86. @Test(groups = "unit", expectedExceptions = IllegalStateException.class)
  87. public void invalidResourceAddStream() throws IOException {
  88. invalidAdd(AddType.STREAM, ExceptionType.CREATE_RESOURCE);
  89. }
  90. @Test(groups = "unit", expectedExceptions = IllegalStateException.class)
  91. public void invalidResourceAddBytes() throws IOException {
  92. invalidAdd(AddType.BYTES, ExceptionType.CREATE_RESOURCE);
  93. }
  94. @Test(groups = "unit", expectedExceptions = IllegalStateException.class)
  95. public void invalidResourceAddNewStream() throws IOException {
  96. invalidAdd(AddType.NEW_STREAM, ExceptionType.CREATE_RESOURCE);
  97. }
  98. @Test(groups = "unit", expectedExceptions = IllegalStateException.class)
  99. public void invalidResourceAddCopy() throws IOException {
  100. invalidAdd(AddType.COPY, ExceptionType.CREATE_RESOURCE);
  101. }
  102. enum AddType {
  103. STREAM, BYTES, NEW_STREAM, COPY;
  104. }
  105. enum ExceptionType {
  106. CREATE_FILE, CREATE_RESOURCE;
  107. }
  108. public void invalidAdd(final AddType addType, final ExceptionType exceptionType) throws IOException {
  109. final File tempFile = File.createTempFile("tpxtest", "");
  110. if(exceptionType.equals(ExceptionType.CREATE_FILE)) {
  111. expect(fileSupplier.get()).andThrow(new NullPointerException());
  112. } else if(exceptionType.equals(ExceptionType.CREATE_RESOURCE)) {
  113. expect(fileSupplier.get()).andReturn(tempFile);
  114. expect(factory.apply(tempFile)).andThrow(new IllegalArgumentException());
  115. }
  116. replay();
  117. final byte[] bytes = "Hello World".getBytes();
  118. if(addType.equals(AddType.STREAM)) {
  119. tracker.add(new ByteArrayInputStream(bytes));
  120. } else if(addType.equals(AddType.BYTES)) {
  121. tracker.add(bytes);
  122. } else if(addType.equals(AddType.NEW_STREAM)) {
  123. final OutputStream outputStream = tracker.newStream();
  124. IOUtils.copy(new ByteArrayInputStream(bytes), outputStream);
  125. outputStream.close();
  126. } else if(addType.equals(AddType.COPY)) {
  127. final File fileToCopy = File.createTempFile("tpxtest", "");
  128. FileUtils.writeStringToFile(fileToCopy, "Hello World");
  129. tracker.addCopy(fileToCopy);
  130. }
  131. }
  132. public void add(final AddType addType) throws IOException {
  133. final File tempFile = File.createTempFile("tpxtest", "");
  134. final DisposableResource disposableResource = createMock(DisposableResource.class);
  135. expect(fileSupplier.get()).andReturn(tempFile);
  136. expect(factory.apply(tempFile)).andReturn(disposableResource);
  137. replay();
  138. final byte[] bytes = "Hello World".getBytes();
  139. if(addType.equals(AddType.STREAM)) {
  140. tracker.add(new ByteArrayInputStream(bytes));
  141. } else if(addType.equals(AddType.BYTES)) {
  142. tracker.add(bytes);
  143. } else if(addType.equals(AddType.NEW_STREAM)) {
  144. final OutputStream outputStream = tracker.newStream();
  145. IOUtils.copy(new ByteArrayInputStream(bytes), outputStream);
  146. outputStream.close();
  147. } else if(addType.equals(AddType.COPY)) {
  148. final File fileToCopy = File.createTempFile("tpxtest", "");
  149. FileUtils.writeStringToFile(fileToCopy, "Hello World");
  150. tracker.addCopy(fileToCopy);
  151. }
  152. reset();
  153. assert FileUtils.readFileToString(tempFile).equals("Hello World");
  154. assert tracker.getResources().size() == 1;
  155. assert tracker.getResources().get(0) == disposableResource;
  156. }
  157. @Test(groups = "unit")
  158. public void addFilesCheckOrder() throws IOException {
  159. final File tempFile = File.createTempFile("tpxtest", ""), tempFile2 = File.createTempFile("tpxtest", "");
  160. final DisposableResource disposableResource = createMock(DisposableResource.class);
  161. final DisposableResource disposableResource2 = createMock(DisposableResource.class);
  162. expect(factory.apply(tempFile)).andReturn(disposableResource);
  163. expect(factory.apply(tempFile2)).andReturn(disposableResource2);
  164. replay();
  165. tracker.add(tempFile);
  166. tracker.add(tempFile2);
  167. reset();
  168. assert tracker.getResources().size() == 2;
  169. assert tracker.getResources().get(0) == disposableResource;
  170. assert tracker.getResources().get(1) == disposableResource2;
  171. }
  172. @Test(groups = "unit")
  173. public void empty() {
  174. assert tracker.getResources().size() == 0;
  175. }
  176. @Test(groups = "unit", expectedExceptions = IllegalStateException.class)
  177. public void invalidCopy() throws IOException {
  178. tracker.addCopy(File.createTempFile("tpx", "test"));
  179. }
  180. }