Skip to content

Commit

Permalink
[tests][m2e] Add unit tests.
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 Dec 27, 2014
1 parent 20c9e33 commit 989736d
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 2 deletions.
1 change: 1 addition & 0 deletions plugins/io.sarl.m2e/META-INF/MANIFEST.MF
Expand Up @@ -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

23 changes: 21 additions & 2 deletions plugins/io.sarl.m2e/src/io/sarl/m2e/SARLMavenEclipsePlugin.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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}.
Expand Down Expand Up @@ -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.
Expand Down
15 changes: 15 additions & 0 deletions 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"
3 changes: 3 additions & 0 deletions 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
4 changes: 4 additions & 0 deletions 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
29 changes: 29 additions & 0 deletions tests/io.sarl.m2e.tests/pom.xml
@@ -0,0 +1,29 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.sarl</groupId>
<artifactId>io.sarl.tests</artifactId>
<version>0.2.0-SNAPSHOT</version>
</parent>

<groupId>io.sarl.lang</groupId>
<artifactId>io.sarl.m2e.tests</artifactId>
<packaging>eclipse-test-plugin</packaging>

<name>SARL Maven Eclipse Contribution Tests</name>

<build>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-surefire-plugin</artifactId>
<configuration>
<argLine>-ea</argLine>
</configuration>
</plugin>
</plugins>
</build>

</project>
@@ -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<IStatus> 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<IStatus> arg = ArgumentCaptor.forClass(IStatus.class);
verify(this.logger, times(1)).log(arg.capture());
assertSame(status, arg.getValue());
}

}

}
1 change: 1 addition & 0 deletions tests/pom.xml
Expand Up @@ -20,6 +20,7 @@
<module>io.sarl.lang.ui.tests</module>
<module>io.sarl.core.tests</module>
<module>io.sarl.eclipse.tests</module>
<module>io.sarl.m2e.tests</module>
</modules>

<!-- ======================================= -->
Expand Down

0 comments on commit 989736d

Please sign in to comment.