All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tngtech.java.junit.dataprovider.internal.placeholder.ParameterPlaceholder Maven / Gradle / Ivy

Go to download

A TestNG like dataprovider runner for JUnit having a simplified syntax compared to all the existing JUnit4 features.

The newest version!
/*
 * Copyright 2019 TNG Technology Consulting GmbH
 *
 * Licensed 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 com.tngtech.java.junit.dataprovider.internal.placeholder;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.junit.dataprovider.placeholder.ArgumentPlaceholder;
import com.tngtech.junit.dataprovider.placeholder.ReplacementData;

/**
 * This placeholder format the parameters of a dataprovider test as comma-separated {@link String} according to the
 * given index or range subscript (see {@link DataProvider#format()}. Furthermore the following parameter values are
 * treated specially:
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
Special {@link String} treatment
Parameter valuetarget {@link String}
null<null>
"" (= empty string)<empty string>
array (e.g. String[]){@code "[" + formatPattern(array) + "]"}
other{@link Object#toString()}
*/ public class ParameterPlaceholder extends ArgumentPlaceholder { /** * {@link String} representation of {@code null} *

* This field is package private (= visible) for testing. *

*/ static final String STRING_NULL = ArgumentPlaceholder.STRING_NULL; /** * {@link String} representation of {@code ""} *

* This field is package private (= visible) for testing. *

*/ static final String STRING_EMPTY = ArgumentPlaceholder.STRING_EMPTY; /** * {@link String} representation of an non-printable character *

* This field is package private (= visible) for testing. *

*/ static final String STRING_NON_PRINTABLE = ArgumentPlaceholder.STRING_NON_PRINTABLE; // -- Begin: copied from origin BasePlaceholder for backwards compatibility reasons -------------------------------- protected Method method; protected int idx; protected Object[] parameters; /** * Sets the given arguments as context for processing or replacement generation, respectively. * * @param method - test method * @param idx - index of the dataprovider row * @param parameters of the current dataprovider test to be executed */ public void setContext(Method method, int idx, Object[] parameters) { this.method = method; this.idx = idx; this.parameters = Arrays.copyOf(parameters, parameters.length); } /** * Executes this placeholder for the given {@link String} by searching all occurrences of the regular expression * supplied in the constructor and replaces them with the retrieved replacement from * {@link #getReplacementFor(String)}. If the regular expression does not match, an exact copy of the given * {@link String} is returned. * * @param formatPattern to be processed * @return the given {@code formatPattern} containing the generated replacements instead of matching patterns */ public String process(String formatPattern) { ReplacementData data = ReplacementData.of(method, idx, Arrays.asList(parameters)); return super.process(data, formatPattern); } // -- End: copied from origin BasePlaceholder for backwards compatibility reasons ---------------------------------- protected String getReplacementFor(String placeholder) { ReplacementData data = ReplacementData.of(method, idx, Arrays.asList(parameters)); return super.getReplacementFor(placeholder, data); } /** * Formats the given parameters by retrieving it's {@link String} representation and separate it by comma (= * {@code ,}). *

* Note: For new and future-proof implementations, please use {@link #formatAll(java.util.List)} instead. * * @param parameters to be formatted * @return the {@link String} representation of the given {@link Object}{@code []} * * @see #formatAll(java.util.List) */ protected String formatAll(Object[] parameters) { return super.formatAll(Arrays.asList(parameters)); } @Override protected String formatAll(List arguments) { return formatAll(arguments.toArray()); } @Override protected String format(Object param) { return super.format(param); } }