blob: b66bffe2bbb93e724f80f3e041d8e800ed10b86b [file] [log] [blame]
Dave Borowitz97a70312014-04-28 15:50:23 -07001// Copyright (C) 2014 Google Inc. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package com.google.gitiles;
16
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070017import static com.google.gitiles.DateFormatter.Format.DEFAULT;
18import static com.google.gitiles.DateFormatter.Format.ISO;
19import static java.util.TimeZone.getTimeZone;
Dave Borowitz97a70312014-04-28 15:50:23 -070020import static org.junit.Assert.assertEquals;
21
Dave Borowitz6d920bc2014-04-28 15:51:37 -070022import com.google.common.base.Optional;
Dave Borowitz97a70312014-04-28 15:50:23 -070023
24import org.eclipse.jgit.lib.PersonIdent;
25import org.eclipse.jgit.util.GitDateParser;
26import org.junit.Test;
Dave Borowitz3dc854f2014-11-04 16:19:37 -080027import org.junit.runner.RunWith;
28import org.junit.runners.JUnit4;
Dave Borowitz97a70312014-04-28 15:50:23 -070029
30import java.text.ParseException;
31import java.util.Date;
32import java.util.TimeZone;
33
Dave Borowitz3dc854f2014-11-04 16:19:37 -080034@RunWith(JUnit4.class)
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070035public class DateFormatterTest {
Dave Borowitz97a70312014-04-28 15:50:23 -070036 @Test
37 public void defaultIncludingTimeZone() throws Exception {
Dave Borowitz97a70312014-04-28 15:50:23 -070038 PersonIdent ident = newIdent("Mon Jan 2 15:04:05 2006", "-0700");
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070039 DateFormatter df = new DateFormatter(Optional.<TimeZone> absent(), DEFAULT);
40 assertEquals("Mon Jan 02 15:04:05 2006 -0700", df.format(ident));
Dave Borowitz97a70312014-04-28 15:50:23 -070041 }
42
43 @Test
Dave Borowitz6d920bc2014-04-28 15:51:37 -070044 public void defaultWithUtc() throws Exception {
Dave Borowitz6d920bc2014-04-28 15:51:37 -070045 PersonIdent ident = newIdent("Mon Jan 2 15:04:05 2006", "-0700");
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070046 DateFormatter df = new DateFormatter(Optional.of(getTimeZone("UTC")), DEFAULT);
47 assertEquals("Mon Jan 02 22:04:05 2006", df.format(ident));
Dave Borowitz6d920bc2014-04-28 15:51:37 -070048 }
49
50 @Test
51 public void defaultWithOtherTimeZone() throws Exception {
Dave Borowitz6d920bc2014-04-28 15:51:37 -070052 PersonIdent ident = newIdent("Mon Jan 2 15:04:05 2006", "-0700");
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070053 DateFormatter df = new DateFormatter(Optional.of(getTimeZone("GMT-0400")), DEFAULT);
54 assertEquals("Mon Jan 02 18:04:05 2006", df.format(ident));
Dave Borowitz6d920bc2014-04-28 15:51:37 -070055 }
56
57 @Test
Dave Borowitz97a70312014-04-28 15:50:23 -070058 public void isoIncludingTimeZone() throws Exception {
Dave Borowitz97a70312014-04-28 15:50:23 -070059 PersonIdent ident = newIdent("Mon Jan 2 15:04:05 2006", "-0700");
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070060 DateFormatter df = new DateFormatter(Optional.<TimeZone> absent(), ISO);
61 assertEquals("2006-01-02 15:04:05 -0700", df.format(ident));
Dave Borowitz97a70312014-04-28 15:50:23 -070062 }
63
Dave Borowitz6d920bc2014-04-28 15:51:37 -070064 @Test
65 public void isoWithUtc() throws Exception {
Dave Borowitz6d920bc2014-04-28 15:51:37 -070066 PersonIdent ident = newIdent("Mon Jan 2 15:04:05 2006", "-0700");
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070067 DateFormatter df = new DateFormatter(Optional.of(getTimeZone("UTC")), ISO);
68 assertEquals("2006-01-02 22:04:05", df.format(ident));
Dave Borowitz6d920bc2014-04-28 15:51:37 -070069 }
70
71 @Test
72 public void isoWithOtherTimeZone() throws Exception {
Dave Borowitz6d920bc2014-04-28 15:51:37 -070073 PersonIdent ident = newIdent("Mon Jan 2 15:04:05 2006", "-0700");
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070074 DateFormatter df = new DateFormatter(Optional.of(getTimeZone("GMT-0400")), ISO);
75 assertEquals("2006-01-02 18:04:05", df.format(ident));
Dave Borowitz6d920bc2014-04-28 15:51:37 -070076 }
77
Dave Borowitz97a70312014-04-28 15:50:23 -070078 private PersonIdent newIdent(String whenStr, String tzStr) throws ParseException {
79 whenStr += " " + tzStr;
80 Date when = GitDateParser.parse(whenStr, null);
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070081 TimeZone tz = getTimeZone("GMT" + tzStr);
Dave Borowitz97a70312014-04-28 15:50:23 -070082 PersonIdent ident = new PersonIdent("A User", "[email protected]", when, tz);
83 // PersonIdent.toString() uses its own format with "d" instead of "dd",
84 // hence the mismatches in 2 vs. 02 above. Nonetheless I think this sanity
85 // check is useful enough to keep around.
86 assertEquals("PersonIdent[A User, [email protected], " + whenStr + "]", ident.toString());
87 return ident;
88 }
89}