01: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
02: * This code is licensed under the GPL 2.0 license, availible at the root
03: * application directory.
04: */
05: package org.geoserver.ows.kvp;
06:
07: import java.text.DateFormat;
08: import java.text.ParseException;
09: import java.text.SimpleDateFormat;
10: import java.util.List;
11:
12: import org.geoserver.ows.kvp.TimeKvpParser;
13:
14: import junit.framework.TestCase;
15:
16: /**
17: * Test for the time parameter in a WMS request.
18: *
19: * @author Cédric Briançon
20: */
21: public class TimeKvpParserTest extends TestCase {
22: /**
23: * A time period for testing.
24: */
25: private final static String PERIOD = "2007-01-01T12Z/2007-01-31T12Z/P1DT12H";
26:
27: /**
28: * Format of dates.
29: */
30: private final static DateFormat format = new SimpleDateFormat(
31: "yyyy-MM-dd'T'HH'Z'");
32:
33: /**
34: * Tests only the increment part of the time parameter.
35: *
36: * @throws ParseException if the string can't be parsed.
37: */
38: public void testPeriod() throws ParseException {
39: final long millisInDay = TimeKvpParser.MILLIS_IN_DAY;
40: assertEquals(millisInDay, TimeKvpParser.parsePeriod("P1D"));
41: assertEquals(3 * millisInDay, TimeKvpParser.parsePeriod("P3D"));
42: assertEquals(14 * millisInDay, TimeKvpParser.parsePeriod("P2W"));
43: assertEquals(8 * millisInDay, TimeKvpParser
44: .parsePeriod("P1W1D"));
45: assertEquals(millisInDay, TimeKvpParser.parsePeriod("PT24H"));
46: assertEquals(Math.round(1.5 * millisInDay), TimeKvpParser
47: .parsePeriod("P1.5D"));
48: }
49:
50: /**
51: * Compares the dates obtained by parsing the time parameter with the expected values.
52: *
53: * @throws ParseException if the string can't be parsed.
54: */
55: public void testInterval() throws ParseException {
56: TimeKvpParser timeKvpParser = new TimeKvpParser("TIME");
57: List l = (List) timeKvpParser.parse(PERIOD);
58: // Verify that the list contains at least one element.
59: assertFalse(l.isEmpty());
60: assertEquals(format.parse("2007-01-01T12Z"), l.get(0));
61: assertEquals(format.parse("2007-01-03T00Z"), l.get(1));
62: assertEquals(format.parse("2007-01-04T12Z"), l.get(2));
63: assertEquals(format.parse("2007-01-06T00Z"), l.get(3));
64: assertEquals(format.parse("2007-01-07T12Z"), l.get(4));
65: assertEquals(format.parse("2007-01-09T00Z"), l.get(5));
66: assertEquals(format.parse("2007-01-10T12Z"), l.get(6));
67: assertEquals(format.parse("2007-01-12T00Z"), l.get(7));
68: }
69: }
|