blob: f2adff30289b411ea3582a87d4b8957b0ec6edd9 [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 Borowitzfde41fd2015-09-16 15:14:38 -040017import static com.google.common.truth.Truth.assertThat;
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070018import static com.google.gitiles.DateFormatter.Format.DEFAULT;
19import static com.google.gitiles.DateFormatter.Format.ISO;
20import static java.util.TimeZone.getTimeZone;
Dave Borowitz97a70312014-04-28 15:50:23 -070021
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 Borowitzcf38c032016-05-02 11:06:23 -040039 DateFormatter df = new DateFormatter(Optional.<TimeZone>absent(), DEFAULT);
Dave Borowitzfde41fd2015-09-16 15:14:38 -040040 assertThat(df.format(ident)).isEqualTo("Mon Jan 02 15:04:05 2006 -0700");
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);
Dave Borowitzfde41fd2015-09-16 15:14:38 -040047 assertThat(df.format(ident)).isEqualTo("Mon Jan 02 22:04:05 2006");
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);
Dave Borowitzfde41fd2015-09-16 15:14:38 -040054 assertThat(df.format(ident)).isEqualTo("Mon Jan 02 18:04:05 2006");
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 Borowitzcf38c032016-05-02 11:06:23 -040060 DateFormatter df = new DateFormatter(Optional.<TimeZone>absent(), ISO);
Dave Borowitzfde41fd2015-09-16 15:14:38 -040061 assertThat(df.format(ident)).isEqualTo("2006-01-02 15:04:05 -0700");
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);
Dave Borowitzfde41fd2015-09-16 15:14:38 -040068 assertThat(df.format(ident)).isEqualTo("2006-01-02 22:04:05");
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);
Dave Borowitzfde41fd2015-09-16 15:14:38 -040075 assertThat(df.format(ident)).isEqualTo("2006-01-02 18:04:05");
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.
Dave Borowitze360d5c2015-09-16 16:53:30 -040086 assertThat(ident.toString())
87 .isEqualTo("PersonIdent[A User, [email protected], " + whenStr + "]");
Dave Borowitz97a70312014-04-28 15:50:23 -070088 return ident;
89 }
90}