01: /* ====================================================================
02: Licensed to the Apache Software Foundation (ASF) under one or more
03: contributor license agreements. See the NOTICE file distributed with
04: this work for additional information regarding copyright ownership.
05: The ASF licenses this file to You under the Apache License, Version 2.0
06: (the "License"); you may not use this file except in compliance with
07: the License. You may obtain a copy of the License at
08:
09: http://www.apache.org/licenses/LICENSE-2.0
10:
11: Unless required by applicable law or agreed to in writing, software
12: distributed under the License is distributed on an "AS IS" BASIS,
13: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: See the License for the specific language governing permissions and
15: limitations under the License.
16: ==================================================================== */
17:
18: package org.apache.poi.hslf.util;
19:
20: import java.text.SimpleDateFormat;
21: import java.util.Date;
22:
23: import junit.framework.TestCase;
24:
25: /**
26: * Tests that SystemTimeUtils works properly.
27: *
28: * @author Nick Burch (nick at torchbox dot com)
29: */
30: public class TestSystemTimeUtils extends TestCase {
31: // From real files
32: private byte[] data_a = new byte[] { 0xD6 - 256, 07, 01, 00, 02,
33: 00, 0x18, 00, 0x0A, 00, 0x1A, 00, 0x0F, 00, 0xCD - 256, 00 };
34: private byte[] data_b = new byte[] { 00, 00, 0xE1 - 256, 0x2E,
35: 0x1C, 00, 00, 00, 01, 00, 00, 00, 0xD6 - 256, 0x07, 01, 00,
36: 02, 00, 0x18, 00, 0x15, 00, 0x19, 00, 03, 00, 0xD5 - 256,
37: 02, 0x0A, 00, 00, 00, 0x0A, 00, 00, 00 };
38:
39: private SimpleDateFormat sdf = new SimpleDateFormat(
40: "yyyy-MM-dd hh:mm:ss.SSS");
41:
42: public void testGetDateA() throws Exception {
43: Date date = SystemTimeUtils.getDate(data_a);
44:
45: // Is 2006-01-24 (2nd day of week) 10:26:15.205
46: Date exp = sdf.parse("2006-01-24 10:26:15.205");
47: assertEquals(exp.getTime(), date.getTime());
48: assertEquals(exp, date);
49: }
50:
51: public void testGetDateB() throws Exception {
52: Date date = SystemTimeUtils.getDate(data_b, 8 + 4);
53:
54: // Is 2006-01-24 (2nd day of week) 21:25:03.725
55: Date exp = sdf.parse("2006-01-24 21:25:03.725");
56: assertEquals(exp.getTime(), date.getTime());
57: assertEquals(exp, date);
58: }
59:
60: public void testWriteDateA() throws Exception {
61: byte[] out_a = new byte[data_a.length];
62: Date date = sdf.parse("2006-01-24 10:26:15.205");
63: SystemTimeUtils.storeDate(date, out_a);
64:
65: for (int i = 0; i < out_a.length; i++) {
66: assertEquals(data_a[i], out_a[i]);
67: }
68: }
69:
70: public void testWriteDateB() throws Exception {
71: byte[] out_b = new byte[data_b.length];
72: // Copy over start and end, ignoring the 16 byte date field in the middle
73: System.arraycopy(data_b, 0, out_b, 0, 12);
74: System.arraycopy(data_b, 12 + 16, out_b, 12 + 16,
75: data_b.length - 12 - 16);
76:
77: Date date = sdf.parse("2006-01-24 21:25:03.725");
78: SystemTimeUtils.storeDate(date, out_b, 12);
79:
80: for (int i = 0; i < out_b.length; i++) {
81: assertEquals(data_b[i], out_b[i]);
82: }
83: }
84: }
|