Skip to content

Commit

Permalink
test(battery): Split up the override property from the executable to …
Browse files Browse the repository at this point in the history
…add support for source file executables. Split the name and resource folder so resources can be shared between tests.
  • Loading branch information
taikuukaits committed Oct 15, 2019
1 parent 5a92ffb commit 1da692b
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package com.synopsys.integration.detect.battery;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;

import com.synopsys.integration.detect.configuration.DetectProperty;

import freemarker.template.TemplateException;

public abstract class BatteryExecutable {
public final DetectProperty detectProperty;
public class BatteryExecutable {
public DetectProperty detectProperty = null;
public String windowsSourceFileName = null;
public String linuxSourceFileName = null;
public BatteryExecutableCreator creator = null;

protected BatteryExecutable(final DetectProperty detectProperty) {
this.detectProperty = detectProperty;
public static BatteryExecutable sourceFileExecutable(final String windowsSourceFileName, final String linuxSourceFileName, final BatteryExecutableCreator creator) {
final BatteryExecutable executable = new BatteryExecutable();
executable.creator = creator;
executable.linuxSourceFileName = linuxSourceFileName;
executable.windowsSourceFileName = windowsSourceFileName;
return executable;
}

public abstract File createExecutable(final int id, final File mockDirectory, AtomicInteger commandCount) throws IOException, TemplateException;
public static BatteryExecutable propertyOverrideExecutable(final DetectProperty detectProperty, final BatteryExecutableCreator creator) {
final BatteryExecutable executable = new BatteryExecutable();
executable.creator = creator;
executable.detectProperty = detectProperty;
return executable;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.synopsys.integration.detect.battery;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;

import freemarker.template.TemplateException;

public abstract class BatteryExecutableCreator {
public abstract File createExecutable(final int id, final File mockDirectory, AtomicInteger commandCount) throws IOException, TemplateException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -52,33 +53,52 @@ public final class BatteryTest {
private File sourceDirectory;

private final Gson gson = new Gson();
private final String name;
private final String testName;
private final String resourcePrefix;

private final AtomicInteger commandCount = new AtomicInteger();
private final AtomicInteger executableCount = new AtomicInteger();

public BatteryTest(final String name) {
this.name = name;
this.testName = name;
this.resourcePrefix = name;
}

public BatteryTest(final String testName, final String resourcePrefix) {
this.testName = testName;
this.resourcePrefix = resourcePrefix;
}

private List<String> prefixResources(final String... resourceFiles) {
return Arrays.stream(resourceFiles)
.map(it -> "/" + this.name + "/" + it)
.map(it -> "/" + this.resourcePrefix + "/" + it)
.collect(Collectors.toList());
}

public void executableFromResourceFiles(final DetectProperty detectProperty, final String... resourceFiles) {
executables.add(new ResourceTypingExecutable(detectProperty, prefixResources(resourceFiles)));
final ResourceTypingExecutableCreator creator = new ResourceTypingExecutableCreator(prefixResources(resourceFiles));
executables.add(BatteryExecutable.propertyOverrideExecutable(detectProperty, creator));
}

public ResourceCopyingExecutable executableThatCopiesFiles(final DetectProperty detectProperty, final String... resourceFiles) {
final ResourceCopyingExecutable resourceCopyingExecutable = new ResourceCopyingExecutable(detectProperty, prefixResources(resourceFiles));
executables.add(resourceCopyingExecutable);
public void executableSourceFileFromResourceFiles(final String windowsName, final String linuxName, final String... resourceFiles) {
final ResourceTypingExecutableCreator creator = new ResourceTypingExecutableCreator(prefixResources(resourceFiles));
executables.add(BatteryExecutable.sourceFileExecutable(windowsName, linuxName, creator));
}

public ResourceCopyingExecutableCreator executableThatCopiesFiles(final DetectProperty detectProperty, final String... resourceFiles) {
final ResourceCopyingExecutableCreator resourceCopyingExecutable = new ResourceCopyingExecutableCreator(prefixResources(resourceFiles));
executables.add(BatteryExecutable.propertyOverrideExecutable(detectProperty, resourceCopyingExecutable));
return resourceCopyingExecutable;
}

public ResourceCopyingExecutableCreator executableSourceFileThatCopiesFiles(final String windowsName, final String linuxName, final String... resourceFiles) {
final ResourceCopyingExecutableCreator resourceCopyingExecutable = new ResourceCopyingExecutableCreator(prefixResources(resourceFiles));
executables.add(BatteryExecutable.sourceFileExecutable(windowsName, linuxName, resourceCopyingExecutable));
return resourceCopyingExecutable;
}

public void executable(final DetectProperty detectProperty, final String... responses) {
executables.add(new StringTypingExecutable(detectProperty, Arrays.asList(responses)));
executables.add(BatteryExecutable.propertyOverrideExecutable(detectProperty, new StringTypingExecutableCreator(Arrays.asList(responses))));
}

public void git(final String origin, final String branch) {
Expand Down Expand Up @@ -176,7 +196,7 @@ private boolean executeDetectJar(final List<String> detectArguments) throws Exec
}

private void initializeDirectories() throws IOException {
testDirectory = new File(batteryDirectory, name);
testDirectory = new File(batteryDirectory, testName);

if (testDirectory.exists()) {
FileUtils.deleteDirectory(testDirectory);
Expand Down Expand Up @@ -205,8 +225,21 @@ private List<String> createExecutables() throws IOException, TemplateException {

for (final BatteryExecutable executable : executables) {
final int id = executableCount.getAndIncrement();
final File commandFile = executable.createExecutable(id, mockDirectory, commandCount);
properties.add("--" + executable.detectProperty.getPropertyKey() + "=" + commandFile.getCanonicalPath());
Assertions.assertNotNull(executable.creator, "Every battery executable must have a 'creator' or a way to actually generate the executable..");
final File commandFile = executable.creator.createExecutable(id, mockDirectory, commandCount);
if (executable.detectProperty != null) {
properties.add("--" + executable.detectProperty.getPropertyKey() + "=" + commandFile.getCanonicalPath());
} else if (executable.linuxSourceFileName != null && executable.windowsSourceFileName != null) {
final File target;
if (SystemUtils.IS_OS_WINDOWS) {
target = new File(sourceDirectory, executable.windowsSourceFileName);
} else {
target = new File(sourceDirectory, executable.linuxSourceFileName);
}
FileUtils.moveFile(commandFile, target);
} else {
throw new RuntimeException("Every battery executable must either specify an override property or a location (for both linux and windows) in the source directory for the executable to go.");
}
}
return properties;
}
Expand All @@ -218,14 +251,14 @@ private void createFiles() throws IOException {
}

for (final String resourceFileName : resourceFileNames) {
final InputStream inputStream = BatteryFiles.asInputStream("/" + name + "/" + resourceFileName);
final InputStream inputStream = BatteryFiles.asInputStream("/" + resourcePrefix + "/" + resourceFileName);
final File file = new File(sourceDirectory, resourceFileName);
Assertions.assertNotNull(inputStream, "Could not find resource file: " + file);
FileUtils.copyInputStreamToFile(inputStream, file);
}

for (final String resourceZipFileName : resourceZipNames) {
final File zipFile = BatteryFiles.asFile("/" + name + "/" + resourceZipFileName + ".zip");
final File zipFile = BatteryFiles.asFile("/" + resourcePrefix + "/" + resourceZipFileName + ".zip");
final File target = new File(sourceDirectory, resourceZipFileName);
ZipUtil.unpack(zipFile, target);
}
Expand All @@ -236,7 +269,7 @@ private void assertBdio() throws IOException, JSONException {
Assertions.assertTrue(bdio != null && bdio.length > 0, "Bdio output files could not be found.");

if (shouldExpectBdioResources) {
final File expectedBdioFolder = BatteryFiles.asFile("/" + name + "/bdio");
final File expectedBdioFolder = BatteryFiles.asFile("/" + resourcePrefix + "/bdio");
final File[] expectedBdioFiles = expectedBdioFolder.listFiles();
Assertions.assertTrue(expectedBdioFiles != null && expectedBdioFiles.length > 0, "Expected bdio resource files could not be found: " + expectedBdioFolder.getCanonicalPath());
Assertions.assertEquals(expectedBdioFiles.length, bdio.length, "Detect did not create the expected number of bdio files.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
import org.apache.commons.lang3.SystemUtils;
import org.junit.jupiter.api.Assertions;

import com.synopsys.integration.detect.configuration.DetectProperty;

import freemarker.template.TemplateException;

public class ResourceCopyingExecutable extends BatteryExecutable {
public class ResourceCopyingExecutableCreator extends BatteryExecutableCreator {
private final List<String> toCopy;
private OperatingSystemInfo windowsInfo = null;
private OperatingSystemInfo linuxInfo = null;
Expand All @@ -32,21 +30,20 @@ private OperatingSystemInfo(final int extractionFolderIndex, final String extrac
}
}

protected ResourceCopyingExecutable(final DetectProperty detectProperty, final List<String> toCopy) {
super(detectProperty);
protected ResourceCopyingExecutableCreator(final List<String> toCopy) {
this.toCopy = toCopy;
}

public ResourceCopyingExecutable onAnySystem(final int extractionFolderIndex, final String extractionFolderPrefix) {
public ResourceCopyingExecutableCreator onAnySystem(final int extractionFolderIndex, final String extractionFolderPrefix) {
return onWindows(extractionFolderIndex, extractionFolderPrefix).onLinux(extractionFolderIndex, extractionFolderPrefix);
}

public ResourceCopyingExecutable onWindows(final int extractionFolderIndex, final String extractionFolderPrefix) {
public ResourceCopyingExecutableCreator onWindows(final int extractionFolderIndex, final String extractionFolderPrefix) {
this.windowsInfo = new OperatingSystemInfo(extractionFolderIndex, extractionFolderPrefix);
return this;
}

public ResourceCopyingExecutable onLinux(final int extractionFolderIndex, final String extractionFolderPrefix) {
public ResourceCopyingExecutableCreator onLinux(final int extractionFolderIndex, final String extractionFolderPrefix) {
this.linuxInfo = new OperatingSystemInfo(extractionFolderIndex, extractionFolderPrefix);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@

import org.junit.jupiter.api.Assertions;

import com.synopsys.integration.detect.configuration.DetectProperty;

public class ResourceTypingExecutable extends TypingExecutable {
public class ResourceTypingExecutableCreator extends TypingExecutableCreator {
private final List<String> toType;

protected ResourceTypingExecutable(final DetectProperty detectProperty, final List<String> toType) {
super(detectProperty);
protected ResourceTypingExecutableCreator(final List<String> toType) {
this.toType = toType;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@

import org.apache.commons.io.FileUtils;

import com.synopsys.integration.detect.configuration.DetectProperty;

public class StringTypingExecutable extends TypingExecutable {
public class StringTypingExecutableCreator extends TypingExecutableCreator {
private final List<String> toType;

protected StringTypingExecutable(final DetectProperty detectProperty, final List<String> toType) {
super(detectProperty);
protected StringTypingExecutableCreator(final List<String> toType) {
this.toType = toType;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,10 @@
import org.apache.commons.lang3.SystemUtils;
import org.assertj.core.util.Lists;

import com.synopsys.integration.detect.configuration.DetectProperty;

import freemarker.template.TemplateException;

//This executable types text from a set of files when executed.
public abstract class TypingExecutable extends BatteryExecutable {

protected TypingExecutable(final DetectProperty detectProperty) {
super(detectProperty);
}
public abstract class TypingExecutableCreator extends BatteryExecutableCreator {

@Override
public File createExecutable(final int id, final File mockDirectory, final AtomicInteger commandCount) throws IOException, TemplateException {
Expand Down

0 comments on commit 1da692b

Please sign in to comment.