com.technophobia.substeps.model.ParentStep Maven / Gradle / Ivy
/*
* Copyright Technophobia Ltd 2012
*
* This file is part of Substeps.
*
* Substeps is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Substeps is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Substeps. If not, see .
*/
package com.technophobia.substeps.model;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* represents a step that consists of sub steps
*
* @author ian
*/
public class ParentStep {
private static final Logger log = LoggerFactory.getLogger(ParentStep.class);
public static final ParentStepNameComparator PARENT_STEP_COMPARATOR = new ParentStepNameComparator();
private final Step parent;
private List substeps;
private ExampleParameter paramValueMap;
public ParentStep(final Step parent) {
this.parent = parent;
}
public int getSourceLineNumber() {
return this.parent.getSourceLineNumber();
}
public void addStep(final Step step) {
if (this.substeps == null) {
this.substeps = new ArrayList();
}
this.substeps.add(step);
}
public Step getParent() {
return this.parent;
}
public List getSteps() {
return this.substeps;
}
// only called by tests
public void initialiseParamValues(final Step step) {
final HashMap map = new HashMap();
final String[] paramValues = Arguments.getArgs(this.parent.getPattern(),
step.getLine(), null, Configuration.INSTANCE.getConfig());
if (paramValues != null) {
for (int i = 0; i < paramValues.length; i++) {
map.put(this.parent.getParamNames().get(i), paramValues[i]);
}
}
this.paramValueMap = new ExampleParameter(step.getSourceLineNumber(),
map);
}
public void initialiseParamValues(final int lineNumber, final String line) {
initialiseParamValues(lineNumber, line, null);
}
public void initialiseParamValues(final int lineNumber, final String line, String[] keywordPrecedence) {
log.debug("initialiseParamValues with line: " + line);
final String[] paramValues = Arguments.getArgs(this.parent.getPattern(),
line, keywordPrecedence, Configuration.INSTANCE.getConfig());
if (paramValues != null) {
final Map map = new HashMap();
for (int i = 0; i < paramValues.length; i++) {
String key = this.parent.getParamNames().get(i);
log.debug("putting value: " + paramValues[i] +
" under key: " + key + " i " + i);
map.put(this.parent.getParamNames().get(i), paramValues[i]);
}
this.paramValueMap = new ExampleParameter(lineNumber, map);
}
}
public ExampleParameter getParamValueMap() {
return this.paramValueMap;
}
public final String getSubStepFileUri() {
return this.getParent().getSource().getAbsolutePath();
}
public final String getSubStepFile() {
return this.getParent().getSource().getName();
}
public ParentStep cloneWithAltLine(final String altLine) {
final ParentStep clone = new ParentStep(
this.parent.cloneWithAlternativeLine(altLine));
clone.substeps = this.substeps;
clone.paramValueMap = this.paramValueMap;
return clone;
}
}