blob: 112e9d81f2b1ea6d13d98b24800799cd89e82748 [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 Borowitz3b744b12016-08-19 16:11:10 -040022import java.text.ParseException;
23import java.util.Date;
David Pursehouse7a7f5472016-10-14 09:59:20 +090024import java.util.Optional;
Dave Borowitz3b744b12016-08-19 16:11:10 -040025import java.util.TimeZone;
Dave Borowitz97a70312014-04-28 15:50:23 -070026import org.eclipse.jgit.lib.PersonIdent;
27import org.eclipse.jgit.util.GitDateParser;
28import org.junit.Test;
Dave Borowitz3dc854f2014-11-04 16:19:37 -080029import org.junit.runner.RunWith;
30import org.junit.runners.JUnit4;
Dave Borowitz97a70312014-04-28 15:50:23 -070031
Dave Borowitz3dc854f2014-11-04 16:19:37 -080032@RunWith(JUnit4.class)
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070033public class DateFormatterTest {
Dave Borowitz97a70312014-04-28 15:50:23 -070034 @Test
35 public void defaultIncludingTimeZone() throws Exception {
Dave Borowitz97a70312014-04-28 15:50:23 -070036 PersonIdent ident = newIdent("Mon Jan 2 15:04:05 2006", "-0700");
David Pursehouse7a7f5472016-10-14 09:59:20 +090037 DateFormatter df = new DateFormatter(Optional.empty(), DEFAULT);
Dave Borowitzfde41fd2015-09-16 15:14:38 -040038 assertThat(df.format(ident)).isEqualTo("Mon Jan 02 15:04:05 2006 -0700");
Dave Borowitz97a70312014-04-28 15:50:23 -070039 }
40
41 @Test
Dave Borowitz6d920bc2014-04-28 15:51:37 -070042 public void defaultWithUtc() throws Exception {
Dave Borowitz6d920bc2014-04-28 15:51:37 -070043 PersonIdent ident = newIdent("Mon Jan 2 15:04:05 2006", "-0700");
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070044 DateFormatter df = new DateFormatter(Optional.of(getTimeZone("UTC")), DEFAULT);
Dave Borowitzfde41fd2015-09-16 15:14:38 -040045 assertThat(df.format(ident)).isEqualTo("Mon Jan 02 22:04:05 2006");
Dave Borowitz6d920bc2014-04-28 15:51:37 -070046 }
47
48 @Test
49 public void defaultWithOtherTimeZone() throws Exception {
Dave Borowitz6d920bc2014-04-28 15:51:37 -070050 PersonIdent ident = newIdent("Mon Jan 2 15:04:05 2006", "-0700");
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070051 DateFormatter df = new DateFormatter(Optional.of(getTimeZone("GMT-0400")), DEFAULT);
Dave Borowitzfde41fd2015-09-16 15:14:38 -040052 assertThat(df.format(ident)).isEqualTo("Mon Jan 02 18:04:05 2006");
Dave Borowitz6d920bc2014-04-28 15:51:37 -070053 }
54
55 @Test
Dave Borowitz97a70312014-04-28 15:50:23 -070056 public void isoIncludingTimeZone() throws Exception {
Dave Borowitz97a70312014-04-28 15:50:23 -070057 PersonIdent ident = newIdent("Mon Jan 2 15:04:05 2006", "-0700");
David Pursehouse7a7f5472016-10-14 09:59:20 +090058 DateFormatter df = new DateFormatter(Optional.empty(), ISO);
Dave Borowitzfde41fd2015-09-16 15:14:38 -040059 assertThat(df.format(ident)).isEqualTo("2006-01-02 15:04:05 -0700");
Dave Borowitz97a70312014-04-28 15:50:23 -070060 }
61
Dave Borowitz6d920bc2014-04-28 15:51:37 -070062 @Test
63 public void isoWithUtc() throws Exception {
Dave Borowitz6d920bc2014-04-28 15:51:37 -070064 PersonIdent ident = newIdent("Mon Jan 2 15:04:05 2006", "-0700");
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070065 DateFormatter df = new DateFormatter(Optional.of(getTimeZone("UTC")), ISO);
Dave Borowitzfde41fd2015-09-16 15:14:38 -040066 assertThat(df.format(ident)).isEqualTo("2006-01-02 22:04:05");
Dave Borowitz6d920bc2014-04-28 15:51:37 -070067 }
68
69 @Test
70 public void isoWithOtherTimeZone() throws Exception {
Dave Borowitz6d920bc2014-04-28 15:51:37 -070071 PersonIdent ident = newIdent("Mon Jan 2 15:04:05 2006", "-0700");
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070072 DateFormatter df = new DateFormatter(Optional.of(getTimeZone("GMT-0400")), ISO);
Dave Borowitzfde41fd2015-09-16 15:14:38 -040073 assertThat(df.format(ident)).isEqualTo("2006-01-02 18:04:05");
Dave Borowitz6d920bc2014-04-28 15:51:37 -070074 }
75
Dave Borowitz97a70312014-04-28 15:50:23 -070076 private PersonIdent newIdent(String whenStr, String tzStr) throws ParseException {
77 whenStr += " " + tzStr;
78 Date when = GitDateParser.parse(whenStr, null);
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070079 TimeZone tz = getTimeZone("GMT" + tzStr);
Dave Borowitz97a70312014-04-28 15:50:23 -070080 PersonIdent ident = new PersonIdent("A User", "[email protected]", when, tz);
81 // PersonIdent.toString() uses its own format with "d" instead of "dd",
82 // hence the mismatches in 2 vs. 02 above. Nonetheless I think this sanity
83 // check is useful enough to keep around.
Dave Borowitze360d5c2015-09-16 16:53:30 -040084 assertThat(ident.toString())
85 .isEqualTo("PersonIdent[A User, [email protected], " + whenStr + "]");
Dave Borowitz97a70312014-04-28 15:50:23 -070086 return ident;
87 }
88}