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