3 import static org.hamcrest.CoreMatchers.is;
4 import static org.hamcrest.CoreMatchers.nullValue;
5 import static org.junit.Assert.*;
7 import java.text.DateFormat;
8 import java.text.ParseException;
9 import java.text.SimpleDateFormat;
10 import java.util.Date;
11 import java.util.Iterator;
13 import javax.xml.parsers.DocumentBuilder;
14 import javax.xml.parsers.DocumentBuilderFactory;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.experimental.runners.Enclosed;
19 import org.junit.runner.RunWith;
20 import org.w3c.dom.DOMImplementation;
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Element;
24 @RunWith(Enclosed.class)
25 public class ElementMapTRKPTTest {
27 public static class Keyのみ {
28 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
29 ElementMapTRKPT map = null;
31 static String[] values = {
32 "1970-01-01 08:59:59.999",
33 "1970-01-01 09:00:00.000",
34 "1970-01-01 09:00:00.001",
35 "2018-10-25 07:59:59.999",
36 "2018-10-25 08:00:00.000",
37 "2018-10-25 08:00:00.001"
41 public void setUp() throws Exception {
42 timeL = (sdf.parse("2018-10-25 08:00:00.000")).getTime();
43 map = new ElementMapTRKPT();
44 map.put(new Date(timeL), null); // 5-6: 2018-10-25 08:00:00.000
45 map.put(new Date(timeL + 1L), null); // 7: 2018-10-25 08:00:00.001
46 map.put(new Date(timeL - 1L), null); // 4: 2018-10-25 07:59:59.999
47 map.put(new Date(1L), null); // 3: 1970-01-01 09:00:00.001
48 map.put(new Date(0L), null); // 2: 1970-01-01 09:00:00.000
49 map.put(new Date(-1L), null); // 1: 1970-01-01 08:59:59.999
50 map.put(new Date(timeL), null); // 5-6: 2018-10-25 08:00:00.000
54 public void 同一キーをPUTした場合() {
55 assertThat(map.size(), is(6));
59 public void イテレータを使って読みだす() {
60 assertThat(map.size(), is(6));
63 for (Iterator<Date> itr = map.keySet().iterator(); itr.hasNext(); ) {
64 Date key = itr.next();
65 assertThat(sdf.format(key), is(values[i++]));
70 public void 拡張FOR文を使って読みだす() {
71 assertThat(map.size(), is(6));
74 for (Date key : map.keySet()) {
75 assertThat(sdf.format(key), is(values[i++]));
80 public static class Keyとvalueのセット {
81 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
82 ElementMapTRKPT map = null;
86 * <trkpt lat="35.8812697884" lon="137.9952202085"><time>2017-05-29T01:23:18Z</time></trkpt>
87 * <trkpt lat="35.8811769169" lon="137.9951928835"><time>2017-05-29T01:23:21Z</time><ele>614.90</ele></trkpt>
88 * <trkpt lat="35.881112963" lon="137.9951796401"><time>2017-05-29T01:23:24Z</time><ele>615.00</ele></trkpt>
89 * <trkpt lat="35.881072646" lon="137.9951728508"><time>2017-05-29T01:23:27Z</time><ele>615.03</ele></trkpt>
91 static String[][] values = {
92 {"2017-05-29T01:23:18Z", "35.8812697884", "137.9952202085", null},
93 {"2017-05-29T01:23:21Z", "35.8811769169", "137.9951928835", "614.90"},
94 {"2017-05-29T01:23:24Z", "35.881112963", "137.9951796401", "615.00"},
95 {"2017-05-29T01:23:27Z", "35.881072646", "137.9951728508", "615.03"}
98 Element createElement(Document document, String[] values) {
99 Element trkpt = document.createElement("trkpt");
100 trkpt.setAttribute("lat", values[1]);
101 trkpt.setAttribute("lon", values[2]);
102 Element timeE = document.createElement("time");
103 timeE.appendChild(document.createTextNode(values[0]));
104 trkpt.appendChild(timeE);
105 if (values[3] != null) {
106 Element eleE = document.createElement("ele");
107 eleE.appendChild(document.createTextNode(values[3]));
108 trkpt.appendChild(eleE);
114 public void setUp() throws Exception {
115 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
116 DocumentBuilder builder = factory.newDocumentBuilder();
117 DOMImplementation domImpl=builder.getDOMImplementation();
118 Document document = domImpl.createDocument("","trkpt",null);
120 map = new ElementMapTRKPT();
121 for (int cnt = 4; cnt > 0; cnt--) {
122 map.put(new TagTrkpt(createElement(document, values[cnt - 1])));
127 public void コンテンツの数をチェック() {
128 assertThat(map.size(), is(4));
132 public void KEYが時間順に取り出せるか() {
134 for (Date key : map.keySet()) {
136 String s = sdf.format(ImportPicture.toUTCDate(values[i++][0]));
137 assertThat(sdf.format(key), is(s));
138 } catch (ParseException e) {
145 public void get_17() throws ParseException {
146 TagTrkpt tag = map.getValue(ImportPicture.toUTCDate("2017-05-29T01:23:17Z"));
147 assertThat(tag, is(nullValue()));
151 public void get_18() throws ParseException {
152 TagTrkpt tag = map.getValue(ImportPicture.toUTCDate("2017-05-29T01:23:18Z"));
153 assertThat(sdf.format(tag.time), is("2017-05-29T10:23:18Z"));
154 assertThat(tag.eleStr, is(nullValue()));
158 public void get_19() throws ParseException {
159 TagTrkpt tag = map.getValue(ImportPicture.toUTCDate("2017-05-29T01:23:19Z"));
160 assertThat(sdf.format(tag.time), is("2017-05-29T10:23:18Z"));
161 assertThat(tag.eleStr, is(nullValue()));
165 public void get_20() throws ParseException {
166 TagTrkpt tag = map.getValue(ImportPicture.toUTCDate("2017-05-29T01:23:20Z"));
167 assertThat(sdf.format(tag.time), is("2017-05-29T10:23:18Z"));
168 assertThat(tag.eleStr, is(nullValue()));
172 public void get_21() throws ParseException {
173 TagTrkpt tag = map.getValue(ImportPicture.toUTCDate("2017-05-29T01:23:21Z"));
174 assertThat(sdf.format(tag.time), is("2017-05-29T10:23:21Z"));
175 assertThat(tag.eleStr, is("614.90"));
179 public void get_22() throws ParseException {
180 TagTrkpt tag = map.getValue(ImportPicture.toUTCDate("2017-05-29T01:23:22Z"));
181 assertThat(sdf.format(tag.time), is("2017-05-29T10:23:21Z"));
182 assertThat(tag.eleStr, is("614.90"));
186 public void get_23() throws ParseException {
187 TagTrkpt tag = map.getValue(ImportPicture.toUTCDate("2017-05-29T01:23:23Z"));
188 assertThat(sdf.format(tag.time), is("2017-05-29T10:23:21Z"));
189 assertThat(tag.eleStr, is("614.90"));
193 public void get_24() throws ParseException {
194 TagTrkpt tag = map.getValue(ImportPicture.toUTCDate("2017-05-29T01:23:24Z"));
195 assertThat(sdf.format(tag.time), is("2017-05-29T10:23:24Z"));
196 assertThat(tag.eleStr, is("615.00"));
200 public void get_25() throws ParseException {
201 TagTrkpt tag = map.getValue(ImportPicture.toUTCDate("2017-05-29T01:23:25Z"));
202 assertThat(sdf.format(tag.time), is("2017-05-29T10:23:24Z"));
203 assertThat(tag.eleStr, is("615.00"));
207 public void get_26() throws ParseException {
208 TagTrkpt tag = map.getValue(ImportPicture.toUTCDate("2017-05-29T01:23:26Z"));
209 assertThat(sdf.format(tag.time), is("2017-05-29T10:23:24Z"));
210 assertThat(tag.eleStr, is("615.00"));
214 public void get_27() throws ParseException {
215 TagTrkpt tag = map.getValue(ImportPicture.toUTCDate("2017-05-29T01:23:27Z"));
216 assertThat(sdf.format(tag.time), is("2017-05-29T10:23:27Z"));
217 assertThat(tag.eleStr, is("615.03"));
221 public void get_28() throws ParseException {
222 TagTrkpt tag = map.getValue(ImportPicture.toUTCDate("2017-05-29T01:23:28Z"));
223 assertThat(sdf.format(tag.time), is("2017-05-29T10:23:27Z"));
224 assertThat(tag.eleStr, is("615.03"));
228 public void get_30() throws ParseException {
229 TagTrkpt tag = map.getValue(ImportPicture.toUTCDate("2017-05-29T01:23:30Z"));
230 assertThat(sdf.format(tag.time), is("2017-05-29T10:23:27Z"));
231 assertThat(tag.eleStr, is("615.03"));
235 public void get_31() throws ParseException {
236 TagTrkpt tag = map.getValue(ImportPicture.toUTCDate("2017-05-29T01:23:31Z"));
237 assertThat(tag, is(nullValue()));
241 public static class タイムスタンプの書式 {
243 public void EXIF時刻書式テスト() throws Exception {
244 String dateTimeOriginal = "2017:06:30 09:59:59";
245 Date time = ImportPicture.toEXIFDate(dateTimeOriginal);
246 assertThat(ImportPicture.toEXIFString(time), is("2017:06:30 09:59:59"));
247 assertThat(ImportPicture.toUTCString(time), is("2017-06-30T00:59:59Z"));
248 DateFormat dfUTC = new SimpleDateFormat(ImportPicture.TIME_FORMAT_STRING);
249 assertThat(dfUTC.format(time), is("2017-06-30T09:59:59Z"));