Permalink
Browse files

tests: Refactor for Gitmate

  • Loading branch information...
arafsheikh committed Jun 4, 2016
1 parent b9119a4 commit b74a29d1ff1d4682e9fba4680b46234703f7a5b7
@@ -1,8 +1,9 @@
package com.coala.core.tests;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import static org.junit.Assert.assertEquals;
import com.coala.core.handlers.Plugin;
import com.coala.core.handlers.RemoveMarkers;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
@@ -16,55 +17,62 @@
import org.junit.Before;
import org.junit.Test;
import com.coala.core.handlers.Plugin;
import com.coala.core.handlers.RemoveMarkers;
import junit.framework.TestCase;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
public class MarkerTest extends TestCase {
public class MarkerTest {
private IProject project;
private Plugin plugin;
private RemoveMarkers removeMarkers;
private IFile file;
private IProject project;
private Plugin plugin;
private RemoveMarkers removeMarkers;
private IFile file;
@Before
public void setUp() throws Exception {
String data = "package test_proj;\n\npublic class newClass {\n\n}";
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
project = root.getProject(TestUtils.generateRandomStr(5));
if (!project.exists()) {
project.create(null);
}
if (!project.isOpen()) {
project.open(null);
}
file = project.getFile("test.java");
if (!file.exists()) {
byte[] bytes = data.getBytes();
InputStream source = new ByteArrayInputStream(bytes);
file.create(source, IResource.NONE, null);
}
plugin = new Plugin();
removeMarkers = new RemoveMarkers();
/**
* Setup before test.
* @throws Exception exception
*/
@Before
public void setUp() throws Exception {
String data = "package test_proj;\n\npublic class newClass {\n\n}";
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
project = root.getProject(TestUtils.generateRandomStr(5));
if (!project.exists()) {
project.create(null);
}
@Test
public void test() throws IOException, CoreException, ExecutionException {
removeMarkers.removeAllMarkers(file);
plugin.createCoolMarker(file, 0, 0, "First marker");
plugin.createCoolMarker(file, 0, 0, "Second marker");
IMarker[] markers = file.findMarkers("com.coala.core.coolproblem", true,
IResource.DEPTH_INFINITE);
assertEquals(markers.length, 2);
if (!project.isOpen()) {
project.open(null);
}
file = project.getFile("test.java");
if (!file.exists()) {
byte[] bytes = data.getBytes();
InputStream source = new ByteArrayInputStream(bytes);
file.create(source, IResource.NONE, null);
}
plugin = new Plugin();
removeMarkers = new RemoveMarkers();
}
@Test
public void test() throws IOException, CoreException, ExecutionException {
removeMarkers.removeAllMarkers(file);
plugin.createCoolMarker(file, 0, 0, "First marker");
plugin.createCoolMarker(file, 0, 0, "Second marker");
IMarker[] markers = file.findMarkers("com.coala.core.coolproblem", true,
IResource.DEPTH_INFINITE);
assertEquals(markers.length, 2);
}
@After
public void tearDown() throws CoreException {
if (project.isOpen()) {
project.close(null);
}
project.delete(true, true, null);
ResourcesPlugin.getWorkspace().save(true, null);
/**
* Tear-down after test.
* @throws CoreException exception
*/
@After
public void tearDown() throws CoreException {
if (project.isOpen()) {
project.close(null);
}
project.delete(true, true, null);
ResourcesPlugin.getWorkspace().save(true, null);
}
}
@@ -2,10 +2,8 @@
import static org.junit.Assert.assertEquals;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;
import com.coala.core.handlers.Plugin;
import com.coala.core.handlers.RemoveMarkers;
import org.apache.commons.exec.ExecuteException;
import org.eclipse.core.resources.IFile;
@@ -19,63 +17,72 @@
import org.junit.Before;
import org.junit.Test;
import com.coala.core.handlers.Plugin;
import com.coala.core.handlers.RemoveMarkers;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;
public class RunCmdTest {
private IProject project;
private Plugin plugin;
private RemoveMarkers removeMarkers;
private IFile file;
private IProject project;
private Plugin plugin;
private RemoveMarkers removeMarkers;
private IFile file;
@Before
public void setUp() throws Exception {
String data = "package test_proj;\n\npublic class newClass {\n\n}";
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
project = root.getProject(TestUtils.generateRandomStr(5));
if (!project.exists()) {
project.create(null);
}
if (!project.isOpen()) {
project.open(null);
}
file = project.getFile("test.java");
if (!file.exists()) {
byte[] bytes = data.getBytes();
InputStream source = new ByteArrayInputStream(bytes);
file.create(source, IResource.NONE, null);
}
plugin = new Plugin();
removeMarkers = new RemoveMarkers();
/**
* Setup before test.
* @throws Exception exception
*/
@Before
public void setUp() throws Exception {
String data = "package test_proj;\n\npublic class newClass {\n\n}";
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
project = root.getProject(TestUtils.generateRandomStr(5));
if (!project.exists()) {
project.create(null);
}
@Test
public void test() throws InterruptedException, CoreException,
ExecuteException, IOException {
removeMarkers.removeAllMarkers(file);
plugin.runcoalaOnFile(file, "CheckstyleBear");
sleep(15);
IMarker[] markers = file.findMarkers("com.coala.core.coolproblem", true,
IResource.DEPTH_INFINITE);
assertEquals(markers.length, 3);
if (!project.isOpen()) {
project.open(null);
}
file = project.getFile("test.java");
if (!file.exists()) {
byte[] bytes = data.getBytes();
InputStream source = new ByteArrayInputStream(bytes);
file.create(source, IResource.NONE, null);
}
plugin = new Plugin();
removeMarkers = new RemoveMarkers();
}
@Test
public void test() throws InterruptedException, CoreException, ExecuteException, IOException {
removeMarkers.removeAllMarkers(file);
plugin.runcoalaOnFile(file, "CheckstyleBear");
sleep(15);
IMarker[] markers = file.findMarkers("com.coala.core.coolproblem", true,
IResource.DEPTH_INFINITE);
assertEquals(markers.length, 3);
}
@After
public void tearDown() throws Exception {
if (project.isOpen()) {
project.close(null);
}
project.delete(true, true, null);
ResourcesPlugin.getWorkspace().save(true, null);
/**
* Tear-down after test.
* @throws Exception exception
*/
@After
public void tearDown() throws Exception {
if (project.isOpen()) {
project.close(null);
}
project.delete(true, true, null);
ResourcesPlugin.getWorkspace().save(true, null);
}
private void sleep(int seconds) {
try {
TimeUnit.SECONDS.sleep(seconds);
} catch (InterruptedException e) {
e.printStackTrace();
}
private void sleep(int seconds) {
try {
TimeUnit.SECONDS.sleep(seconds);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
@@ -4,15 +4,19 @@
public class TestUtils {
public static String generateRandomStr(int length) {
String candidateChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i < length; i++) {
sb.append(candidateChars
.charAt(random.nextInt(candidateChars.length())));
}
return sb.toString();
/**
* Generate a random string.
* @param length Length of string
* @return Random string of given length
*/
public static String generateRandomStr(int length) {
String candidateChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i < length; i++) {
sb.append(candidateChars.charAt(random.nextInt(candidateChars.length())));
}
return sb.toString();
}
}

0 comments on commit b74a29d

Please sign in to comment.