From 989736dd6eaac0f57e1b290a106f66207a647764 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Galland?= Date: Sun, 28 Dec 2014 00:18:41 +0100 Subject: [PATCH] [tests][m2e] Add unit tests. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Galland --- plugins/io.sarl.m2e/META-INF/MANIFEST.MF | 1 + .../io/sarl/m2e/SARLMavenEclipsePlugin.java | 23 ++- tests/io.sarl.m2e.tests/META-INF/MANIFEST.MF | 15 ++ .../OSGI-INF/l10n/bundle.properties | 3 + tests/io.sarl.m2e.tests/build.properties | 4 + tests/io.sarl.m2e.tests/pom.xml | 29 +++ .../m2e/tests/SARLMavenEclipsePluginTest.java | 195 ++++++++++++++++++ tests/pom.xml | 1 + 8 files changed, 269 insertions(+), 2 deletions(-) create mode 100644 tests/io.sarl.m2e.tests/META-INF/MANIFEST.MF create mode 100644 tests/io.sarl.m2e.tests/OSGI-INF/l10n/bundle.properties create mode 100644 tests/io.sarl.m2e.tests/build.properties create mode 100644 tests/io.sarl.m2e.tests/pom.xml create mode 100644 tests/io.sarl.m2e.tests/src/io/sarl/m2e/tests/SARLMavenEclipsePluginTest.java diff --git a/plugins/io.sarl.m2e/META-INF/MANIFEST.MF b/plugins/io.sarl.m2e/META-INF/MANIFEST.MF index 2aa7d5a86a..a1660b09cb 100644 --- a/plugins/io.sarl.m2e/META-INF/MANIFEST.MF +++ b/plugins/io.sarl.m2e/META-INF/MANIFEST.MF @@ -16,4 +16,5 @@ Require-Bundle: io.sarl.eclipse;bundle-version="[0.2.0,0.3.0)", org.eclipse.jdt.core;bundle-version="3.10.0", org.eclipse.jdt.launching;bundle-version="3.7.101" Bundle-Activator: io.sarl.m2e.SARLMavenEclipsePlugin +Export-Package: io.sarl.m2e diff --git a/plugins/io.sarl.m2e/src/io/sarl/m2e/SARLMavenEclipsePlugin.java b/plugins/io.sarl.m2e/src/io/sarl/m2e/SARLMavenEclipsePlugin.java index d4d2043edb..2fc52626d3 100644 --- a/plugins/io.sarl.m2e/src/io/sarl/m2e/SARLMavenEclipsePlugin.java +++ b/plugins/io.sarl.m2e/src/io/sarl/m2e/SARLMavenEclipsePlugin.java @@ -24,6 +24,7 @@ import org.apache.maven.artifact.ArtifactUtils; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.ILog; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Plugin; import org.eclipse.core.runtime.Status; @@ -51,7 +52,17 @@ public class SARLMavenEclipsePlugin extends Plugin { /** */ public SARLMavenEclipsePlugin() { - // + setDefault(this); + } + + /** Replies the logger. + * + * Thus function is a non-final version of {@link #getLog()}. + * + * @return the logger. + */ + public ILog getILog() { + return getLog(); } /** Replies the instance of the plugin. @@ -62,6 +73,14 @@ public static SARLMavenEclipsePlugin getDefault() { return instance; } + /** Set the default instance of the plugin. + * + * @param defaultInstance - the default plugin instance. + */ + public static void setDefault(SARLMavenEclipsePlugin defaultInstance) { + instance = defaultInstance; + } + /** Create a status. * * @param severity - the severity level, see {@link IStatus}. @@ -110,7 +129,7 @@ public static void log(Throwable e) { * @param status status to log */ public static void log(IStatus status) { - getDefault().getLog().log(status); + getDefault().getILog().log(status); } /** Maven version parser. diff --git a/tests/io.sarl.m2e.tests/META-INF/MANIFEST.MF b/tests/io.sarl.m2e.tests/META-INF/MANIFEST.MF new file mode 100644 index 0000000000..eb3028261b --- /dev/null +++ b/tests/io.sarl.m2e.tests/META-INF/MANIFEST.MF @@ -0,0 +1,15 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: %Bundle-Name +Bundle-Vendor: %Bundle-Vendor +Bundle-Version: 0.2.0.qualifier +Bundle-SymbolicName: io.sarl.m2e.tests; singleton:=true +Bundle-RequiredExecutionEnvironment: JavaSE-1.7 +Require-Bundle: io.sarl.m2e;bundle-version="[0.2.0,0.3.0)", + io.sarl.tests.testdata;bundle-version="[0.2.0,0.3.0)", + org.eclipse.jdt.core;bundle-version="3.10.0", + org.eclipse.xtext.junit4;bundle-version="2.7.3", + org.eclipse.xtext.xbase.junit;bundle-version="2.7.3", + org.eclipse.core.filesystem;bundle-version="1.4.100", + org.mockito.mockito-core;bundle-version="1.9.5", + org.junit;bundle-version="4.11.0" diff --git a/tests/io.sarl.m2e.tests/OSGI-INF/l10n/bundle.properties b/tests/io.sarl.m2e.tests/OSGI-INF/l10n/bundle.properties new file mode 100644 index 0000000000..85e8ac75bd --- /dev/null +++ b/tests/io.sarl.m2e.tests/OSGI-INF/l10n/bundle.properties @@ -0,0 +1,3 @@ +#Properties file for io.sarl.m2e.tests +Bundle-Vendor = SARL.io +Bundle-Name = SARL Maven Eclipse Test Suite \ No newline at end of file diff --git a/tests/io.sarl.m2e.tests/build.properties b/tests/io.sarl.m2e.tests/build.properties new file mode 100644 index 0000000000..a5ce8b7b31 --- /dev/null +++ b/tests/io.sarl.m2e.tests/build.properties @@ -0,0 +1,4 @@ +source.. = src/ +bin.includes = META-INF/,\ + OSGI-INF/ +jre.compilation.profile = JavaSE-1.7 diff --git a/tests/io.sarl.m2e.tests/pom.xml b/tests/io.sarl.m2e.tests/pom.xml new file mode 100644 index 0000000000..327cfe71c9 --- /dev/null +++ b/tests/io.sarl.m2e.tests/pom.xml @@ -0,0 +1,29 @@ + + 4.0.0 + + + io.sarl + io.sarl.tests + 0.2.0-SNAPSHOT + + + io.sarl.lang + io.sarl.m2e.tests + eclipse-test-plugin + + SARL Maven Eclipse Contribution Tests + + + + + org.eclipse.tycho + tycho-surefire-plugin + + -ea + + + + + + diff --git a/tests/io.sarl.m2e.tests/src/io/sarl/m2e/tests/SARLMavenEclipsePluginTest.java b/tests/io.sarl.m2e.tests/src/io/sarl/m2e/tests/SARLMavenEclipsePluginTest.java new file mode 100644 index 0000000000..7c00e0ce8b --- /dev/null +++ b/tests/io.sarl.m2e.tests/src/io/sarl/m2e/tests/SARLMavenEclipsePluginTest.java @@ -0,0 +1,195 @@ +/* + * $Id$ + * + * SARL is an general-purpose agent programming language. + * More details on http://www.sarl.io + * + * Copyright (C) 2014 Sebastian RODRIGUEZ, Nicolas GAUD, Stéphane GALLAND. + * + * 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.m2e.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.mockito.Mockito.*; +import io.sarl.m2e.SARLMavenEclipsePlugin; + +import org.eclipse.core.runtime.ILog; +import org.eclipse.core.runtime.IStatus; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; +import org.mockito.ArgumentCaptor; +import org.osgi.framework.Version; + + +/** + * @author $Author: sgalland$ + * @version $FullVersion$ + * @mavengroupid $GroupId$ + * @mavenartifactid $ArtifactId$ + */ +@RunWith(Suite.class) +@SuiteClasses({ + SARLMavenEclipsePluginTest.DefaultPlugin.class, + SARLMavenEclipsePluginTest.PluginInstance.class, + SARLMavenEclipsePluginTest.Logging.class, +}) +@SuppressWarnings("all") +public final class SARLMavenEclipsePluginTest { + + /** + * @author $Author: sgalland$ + * @version $FullVersion$ + * @mavengroupid $GroupId$ + * @mavenartifactid $ArtifactId$ + */ + public static class DefaultPlugin { + + @Before + public void setUp() { + SARLMavenEclipsePlugin.setDefault(null); + } + + @After + public void tearDown() { + SARLMavenEclipsePlugin.setDefault(null); + } + + @Test + public void getDefault_noBundleInitialization() { + assertNull(SARLMavenEclipsePlugin.getDefault()); + } + + @Test + public void getDefault_bundleInitialization() { + SARLMavenEclipsePlugin plugin = mock(SARLMavenEclipsePlugin.class); + SARLMavenEclipsePlugin.setDefault(plugin); + assertSame(plugin, SARLMavenEclipsePlugin.getDefault()); + } + + } + + /** + * @author $Author: sgalland$ + * @version $FullVersion$ + * @mavengroupid $GroupId$ + * @mavenartifactid $ArtifactId$ + */ + public static class PluginInstance { + + private SARLMavenEclipsePlugin plugin; + private SARLMavenEclipsePlugin spy; + + @Before + public void setUp() { + SARLMavenEclipsePlugin.setDefault(null); + this.plugin = new SARLMavenEclipsePlugin(); + this.spy = spy(this.plugin); + } + + @After + public void tearDown() { + this.plugin = null; + this.spy = null; + SARLMavenEclipsePlugin.setDefault(null); + } + + @Test + public void getDefault() { + assertSame(this.plugin, SARLMavenEclipsePlugin.getDefault()); + } + + @Test + public void createStatusIntThrowable() { + Throwable ex = mock(Throwable.class); + IStatus status = this.spy.createStatus(IStatus.ERROR, ex); + assertNotNull(status); + assertEquals(IStatus.ERROR, status.getSeverity()); + assertSame(ex, status.getException()); + } + + @Test + public void createStatusIntString() { + String msg = "my error message"; + IStatus status = this.spy.createStatus(IStatus.ERROR, msg); + assertNotNull(status); + assertEquals(IStatus.ERROR, status.getSeverity()); + assertEquals(msg, status.getMessage()); + } + + @Test + public void parseMavenVersion() { + Version version = SARLMavenEclipsePlugin.parseMavenVersion("1.2.3"); + assertNotNull(version); + assertEquals(1, version.getMajor()); + assertEquals(2, version.getMinor()); + assertEquals(3, version.getMicro()); + } + + } + + /** + * @author $Author: sgalland$ + * @version $FullVersion$ + * @mavengroupid $GroupId$ + * @mavenartifactid $ArtifactId$ + */ + public static class Logging { + + private ILog logger; + private SARLMavenEclipsePlugin plugin; + + @Before + public void setUp() { + SARLMavenEclipsePlugin.setDefault(null); + this.logger = mock(ILog.class); + this.plugin = mock(SARLMavenEclipsePlugin.class); + SARLMavenEclipsePlugin.setDefault(this.plugin); + when(this.plugin.getILog()).thenReturn(this.logger); + } + + @After + public void tearDown() { + this.logger = null; + this.plugin = null; + SARLMavenEclipsePlugin.setDefault(null); + } + + @Test + public void logThrowable() { + Throwable ex = mock(Throwable.class); + this.plugin.log(ex); + ArgumentCaptor arg = ArgumentCaptor.forClass(IStatus.class); + verify(this.logger, times(1)).log(arg.capture()); + assertSame(ex, arg.getValue().getException()); + } + + @Test + public void logIStatus() { + IStatus status = mock(IStatus.class); + this.plugin.log(status); + ArgumentCaptor arg = ArgumentCaptor.forClass(IStatus.class); + verify(this.logger, times(1)).log(arg.capture()); + assertSame(status, arg.getValue()); + } + + } + +} \ No newline at end of file diff --git a/tests/pom.xml b/tests/pom.xml index 39103beb01..5f9af73ac7 100644 --- a/tests/pom.xml +++ b/tests/pom.xml @@ -20,6 +20,7 @@ io.sarl.lang.ui.tests io.sarl.core.tests io.sarl.eclipse.tests + io.sarl.m2e.tests