Skip to content

Commit

Permalink
[examples] Test if the wizard specification in plugin.xml is correct.
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed May 18, 2020
1 parent b9baea8 commit 5f39b74
Show file tree
Hide file tree
Showing 18 changed files with 143 additions and 34 deletions.
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<launchConfigurations>
<launchConfigurations id="io.sarl.demos.basic.behaviorinheritance">
<agent class="io.sarl.demos.basic.inheritance.HelloChildAgent" name="Basic behavior inheritance" />
</launchConfigurations>
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<launchConfigurations>
<launchConfigurations id="io.sarl.examples.behaviors">
<agent class="io.sarl.demos.basic.behaviors.BehaviorPlayer" name="Basic behavior demo" />
</launchConfigurations>
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<launchConfigurations>
<launchConfigurations id="io.sarl.demos.basic.countdown">
<agent class="io.sarl.demos.basic.countdown.CountDownAgent" name="Count down agent" />
</launchConfigurations>
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<launchConfigurations>
<launchConfigurations id="io.sarl.examples.helloworld">
<agent class="io.sarl.demos.basic.helloworld.HelloAgent" name="Agent 'Hello world'" />
</launchConfigurations>
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<launchConfigurations>
<launchConfigurations id="io.sarl.demos.basic.holarchy">
<agent class="io.sarl.demos.basic.holarchy.HolarchyManager" name="Basic holarchy demo" />
</launchConfigurations>
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<launchConfigurations>
<launchConfigurations id="io.sarl.demos.boids.awt">
<application class="io.sarl.demos.boids.BoidsSimulationLauncher" name="Reynolds Boids - AWT" />
</launchConfigurations>
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<launchConfigurations>
<launchConfigurations id="io.sarl.demos.counter">
<agent class="io.sarl.demos.counter.CounterBootAgent" name="Counting agent" />
</launchConfigurations>
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<launchConfigurations>
<launchConfigurations id="io.sarl.demos.factorial">
<agent class="io.sarl.demos.factorial.basic.FactorialAgent" name="Basic factorial agent" />
<agent class="io.sarl.demos.factorial.distributed.DistributedFactorialBootAgent" name="Distributed factorial agents" />
</launchConfigurations>
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<launchConfigurations>
<launchConfigurations id="io.sarl.demos.fireworks">
<application class="io.sarl.demos.fireworks.Fireworks" name="Fireworks" />
</launchConfigurations>
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<launchConfigurations>
<launchConfigurations id="io.sarl.demos.gameoflife">
<agent class="io.sarl.demos.gameoflife.Boot" name="Game of Life" />
</launchConfigurations>
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<launchConfigurations>
<launchConfigurations id="io.sarl.demos.sierpinskifractals">
<application class="io.sarl.demos.sierpinskiFractals.SierpinskiFractals" name="Sierpinski fractals" />
</launchConfigurations>
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<launchConfigurations>
<launchConfigurations id="io.sarl.templates.javafx">
<application class="io.sarl.template.javafx.MyAppLauncher" name="JavaFX template application" />
</launchConfigurations>
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<launchConfigurations>
<launchConfigurations id="io.sarl.tutorials.holonicauction">
<agent class="io.sarl.examples.holonicauction.Auctioneer" name="Holonic auction" />
</launchConfigurations>
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<launchConfigurations>
<launchConfigurations id="io.sarl.tutorials.pingpong">
<agent class="io.sarl.examples.pingpong.BootAgent" name="Ping-Pong in default space" />
</launchConfigurations>
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<launchConfigurations>
<launchConfigurations id="io.sarl.tutorials.pingpongspace">
<agent class="io.sarl.examples.pingpongspace.BootAgent" name="Ping-Pong in specific space" />
</launchConfigurations>
Expand Up @@ -76,9 +76,28 @@ public class SarlExampleInstallerWizard extends ExampleInstallerWizard {

private List<ConfigurationToLaunch> configurationsToLaunch;

@Inject
private ILaunchConfigurationConfigurator launchConfigConfigurator;

/** Change the launch configuration configurator.
*
* @param launchConfigConfigurator the new configurator.
* @since 0.11
*/
@Inject
public void setLaunchConfigurationConfigurator(ILaunchConfigurationConfigurator launchConfigConfigurator) {
this.launchConfigConfigurator = launchConfigConfigurator;
}

/** Replies the launch configuration configurator.
*
* @return configurator.
* @since 0.11
*/
public ILaunchConfigurationConfigurator getLaunchConfigurationConfigurator() {
assert this.launchConfigConfigurator != null : "Extension factory is missed for the example in the plugin.xml file";
return this.launchConfigConfigurator;
}

@Override
public void addPages() {
// Override the default "addPages" in order to create a configuration page.
Expand Down Expand Up @@ -170,7 +189,7 @@ protected void postProjectInstallation(ProjectDescriptor projectDescriptor, IPro
if (this.configurationPage.isLaunchConfigurationInstallable()) {
try {
final List<ConfigurationToLaunch> configs = getConfigurationsToLaunch(project);
configs.forEach(it -> {
for (final ConfigurationToLaunch it : configs) {
try {
if (it.isAgentLaunch()) {
createAgentLaunchConfiguration(project, it.getType(), it.getName());
Expand All @@ -182,11 +201,15 @@ protected void postProjectInstallation(ProjectDescriptor projectDescriptor, IPro
io.sarl.eclipse.util.Messages.AbstractSarlScriptInteractiveSelector_1,
exception.getStatus().getMessage(), exception);
}
});
}
} catch (CoreException exception) {
SARLExamplePlugin.getDefault().openError(getShell(),
io.sarl.eclipse.util.Messages.AbstractSarlScriptInteractiveSelector_1,
exception.getStatus().getMessage(), exception);
} catch (Exception exception) {
SARLExamplePlugin.getDefault().openError(getShell(),
io.sarl.eclipse.util.Messages.AbstractSarlScriptInteractiveSelector_1,
exception.getMessage(), exception);
}
}

Expand Down Expand Up @@ -233,8 +256,12 @@ public List<ConfigurationToLaunch> getConfigurationsToLaunch(IProject project) t
*/
protected void createApplicationLaunchConfiguration(IProject project, String mainClassfullyQualifedName,
String configurationName) throws CoreException {
this.launchConfigConfigurator.newApplicationLaunchConfiguration(project.getName(),
configurationName, mainClassfullyQualifedName, SarlStandardClasspathProvider.class);
final ILaunchConfigurationConfigurator configurator = getLaunchConfigurationConfigurator();
if (configurator != null) {
final String projectName = project.getName();
configurator.newApplicationLaunchConfiguration(projectName,
configurationName, mainClassfullyQualifedName, SarlStandardClasspathProvider.class);
}
}

/** Create the application launch configuration.
Expand All @@ -247,8 +274,12 @@ protected void createApplicationLaunchConfiguration(IProject project, String mai
*/
protected void createAgentLaunchConfiguration(IProject project, String agentFullyQualifiedName,
String configurationName) throws CoreException {
this.launchConfigConfigurator.newAgentLaunchConfiguration(project.getName(),
configurationName, agentFullyQualifiedName);
final ILaunchConfigurationConfigurator configurator = getLaunchConfigurationConfigurator();
if (configurator != null) {
final String projectName = project.getName();
configurator.newAgentLaunchConfiguration(projectName,
configurationName, agentFullyQualifiedName);
}
}

@Override
Expand Down
Expand Up @@ -22,32 +22,35 @@
package io.sarl.examples.tests;

import static io.sarl.examples.tests.ExamplesTestUtils.DEFAULT_RELATIVE_PATH;
import static io.sarl.examples.tests.ExamplesTestUtils.assertFile;
import static io.sarl.examples.tests.ExamplesTestUtils.assertNoIssue;
import static io.sarl.examples.tests.ExamplesTestUtils.compileFiles;
import static io.sarl.examples.tests.ExamplesTestUtils.compileMaven;
import static io.sarl.examples.tests.ExamplesTestUtils.copySourceFiles;
import static io.sarl.examples.tests.ExamplesTestUtils.createProject;
import static io.sarl.examples.tests.ExamplesTestUtils.dynamicTests;
import static io.sarl.examples.tests.ExamplesTestUtils.getSarlBatchCompiler;
import static io.sarl.examples.tests.ExamplesTestUtils.getSourceGenPath;
import static io.sarl.examples.tests.ExamplesTestUtils.getSourcePath;
import static io.sarl.examples.tests.ExamplesTestUtils.isMavenProject;
import static io.sarl.examples.tests.ExamplesTestUtils.readExtensionPointFromXml;
import static io.sarl.examples.tests.ExamplesTestUtils.readFileToOpenFromXml;
import static io.sarl.examples.tests.ExamplesTestUtils.readWizardClassesFromXml;
import static io.sarl.examples.tests.ExamplesTestUtils.readXmlNode;
import static io.sarl.examples.tests.ExamplesTestUtils.*;
import static io.sarl.examples.tests.ExamplesTestUtils.assertFile;
import static io.sarl.examples.tests.ExamplesTestUtils.unpackFiles;
import static io.sarl.examples.wizard.SarlExampleLaunchConfiguration.LAUNCH_PROPERTY_FILE;
import static io.sarl.examples.wizard.SarlExampleLaunchConfiguration.readLaunchConfigurationFromXml;
import static io.sarl.examples.wizard.SarlExampleLaunchConfiguration.readXmlAttribute;
import static io.sarl.examples.wizard.SarlExampleLaunchConfiguration.readXmlContent;
import static io.sarl.tests.api.tools.TestUtils.isEclipseRuntimeEnvironment;
import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import static io.sarl.tests.api.tools.TestUtils.*;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import org.arakhne.afc.vmutil.FileSystem;
Expand All @@ -57,13 +60,12 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import org.opentest4j.AssertionFailedError;
import org.opentest4j.TestAbortedException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import io.sarl.lang.compiler.batch.SarlBatchCompiler;
import io.sarl.lang.util.OutParameter;
import io.sarl.tests.api.tools.TestUtils;
import io.sarl.examples.SARLExampleExecutableExtensionFactory;

/** Class for testing the examples.
*
Expand Down Expand Up @@ -182,7 +184,7 @@ public Stream<DynamicTest> fileToOpenInEditor() throws Exception {
final File pluginFile = new File(DEFAULT_RELATIVE_PATH, "plugin.xml");
final Document document = readXmlContent(pluginFile);
Node node = readXmlNode(document, "plugin");
node = readExtensionPointFromXml(node, example.archive);
node = readFileToOpenFromXml(node, example.archive);
assumeTrue(node != null);

String locationStr = readXmlAttribute(node, "location");
Expand All @@ -205,4 +207,45 @@ public Stream<DynamicTest> fileToOpenInEditor() throws Exception {
});
}

/** Replies the dynamics tests for example wizard injection.
*
* @return the dynamic tests.
* @throws Exception in case of error for recovering the plugin's description.
* @since 0.11
*/
@TestFactory
@DisplayName("Definition of example wizards")
public Stream<DynamicTest> exampleWizards() throws Exception {
final File pluginFile = new File(DEFAULT_RELATIVE_PATH, "plugin.xml");
final Document document = readXmlContent(pluginFile);
assertNotNull(document, "Cannot read XML from the plugin.xml file");
final String prefix = SARLExampleExecutableExtensionFactory.class.getName() + ":";
return dynamicTests(false, example -> {
final File projectRoot = createProject();
final List<File> installedFiles = installFiles(example, projectRoot, false);
final File launchConfiguration = new File(projectRoot, LAUNCH_PROPERTY_FILE);
assumeTrue(launchConfiguration.exists());

final Document launchDocument = readXmlContent(launchConfiguration);
assertNotNull(launchDocument, "Expecting the example identifier into the launch.xml file");
final Node configurationNode = readXmlNode(launchDocument, "launchConfigurations");
assertNotNull(configurationNode, "Expecting the example identifier into the launch.xml file");
final String exampleId = readXmlAttribute(configurationNode, "id");
assertFalse(Strings.isEmpty(exampleId), "Expecting the example identifier into the launch.xml file");

final List<String> classNames = readWizardClassesFromXml(document, exampleId);
boolean foundOne = false;
for (final String className : classNames) {
assertNotNull(className, "Unexpected null value for a wizard class name of " + exampleId);
if (!className.startsWith(prefix)) {
throw new AssertionFailedError("Wizard class of " + exampleId + " must be loaded with the extension factory",
prefix + "package.classname", className);
}
foundOne = true;
}

assertTrue(foundOne, "Expecting definition of a wizard for the example " + exampleId);
});
}

}
Expand Up @@ -37,6 +37,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
Expand Down Expand Up @@ -206,13 +207,14 @@ public static Node readXmlNode(Node root, String name) {
return null;
}

/** Read the XML node that describes an extension point..
/** Read the XML node that describes a file to open
*
* @param root the root.
* @param exampleZipFile the filename of the example's archive.
* @return the extension point node.
* @since 0.11
*/
public static Node readExtensionPointFromXml(Node root, File exampleZipFile) {
public static Node readFileToOpenFromXml(Node root, File exampleZipFile) {
NodeList nodes = root.getChildNodes();
final int len = nodes.getLength();
for (int i = 0; i < len; ++i) {
Expand Down Expand Up @@ -523,4 +525,37 @@ public static File createProject() throws Exception {
return rootPath;
}

/** Read the XML nodes that describes the wizard classes.
*
* @param root the root.
* @param exampleId the identifier of the example.
* @return the class names.
* @since 0.11
*/
public static List<String> readWizardClassesFromXml(Node root, String exampleId) {
final List<String> classNames = new ArrayList<>();
final Node pluginNode = readXmlNode(root, "plugin");
if (pluginNode != null) {
NodeList nodes = pluginNode.getChildNodes();
final int len = nodes.getLength();
for (int i = 0; i < len; ++i) {
Node node = nodes.item(i);
if (node != null) {
if ("extension".equals(node.getNodeName())
&& "org.eclipse.ui.newWizards".equals(readXmlAttribute(node, "point"))) {
final Node wizardNode = readXmlNode(node, "wizard");
if (wizardNode != null) {
final String id = readXmlAttribute(wizardNode, "id");
if (Objects.equals(exampleId, id)) {
final String className = readXmlAttribute(wizardNode, "class");
classNames.add(className);
}
}
}
}
}
}
return classNames;
}

}

0 comments on commit 5f39b74

Please sign in to comment.