OSDN Git Service

enhance: LAT&LONの欄の幅を変更
[importpicture/importpicture.git] / importPicture / test / osm / jp / gpx / ImportPictureTest.java
1 package osm.jp.gpx;
2
3 import static org.hamcrest.CoreMatchers.is;
4 import static org.junit.Assert.*;
5
6 import java.io.BufferedInputStream;
7 import java.io.BufferedOutputStream;
8 import java.io.File;
9 import java.io.FileInputStream;
10 import java.io.FileOutputStream;
11 import java.io.IOException;
12 import java.nio.channels.FileChannel;
13
14 import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
15 import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
16 import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.junit.runner.*;
20 import org.junit.experimental.runners.Enclosed;
21
22 import hayashi.tools.files.DeleteDir;
23 import osm.jp.gpx.utils.TarGz;
24
25 @RunWith(Enclosed.class)
26 public class ImportPictureTest {
27         
28         public static class SONYのカメラの場合 {
29                 
30                 @Before
31                 public void setUp() throws Exception {
32                         // カメラディレクトリを削除する
33                         File dir = new File("testdata/cameradata");
34                         if (dir.exists()) {
35                                 DeleteDir.delete(dir);
36                         }
37                         File outDir = new File("testdata/output");
38                         if (outDir.exists()) {
39                                 DeleteDir.delete(outDir);
40                         }
41                         outDir.mkdir();
42                         
43                         // カメラディレクトリを作成する
44                         uncompress(new File("testdata", "Sony20170518.tar.gz"), dir);
45                         
46                         // GPXファイルをセット
47                 try (   FileInputStream inStream = new FileInputStream(new File("testdata", "20170518.gpx"));
48                                 FileOutputStream outStream = new FileOutputStream(new File("testdata/cameradata/10170518/20170518.gpx"));
49                         FileChannel inChannel = inStream.getChannel();
50                         FileChannel outChannel = outStream.getChannel();        )
51                 {
52                     inChannel.transferTo(0, inChannel.size(), outChannel);
53                 }
54                 
55                 // プロパティファイルを設定
56                         File iniFile = new File("AdjustTime.ini");
57                         File orgFile = new File("AdjustTime.ini.org");
58                         File testFile = new File("testdata", "AdjustTime.20170518.ini");
59                         if (orgFile.exists()) {
60                                 orgFile.delete();
61                         }
62                         if (iniFile.exists()) {
63                                 iniFile.renameTo(orgFile);
64                         }
65                 try (   FileInputStream inStream = new FileInputStream(testFile);
66                                 FileOutputStream outStream = new FileOutputStream(new File("AdjustTime.ini"));
67                                 FileChannel inChannel = inStream.getChannel();
68                                 FileChannel outChannel = outStream.getChannel();        )
69                 {
70                     inChannel.transferTo(0, inChannel.size(),outChannel);
71                 }
72                 }
73
74                 @Test
75                 public void FILE_UPDATE時間を基準にして時間外のファイルはコピー対象外の時() throws Exception {
76                 // "AdjustTime.20170517.ini"のデフォルト状態で実行
77                         AppParameters params = new AppParameters();
78                         params.setProperty(AppParameters.GPX_BASETIME, "FILE_UPDATE");
79                         params.setProperty(AppParameters.IMG_OUTPUT_ALL, "false");
80                         params.store();
81                 
82                 // 実行する
83                         testdo();
84
85                 File gpxfile = new File("./testdata/output/10170518", "20170518_.gpx");
86                 assertThat(gpxfile.exists(), is(true));
87                 File file0 = new File("./testdata/output/10170518", "DSC05183.JPG");
88                 assertThat(file0.exists(), is(false));
89                 File file1 = new File("./testdata/output/10170518", "DSC05184.JPG");
90                 assertThat(file1.exists(), is(true));
91                 File file2 = new File("./testdata/output/10170518", "DSC05196.JPG");
92                 assertThat(file2.exists(), is(true));
93                 File file3 = new File("./testdata/output/10170518", "DSC05204.JPG");
94                 assertThat(file3.exists(), is(true));
95                 File file4 = new File("./testdata/output/10170518", "DSC05205.JPG");
96                 assertThat(file4.exists(), is(false));
97                 }
98                 
99                 @Test
100                 public void FILE_UPDATE時間を基準にして時間外のファイルもコピーする時() throws Exception {
101                         AppParameters params = new AppParameters();
102                         params.setProperty(AppParameters.GPX_BASETIME, "FILE_UPDATE");
103                         params.setProperty(AppParameters.IMG_OUTPUT_ALL, "true");
104                         params.store();
105                 
106                 // 実行する
107                         testdo();
108
109                 File gpxfile = new File("./testdata/output/10170518", "20170518_.gpx");
110                 assertThat(gpxfile.exists(), is(true));
111                 File file0 = new File("./testdata/output/10170518", "DSC05183.JPG");
112                 assertThat(file0.exists(), is(true));
113                 File file1 = new File("./testdata/output/10170518", "DSC05184.JPG");
114                 assertThat(file1.exists(), is(true));
115                 File file2 = new File("./testdata/output/10170518", "DSC05196.JPG");
116                 assertThat(file2.exists(), is(true));
117                 File file3 = new File("./testdata/output/10170518", "DSC05204.JPG");
118                 assertThat(file3.exists(), is(true));
119                 File file4 = new File("./testdata/output/10170518", "DSC05205.JPG");
120                 assertThat(file4.exists(), is(true));
121                 }
122                 
123                 /**
124                  * 実行する
125                  * @throws Exception
126                  */
127                 public void testdo() {
128                 try {
129                         String[] argv = {
130                                         "./testdata/cameradata/10170518",
131                                         "DSC05183.JPG",
132                                         "2017-05-18T09:16:48",
133                                         "./testdata/output",
134                                         "./testdata/cameradata/10170518"
135                         };
136                         ImportPicture.main(argv);
137                         }
138                         catch (Exception e) {
139                                 fail("Exceptionが発生した。");
140                         }
141                 }
142         }
143
144         public static class WiMiUSのカメラの場合 {
145                 
146                 @Before
147                 public void setUp() throws Exception {
148                         // カメラディレクトリを削除する
149                         File dir = new File("testdata/cameradata");
150                         if (dir.exists()) {
151                                 DeleteDir.delete(dir);
152                         }
153                         File outDir = new File("testdata/output");
154                         if (outDir.exists()) {
155                                 DeleteDir.delete(outDir);
156                         }
157                         outDir.mkdir();
158                         
159                         // カメラディレクトリを作成する
160                         TarGz.uncompress(new File("testdata", "WiMiUS20170518.tar.gz"), dir);
161                         
162                         // GPXファイルをセット
163                 try (   FileInputStream inStream = new FileInputStream(new File("testdata", "20170518.gpx"));
164                                 FileOutputStream outStream = new FileOutputStream(new File("testdata/cameradata/20170518.gpx"));
165                         FileChannel inChannel = inStream.getChannel();
166                         FileChannel outChannel = outStream.getChannel();        )
167                 {
168                     inChannel.transferTo(0, inChannel.size(), outChannel);
169                 }
170                 
171                 // プロパティファイルを設定
172                         File iniFile = new File("AdjustTime.ini");
173                         File orgFile = new File("AdjustTime.ini.org");
174                         File testFile = new File("testdata", "AdjustTime.20170518.ini");
175                         if (orgFile.exists()) {
176                                 orgFile.delete();
177                         }
178                         if (iniFile.exists()) {
179                                 iniFile.renameTo(orgFile);
180                         }
181                 try (   FileInputStream inStream = new FileInputStream(testFile);
182                                 FileOutputStream outStream = new FileOutputStream(new File("AdjustTime.ini"));
183                                 FileChannel inChannel = inStream.getChannel();
184                                 FileChannel outChannel = outStream.getChannel();        )
185                 {
186                     inChannel.transferTo(0, inChannel.size(),outChannel);
187                 }
188
189                         AppParameters params = new AppParameters();
190                         params.setProperty(AppParameters.IMG_TIME, "2017-05-18T09:20:30");
191                         params.setProperty(AppParameters.IMG_BASE_FILE, "20170518_092031A.jpg");
192                         params.setProperty(AppParameters.IMG_SOURCE_FOLDER, "./testdata/cameradata");
193                         params.setProperty(AppParameters.GPX_SOURCE_FOLDER, "./testdata/cameradata/20170518.gpx");
194                         params.store();
195                 }
196
197                 @Test
198                 public void FILE_UPDATE時間を基準にして時間外のファイルはコピー対象外の時() throws Exception {
199                         AppParameters params = new AppParameters();
200                         params.setProperty(AppParameters.GPX_BASETIME, "FILE_UPDATE");
201                         params.setProperty(AppParameters.IMG_OUTPUT_ALL, "false");
202                         params.store();
203                 
204                 // 実行する
205                         testdo();
206
207                 File gpxfile = new File("./testdata/output/cameradata", "20170518_.gpx");
208                 assertThat(gpxfile.exists(), is(true));
209                 File file0 = new File("./testdata/output/cameradata", "20170518_092031A.jpg");
210                 assertThat(file0.exists(), is(false));
211                 File file1 = new File("./testdata/output/cameradata", "20170518_094226A_snap.jpg");
212                 assertThat(file1.exists(), is(true));
213                 File file3 = new File("./testdata/output/cameradata", "20170518_094737A.jpg");
214                 assertThat(file3.exists(), is(true));
215                 File file4 = new File("./testdata/output/cameradata", "20170518_094827A.jpg");
216                 assertThat(file4.exists(), is(false));
217                 }
218                 
219                 @Test
220                 public void FILE_UPDATE時間を基準にして時間外のファイルもコピーする時() throws Exception {
221                         AppParameters params = new AppParameters();
222                         params.setProperty(AppParameters.GPX_BASETIME, "FILE_UPDATE");
223                         params.setProperty(AppParameters.IMG_OUTPUT_ALL, "true");
224                         params.store();
225                 
226                 // 実行する
227                         testdo();
228
229                 File gpxfile = new File("./testdata/output/cameradata", "20170518_.gpx");
230                 assertThat(gpxfile.exists(), is(true));
231                 File file0 = new File("./testdata/output/cameradata", "20170518_092031A.jpg");
232                 assertThat(file0.exists(), is(true));
233                 File file1 = new File("./testdata/output/cameradata", "20170518_094226A_snap.jpg");
234                 assertThat(file1.exists(), is(true));
235                 File file3 = new File("./testdata/output/cameradata", "20170518_094737A.jpg");
236                 assertThat(file3.exists(), is(true));
237                 File file4 = new File("./testdata/output/cameradata", "20170518_094827A.jpg");
238                 assertThat(file4.exists(), is(true));
239                 }
240                 
241                 /**
242                  * 実行する
243                  * @throws Exception
244                  */
245                 public void testdo() {
246                 try {
247                         String[] argv = {
248                                         "./testdata/cameradata",
249                                         "20170518_092031A.jpg",
250                                         "2017-05-18T09:20:30",
251                                         "./testdata/output",
252                                         "./testdata/cameradata"
253                         };
254                         ImportPicture.main(argv);
255                         }
256                         catch (Exception e) {
257                                 fail("Exceptionが発生した。");
258                         }
259                 }
260         }
261
262         public static class GPXが複数のTRKSEGに分割している場合 {
263                 
264                 @Before
265                 public void setUp() throws Exception {
266                         // カメラディレクトリを削除する
267                         File dir = new File("testdata/cameradata");
268                         if (dir.exists()) {
269                                 DeleteDir.delete(dir);
270                         }
271                         File outDir = new File("testdata/output");
272                         if (outDir.exists()) {
273                                 DeleteDir.delete(outDir);
274                         }
275                         outDir.mkdir();
276                         
277                         // カメラディレクトリを作成する
278                         TarGz.uncompress(new File("testdata", "separate.tar.gz"), dir);
279                         
280                         // GPXファイルをセット
281                 try (   FileInputStream inStream = new FileInputStream(new File("testdata", "separate.gpx"));
282                                 FileOutputStream outStream = new FileOutputStream(new File("testdata/cameradata/separate.gpx"));
283                         FileChannel inChannel = inStream.getChannel();
284                         FileChannel outChannel = outStream.getChannel();        )
285                 {
286                     inChannel.transferTo(0, inChannel.size(), outChannel);
287                 }
288                 
289                 // プロパティファイルを設定
290                         File iniFile = new File("AdjustTime.ini");
291                         File orgFile = new File("AdjustTime.ini.org");
292                         File testFile = new File("testdata", "AdjustTime.separate.ini");
293                         if (orgFile.exists()) {
294                                 orgFile.delete();
295                         }
296                         if (iniFile.exists()) {
297                                 iniFile.renameTo(orgFile);
298                         }
299                 try (   FileInputStream inStream = new FileInputStream(testFile);
300                                 FileOutputStream outStream = new FileOutputStream(new File("AdjustTime.ini"));
301                                 FileChannel inChannel = inStream.getChannel();
302                                 FileChannel outChannel = outStream.getChannel();        )
303                 {
304                     inChannel.transferTo(0, inChannel.size(),outChannel);
305                 }
306
307                         AppParameters params = new AppParameters();
308                         params.setProperty(AppParameters.IMG_TIME, "2017-05-29T10:31:46");
309                         params.setProperty(AppParameters.IMG_BASE_FILE, "20170529_103146A.jpg");
310                         params.setProperty(AppParameters.IMG_SOURCE_FOLDER, "./testdata/cameradata/separate");
311                         params.setProperty(AppParameters.GPX_SOURCE_FOLDER, "./testdata/cameradata/separate.gpx");
312                         params.store();
313                 }
314
315                 @Test
316                 public void FILE_UPDATE時間を基準にして時間外のファイルはコピー対象外の時() throws Exception {
317                         AppParameters params = new AppParameters();
318                         params.setProperty(AppParameters.GPX_BASETIME, "FILE_UPDATE");
319                         params.setProperty(AppParameters.IMG_OUTPUT_ALL, "false");
320                         params.store();
321                 
322                 // 実行する
323                         testdo();
324
325                 File gpxfile = new File("./testdata/output/cameradata/separate", "separate_.gpx");
326                 assertThat(gpxfile.exists(), is(true));
327                 File file0 = new File("./testdata/output/cameradata/separate", "20170529_102305A.jpg");
328                 assertThat(file0.exists(), is(false));
329                 File file1 = new File("./testdata/output/cameradata/separate", "20170529_103146A.jpg");
330                 assertThat(file1.exists(), is(true));
331                 File file3 = new File("./testdata/output/cameradata/separate", "20170529_103515A.jpg");
332                 assertThat(file3.exists(), is(true));
333                 File file4 = new File("./testdata/output/cameradata/separate", "20170529_104119A.jpg");
334                 assertThat(file4.exists(), is(true));
335                 }
336
337                 @Test
338                 public void EXIF時間を基準にして時間外のファイルはコピー対象外の時() throws Exception {
339                         AppParameters params = new AppParameters();
340                         params.setProperty(AppParameters.GPX_BASETIME, "EXIF_TIME");
341                         params.setProperty(AppParameters.IMG_OUTPUT_ALL, "false");
342                         params.store();
343                 
344                 // 実行する
345                         testdo();
346
347                 File gpxfile = new File("./testdata/output/cameradata/separate", "separate_.gpx");
348                 assertThat(gpxfile.exists(), is(true));
349                 File file0 = new File("./testdata/output/cameradata/separate", "20170529_102305A.jpg");
350                 assertThat(file0.exists(), is(false));
351                 File file1 = new File("./testdata/output/cameradata/separate", "20170529_103146A.jpg");
352                 assertThat(file1.exists(), is(true));
353                 File file3 = new File("./testdata/output/cameradata/separate", "20170529_103515A.jpg");
354                 assertThat(file3.exists(), is(true));
355                 File file4 = new File("./testdata/output/cameradata/separate", "20170529_104119A.jpg");
356                 assertThat(file4.exists(), is(true));
357                 }
358                 
359                 /**
360                  * 実行する
361                  * @throws Exception
362                  */
363                 public void testdo() {
364                 try {
365                         String[] argv = {
366                                         "./testdata/cameradata/separate",
367                                         "20170529_103146A.jpg",
368                                         "2017-05-29T10:31:46",
369                                         "./testdata/output",
370                                         "./testdata/cameradata/separate.gpx"
371                         };
372                         ImportPicture.main(argv);
373                         }
374                         catch (Exception e) {
375                                 e.printStackTrace();
376                                 fail("Exceptionが発生した。");
377                         }
378                 }
379         }
380
381     /**
382      * *.tar.gz解凍
383      * ファイル更新日時をオリジナルと同じにします。
384      * @param tazFile 解凍する*.tar.gzファイル
385      * @param dest 解凍先フォルダ
386      * @throws IOException 
387      */
388     public static void uncompress(File tazFile, File dest) throws IOException {
389         dest.mkdir();
390         
391         TarArchiveInputStream tarIn = null;
392         tarIn = new TarArchiveInputStream(new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(tazFile))));
393
394         TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
395         while (tarEntry != null) {
396             File destPath = new File(dest, tarEntry.getName());
397             System.out.println("uncompress: " + destPath.getCanonicalPath());
398             if (tarEntry.isDirectory()) {
399                 destPath.mkdirs();
400             }
401             else {
402                 File dir = new File(destPath.getParent());
403                 if (!dir.exists()) {
404                         dir.mkdirs();
405                 }
406                 destPath.createNewFile();
407                 byte[] btoRead = new byte[1024];
408                 BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath));
409                 int len = 0;
410
411                 while ((len = tarIn.read(btoRead)) != -1) {
412                     bout.write(btoRead, 0, len);
413                 }
414
415                 bout.close();
416                 destPath.setLastModified(tarEntry.getLastModifiedDate().getTime());
417                 btoRead = null;
418             }
419             tarEntry = tarIn.getNextTarEntry();
420         }
421         tarIn.close();
422     }
423 }