| Dave Borowitz | 97a7031 | 2014-04-28 15:50:23 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package com.google.gitiles; |
| 16 | |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 17 | import static com.google.common.truth.Truth.assertThat; |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 18 | import static com.google.gitiles.DateFormatter.Format.DEFAULT; |
| 19 | import static com.google.gitiles.DateFormatter.Format.ISO; |
| 20 | import static java.util.TimeZone.getTimeZone; |
| Dave Borowitz | 97a7031 | 2014-04-28 15:50:23 -0700 | [diff] [blame] | 21 | |
| Dave Borowitz | 6d920bc | 2014-04-28 15:51:37 -0700 | [diff] [blame] | 22 | import com.google.common.base.Optional; |
| Dave Borowitz | 97a7031 | 2014-04-28 15:50:23 -0700 | [diff] [blame] | 23 | |
| 24 | import org.eclipse.jgit.lib.PersonIdent; |
| 25 | import org.eclipse.jgit.util.GitDateParser; |
| 26 | import org.junit.Test; |
| Dave Borowitz | 3dc854f | 2014-11-04 16:19:37 -0800 | [diff] [blame] | 27 | import org.junit.runner.RunWith; |
| 28 | import org.junit.runners.JUnit4; |
| Dave Borowitz | 97a7031 | 2014-04-28 15:50:23 -0700 | [diff] [blame] | 29 | |
| 30 | import java.text.ParseException; |
| 31 | import java.util.Date; |
| 32 | import java.util.TimeZone; |
| 33 | |
| Dave Borowitz | 3dc854f | 2014-11-04 16:19:37 -0800 | [diff] [blame] | 34 | @RunWith(JUnit4.class) |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 35 | public class DateFormatterTest { |
| Dave Borowitz | 97a7031 | 2014-04-28 15:50:23 -0700 | [diff] [blame] | 36 | @Test |
| 37 | public void defaultIncludingTimeZone() throws Exception { |
| Dave Borowitz | 97a7031 | 2014-04-28 15:50:23 -0700 | [diff] [blame] | 38 | PersonIdent ident = newIdent("Mon Jan 2 15:04:05 2006", "-0700"); |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 39 | DateFormatter df = new DateFormatter(Optional.<TimeZone>absent(), DEFAULT); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 40 | assertThat(df.format(ident)).isEqualTo("Mon Jan 02 15:04:05 2006 -0700"); |
| Dave Borowitz | 97a7031 | 2014-04-28 15:50:23 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | @Test |
| Dave Borowitz | 6d920bc | 2014-04-28 15:51:37 -0700 | [diff] [blame] | 44 | public void defaultWithUtc() throws Exception { |
| Dave Borowitz | 6d920bc | 2014-04-28 15:51:37 -0700 | [diff] [blame] | 45 | PersonIdent ident = newIdent("Mon Jan 2 15:04:05 2006", "-0700"); |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 46 | DateFormatter df = new DateFormatter(Optional.of(getTimeZone("UTC")), DEFAULT); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 47 | assertThat(df.format(ident)).isEqualTo("Mon Jan 02 22:04:05 2006"); |
| Dave Borowitz | 6d920bc | 2014-04-28 15:51:37 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | @Test |
| 51 | public void defaultWithOtherTimeZone() throws Exception { |
| Dave Borowitz | 6d920bc | 2014-04-28 15:51:37 -0700 | [diff] [blame] | 52 | PersonIdent ident = newIdent("Mon Jan 2 15:04:05 2006", "-0700"); |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 53 | DateFormatter df = new DateFormatter(Optional.of(getTimeZone("GMT-0400")), DEFAULT); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 54 | assertThat(df.format(ident)).isEqualTo("Mon Jan 02 18:04:05 2006"); |
| Dave Borowitz | 6d920bc | 2014-04-28 15:51:37 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | @Test |
| Dave Borowitz | 97a7031 | 2014-04-28 15:50:23 -0700 | [diff] [blame] | 58 | public void isoIncludingTimeZone() throws Exception { |
| Dave Borowitz | 97a7031 | 2014-04-28 15:50:23 -0700 | [diff] [blame] | 59 | PersonIdent ident = newIdent("Mon Jan 2 15:04:05 2006", "-0700"); |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 60 | DateFormatter df = new DateFormatter(Optional.<TimeZone>absent(), ISO); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 61 | assertThat(df.format(ident)).isEqualTo("2006-01-02 15:04:05 -0700"); |
| Dave Borowitz | 97a7031 | 2014-04-28 15:50:23 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| Dave Borowitz | 6d920bc | 2014-04-28 15:51:37 -0700 | [diff] [blame] | 64 | @Test |
| 65 | public void isoWithUtc() throws Exception { |
| Dave Borowitz | 6d920bc | 2014-04-28 15:51:37 -0700 | [diff] [blame] | 66 | PersonIdent ident = newIdent("Mon Jan 2 15:04:05 2006", "-0700"); |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 67 | DateFormatter df = new DateFormatter(Optional.of(getTimeZone("UTC")), ISO); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 68 | assertThat(df.format(ident)).isEqualTo("2006-01-02 22:04:05"); |
| Dave Borowitz | 6d920bc | 2014-04-28 15:51:37 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Test |
| 72 | public void isoWithOtherTimeZone() throws Exception { |
| Dave Borowitz | 6d920bc | 2014-04-28 15:51:37 -0700 | [diff] [blame] | 73 | PersonIdent ident = newIdent("Mon Jan 2 15:04:05 2006", "-0700"); |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 74 | DateFormatter df = new DateFormatter(Optional.of(getTimeZone("GMT-0400")), ISO); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 75 | assertThat(df.format(ident)).isEqualTo("2006-01-02 18:04:05"); |
| Dave Borowitz | 6d920bc | 2014-04-28 15:51:37 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| Dave Borowitz | 97a7031 | 2014-04-28 15:50:23 -0700 | [diff] [blame] | 78 | private PersonIdent newIdent(String whenStr, String tzStr) throws ParseException { |
| 79 | whenStr += " " + tzStr; |
| 80 | Date when = GitDateParser.parse(whenStr, null); |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 81 | TimeZone tz = getTimeZone("GMT" + tzStr); |
| Dave Borowitz | 97a7031 | 2014-04-28 15:50:23 -0700 | [diff] [blame] | 82 | 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 Borowitz | e360d5c | 2015-09-16 16:53:30 -0400 | [diff] [blame] | 86 | assertThat(ident.toString()) |
| 87 | .isEqualTo("PersonIdent[A User, [email protected], " + whenStr + "]"); |
| Dave Borowitz | 97a7031 | 2014-04-28 15:50:23 -0700 | [diff] [blame] | 88 | return ident; |
| 89 | } |
| 90 | } |