Skip to content

Commit

Permalink
[examples] Enable the Maven compilation of the tests during the unit …
Browse files Browse the repository at this point in the history
…tests' execution.

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Aug 23, 2018
1 parent 5e66a30 commit d7d4a94
Showing 1 changed file with 76 additions and 24 deletions.
Expand Up @@ -22,10 +22,12 @@

package io.sarl.examples.tests;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.*;
import static org.junit.Assert.assertNotNull;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -38,16 +40,15 @@
import java.util.TreeSet;

import com.google.inject.Injector;
import org.apache.log4j.Level;
import org.arakhne.afc.vmutil.ClasspathUtil;
import org.arakhne.afc.vmutil.FileSystem;
import org.eclipse.xtext.diagnostics.Severity;
import org.eclipse.xtext.util.Strings;
import org.eclipse.xtext.xbase.lib.Pair;
import org.eclipse.xtext.xbase.validation.IssueCodes;
import org.junit.After;
import org.junit.Assume;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ComparisonFailure;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -77,6 +78,8 @@ public class ExamplesTest extends AbstractSarlTest {

private static final File DEFAULT_RELATIVE_PATH = FileSystem.convertStringToFile("file:../io.sarl.examples.plugin"); //$NON-NLS-1$

private static final String CONTENTS_FOLDER_NAME = "contents"; //$NON-NLS-1$

private static final String PROJECTS_FOLDER_NAME = "projects"; //$NON-NLS-1$

/** Replies the archives for the examples.
Expand All @@ -87,7 +90,7 @@ public class ExamplesTest extends AbstractSarlTest {
@Parameters(name = "Example {1}")
public static Collection<Object[]> getExampleArchives() throws Exception {
final Set<String> names = new TreeSet<>();
final Map<String, File> rawSources = new TreeMap<>();
final Map<String, Pair<File, File>> rawSources = new TreeMap<>();

File rootPath = null;
final String projectdir = System.getProperty(ROOT_TEST_FOLDER_PROPERTY);
Expand All @@ -98,12 +101,15 @@ public static Collection<Object[]> getExampleArchives() throws Exception {
rootPath = DEFAULT_RELATIVE_PATH;
}

final File projectFolder = new File(rootPath, PROJECTS_FOLDER_NAME);
final File sourceFolder = new File(rootPath, PROJECTS_FOLDER_NAME);
final File projectFolder = new File(rootPath, CONTENTS_FOLDER_NAME);
if (projectFolder.isDirectory()) {
for (File child : projectFolder.listFiles()) {
if (child.isDirectory()) {
if (child.isFile()) {
final String basename = child.getName();
rawSources.putIfAbsent(basename, child);
final String sbasename = FileSystem.shortBasename(child);
rawSources.putIfAbsent(basename, Pair.of(child,
new File(sourceFolder, sbasename)));
names.add(basename);
}
}
Expand All @@ -116,27 +122,32 @@ public static Collection<Object[]> getExampleArchives() throws Exception {
final List<Object[]> list = new ArrayList<>();

for (final String name : names) {
final File folder = rawSources.get(name);
list.add(new Object[] {name, folder});
final Pair<File, File> pair = rawSources.get(name);
final File zipFile = pair.getKey();
final File projectSourceFolder = pair.getValue();
list.add(new Object[] {name, zipFile, projectSourceFolder});
}

return list;
}

private final String name;

private final File exampleFolder;
private final File exampleZipFile;

private final File exampleSourceFile;

private SarlBatchCompiler compiler;

/** Constructor.
*
* @param name the name of the test.
* @param exampleFolder the folder to open.
* @param exampleZipFile the path to the zip file to open.
*/
public ExamplesTest(String name, File exampleFolder) {
public ExamplesTest(String name, File exampleZipFile, File exampleSourceFile) {
this.name = name;
this.exampleFolder = exampleFolder.getAbsoluteFile();
this.exampleZipFile = exampleZipFile.getAbsoluteFile();
this.exampleSourceFile = exampleSourceFile.getAbsoluteFile();
}

@Before
Expand All @@ -157,17 +168,32 @@ public String toString() {

@Test
public void path() {
assertNotNull(this.exampleFolder);
assertNotNull(this.exampleZipFile);
}

@Test
public void compilation() throws Exception {
Assume.assumeTrue(this.exampleFolder != null);
Assume.assumeTrue(this.exampleZipFile != null);
final File projectRoot = createProject();
final List<File> installedFiles = copyFiles(projectRoot);
final List<File> installedFiles = unpackFiles(projectRoot);
assertFalse("No installed file in " + projectRoot, installedFiles.isEmpty());
List<String> issues = compileFiles(projectRoot, installedFiles);
assertNoIssue(issues);
if (isMavenProject()) {
// Maven compilation
final String errors = compileMaven(projectRoot);
assertTrue(errors, Strings.isEmpty(errors));
} else {
// Standard SARL compilation
List<String> issues = compileFiles(projectRoot, installedFiles);
assertNoIssue(issues);
}
}

private boolean isMavenProject() {
if (this.exampleSourceFile != null) {
final File pomFile = new File(this.exampleSourceFile, "pom.xml");
return pomFile.exists();
}
return false;
}

private void assertNoIssue(List<String> issues) {
Expand All @@ -176,6 +202,31 @@ private void assertNoIssue(List<String> issues) {
}
}

private String compileMaven(File root) throws Exception {
final String[] command = new String[] {
"mvn", "-q", "clean", "package"
};
final Process p = Runtime.getRuntime().exec(command, null, root);
p.waitFor();
final StringBuilder output = new StringBuilder();
final BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = reader.readLine();
while (line != null) {
output.append(line + "\n");
line = reader.readLine();
}
final BufferedReader readerErr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
line = reader.readLine();
while (line != null) {
output.append(line + "\n");
line = reader.readLine();
}
if (p.exitValue() != 0) {
return output.toString();
}
return null;
}

private List<String> compileFiles(File root, List<File> installedFiles) throws Exception {
final List<String> issues = new ArrayList<>();
compiler.setBasePath(root.getAbsolutePath());
Expand Down Expand Up @@ -221,22 +272,23 @@ private List<File> getClasspath() throws Exception {
return classpath;
}

private List<File> copyFiles(File root) throws Exception {
private List<File> unpackFiles(File root) throws Exception {
FileSystem.unzipFile(this.exampleZipFile, root);

final List<File> installedFiles = new ArrayList<>();
final List<File> folders = new ArrayList<>();
folders.add(this.exampleFolder);
folders.add(root);
while (!folders.isEmpty()) {
final File folder = folders.remove(0);
for (final File file : folder.listFiles()) {
if (file.isDirectory()) {
folders.add(file);
} else if (file.isFile()) {
if (!isIgnorableFile(file)) {
final File relPathFile = FileSystem.makeRelative(file, this.exampleFolder);
final File targetFile = FileSystem.join(root, relPathFile);
targetFile.getParentFile().mkdirs();
FileSystem.copy(file, targetFile);
final File relPathFile = FileSystem.makeRelative(file, root);
installedFiles.add(relPathFile);
} else {
file.delete();
}
}
}
Expand Down

0 comments on commit d7d4a94

Please sign in to comment.