Skip to content

Commit

Permalink
extracted common code for incremental standalone builder tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenzoBettini committed May 16, 2024
1 parent 103079b commit 1eb0c6b
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 378 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*******************************************************************************
* Copyright (c) 2024 Lorenzo Bettini and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.xtext.builder.standalone.incremental;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.eclipse.xtext.builder.standalone.ILanguageConfiguration;
import org.eclipse.xtext.builder.standalone.LanguageAccess;
import org.eclipse.xtext.builder.standalone.LanguageAccessFactory;
import org.eclipse.xtext.builder.standalone.StandaloneBuilder;
import org.eclipse.xtext.builder.standalone.StandaloneBuilderInjectorProvider;
import org.eclipse.xtext.builder.standalone.TestableStandaloneBuilder;
import org.eclipse.xtext.testing.InjectWith;
import org.eclipse.xtext.testing.XtextRunner;
import org.eclipse.xtext.util.Files;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;

import com.google.inject.Inject;

/**
* @author Lorenzo Bettini
*
*/
@RunWith(XtextRunner.class)
@InjectWith(StandaloneBuilderInjectorProvider.class)
public abstract class AbstractIncrementalStandaloneBuilderTest {

protected static final File PROJECT_DIR = new File("projectUnderTest").getAbsoluteFile();

@Inject
protected TestableStandaloneBuilder testBuilder;

protected static void copy(Path from, Path to) {
try {
java.nio.file.Files.walk(from).forEach(src -> {
try {
java.nio.file.Files.copy(src, to.resolve(from.relativize(src)));
} catch (IOException e) {
throw new RuntimeException(e);
}
});
} catch (IOException e) {
throw new RuntimeException(e);
}
}

protected File getFile(String projectRelativePath) {
return new File(PROJECT_DIR, projectRelativePath);
}

protected StandaloneBuilder initBuilder(ILanguageConfiguration... configs) {
return initBuilder(configs, "src");
}

private StandaloneBuilder initBuilder(ILanguageConfiguration[] configs, String... srcDirs) {
List<String> paths = new ArrayList<String>();
for (String srcDir : srcDirs) {
paths.add(new File(PROJECT_DIR, srcDir).getAbsolutePath());
}
testBuilder.setSourceDirs(paths);
testBuilder.resetCallStatistic();
Map<String, LanguageAccess> languages = new LanguageAccessFactory()
.createLanguageAccess(List.of(configs), getClass().getClassLoader());
testBuilder.setBaseDir(PROJECT_DIR.getAbsolutePath());
testBuilder.setLanguages(languages);
testBuilder.setClassPathEntries(Collections.emptyList());
return testBuilder;
}

@After
public void cleanup() throws IOException {
deleteFolder(PROJECT_DIR);
}

@BeforeClass
public static void removeOldProjectDir() throws FileNotFoundException {
deleteFolder(PROJECT_DIR);
}

protected static void deleteFolder(File folder) throws FileNotFoundException {
if (folder.exists()) {
Files.sweepFolder(folder);
folder.delete();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*******************************************************************************
* Copyright (c) 2024 Lorenzo Bettini and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.xtext.builder.standalone.incremental;

import java.util.Set;

import org.eclipse.xtext.builder.standalone.ILanguageConfiguration;
import org.eclipse.xtext.generator.IFileSystemAccess;
import org.eclipse.xtext.generator.OutputConfiguration;

import com.google.common.collect.ImmutableSet;

public class ContentAssistFragmentTestLangConfiguration implements ILanguageConfiguration {

public ContentAssistFragmentTestLangConfiguration() {
}

@Override
public String getSetup() {
return "org.eclipse.xtext.xbase.testlanguages.ContentAssistFragmentTestLangStandaloneSetup";
}

@Override
public Set<OutputConfiguration> getOutputConfigurations() {
OutputConfiguration config = new OutputConfiguration(IFileSystemAccess.DEFAULT_OUTPUT);
config.setOutputDirectory("src-gen");
return ImmutableSet.of(config);
}

@Override
public boolean isJavaSupport() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,59 +8,34 @@
*******************************************************************************/
package org.eclipse.xtext.builder.standalone.incremental;

import static com.google.common.io.Files.readLines;
import static java.nio.charset.StandardCharsets.ISO_8859_1;
import static java.nio.file.Files.readAllBytes;
import static java.nio.file.Files.write;
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.xtext.builder.standalone.ILanguageConfiguration;
import org.eclipse.xtext.builder.standalone.LanguageAccess;
import org.eclipse.xtext.builder.standalone.LanguageAccessFactory;
import org.eclipse.xtext.builder.standalone.StandaloneBuilder;
import org.eclipse.xtext.builder.standalone.StandaloneBuilderInjectorProvider;
import org.eclipse.xtext.builder.standalone.TestableStandaloneBuilder;
import org.eclipse.xtext.generator.IFileSystemAccess;
import org.eclipse.xtext.generator.OutputConfiguration;
import org.eclipse.xtext.testing.InjectWith;
import org.eclipse.xtext.testing.XtextRunner;
import org.eclipse.xtext.util.Files;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Inject;

@RunWith(XtextRunner.class)
@InjectWith(StandaloneBuilderInjectorProvider.class)
public class IncrementalStandaloneBuilderTest {
public class IncrementalStandaloneBuilderTest extends AbstractIncrementalStandaloneBuilderTest {

private static final File ORIGINAL_PROJECT_DIR = new File("test-data/standalone.incremental");
private static final File PROJECT_DIR = new File("projectUnderTest").getAbsoluteFile();

@Inject
private TestableStandaloneBuilder testBuilder;

@BeforeClass
public static void removeOldProjectDir() throws FileNotFoundException {
deleteFolder(PROJECT_DIR);
}

@Before
public void setUp() {
copy(ORIGINAL_PROJECT_DIR.toPath(), PROJECT_DIR.toPath());
Expand All @@ -71,31 +46,11 @@ public void setUp() {
testBuilder.setTempDir(new File(PROJECT_DIR, "tmp"));
}

private static void copy(Path from, Path to) {
try {
java.nio.file.Files.walk(from).forEach(src -> {
try {
java.nio.file.Files.copy(src, to.resolve(from.relativize(src)));
} catch (IOException e) {
throw new RuntimeException(e);
}
});
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@After
public void cleanup() throws IOException {
deleteFolder(PROJECT_DIR);
}


@Test
public void testSingleLanguageCleanBuild() {
initBuilder(new TestLanguageConfiguration());
assertTrue(testBuilder.launch());

assertEquals(1, testBuilder.getGenerateCalled());
assertEquals(2, testBuilder.getGenerateResources());

Expand All @@ -106,12 +61,12 @@ public void testSingleLanguageCleanBuild() {
out = getFile("src-gen/SecondObjectB.txt");
assertTrue(out.exists());
}

@Test
public void testRelaunchWithoutChanges() {
initBuilder(new TestLanguageConfiguration());
assertTrue(testBuilder.launch());

assertTrue(testBuilder.launch());
assertEquals(0, testBuilder.getGenerateResources());
}
Expand All @@ -120,7 +75,7 @@ public void testRelaunchWithoutChanges() {
public void testRelaunchAfterDeleteOutputFile() {
initBuilder(new TestLanguageConfiguration());
assertTrue(testBuilder.launch());

File out = getFile("src-gen/SecondObjectA.txt");
assertTrue(out.exists());
out.delete();
Expand All @@ -129,100 +84,71 @@ public void testRelaunchAfterDeleteOutputFile() {
assertEquals(1, testBuilder.getGenerateResources());
assertTrue(out.exists());
}

@Test
public void testRelaunchAfterTinkeringWithOutputFile() throws IOException {
initBuilder(new TestLanguageConfiguration());
assertTrue(testBuilder.launch());

File out = getFile("src-gen/SecondObjectB.txt");
assertTrue(out.exists());
byte[] bytes = java.nio.file.Files.readAllBytes(out.toPath());
java.nio.file.Files.write(out.toPath(), Arrays.asList(""), ISO_8859_1, TRUNCATE_EXISTING);
byte[] bytes = readAllBytes(out.toPath());
write(out.toPath(), Arrays.asList(""), ISO_8859_1, TRUNCATE_EXISTING);

assertTrue(testBuilder.launch());
assertEquals(1, testBuilder.getGenerateResources());
assertTrue(out.exists());
Assert.assertArrayEquals(bytes, java.nio.file.Files.readAllBytes(out.toPath()));
Assert.assertArrayEquals(bytes, readAllBytes(out.toPath()));
}

@Test
public void testRelaunchAfterDeleteModelFile() throws IOException {
initBuilder(new TestLanguageConfiguration());
assertTrue(testBuilder.launch());

File modelFile = getFile("src/Second.buildertestlanguage");
assertTrue(modelFile.delete());

assertTrue(testBuilder.launch());
assertEquals(0, testBuilder.getGenerateResources());

File out = getFile("src-gen/SecondObjectA.txt");
assertFalse(out.exists());
out = getFile("src-gen/SecondObjectB.txt");
assertFalse(out.exists());
}

@Test
public void testRelaunchAfterMoveModelElement() throws IOException {
initBuilder(new TestLanguageConfiguration());
assertTrue(testBuilder.launch());

File firstModel = getFile("src/First.buildertestlanguage");
File secondModel = getFile("src/Second.buildertestlanguage");

File firstOut = getFile("src-gen/FirstObject.txt");
List<String> outputContent = com.google.common.io.Files.readLines(firstOut, ISO_8859_1);

java.nio.file.Files.write(firstModel.toPath(), Arrays.asList("object NewObject"), ISO_8859_1, TRUNCATE_EXISTING);
List<String> outputContent = readLines(firstOut, ISO_8859_1);

List<String> secondContent = com.google.common.io.Files.readLines(secondModel, ISO_8859_1);
secondContent.replaceAll(s->{
java.nio.file.Files.write(firstModel.toPath(), Arrays.asList("object NewObject"), ISO_8859_1,
TRUNCATE_EXISTING);

List<String> secondContent = readLines(secondModel, ISO_8859_1);
secondContent.replaceAll(s -> {
if (s.contains("object SecondObjectB references Namespace.SecondObjectA")) {
return s.replace("SecondObjectB", "FirstObject");
}
return s;
});
java.nio.file.Files.write(secondModel.toPath(), secondContent, ISO_8859_1, TRUNCATE_EXISTING);
write(secondModel.toPath(), secondContent, ISO_8859_1, TRUNCATE_EXISTING);

assertTrue(testBuilder.launch());
assertEquals(2, testBuilder.getGenerateResources());

assertTrue(firstOut.exists());
List<String> newOutputContent = com.google.common.io.Files.readLines(firstOut, ISO_8859_1);
List<String> newOutputContent = readLines(firstOut, ISO_8859_1);
assertFalse(outputContent.equals(newOutputContent));
}

private File getFile(String projectRelativePath) {
return new File(PROJECT_DIR, projectRelativePath);
}

private static void deleteFolder(File folder) throws FileNotFoundException {
if (folder.exists()) {
Files.sweepFolder(folder);
folder.delete();
}
}

private StandaloneBuilder initBuilder(ILanguageConfiguration... configs) {
return initBuilder(configs, "src");
}

private StandaloneBuilder initBuilder(ILanguageConfiguration[] configs, String... srcDirs) {
List<String> patthes = new ArrayList<String>();
for (String srcDir : srcDirs) {
patthes.add(new File(PROJECT_DIR, srcDir).getAbsolutePath());
}
testBuilder.setSourceDirs(patthes);
testBuilder.resetCallStatistic();
Map<String, LanguageAccess> languages = new LanguageAccessFactory()
.createLanguageAccess(ImmutableList.copyOf(configs), getClass().getClassLoader());
testBuilder.setBaseDir(PROJECT_DIR.getAbsolutePath());
testBuilder.setLanguages(languages);
testBuilder.setClassPathEntries(ImmutableList.<String>of());
return testBuilder;
}

public static class TestLanguageConfiguration implements ILanguageConfiguration {

/* @NonNull */
Expand Down

0 comments on commit 1eb0c6b

Please sign in to comment.