Skip to content

Commit

Permalink
Continued to refactor tests to avoid interference between tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkoseifert committed Apr 11, 2015
1 parent f15c830 commit 5267bfa
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@
import java.util.zip.ZipFile;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.emftext.language.java.JavaClasspath;
import org.emftext.language.java.test.AbstractJavaParserTestCase;
import org.emftext.language.java.test.util.ThreadedSuite;
Expand All @@ -42,43 +40,6 @@
*/
public abstract class AbstractZipFileInputTestCase extends AbstractJavaParserTestCase {

private static class DelegatingTestCase extends TestCase {

private final ZipFileEntryTestCase zipFileEntryTestCase;

private DelegatingTestCase(ZipFileEntryTestCase zipFileEntryTestCase) {
super("Parse " + (zipFileEntryTestCase.isExcludeFromReprint() ? "" : "and reprint: ")
+ zipFileEntryTestCase.getZipEntry().getName());
this.zipFileEntryTestCase = zipFileEntryTestCase;
}

@Override
protected void runTest() throws Throwable {
zipFileEntryTestCase.runTest();
}
}

private static class CustomClasspathInitializer implements JavaClasspath.Initializer {

private final boolean requiresStdLib;

public CustomClasspathInitializer(boolean requiresStdLib) {
super();
this.requiresStdLib = requiresStdLib;
}

public boolean requiresStdLib() {
return requiresStdLib;
}

public boolean requiresLocalClasspath() {
return true;
}

public void initialize(Resource resource) {
}
}

protected final static String BULK_INPUT_DIR = "input/";

/**
Expand Down Expand Up @@ -170,15 +131,9 @@ protected static Collection<Test> getTestsForZipFileEntries(String zipFilePath,
final ZipFile zipFile = new ZipFile(zipFilePath);
Enumeration<? extends ZipEntry> entries = zipFile.entries();

IClasspathSetter classpathSetter = null;
if (!prefixUsedInZipFile) {
// Reuse global classpath
boolean requiresStdLib = false;
JavaClasspath.getInitializers().add(new CustomClasspathInitializer(requiresStdLib));
JavaClasspath globalCP = JavaClasspath.get();
String plainZipFileName = zipFile.getName().substring(AbstractZipFileInputTestCase.BULK_INPUT_DIR.length());
plainZipFileName = plainZipFileName.substring(0, plainZipFileName.length() - File.separator.length()
- "src.zip".length());
registerLibs("input/" + plainZipFileName, globalCP, "");
classpathSetter = new ZipFileClasspathSetter(zipFile);
} else {
// For the JDT test file register only the standard library
boolean requiresStdLib = true;
Expand All @@ -203,7 +158,7 @@ protected static Collection<Test> getTestsForZipFileEntries(String zipFilePath,
}
if (entryName.endsWith(".java")) {
final ZipFileEntryTestCase newTest = new ZipFileEntryTestCase(zipFile, entry, excludeFromReprint,
prefixUsedInZipFile);
prefixUsedInZipFile, classpathSetter);
tests.add(new DelegatingTestCase(newTest));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.emftext.language.java.test.bulk;

import org.eclipse.emf.ecore.resource.Resource;
import org.emftext.language.java.JavaClasspath;

public class CustomClasspathInitializer implements JavaClasspath.Initializer {

private final boolean requiresStdLib;

public CustomClasspathInitializer(boolean requiresStdLib) {
this.requiresStdLib = requiresStdLib;
}

@Override
public boolean requiresStdLib() {
return requiresStdLib;
}

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

@Override
public void initialize(Resource resource) {
// Do nothing
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.emftext.language.java.test.bulk;

import junit.framework.TestCase;

public class DelegatingTestCase extends TestCase {

private final ZipFileEntryTestCase zipFileEntryTestCase;

public DelegatingTestCase(ZipFileEntryTestCase zipFileEntryTestCase) {
super("Parse " + (zipFileEntryTestCase.isExcludeFromReprint() ? "" : "and reprint: ")
+ zipFileEntryTestCase.getZipEntry().getName());
this.zipFileEntryTestCase = zipFileEntryTestCase;
}

@Override
protected void runTest() throws Throwable {
zipFileEntryTestCase.runTest();
}

@Override
protected void tearDown() throws Exception {
zipFileEntryTestCase.tearDown();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.emftext.language.java.test.bulk;

import org.eclipse.emf.ecore.resource.ResourceSet;

public interface IClasspathSetter {

public void setUpClasspath(ResourceSet resourceSet) throws Exception;

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
import org.eclipse.core.runtime.CoreException;

/**
* Uses JaMoPP to parse and print the source files of
* the Netbeans IDE.
* Uses JaMoPP to parse and print the source files of the Netbeans IDE.
*/
public class NetbeansTest extends AbstractZipFileInputTestCase {

Expand All @@ -47,6 +46,5 @@ public class NetbeansTest extends AbstractZipFileInputTestCase {

public static Test suite() throws CoreException, IOException {
return constructSuite(TEST_FOLDER, START_ENTRY, THREAD_NO, EXCLUDED_TESTS);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.emftext.language.java.test.bulk;

import java.io.File;
import java.util.Collections;
import java.util.Set;
import java.util.zip.ZipFile;

import org.eclipse.emf.ecore.resource.ResourceSet;
import org.emftext.language.java.JavaClasspath;
import org.emftext.language.java.JavaClasspath.Initializer;

public class ZipFileClasspathSetter implements IClasspathSetter {

private final ZipFile zipFile;

public ZipFileClasspathSetter(ZipFile zipFile) {
this.zipFile = zipFile;
}

@Override
public void setUpClasspath(ResourceSet resourceSet) throws Exception {
// Create custom class path setup for resource set
boolean requiresStdLib = false;
Initializer classpathInitializer = new CustomClasspathInitializer(requiresStdLib);
Set<Initializer> classpathInitializers = Collections.singleton(classpathInitializer);
JavaClasspath localClasspath = JavaClasspath.get(resourceSet, classpathInitializers);
String plainZipFileName = zipFile.getName().substring(AbstractZipFileInputTestCase.BULK_INPUT_DIR.length());
plainZipFileName = plainZipFileName.substring(0, plainZipFileName.length() - File.separator.length()
- "src.zip".length());
AbstractZipFileInputTestCase.registerLibs("input/" + plainZipFileName, localClasspath, "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@
import static org.junit.Assert.fail;

import java.io.File;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.emftext.language.java.JavaClasspath;
import org.emftext.language.java.resource.java.IJavaOptions;
import org.emftext.language.java.test.AbstractJavaParserTestCase;
Expand All @@ -39,6 +37,7 @@ public class ZipFileEntryTestCase extends AbstractJavaParserTestCase {
private final ZipEntry zipEntry;
private final boolean excludeFromReprint;
private final boolean prefixUsedInZipFile;
private final IClasspathSetter classpathSetter;

/**
* Creates a new test for the given entry in a ZIP file.
Expand All @@ -48,13 +47,13 @@ public class ZipFileEntryTestCase extends AbstractJavaParserTestCase {
* @param excludeFromReprint
* @param prefixUsedInZipFile
*/
public ZipFileEntryTestCase(ZipFile zipFile, ZipEntry zipEntry,
boolean excludeFromReprint, boolean prefixUsedInZipFile) {
super();
public ZipFileEntryTestCase(ZipFile zipFile, ZipEntry zipEntry, boolean excludeFromReprint,
boolean prefixUsedInZipFile, IClasspathSetter classpathSetter) {
this.zipFile = zipFile;
this.excludeFromReprint = excludeFromReprint;
this.prefixUsedInZipFile = prefixUsedInZipFile;
this.zipEntry = zipEntry;
this.classpathSetter = classpathSetter;
}

public void runTest() {
Expand Down Expand Up @@ -88,20 +87,21 @@ protected Map<Object, Object> getLoadOptions() {
return map;
}

protected ResourceSet getResourceSet() {
ResourceSet rs = new ResourceSetImpl();
rs.getLoadOptions().putAll(getLoadOptions());
return rs;
protected void setUpClasspath(ResourceSet resourceSet) throws Exception {
// Sub class can override this method
if (classpathSetter != null) {
classpathSetter.setUpClasspath(resourceSet);
}
}

private void parseAndReprintEntry(ZipEntry entry) throws Exception {
String plainZipFileName = zipFile.getName().substring(AbstractZipFileInputTestCase.BULK_INPUT_DIR.length());
plainZipFileName = plainZipFileName.substring(0, plainZipFileName.length() - File.separator.length() - "src.zip".length());

parseAndReprint(zipFile, entry, "output/" + plainZipFileName, "input/" + plainZipFileName);
}

private void parseAllEntries() throws IOException {
private void parseAllEntries() throws Exception {
parseResource(zipFile, zipEntry);
}

Expand Down Expand Up @@ -146,4 +146,8 @@ public boolean isExcludeFromReprint() {
public ZipEntry getZipEntry() {
return zipEntry;
}

public void tearDown() {
JavaClasspath.reset();
}
}

0 comments on commit 5267bfa

Please sign in to comment.