/* * * 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.qpid.junit.extensions.util; import java.util.Properties; /** * TestContextProperties is an extension of {@link ParsedProperties} that keeps track of property key/value pairs * that are used by tests being run under the {@link org.apache.qpid.junit.extensions.TKTestRunner}. To keep the * test runner notified of configurable test parameters, tests should establish their required property values by * initiliazing fields or statics or in the constructor, through this class. The tk test runner automatically places * any additional properties specified on the command line into the this class, and these are held statically. * *
Here is an example: * ** public class MyTestClass extends TestCase { * ParsedProperties testProps = TestContextProperties.getInstance(); * private int testParam = testProps.setPropertyIfNull("testParam", 1); * ... ** * This has the effect of setting up the field testParam with the default value of 1, unless it is overridden * by values passed to the tk test runner. It also notifies the tk test runner of the name and value of the test * parameter actually used for the test, so that this can be logged in the test output file. * *
Responsibilities | Collaborations * |
---|---|
Log all name/value pairs read or written. * |
put
. Provided for parallelism with the getProperty
* method. Enforces use of strings for property keys and values. The value returned is the result of the
* Hashtable call to put
. The property key/value pair is remembered and made
* available to {@link org.apache.qpid.junit.extensions.TKTestRunner}.
*
* @param key The key to be placed into this property list.
* @param value The value corresponding to key.
*
* @return The previous value of the specified key in this property list, or null
if it did not have one.
*/
public synchronized Object setProperty(String key, String value)
{
// log.debug("public synchronized Object setProperty(String key = " + key + ", String value = " + value + "): called");
Object result = super.setProperty(key, value);
accessedProps.setProperty(key, value);
return result;
}
/**
* Helper method for setting properties to defaults when they are not already set. The property key/value pair is
* remembered and made available to {@link org.apache.qpid.junit.extensions.TKTestRunner}.
*
* @param key The name of the system property to set.
* @param value The value to set it to.
*
* @return The value of the property, which will be the value passed in if it was null, or the existing value otherwise.
*/
public String setPropertyIfNull(String key, String value)
{
// log.debug("public String setPropertyIfNull(String key = " + key + ", String value = " + value + "): called");
String result = super.setPropertyIfNull(key, value);
if (value != null)
{
accessedProps.setProperty(key, result);
}
// log.debug("result = " + result);
return result;
}
}