Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[sarldoc] Add basic unit tests in order to check if the sarldoc execu…
…table could be launched. Signed-off-by: Stéphane Galland <galland@arakhne.org>
- Loading branch information
1 parent
bcf9b90
commit 835acc4
Showing
3 changed files
with
313 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
294 changes: 294 additions & 0 deletions
294
products/sarldoc/src/test/java/io/sarl/sarldoc/tests/MainTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,294 @@ | ||
/* | ||
* $Id$ | ||
* | ||
* SARL is an general-purpose agent programming language. | ||
* More details on http://www.sarl.io | ||
* | ||
* Copyright (C) 2014-2019 the original authors or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.sarl.sarldoc.tests; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
import java.net.URL; | ||
import java.nio.charset.Charset; | ||
import java.nio.file.Files; | ||
import java.util.Iterator; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import org.arakhne.afc.vmutil.ClasspathUtil; | ||
import org.arakhne.afc.vmutil.FileSystem; | ||
import org.arakhne.afc.vmutil.Resources; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import io.sarl.lang.SARLVersion; | ||
import io.sarl.lang.sarl.SarlPackage; | ||
import io.sarl.lang.sarlc.configs.subconfigs.JavaCompiler; | ||
import io.sarl.maven.bootiqueapp.utils.SystemPath; | ||
import io.sarl.sarldoc.Main; | ||
|
||
/** Tests for {@code Main}. | ||
* | ||
* @author $Author: sgalland$ | ||
* @version $FullVersion$ | ||
* @mavengroupid $GroupId$ | ||
* @mavenartifactid $ArtifactId$ | ||
* @since 0.10 | ||
*/ | ||
@SuppressWarnings("all") | ||
public class MainTest { | ||
|
||
private static final boolean CAPTURE_OUTPUTS = true; | ||
|
||
private static final boolean DELETE_GENERATED_FOLDER = true; | ||
|
||
@Nullable | ||
private File rootFolder; | ||
|
||
@Nullable | ||
private File srcFolder; | ||
|
||
@Nullable | ||
private File genFolder; | ||
|
||
@Nullable | ||
private File tempFolder; | ||
|
||
@Nullable | ||
private File binFolder; | ||
|
||
@Nullable | ||
private File docFolder; | ||
|
||
@Nullable | ||
private File srcFile; | ||
|
||
@Nullable | ||
private PrintStream originalStdout; | ||
|
||
@Nullable | ||
private PrintStream originalStderr; | ||
|
||
@Nullable | ||
private ByteArrayOutputStream captureStdout; | ||
|
||
@Nullable | ||
private ByteArrayOutputStream captureStderr; | ||
|
||
private void restoreOutputs() throws IOException { | ||
System.setOut(this.originalStdout); | ||
System.setErr(this.originalStderr); | ||
if (this.captureStdout != null) { | ||
this.captureStdout.close(); | ||
this.captureStdout = null; | ||
} | ||
if (this.captureStderr != null) { | ||
this.captureStderr.close(); | ||
this.captureStderr = null; | ||
} | ||
} | ||
|
||
@Before | ||
public void setUp() { | ||
this.rootFolder = null; | ||
if (CAPTURE_OUTPUTS) { | ||
this.originalStdout = System.out; | ||
this.captureStdout = new ByteArrayOutputStream(); | ||
System.setOut(new PrintStream(this.captureStdout)); | ||
this.originalStderr = System.err; | ||
this.captureStderr = new ByteArrayOutputStream(); | ||
System.setErr(new PrintStream(this.captureStderr)); | ||
} | ||
} | ||
|
||
@After | ||
public void tearDown() throws IOException { | ||
if (this.rootFolder != null && DELETE_GENERATED_FOLDER) { | ||
FileSystem.delete(this.rootFolder); | ||
} | ||
if (CAPTURE_OUTPUTS) { | ||
restoreOutputs(); | ||
} | ||
} | ||
|
||
private void prepareProject(String name) throws IOException { | ||
this.rootFolder = FileSystem.createTempDirectory("sarlctests_" + name, ""); | ||
this.srcFolder = FileSystem.join(this.rootFolder, "src", "main", "sarl"); | ||
this.srcFolder.mkdirs(); | ||
this.genFolder = FileSystem.join(this.rootFolder, "src", "main", "generated-sources", "sarl"); | ||
this.genFolder.mkdirs(); | ||
this.binFolder = FileSystem.join(this.rootFolder, "target", "classes"); | ||
this.binFolder.mkdirs(); | ||
this.docFolder = FileSystem.join(this.rootFolder, "target", "site", "apidocs"); | ||
this.docFolder.mkdirs(); | ||
this.tempFolder = FileSystem.join(this.rootFolder, "target", "sarl-build"); | ||
this.tempFolder.mkdirs(); | ||
|
||
final URL resource = Resources.getResource(MainTest.class, "resources/" + name + ".sarl"); | ||
this.srcFile = FileSystem.join(this.srcFolder, name + ".sarl"); | ||
FileSystem.copy(resource, this.srcFile); | ||
} | ||
|
||
private static final String EXPECTED_TEST1 = "package io.sarl.lang.sarlc.tests.resources.test1;\n" + | ||
"\n" + | ||
"import io.sarl.lang.annotation.DefaultValue;\n" + | ||
"import io.sarl.lang.annotation.DefaultValueSource;\n" + | ||
"import io.sarl.lang.annotation.DefaultValueUse;\n" + | ||
"import io.sarl.lang.annotation.SarlElementType;\n" + | ||
"import io.sarl.lang.annotation.SarlSourceCode;\n" + | ||
"import io.sarl.lang.annotation.SarlSpecification;\n" + | ||
"import io.sarl.lang.annotation.SyntheticMember;\n" + | ||
"import org.eclipse.xtend.lib.annotations.Accessors;\n" + | ||
"import org.eclipse.xtext.xbase.lib.Pure;\n" + | ||
"\n" + | ||
"@SarlSpecification(\"" + SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING + "\")\n" + | ||
"@SarlElementType(" + SarlPackage.SARL_CLASS + ")\n" + | ||
"@SuppressWarnings(\"all\")\n" + | ||
"public class Foo {\n" + | ||
" @Accessors\n" + | ||
" private int fooField;\n" + | ||
" \n" + | ||
" @DefaultValueSource\n" + | ||
" public Foo(@DefaultValue(\"io.sarl.lang.sarlc.tests.resources.test1.Foo#NEW_0\") final int value) {\n" + | ||
" this.fooField = value;\n" + | ||
" }\n" + | ||
" \n" + | ||
" /**\n" + | ||
" * Default value for the parameter value\n" + | ||
" */\n" + | ||
" @SyntheticMember\n" + | ||
" @SarlSourceCode(\"0\")\n" + | ||
" private static final int $DEFAULT_VALUE$NEW_0 = 0;\n" + | ||
" \n" + | ||
" @DefaultValueUse(\"int\")\n" + | ||
" @SyntheticMember\n" + | ||
" public Foo() {\n" + | ||
" this($DEFAULT_VALUE$NEW_0);\n" + | ||
" }\n" + | ||
" \n" + | ||
" @Override\n" + | ||
" @Pure\n" + | ||
" @SyntheticMember\n" + | ||
" public boolean equals(final Object obj) {\n" + | ||
" if (this == obj)\n" + | ||
" return true;\n" + | ||
" if (obj == null)\n" + | ||
" return false;\n" + | ||
" if (getClass() != obj.getClass())\n" + | ||
" return false;\n" + | ||
" Foo other = (Foo) obj;\n" + | ||
" if (other.fooField != this.fooField)\n" + | ||
" return false;\n" + | ||
" return super.equals(obj);\n" + | ||
" }\n" + | ||
" \n" + | ||
" @Override\n" + | ||
" @Pure\n" + | ||
" @SyntheticMember\n" + | ||
" public int hashCode() {\n" + | ||
" int result = super.hashCode();\n" + | ||
" final int prime = 31;\n" + | ||
" result = prime * result + this.fooField;\n" + | ||
" return result;\n" + | ||
" }\n" + | ||
" \n" + | ||
" @Pure\n" + | ||
" public int getFooField() {\n" + | ||
" return this.fooField;\n" + | ||
" }\n" + | ||
" \n" + | ||
" public void setFooField(final int fooField) {\n" + | ||
" this.fooField = fooField;\n" + | ||
" }\n" + | ||
"}\n"; | ||
|
||
@Test | ||
public void generateTest1() throws IOException { | ||
prepareProject("test1"); | ||
|
||
SystemPath classPath = new SystemPath(); | ||
Iterator<URL> iterator = ClasspathUtil.getClasspath(); | ||
while (iterator.hasNext()) { | ||
final URL classPathElement = iterator.next(); | ||
final File localElement = FileSystem.convertURLToFile(classPathElement); | ||
classPath.add(localElement); | ||
} | ||
|
||
final int retcode = Main.run( | ||
"--encoding", "UTF-8", | ||
"--javasource", SARLVersion.MINIMAL_JDK_VERSION, | ||
"--javacompiler", JavaCompiler.JAVAC.name(), | ||
"--tempdir", this.tempFolder.getAbsolutePath(), | ||
"--directory", this.genFolder.getAbsolutePath(), | ||
"--outputdir", this.binFolder.getAbsolutePath(), | ||
"--docdirectory", this.docFolder.getAbsolutePath(), | ||
"--cp", classPath.toString(), | ||
this.srcFolder.getAbsolutePath()); | ||
//this.originalStdout.print(new String(this.captureStdout.toByteArray())); | ||
//this.originalStderr.print(new String(this.captureStderr.toByteArray())); | ||
assertEquals(0, retcode); | ||
|
||
File javaFile = FileSystem.join(this.genFolder, "io", "sarl", "lang", | ||
"sarlc", "tests", "resources", "test1", "Foo.java"); | ||
assertTrue("The Java file " + javaFile.getAbsolutePath() + " was not found", javaFile.exists()); | ||
String content = new String(Files.readAllBytes(javaFile.toPath()), Charset.defaultCharset()); | ||
assertEquals(EXPECTED_TEST1, content); | ||
|
||
File classFile = FileSystem.join(this.binFolder, "io", "sarl", "lang", | ||
"sarlc", "tests", "resources", "test1", "Foo.class"); | ||
assertTrue("The binary file " + classFile.getAbsolutePath() + " was not found", classFile.exists()); | ||
|
||
File docFile = FileSystem.join(this.docFolder, "io", "sarl", "lang", | ||
"sarlc", "tests", "resources", "test1", "Foo.html"); | ||
assertTrue("The documentation file " + docFile.getAbsolutePath() + " was not found", docFile.exists()); | ||
} | ||
|
||
@Test | ||
public void helpOption() throws IOException { | ||
//restoreOutputs(); | ||
final int retcode = Main.run("--help"); | ||
assertEquals(0, retcode); | ||
} | ||
|
||
@Test | ||
public void printConfigOption() throws IOException { | ||
//restoreOutputs(); | ||
final int retcode = Main.run("-C"); | ||
assertEquals(0, retcode); | ||
} | ||
|
||
@Test | ||
public void printConfigHelpOption() throws IOException { | ||
final int retcode = Main.run("-H"); | ||
assertEquals(0, retcode); | ||
} | ||
|
||
@Test | ||
public void illegalOption() throws IOException { | ||
//restoreOutputs(); | ||
final int retcode = Main.run("--thisisanillegaloption"); | ||
assertEquals(255, retcode); | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
products/sarldoc/src/test/resources/io/sarl/sarldoc/tests/resources/test1.sarl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package io.sarl.lang.sarlc.tests.resources.test1 | ||
|
||
import org.eclipse.xtend.lib.annotations.Accessors | ||
|
||
class Foo { | ||
|
||
@Accessors | ||
var fooField : int | ||
|
||
new(value : int = 0) { | ||
this.fooField = value | ||
} | ||
|
||
} |