| 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 | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 17 | import static com.google.common.truth.Truth.assertThat; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 18 | import static com.google.gitiles.ConfigUtil.getDuration; |
| Jonathan Nieder | c49e92e | 2016-06-30 16:00:50 -0700 | [diff] [blame] | 19 | import static org.junit.Assert.fail; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 20 | |
| David Pursehouse | c0037b8 | 2018-03-16 13:56:20 +0900 | [diff] [blame^] | 21 | import java.time.Duration; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 22 | import org.eclipse.jgit.lib.Config; |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 23 | import org.junit.Test; |
| Dave Borowitz | 3dc854f | 2014-11-04 16:19:37 -0800 | [diff] [blame] | 24 | import org.junit.runner.RunWith; |
| 25 | import org.junit.runners.JUnit4; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 26 | |
| 27 | /** Tests for configuration utilities. */ |
| Dave Borowitz | 3dc854f | 2014-11-04 16:19:37 -0800 | [diff] [blame] | 28 | @RunWith(JUnit4.class) |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 29 | public class ConfigUtilTest { |
| 30 | @Test |
| 31 | public void getDurationReturnsDuration() throws Exception { |
| David Pursehouse | c0037b8 | 2018-03-16 13:56:20 +0900 | [diff] [blame^] | 32 | Duration def = Duration.ofSeconds(2); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 33 | Config config = new Config(); |
| 34 | Duration t; |
| 35 | |
| 36 | config.setString("core", "dht", "timeout", "500 ms"); |
| 37 | t = getDuration(config, "core", "dht", "timeout", def); |
| David Pursehouse | c0037b8 | 2018-03-16 13:56:20 +0900 | [diff] [blame^] | 38 | assertThat(t.toMillis()).isEqualTo(500); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 39 | |
| 40 | config.setString("core", "dht", "timeout", "5.2 sec"); |
| Jonathan Nieder | c49e92e | 2016-06-30 16:00:50 -0700 | [diff] [blame] | 41 | try { |
| 42 | getDuration(config, "core", "dht", "timeout", def); |
| 43 | fail("expected IllegalArgumentException"); |
| 44 | } catch (IllegalArgumentException e) { |
| David Pursehouse | ccaa85d | 2017-05-30 10:47:27 +0900 | [diff] [blame] | 45 | assertThat(e).hasMessageThat().isEqualTo("Invalid time unit value: core.dht.timeout=5.2 sec"); |
| Jonathan Nieder | c49e92e | 2016-06-30 16:00:50 -0700 | [diff] [blame] | 46 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 47 | |
| 48 | config.setString("core", "dht", "timeout", "1 min"); |
| 49 | t = getDuration(config, "core", "dht", "timeout", def); |
| David Pursehouse | c0037b8 | 2018-03-16 13:56:20 +0900 | [diff] [blame^] | 50 | assertThat(t.toMillis()).isEqualTo(60000); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 51 | } |
| Dave Borowitz | 133c1e7 | 2015-12-02 12:56:07 -0500 | [diff] [blame] | 52 | |
| 53 | @Test |
| Jonathan Nieder | c49e92e | 2016-06-30 16:00:50 -0700 | [diff] [blame] | 54 | public void getDurationCanReturnDefault() throws Exception { |
| David Pursehouse | c0037b8 | 2018-03-16 13:56:20 +0900 | [diff] [blame^] | 55 | Duration def = Duration.ofSeconds(1); |
| Jonathan Nieder | c49e92e | 2016-06-30 16:00:50 -0700 | [diff] [blame] | 56 | Config config = new Config(); |
| 57 | Duration t; |
| 58 | |
| 59 | t = getDuration(config, "core", null, "blank", def); |
| David Pursehouse | c0037b8 | 2018-03-16 13:56:20 +0900 | [diff] [blame^] | 60 | assertThat(t.toMillis()).isEqualTo(1000); |
| Jonathan Nieder | c49e92e | 2016-06-30 16:00:50 -0700 | [diff] [blame] | 61 | |
| 62 | config.setString("core", null, "blank", ""); |
| 63 | t = getDuration(config, "core", null, "blank", def); |
| David Pursehouse | c0037b8 | 2018-03-16 13:56:20 +0900 | [diff] [blame^] | 64 | assertThat(t.toMillis()).isEqualTo(1000); |
| Jonathan Nieder | c49e92e | 2016-06-30 16:00:50 -0700 | [diff] [blame] | 65 | |
| 66 | config.setString("core", null, "blank", " "); |
| 67 | t = getDuration(config, "core", null, "blank", def); |
| David Pursehouse | c0037b8 | 2018-03-16 13:56:20 +0900 | [diff] [blame^] | 68 | assertThat(t.toMillis()).isEqualTo(1000); |
| Dave Borowitz | 133c1e7 | 2015-12-02 12:56:07 -0500 | [diff] [blame] | 69 | } |
| 70 | |
| Jonathan Nieder | c49e92e | 2016-06-30 16:00:50 -0700 | [diff] [blame] | 71 | @Test |
| 72 | public void nullAsDefault() throws Exception { |
| 73 | Config config = new Config(); |
| 74 | Duration t; |
| Dave Borowitz | 133c1e7 | 2015-12-02 12:56:07 -0500 | [diff] [blame] | 75 | |
| Jonathan Nieder | c49e92e | 2016-06-30 16:00:50 -0700 | [diff] [blame] | 76 | t = getDuration(config, "core", null, "blank", null); |
| 77 | assertThat(t).isNull(); |
| 78 | |
| 79 | config.setString("core", null, "blank", ""); |
| 80 | t = getDuration(config, "core", null, "blank", null); |
| 81 | assertThat(t).isNull(); |
| 82 | |
| 83 | config.setString("core", null, "blank", " "); |
| 84 | t = getDuration(config, "core", null, "blank", null); |
| 85 | assertThat(t).isNull(); |
| Dave Borowitz | 133c1e7 | 2015-12-02 12:56:07 -0500 | [diff] [blame] | 86 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 87 | } |