LCOV - code coverage report
Current view: top level - testing - SystemPropertiesTestRule.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 23 23 100.0 %
Date: 2022-11-19 15:00:39 Functions: 5 5 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2021 The Android Open Source Project
       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.gerrit.testing;
      16             : 
      17             : import static com.google.common.base.Preconditions.checkState;
      18             : 
      19             : import com.google.common.collect.ImmutableMap;
      20             : import com.google.gerrit.common.Nullable;
      21             : import java.util.Map;
      22             : import java.util.Optional;
      23             : import org.junit.rules.ExternalResource;
      24             : 
      25             : /** Setup system properties before tests and return previous value after tests are finished */
      26             : public class SystemPropertiesTestRule extends ExternalResource {
      27             :   ImmutableMap<String, Optional<String>> properties;
      28             :   @Nullable ImmutableMap<String, Optional<String>> previousValues;
      29             : 
      30             :   public SystemPropertiesTestRule(String key, String value) {
      31           1 :     this(ImmutableMap.of(key, Optional.of(value)));
      32           1 :   }
      33             : 
      34           1 :   public SystemPropertiesTestRule(Map<String, Optional<String>> properties) {
      35           1 :     this.properties = ImmutableMap.copyOf(properties);
      36           1 :   }
      37             : 
      38             :   @Override
      39             :   protected void before() throws Throwable {
      40           1 :     super.before();
      41           1 :     checkState(
      42             :         previousValues == null,
      43             :         "after() wasn't called after the previous call to the before() method");
      44           1 :     ImmutableMap.Builder<String, Optional<String>> previousValuesBuilder = ImmutableMap.builder();
      45           1 :     for (String key : properties.keySet()) {
      46           1 :       previousValuesBuilder.put(key, Optional.ofNullable(System.getProperty(key)));
      47           1 :     }
      48           1 :     previousValues = previousValuesBuilder.build();
      49           1 :     properties.entrySet().stream().forEach(this::setSystemProperty);
      50           1 :   }
      51             : 
      52             :   @Override
      53             :   protected void after() {
      54           1 :     checkState(previousValues != null, "before() wasn't called");
      55           1 :     previousValues.entrySet().stream().forEach(this::setSystemProperty);
      56           1 :     previousValues = null;
      57           1 :     super.after();
      58           1 :   }
      59             : 
      60             :   private void setSystemProperty(Map.Entry<String, Optional<String>> keyValue) {
      61           1 :     if (keyValue.getValue().isPresent()) {
      62           1 :       System.setProperty(keyValue.getKey(), keyValue.getValue().get());
      63             :     } else {
      64           1 :       System.clearProperty(keyValue.getKey());
      65             :     }
      66           1 :   }
      67             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750