Permalink
Browse files

add functional test project

  • Loading branch information...
1 parent e057827 commit 2206f4129de518568312ba3d8c8018d20fd3cd32 @ncjones committed Apr 11, 2015
View
@@ -4,3 +4,4 @@ bin
.project
.settings
*.swp
+screenshots
View
@@ -52,6 +52,17 @@ git submodule update
```
+Testing
+-------
+
+Functional tests, implemented with JUnit and SwtBot, are located in the
+`editorconfig-eclipse-functional-test` directory. To run the tests:
+
+```sh
+mvn -f editorconfig-eclipse-functional-test/pom.xml integration-test
+```
+
+
Installing Locally
------------------
@@ -0,0 +1,12 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: EditorConfig Eclipse Functional Test
+Bundle-SymbolicName: editorconfig-eclipse-functional-test;singleton:=true
+Bundle-Version: 0.3.0.qualifier
+Bundle-RequiredExecutionEnvironment: JavaSE-1.6
+Require-Bundle: org.eclipse.ui,
+ org.eclipse.core.runtime,
+ org.eclipse.swtbot.eclipse.finder,
+ org.eclipse.swtbot.junit4_x,
+ org.junit;bundle-version="4.11.0",
+ org.hamcrest.library;bundle-version="1.3.0"
@@ -0,0 +1,4 @@
+source.. = src/main/java/
+output.. = bin/
+bin.includes = META-INF/,\
+ .
@@ -0,0 +1,62 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project>
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>com.ncjones</groupId>
+ <artifactId>editorconfig-eclipse-parent</artifactId>
+ <version>0.3.0-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>editorconfig-eclipse-functional-test</artifactId>
+ <packaging>eclipse-test-plugin</packaging>
+
+ <build>
+ <plugins>
+
+ <plugin>
+ <groupId>org.eclipse.tycho</groupId>
+ <artifactId>target-platform-configuration</artifactId>
+ <version>${tycho-version}</version>
+ <configuration>
+ <dependency-resolution>
+ <extraRequirements>
+ <requirement>
+ <type>eclipse-feature</type>
+ <id>editorconfig-eclipse-feature</id>
+ <versionRange>0.0.0</versionRange>
+ </requirement>
+ <requirement>
+ <type>eclipse-feature</type>
+ <id>org.eclipse.e4.rcp</id>
+ <versionRange>0.0.0</versionRange>
+ </requirement>
+ <requirement>
+ <type>eclipse-feature</type>
+ <id>org.eclipse.pde</id>
+ <versionRange>0.0.0</versionRange>
+ </requirement>
+ </extraRequirements>
+ </dependency-resolution>
+ </configuration>
+ </plugin>
+
+ <plugin>
+ <groupId>org.eclipse.tycho</groupId>
+ <artifactId>tycho-surefire-plugin</artifactId>
+ <version>${tycho-version}</version>
+ <configuration>
+ <includes>
+ <include>**/*Test.java</include>
+ </includes>
+ <useUIHarness>true</useUIHarness>
+ <useUIThread>false</useUIThread>
+ <product>org.eclipse.sdk.ide</product>
+ <application>org.eclipse.ui.ide.workbench</application>
+ </configuration>
+ </plugin>
+
+ </plugins>
+ </build>
+
+</project>
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2015 Nathan Jones
+ *
+ * This file is part of "EditorConfig Eclipse".
+ *
+ * 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 com.ncjones.editorconfig.eclipse.test;
+
+import static org.hamcrest.Matchers.equalTo;
+import static org.junit.Assert.assertThat;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized.Parameter;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(ParameterizedSWTBotRunner.class)
+public class EditorConfigTest {
+
+ @Parameters(name = "{0}")
+ public static Collection<String[]> params() {
+ return Arrays.asList(
+ new String[] {"test.xml"},
+ new String[] {"build.xml"},
+ new String[] {"test.java"},
+ new String[] {"test.txt"}
+ );
+ }
+
+ @Parameter
+ public String fileName;
+
+ private SWTWorkbenchBot bot;
+ private EditorConfigTestContext context;
+
+ @Before
+ public void setUp() throws Exception {
+ bot = new SWTWorkbenchBot();
+ context = new EditorConfigTestContext(bot, "editorconfig-test");
+ bot.closeAllEditors();
+ }
+
+ @Test
+ public void testIndentStyleSpace() throws Exception {
+ context.editorConfig(
+ "root = true",
+ "[*]",
+ "indent_style = space",
+ "indent_size = 2"
+ );
+ context.editFile(fileName, "\t");
+ assertThat(context.fileContents(fileName), equalTo(" "));
+ }
+
+ @Test
+ public void testIndentStyleTab() throws Exception {
+ context.editorConfig(
+ "root = true",
+ "[*]",
+ "indent_style = tab"
+ );
+ context.editFile(fileName, "\t");
+ assertThat(context.fileContents(fileName), equalTo("\t"));
+ }
+
+}
@@ -0,0 +1,132 @@
+/*
+ * Copyright 2015 Nathan Jones
+ *
+ * This file is part of "EditorConfig Eclipse".
+ *
+ * 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 com.ncjones.editorconfig.eclipse.test;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.LinkOption;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardOpenOption;
+import java.util.Arrays;
+
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.core.runtime.preferences.IEclipsePreferences;
+import org.eclipse.core.runtime.preferences.InstanceScope;
+import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
+import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEclipseEditor;
+import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
+import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
+
+public class EditorConfigTestContext {
+
+ private final SWTWorkbenchBot bot;
+ private final String projectName;
+
+ public EditorConfigTestContext(final SWTWorkbenchBot bot, final String projectName) {
+ this.bot = bot;
+ this.projectName = projectName;
+ closeWelcomePageIfPresent();
+ createJavaProjectIfNotExists(projectName);
+ configureEditors();
+ SWTBotPreferences.KEYBOARD_LAYOUT = "EN_US";
+ }
+
+ private void configureEditors() {
+ setPreference("org.eclipse.wst.xml.ui", "org.eclipse.wst.xml.ui.internal.tabletree.XMLMultiPageEditorPart.lastActivePage", "1");
+ }
+
+ private void setPreference(final String prefsNodeName, final String key, final String value) {
+ final IEclipsePreferences prefs = InstanceScope.INSTANCE.getNode(prefsNodeName);
+ prefs.put(key, value);
+ }
+
+
+ private void closeWelcomePageIfPresent() {
+ try {
+ bot.viewByTitle("Welcome").close();
+ } catch (final WidgetNotFoundException e) {
+ }
+ }
+
+ private void createJavaProjectIfNotExists(final String projectName) {
+ final Path projectFilePath = Paths.get(workspaceDir(), projectName, ".project");
+ if (!Files.isRegularFile(projectFilePath, LinkOption.NOFOLLOW_LINKS)) {
+ newJavaProject(projectName);
+ }
+ }
+
+ private void newJavaProject(final String projectName) {
+ bot.menu("File").menu("New").menu("Project...").click();
+ bot.shell("New Project").activate();
+ bot.tree().expandNode("Java").select("Java Project");
+ bot.button("Next >").click();
+ bot.textWithLabel("Project name:").setText(projectName);
+ bot.button("Finish").click();
+ bot.button("Yes").click();
+ }
+
+ public String fileContents(final String fileName) {
+ byte[] bytes;
+ try {
+ bytes = Files.readAllBytes(filePath(fileName));
+ } catch (final IOException e) {
+ throw new RuntimeException(e);
+ }
+ return new String(bytes);
+ }
+
+ private static String workspaceDir() {
+ return Platform.getInstanceLocation().getURL().getFile();
+ }
+
+ public void newFile(final String fileName) {
+ bot.menu("File").menu("New").menu("File").click();
+ bot.textWithLabel("Enter or select the parent folder:").setText(projectName);
+ bot.textWithLabel("File na&me:").setText(fileName);
+ bot.button("Finish").click();
+ }
+
+ public void editFile(final String fileName, final String contents) {
+ if (Files.exists(filePath(fileName), LinkOption.NOFOLLOW_LINKS)) {
+ bot.tree().getTreeItem(projectName).getNode(fileName).doubleClick();
+ } else {
+ newFile(fileName);
+ }
+ final SWTBotEclipseEditor textEditor = bot.editorByTitle(fileName).toTextEditor();
+ textEditor.setText("");
+ textEditor.typeText(contents);
+ textEditor.save();
+ }
+
+ public void editorConfig(final String... configLines) {
+ final Path editorConfigPath = filePath(".editorconfig");
+ try {
+ Files.deleteIfExists(editorConfigPath);
+ Files.createFile(editorConfigPath);
+ Files.write(editorConfigPath, Arrays.asList(configLines), StandardOpenOption.WRITE);
+ } catch (final IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private Path filePath(final String fileName) {
+ return Paths.get(workspaceDir(), projectName, fileName);
+ }
+
+}
@@ -0,0 +1,50 @@
+/*******************************************************************************
+ * Copyright (c) 2008 SWTBot Committers and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Hans Schwaebli - initial API and implementation (Bug 259787)
+ * Toby Weston - initial API and implementation (Bug 259787)
+ *******************************************************************************/
+package com.ncjones.editorconfig.eclipse.test;
+
+import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
+import org.eclipse.swtbot.swt.finder.junit.ScreenshotCaptureListener;
+import org.junit.runner.notification.RunListener;
+import org.junit.runner.notification.RunNotifier;
+import org.junit.runners.Parameterized;
+
+/**
+ * A {@link Parameterized} runner that captures screenshots on test failures.
+ * Mixes the behavior of {@link Parameterized} and {@link SWTBotJunit4ClassRunner}.
+ *
+ * @noextend This class is not intended to be subclassed by clients.
+ * @noinstantiate This class is not intended to be instantiated by clients.
+ */
+public class ParameterizedSWTBotRunner extends Parameterized {
+
+ /**
+ * Creates a SWTBotRunner to run {@code klass}
+ *
+ * @throws Exception if the test class is malformed.
+ */
+ public ParameterizedSWTBotRunner(final Class<?> klass) throws Throwable {
+ super(klass);
+ }
+
+ @Override
+ public void run(final RunNotifier notifier) {
+ final RunListener failureSpy = new ScreenshotCaptureListener();
+ notifier.removeListener(failureSpy); // remove existing listeners that could be added by suite or class runners
+ notifier.addListener(failureSpy);
+ try {
+ super.run(notifier);
+ } finally {
+ notifier.removeListener(failureSpy);
+ }
+ }
+
+}
View
@@ -23,6 +23,11 @@
<layout>p2</layout>
<url>http://download.eclipse.org/releases/luna</url>
</repository>
+ <repository>
+ <id>swtbot</id>
+ <layout>p2</layout>
+ <url>http://download.eclipse.org/technology/swtbot/releases/2.2.1</url>
+ </repository>
</repositories>
<build>

5 comments on commit 2206f41

@ncjones
Owner
ncjones commented on 2206f41 Sep 1, 2015

@paulvi it is in master.

@ncjones
Owner
ncjones commented on 2206f41 Sep 1, 2015

I haven't included it in the main build because it's too slow. The readme describes how to run the functional test suite. Are there any specific issues you are experiencing?

@paulvi
paulvi commented on 2206f41 Sep 2, 2015

This is trifle, it was just first time acquaintance with the project.

Please sign in to comment.