| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 1 | // Copyright 2012 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 | 133c1e7 | 2015-12-02 12:56:07 -0500 | [diff] [blame] | 17 | import static com.google.common.base.Preconditions.checkNotNull; |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 18 | import static com.google.common.truth.Truth.assertThat; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 19 | import static com.google.gitiles.ConfigUtil.getDuration; |
| Dave Borowitz | 133c1e7 | 2015-12-02 12:56:07 -0500 | [diff] [blame] | 20 | import static com.google.gitiles.ConfigUtil.parseDuration; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 21 | |
| 22 | import org.eclipse.jgit.lib.Config; |
| 23 | import org.joda.time.Duration; |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 24 | import org.junit.Test; |
| Dave Borowitz | 3dc854f | 2014-11-04 16:19:37 -0800 | [diff] [blame] | 25 | import org.junit.runner.RunWith; |
| 26 | import org.junit.runners.JUnit4; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 27 | |
| 28 | /** Tests for configuration utilities. */ |
| Dave Borowitz | 3dc854f | 2014-11-04 16:19:37 -0800 | [diff] [blame] | 29 | @RunWith(JUnit4.class) |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 30 | public class ConfigUtilTest { |
| 31 | @Test |
| 32 | public void getDurationReturnsDuration() throws Exception { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 33 | Duration def = Duration.standardSeconds(2); |
| 34 | Config config = new Config(); |
| 35 | Duration t; |
| 36 | |
| 37 | config.setString("core", "dht", "timeout", "500 ms"); |
| 38 | t = getDuration(config, "core", "dht", "timeout", def); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 39 | assertThat(t.getMillis()).isEqualTo(500); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 40 | |
| 41 | config.setString("core", "dht", "timeout", "5.2 sec"); |
| 42 | t = getDuration(config, "core", "dht", "timeout", def); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 43 | assertThat(t.getMillis()).isEqualTo(5200); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 44 | |
| 45 | config.setString("core", "dht", "timeout", "1 min"); |
| 46 | t = getDuration(config, "core", "dht", "timeout", def); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 47 | assertThat(t.getMillis()).isEqualTo(60000); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 48 | } |
| Dave Borowitz | 133c1e7 | 2015-12-02 12:56:07 -0500 | [diff] [blame] | 49 | |
| 50 | @Test |
| 51 | public void parseDurationReturnsDuration() throws Exception { |
| 52 | assertDoesNotParse(null); |
| 53 | assertDoesNotParse(""); |
| 54 | assertDoesNotParse(" "); |
| 55 | assertParses(500, "500 ms"); |
| 56 | assertParses(500, "500ms"); |
| 57 | assertParses(500, " 500 ms "); |
| 58 | assertParses(5200, "5.2 sec"); |
| 59 | assertParses(60000, "1 min"); |
| 60 | } |
| 61 | |
| 62 | private static void assertDoesNotParse(String val) { |
| 63 | assertThat(parseDuration(val)).named(String.valueOf(val)).isNull(); |
| 64 | } |
| 65 | |
| 66 | private static void assertParses(long expectedMillis, String val) { |
| 67 | Duration actual = parseDuration(checkNotNull(val)); |
| 68 | assertThat(actual).named(val).isNotNull(); |
| 69 | assertThat(actual.getMillis()).named(val).isEqualTo(expectedMillis); |
| 70 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 71 | } |