Skip to content
This repository has been archived by the owner on Mar 2, 2020. It is now read-only.

Commit

Permalink
Test procedure export to csv (#472)
Browse files Browse the repository at this point in the history
* refactor test specification export

* wip: only testing is missing

* add tests

* change output folder of test (to bin_test)

* remove duplicate files

* fix imports
  • Loading branch information
junkerm authored Aug 20, 2019
1 parent cfb78b9 commit 2b50b27
Show file tree
Hide file tree
Showing 37 changed files with 474 additions and 213 deletions.
2 changes: 1 addition & 1 deletion bundles/specmate-testspecification/.classpath
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="bin" path="src"/>
<classpathentry kind="src" path="test"/>
<classpathentry kind="src" output="bin_test" path="test"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="module" value="true"/>
Expand Down
5 changes: 3 additions & 2 deletions bundles/specmate-testspecification/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/bin/
/generated/
/bin/
/generated/
/bin_test/
4 changes: 3 additions & 1 deletion bundles/specmate-testspecification/bnd.bnd
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@
specmate-metrics;version=latest
Private-Package: \
com.specmate.testspecification.internal.services,\
com.specmate.testspecification.internal.testskeleton
com.specmate.testspecification.internal.exporters
-privatepackage: com.specmate.testspecification.internal.generators
Export-Package: com.specmate.testspecification.api
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.specmate.testspecification.api;

import com.specmate.model.testspecification.TestSpecificationSkeleton;

public interface ITestExporter {

/** getter for language */
String getLanguage();

/** Generates an export for the test specification */
TestSpecificationSkeleton generate(Object object);

/** Signals that this exporter can export test specifications */
boolean canExportTestSpecification();

/** Signals that this exporter can export test procedures */
boolean canExportTestProcedure();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.specmate.testspecification.api;

public interface ITestProcedureExporter {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version 1.0.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.specmate.testspecification.internal.exporters;

import static com.specmate.testspecification.internal.exporters.ExportUtil.CSV_COL_SEP;
import static com.specmate.testspecification.internal.exporters.ExportUtil.CSV_LINE_SEP;

import java.util.List;
import java.util.StringJoiner;

import org.osgi.service.component.annotations.Component;

import com.specmate.model.support.util.SpecmateEcoreUtil;
import com.specmate.model.testspecification.TestProcedure;
import com.specmate.model.testspecification.TestSpecificationSkeleton;
import com.specmate.model.testspecification.TestStep;
import com.specmate.model.testspecification.TestspecificationFactory;
import com.specmate.testspecification.api.ITestExporter;

@Component(immediate = true)
public class CSVTestProcedureExporter implements ITestExporter {
private static final String HEADER = "Step Name" + CSV_COL_SEP + "Action" + CSV_COL_SEP + "Expected Outcome";

@Override
public boolean canExportTestProcedure() {
return true;
}

@Override
public boolean canExportTestSpecification() {
return false;
}

@Override
public String getLanguage() {
return "csv";
}

@Override
public TestSpecificationSkeleton generate(Object object) {
TestProcedure testprocedure = (TestProcedure) object;
StringJoiner joiner = new StringJoiner(CSV_LINE_SEP);
joiner.add(HEADER);
List<TestStep> testSteps = SpecmateEcoreUtil.pickInstancesOf(testprocedure.getContents(), TestStep.class);
for (TestStep step : testSteps) {
joiner.add(step.getName() + CSV_COL_SEP + step.getDescription() + CSV_COL_SEP + step.getExpectedOutcome());
}
TestSpecificationSkeleton skelleton = TestspecificationFactory.eINSTANCE.createTestSpecificationSkeleton();
skelleton.setName(ExportUtil.replaceInvalidChars(testprocedure.getName()) + ".csv");
skelleton.setLanguage(getLanguage());
skelleton.setCode(joiner.toString());
return skelleton;

}

}
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
package com.specmate.testspecification.internal.testskeleton;
package com.specmate.testspecification.internal.exporters;

import static com.specmate.testspecification.internal.exporters.ExportUtil.replaceInvalidChars;

import java.util.List;
import java.util.StringJoiner;

import org.apache.commons.lang3.StringUtils;
import org.osgi.service.component.annotations.Component;

import com.specmate.model.testspecification.ParameterAssignment;
import com.specmate.model.testspecification.TestCase;
import com.specmate.model.testspecification.TestParameter;
import com.specmate.model.testspecification.TestSpecification;
import com.specmate.testspecification.api.ITestExporter;

public class CSVTestSpecificationSkeleton extends BaseSkeleton {

private static final String TEXT_WRAP = "\"";
private static final String COL_SEP = ";";
/** Exports a test specification as CSV */
@Component(immediate = true, service = ITestExporter.class)
public class CSVTestSpecificationExporter extends TestSpecificationExporterBase {

public CSVTestSpecificationSkeleton(String language) {
super(language);
public CSVTestSpecificationExporter() {
super("csv");
}

@Override
protected void generateHeader(StringBuilder sb, TestSpecification testSpecification2,
List<TestParameter> parameters) {
StringJoiner joiner = new StringJoiner(COL_SEP);
StringJoiner joiner = new StringJoiner(ExportUtil.CSV_COL_SEP);
joiner.add("\"TC\"");
for (TestParameter param : parameters) {
joiner.add(StringUtils.wrap(param.getType().toString() + " - " + param.getName(), TEXT_WRAP));
joiner.add(
StringUtils.wrap(param.getType().toString() + " - " + param.getName(), ExportUtil.CSV_TEXT_WRAP));
}
sb.append(joiner).append("\n");
sb.append(joiner).append(ExportUtil.CSV_LINE_SEP);
}

@Override
Expand All @@ -37,22 +41,23 @@ protected void generateFooter(StringBuilder sb, TestSpecification testSpecificat

@Override
protected void generateTestCaseFooter(StringBuilder sb, TestCase tc) {
sb.append("\n");
sb.append(ExportUtil.CSV_LINE_SEP);
}

@Override
protected void generateTestCaseHeader(StringBuilder sb, TestSpecification ts, TestCase tc) {
sb.append(tc.getName() + COL_SEP);
sb.append(tc.getName() + ExportUtil.CSV_COL_SEP);
}

@Override
protected void generateTestCaseParameterAssignments(StringBuilder sb, List<ParameterAssignment> assignments) {
StringJoiner joiner = new StringJoiner(COL_SEP);
StringJoiner joiner = new StringJoiner(ExportUtil.CSV_COL_SEP);
for (ParameterAssignment assignment : assignments) {
String assignmentValue = assignment.getCondition();
String characterToEscape = "=";
String escapeString = StringUtils.isEmpty(assignmentValue) ? "" : escapeString(assignmentValue, characterToEscape);
joiner.add(StringUtils.wrap(escapeString + assignmentValue, TEXT_WRAP));
String escapeString = StringUtils.isEmpty(assignmentValue) ? ""
: escapeString(assignmentValue, characterToEscape);
joiner.add(StringUtils.wrap(escapeString + assignmentValue, ExportUtil.CSV_TEXT_WRAP));
}
sb.append(joiner.toString());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.specmate.testspecification.internal.exporters;

import java.util.regex.Pattern;

/** Utility methods for exporting test specifications and procedures */
public class ExportUtil {

/** Char to wrap text fields in */
public static final String CSV_TEXT_WRAP = "\"";
/** Char to separate columns */
public static final String CSV_COL_SEP = ";";
/** Char used for line end */
public static final String CSV_LINE_SEP = "\n";

/** pattern that detects a number at the beginning */
private static Pattern startsNumerical = Pattern.compile("^[0-9]");

/** invalid characters for test names */
private static Pattern invalidChars = Pattern.compile("[^a-zA-Z_0-9\\_]");

/** Replaces potentially invalid characters for names */
public static String replaceInvalidChars(String name) {
name = startsNumerical.matcher(name).replaceAll("");
return invalidChars.matcher(name).replaceAll("_");
}

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package com.specmate.testspecification.internal.testskeleton;
package com.specmate.testspecification.internal.exporters;

import static com.specmate.testspecification.internal.exporters.ExportUtil.replaceInvalidChars;

import java.util.List;

import org.osgi.service.component.annotations.Component;

import com.specmate.model.testspecification.ParameterAssignment;
import com.specmate.model.testspecification.TestCase;
import com.specmate.model.testspecification.TestParameter;
import com.specmate.model.testspecification.TestSpecification;
import com.specmate.testspecification.api.ITestExporter;

public class JavaTestSpecificationSkeleton extends BaseSkeleton {
/** Exports a test specification to java */
@Component(immediate = true, service = ITestExporter.class)
public class JavaTestSpecificationExporter extends TestSpecificationExporterBase {

public JavaTestSpecificationSkeleton(String language) {
super(language);
public JavaTestSpecificationExporter() {
super("java");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package com.specmate.testspecification.internal.testskeleton;
package com.specmate.testspecification.internal.exporters;

import static com.specmate.testspecification.internal.exporters.ExportUtil.replaceInvalidChars;

import java.util.List;

import org.osgi.service.component.annotations.Component;

import com.specmate.model.testspecification.ParameterAssignment;
import com.specmate.model.testspecification.TestCase;
import com.specmate.model.testspecification.TestParameter;
import com.specmate.model.testspecification.TestSpecification;
import com.specmate.testspecification.api.ITestExporter;

public class JavascriptTestSpecificationSkeleton extends BaseSkeleton {
/** Exports a test specification to javascript */
@Component(immediate = true, service = ITestExporter.class)
public class JavascriptTestSpecificationExporter extends TestSpecificationExporterBase {

public JavascriptTestSpecificationSkeleton(String language) {
super(language);
public JavascriptTestSpecificationExporter() {
super("javascript");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.specmate.testspecification.internal.testskeleton;
package com.specmate.testspecification.internal.exporters;

import static com.specmate.testspecification.internal.exporters.ExportUtil.replaceInvalidChars;

import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import com.specmate.model.support.util.SpecmateEcoreUtil;
Expand All @@ -14,28 +15,43 @@
import com.specmate.model.testspecification.TestSpecification;
import com.specmate.model.testspecification.TestSpecificationSkeleton;
import com.specmate.model.testspecification.TestspecificationFactory;
import com.specmate.testspecification.api.ITestExporter;

/** Base class for Test Specification Exporters (aka "Skeletons") */
public abstract class BaseSkeleton {
public abstract class TestSpecificationExporterBase implements ITestExporter {

/** the language for the export */
protected String language;

private static Pattern startsNumerical = Pattern.compile("^[0-9]");
private static Pattern invalidChars = Pattern.compile("[^a-zA-Z_0-9\\_]");

Comparator<TestParameter> parameterComparator = (p1, p2) -> compareParameter(p1, p2);
Comparator<ParameterAssignment> assignmentComparator = (a1, a2) -> compareParameter(a1.getParameter(),
a2.getParameter());

public BaseSkeleton(String language) {
/** constructor */
public TestSpecificationExporterBase(String language) {
this.language = language;
}

private static int compareParameter(TestParameter p1, TestParameter p2) {
return p1.getType().compareTo(p2.getType()) * 10 + Integer.signum(p1.getName().compareTo(p2.getName()));
/** getter for language */
@Override
public String getLanguage() {
return language;
}

@Override
public boolean canExportTestProcedure() {
return false;
}

@Override
public boolean canExportTestSpecification() {
return true;
}

public TestSpecificationSkeleton generate(TestSpecification testSpecification) {
/** Generates an export for the test specification */
@Override
public TestSpecificationSkeleton generate(Object obj) {
TestSpecification testSpecification = (TestSpecification) obj;
StringBuilder sb = new StringBuilder();

TestSpecificationSkeleton tss = TestspecificationFactory.eINSTANCE.createTestSpecificationSkeleton();
Expand All @@ -57,6 +73,10 @@ public TestSpecificationSkeleton generate(TestSpecification testSpecification) {
return tss;
}

private static int compareParameter(TestParameter p1, TestParameter p2) {
return p1.getType().compareTo(p2.getType()) * 10 + Integer.signum(p1.getName().compareTo(p2.getName()));
}

/**
* Return Test parameter assignments sorted by type (input before output)
*/
Expand All @@ -75,11 +95,6 @@ private List<TestCase> getTestCases(TestSpecification testSpecification) {
return SpecmateEcoreUtil.pickInstancesOf(testSpecification.getContents(), TestCase.class);
}

protected String replaceInvalidChars(String name) {
name = startsNumerical.matcher(name).replaceAll("");
return invalidChars.matcher(name).replaceAll("_");
}

protected void appendParameterValue(StringBuilder sb, ParameterAssignment pa) {
sb.append("___");
sb.append(replaceInvalidChars(pa.getParameter().getName()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.specmate.testspecification.internal.services;
package com.specmate.testspecification.internal.generators;

import java.util.Comparator;
import java.util.TreeMap;

import com.specmate.model.base.IContainer;
import com.specmate.model.requirements.CEGNode;

@SuppressWarnings("serial")
Expand Down
Loading

0 comments on commit 2b50b27

Please sign in to comment.