Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* The MIT License
*
* Copyright 2018 Intuit Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.intuit.karate.core;
import com.intuit.karate.Action;
import com.intuit.karate.Actions;
import com.intuit.karate.CallContext;
import com.intuit.karate.FileUtils;
import com.intuit.karate.JsonUtils;
import com.intuit.karate.FeatureContext;
import com.intuit.karate.StringUtils;
import com.intuit.karate.XmlUtils;
import com.intuit.karate.exception.KarateAbortException;
import com.intuit.karate.exception.KarateException;
import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import com.intuit.karate.StepActions;
import cucumber.api.java.en.When;
import java.util.Collection;
import java.util.HashMap;
/**
*
* @author pthomas3
*/
public class Engine {
private Engine() {
// only static methods
}
private static final Collection PATTERNS;
static {
Map temp = new HashMap();
List overwrite = new ArrayList();
for (Method method : StepActions.class.getMethods()) {
When when = method.getDeclaredAnnotation(When.class);
if (when != null) {
String regex = when.value();
temp.put(regex, new MethodPattern(method, regex));
} else {
Action action = method.getDeclaredAnnotation(Action.class);
if (action != null) {
String regex = action.value();
overwrite.add(new MethodPattern(method, regex));
}
}
}
for (MethodPattern mp : overwrite) {
temp.put(mp.regex, mp);
}
PATTERNS = temp.values();
}
private static final Runnable NO_OP = () -> {
// no op
};
private static final double MILLION = 1000000;
private static final double BILLION = 1000000000;
public static double nanosToSeconds(long nanos) {
return (double) nanos / BILLION;
}
public static double nanosToMillis(long nanos) {
return (double) nanos / 1000000;
}
public static String getBuildDir() {
String command = System.getProperty("sun.java.command", "");
return command.contains("org.gradle.") ? "build" : "target";
}
public static FeatureResult executeFeatureSync(Feature feature, String tagSelector, CallContext callContext) {
FeatureContext featureContext = new FeatureContext(feature, tagSelector);
if (callContext == null) {
callContext = new CallContext(null, true);
}
ExecutionContext exec = new ExecutionContext(featureContext, callContext, null);
FeatureExecutionUnit unit = new FeatureExecutionUnit(exec);
unit.submit(NO_OP);
return exec.result;
}
private static final String UNKNOWN = "-unknown-";
public static String getFeatureName(Step step) {
if (step.getScenario() == null) {
return UNKNOWN;
}
return step.getScenario().getFeature().getPath().getFileName().toString();
}
public static Result executeStep(Step step, Actions actions) {
String text = step.getText();
List matches = findMethodsMatching(text);
if (matches.isEmpty()) {
KarateException e = new KarateException("no step-definition method match found for: " + text);
return Result.failed(0, e, step);
} else if (matches.size() > 1) {
KarateException e = new KarateException("more than one step-definition method matched: " + text + " - " + matches);
return Result.failed(0, e, step);
}
MethodMatch match = matches.get(0);
Object last;
if (step.getDocString() != null) {
last = step.getDocString();
} else if (step.getTable() != null) {
last = step.getTable().getRowsAsMaps();
} else {
last = null;
}
Object[] args = match.convertArgs(last);
long startTime = System.nanoTime();
try {
match.method.invoke(actions, args);
return Result.passed(getElapsedTime(startTime));
} catch (InvocationTargetException e) { // target will be KarateException
if (e.getTargetException() instanceof KarateAbortException) {
return Result.aborted(getElapsedTime(startTime));
} else {
return Result.failed(getElapsedTime(startTime), e.getTargetException(), step);
}
} catch (Exception e) {
return Result.failed(getElapsedTime(startTime), e, step);
}
}
public static File saveResultJson(String targetDir, FeatureResult result) {
List