blob: a9d58bc6ea037df373e636dbc4f570fe5e53dd90 [file] [log] [blame]
Dave Borowitz9de65952012-08-13 16:09:45 -07001// 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
15package com.google.gitiles;
16
Dave Borowitzfde41fd2015-09-16 15:14:38 -040017import static com.google.common.truth.Truth.assertThat;
Dave Borowitz9de65952012-08-13 16:09:45 -070018import static com.google.gitiles.ConfigUtil.getDuration;
Jonathan Niederc49e92e2016-06-30 16:00:50 -070019import static org.junit.Assert.fail;
Dave Borowitz9de65952012-08-13 16:09:45 -070020
21import org.eclipse.jgit.lib.Config;
22import org.joda.time.Duration;
Dave Borowitzd40bdf12014-04-19 19:33:56 -070023import org.junit.Test;
Dave Borowitz3dc854f2014-11-04 16:19:37 -080024import org.junit.runner.RunWith;
25import org.junit.runners.JUnit4;
Dave Borowitz9de65952012-08-13 16:09:45 -070026
27/** Tests for configuration utilities. */
Dave Borowitz3dc854f2014-11-04 16:19:37 -080028@RunWith(JUnit4.class)
Dave Borowitzd40bdf12014-04-19 19:33:56 -070029public class ConfigUtilTest {
30 @Test
31 public void getDurationReturnsDuration() throws Exception {
Dave Borowitz9de65952012-08-13 16:09:45 -070032 Duration def = Duration.standardSeconds(2);
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);
Dave Borowitzfde41fd2015-09-16 15:14:38 -040038 assertThat(t.getMillis()).isEqualTo(500);
Dave Borowitz9de65952012-08-13 16:09:45 -070039
40 config.setString("core", "dht", "timeout", "5.2 sec");
Jonathan Niederc49e92e2016-06-30 16:00:50 -070041 try {
42 getDuration(config, "core", "dht", "timeout", def);
43 fail("expected IllegalArgumentException");
44 } catch (IllegalArgumentException e) {
David Pursehouseccaa85d2017-05-30 10:47:27 +090045 assertThat(e).hasMessageThat().isEqualTo("Invalid time unit value: core.dht.timeout=5.2 sec");
Jonathan Niederc49e92e2016-06-30 16:00:50 -070046 }
Dave Borowitz9de65952012-08-13 16:09:45 -070047
48 config.setString("core", "dht", "timeout", "1 min");
49 t = getDuration(config, "core", "dht", "timeout", def);
Dave Borowitzfde41fd2015-09-16 15:14:38 -040050 assertThat(t.getMillis()).isEqualTo(60000);
Dave Borowitz9de65952012-08-13 16:09:45 -070051 }
Dave Borowitz133c1e72015-12-02 12:56:07 -050052
53 @Test
Jonathan Niederc49e92e2016-06-30 16:00:50 -070054 public void getDurationCanReturnDefault() throws Exception {
55 Duration def = Duration.standardSeconds(1);
56 Config config = new Config();
57 Duration t;
58
59 t = getDuration(config, "core", null, "blank", def);
60 assertThat(t.getMillis()).isEqualTo(1000);
61
62 config.setString("core", null, "blank", "");
63 t = getDuration(config, "core", null, "blank", def);
64 assertThat(t.getMillis()).isEqualTo(1000);
65
66 config.setString("core", null, "blank", " ");
67 t = getDuration(config, "core", null, "blank", def);
68 assertThat(t.getMillis()).isEqualTo(1000);
Dave Borowitz133c1e72015-12-02 12:56:07 -050069 }
70
Jonathan Niederc49e92e2016-06-30 16:00:50 -070071 @Test
72 public void nullAsDefault() throws Exception {
73 Config config = new Config();
74 Duration t;
Dave Borowitz133c1e72015-12-02 12:56:07 -050075
Jonathan Niederc49e92e2016-06-30 16:00:50 -070076 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 Borowitz133c1e72015-12-02 12:56:07 -050086 }
Dave Borowitz9de65952012-08-13 16:09:45 -070087}