blob: 1cb849d58476cb70c27b8c70dd818977524a53e6 [file] [log] [blame]
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you under the Apache License, Version 2.0 (the
~ "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
package org.apache.sling.cli.impl.junit;
import java.util.HashMap;
import java.util.Map;
import org.junit.rules.ExternalResource;
public class SystemPropertiesRule extends ExternalResource {
private final Map<String, String> propsToRestore = new HashMap<>();
private final Map<String, String> propsToOverride;
public SystemPropertiesRule(Map<String, String> propsToOverride) {
this.propsToOverride = propsToOverride;
}
@Override
protected void before() {
for (Map.Entry<String, String> prop : propsToOverride.entrySet() )
propsToRestore.put(prop.getKey(), set(prop));
}
private String set(Map.Entry<String, String> prop) {
if ( prop.getValue() != null )
return System.setProperty(prop.getKey(), prop.getValue());
return System.clearProperty(prop.getKey());
}
@Override
protected void after() {
for (Map.Entry<String, String> prop : propsToRestore.entrySet() )
set(prop);
}
}