Newer
Older
restamp / src / test / java / osm / surveyor / matchtime / Fixture.java
  1. package osm.surveyor.matchtime;
  2.  
  3. import static org.hamcrest.CoreMatchers.is;
  4. import static org.junit.Assert.assertNotNull;
  5. import static org.junit.Assert.assertThat;
  6. import static org.junit.Assert.fail;
  7.  
  8. import java.io.File;
  9. import java.io.FileInputStream;
  10. import java.io.IOException;
  11. import java.nio.file.Files;
  12. import java.nio.file.Path;
  13. import java.nio.file.Paths;
  14. import java.text.DateFormat;
  15. import java.text.SimpleDateFormat;
  16. import java.util.Date;
  17. import java.util.HashMap;
  18.  
  19. import org.apache.commons.codec.digest.DigestUtils;
  20. import org.junit.experimental.theories.DataPoints;
  21.  
  22. public class Fixture {
  23. String[] args;
  24. String[] ans;
  25. HashMap<String, File> before;
  26.  
  27. public Fixture(
  28. String[] args,
  29. String[] ans
  30. ) {
  31. this.args = args;
  32. this.ans = ans;
  33. this.before = new HashMap<>();
  34. }
  35. /**
  36. * フォルダ内のファイルとファイルのMD5リストを作成
  37. * @param dir
  38. * @return
  39. */
  40. public void setUp() throws IOException {
  41. if (args.length > 0) {
  42. String dirPath = args[0];
  43. File[] files = Paths.get(dirPath).toFile().listFiles();
  44. for (File f : files) {
  45. FileInputStream fis = new FileInputStream(f);
  46. this.before.put(DigestUtils.md5Hex(fis), f);
  47. }
  48. }
  49. }
  50. /**
  51. *
  52. * @param imgDir
  53. * @param ans
  54. */
  55. public void check() {
  56. if (args.length > 5) {
  57. String dirPath = args[0];
  58. File imgDir = Paths.get(dirPath).toFile();
  59. if (this.args.length >= 6) {
  60. imgDir = Paths.get(args[5]).toFile();
  61. }
  62. DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
  63.  
  64. File[] files = imgDir.listFiles();
  65. assertThat(files.length, is(ans.length));
  66. java.util.Arrays.sort(files, (File file1, File file2) -> file1.getName().compareTo(file2.getName()));
  67. int i = 0;
  68. for (File jpgFile : files) {
  69. long msec = jpgFile.lastModified();
  70. String lastModifiedStr = df.format(new Date(msec));
  71. assertThat(lastModifiedStr, is(ans[i]));
  72. i++;
  73. }
  74. }
  75. }
  76. /**
  77. *
  78. * @param map
  79. * @param dir
  80. */
  81. public void checkUnchanged() {
  82. if (this.args.length < 6) {
  83. return;
  84. }
  85. String outStr = args[5];
  86. Path dir = Paths.get(outStr);
  87. assertThat(Files.isDirectory(dir), is(true));
  88. try {
  89. File[] files = dir.toFile().listFiles();
  90. assertThat(files.length, is(this.before.size()));
  91. for (String key : this.before.keySet()) {
  92. File sfile = this.before.get(key);
  93. Path ofile = Paths.get(dir.toString(), sfile.getName());
  94. assertNotNull(ofile);
  95. FileInputStream fis = new FileInputStream(ofile.toFile());
  96. String md5 = DigestUtils.md5Hex(fis);
  97. assertThat(key, is(md5));
  98. }
  99. }
  100. catch (Exception e) {
  101. fail();
  102. }
  103. }
  104.  
  105. @Override
  106. public String toString() {
  107. String msg = "テストパターン";
  108. for (String arg : args) {
  109. msg += String.format("\n%s", arg);
  110. }
  111. return msg;
  112. }
  113.  
  114. @DataPoints
  115. public static Fixture[] datas = {
  116. new Fixture(
  117. new String[]{},
  118. new String[] {}
  119. ),
  120. new Fixture(
  121. new String[]{
  122. "./target/test-classes/images"
  123. },
  124. new String[] {
  125. "2019-09-01 16:26:51 JST",
  126. "2019-09-01 16:26:56 JST",
  127. "2019-09-01 16:27:01 JST",
  128. "2019-09-01 16:27:06 JST",
  129. "2019-09-01 16:27:11 JST",
  130. "2019-09-01 16:27:16 JST",
  131. "2019-09-01 16:27:21 JST",
  132. "2019-09-01 16:27:26 JST",
  133. "2019-09-01 16:27:31 JST"
  134. }
  135. ),
  136. new Fixture(
  137. new String[]{
  138. "./target/test-classes/images",
  139. "00001.jpg",
  140. "2019-09-01 16:26:51 JST",
  141. "00003.jpg",
  142. "2019-09-01 16:27:01 JST",
  143. "./target/test-classes/out"
  144. },
  145. new String[] {
  146. "2019-09-01 16:26:51 JST",
  147. "2019-09-01 16:26:56 JST",
  148. "2019-09-01 16:27:01 JST",
  149. "2019-09-01 16:27:06 JST",
  150. "2019-09-01 16:27:11 JST",
  151. "2019-09-01 16:27:16 JST",
  152. "2019-09-01 16:27:21 JST",
  153. "2019-09-01 16:27:26 JST",
  154. "2019-09-01 16:27:31 JST",
  155. }
  156. ),
  157. new Fixture(
  158. new String[]{
  159. "./target/test-classes/images",
  160. "00002.jpg",
  161. "2019-09-02 16:26:56 JST",
  162. "00004.jpg",
  163. "2019-09-02 16:27:01 JST"
  164. },
  165. new String[] {
  166. "2019-09-02 16:26:53 JST", // 0.0 sec
  167. "2019-09-02 16:26:56 JST", // 3.0 sec
  168. "2019-09-02 16:26:58 JST", // 2.0 sec
  169. "2019-09-02 16:27:01 JST", // 3.0 sec
  170. "2019-09-02 16:27:03 JST", // 2.0 sec
  171. "2019-09-02 16:27:06 JST", // 3.0 sec
  172. "2019-09-02 16:27:08 JST", // 2.0 sec
  173. "2019-09-02 16:27:11 JST", // 3.0 sec
  174. "2019-09-02 16:27:13 JST", // 2.0 sec
  175. }
  176. ),
  177. new Fixture(
  178. new String[]{
  179. "./target/test-classes/images",
  180. "00001.jpg",
  181. "2019-09-03 16:26:53 JST",
  182. "00003.jpg",
  183. "2019-09-03 16:26:58 JST"
  184. },
  185. new String[] {
  186. "2019-09-03 16:26:53 JST", // 0.0
  187. "2019-09-03 16:26:55 JST", // 2.0
  188. "2019-09-03 16:26:58 JST", // 3.0
  189. "2019-09-03 16:27:00 JST", // 2.0
  190. "2019-09-03 16:27:03 JST", // 3.0
  191. "2019-09-03 16:27:05 JST", // 2.0
  192. "2019-09-03 16:27:08 JST", // 3.0
  193. "2019-09-03 16:27:10 JST", // 2.0
  194. "2019-09-03 16:27:13 JST", // 3.0
  195. }
  196. ),
  197. // 4
  198. new Fixture(
  199. new String[]{
  200. "./target/test-classes/images",
  201. "00003.jpg",
  202. "2019-09-04 16:26:58 JST",
  203. "00005.jpg",
  204. "2019-09-04 16:27:03 JST"
  205. },
  206. new String[] {
  207. "2019-09-04 16:26:53 JST", // 0.0
  208. "2019-09-04 16:26:55 JST", // 2.0
  209. "2019-09-04 16:26:58 JST", // 3.0
  210. "2019-09-04 16:27:00 JST", // 2.0
  211. "2019-09-04 16:27:03 JST", // 3.0
  212. "2019-09-04 16:27:05 JST", // 2.0
  213. "2019-09-04 16:27:08 JST", // 3.0
  214. "2019-09-04 16:27:10 JST", // 2.0
  215. "2019-09-04 16:27:13 JST", // 3.0
  216. }
  217. ),
  218. // 5
  219. new Fixture(
  220. new String[]{
  221. "./target/test-classes/images",
  222. "00003.jpg",
  223. "2019-09-04 16:26:58 JST",
  224. "00005.jpg",
  225. "2019-09-04 16:27:03 JST",
  226. "./target/test-classes/out"
  227. },
  228. new String[] {
  229. "2019-09-04 16:26:53 JST", // 0.0
  230. "2019-09-04 16:26:55 JST", // 2.0
  231. "2019-09-04 16:26:58 JST", // 3.0
  232. "2019-09-04 16:27:00 JST", // 2.0
  233. "2019-09-04 16:27:03 JST", // 3.0
  234. "2019-09-04 16:27:05 JST", // 2.0
  235. "2019-09-04 16:27:08 JST", // 3.0
  236. "2019-09-04 16:27:10 JST", // 2.0
  237. "2019-09-04 16:27:13 JST", // 3.0
  238. }
  239. ),
  240. };
  241.  
  242. }