blob: e28b9fa0baa5dc0fa453eddd31b247c17beff63e [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 Borowitz133c1e72015-12-02 12:56:07 -050017import static com.google.common.base.Preconditions.checkNotNull;
Dave Borowitzfde41fd2015-09-16 15:14:38 -040018import static com.google.common.truth.Truth.assertThat;
Dave Borowitz9de65952012-08-13 16:09:45 -070019import static com.google.gitiles.ConfigUtil.getDuration;
Dave Borowitz133c1e72015-12-02 12:56:07 -050020import static com.google.gitiles.ConfigUtil.parseDuration;
Dave Borowitz9de65952012-08-13 16:09:45 -070021
22import org.eclipse.jgit.lib.Config;
23import org.joda.time.Duration;
Dave Borowitzd40bdf12014-04-19 19:33:56 -070024import org.junit.Test;
Dave Borowitz3dc854f2014-11-04 16:19:37 -080025import org.junit.runner.RunWith;
26import org.junit.runners.JUnit4;
Dave Borowitz9de65952012-08-13 16:09:45 -070027
28/** Tests for configuration utilities. */
Dave Borowitz3dc854f2014-11-04 16:19:37 -080029@RunWith(JUnit4.class)
Dave Borowitzd40bdf12014-04-19 19:33:56 -070030public class ConfigUtilTest {
31 @Test
32 public void getDurationReturnsDuration() throws Exception {
Dave Borowitz9de65952012-08-13 16:09:45 -070033 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 Borowitzfde41fd2015-09-16 15:14:38 -040039 assertThat(t.getMillis()).isEqualTo(500);
Dave Borowitz9de65952012-08-13 16:09:45 -070040
41 config.setString("core", "dht", "timeout", "5.2 sec");
42 t = getDuration(config, "core", "dht", "timeout", def);
Dave Borowitzfde41fd2015-09-16 15:14:38 -040043 assertThat(t.getMillis()).isEqualTo(5200);
Dave Borowitz9de65952012-08-13 16:09:45 -070044
45 config.setString("core", "dht", "timeout", "1 min");
46 t = getDuration(config, "core", "dht", "timeout", def);
Dave Borowitzfde41fd2015-09-16 15:14:38 -040047 assertThat(t.getMillis()).isEqualTo(60000);
Dave Borowitz9de65952012-08-13 16:09:45 -070048 }
Dave Borowitz133c1e72015-12-02 12:56:07 -050049
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 Borowitz9de65952012-08-13 16:09:45 -070071}