Permalink
Browse files

tests: Add MarkerTest

  • Loading branch information...
arafsheikh committed May 25, 2016
1 parent e4426d6 commit 68758db76532e1beaccacf80d6e94051ab98ef78
@@ -12,6 +12,7 @@ Require-Bundle: org.eclipse.ui,
org.eclipse.ui.ide;bundle-version="3.11.0",
org.eclipse.core.expressions;bundle-version="3.5.0",
org.eclipse.core.runtime;bundle-version="3.11.1",
org.eclipse.e4.core.di
org.eclipse.e4.core.di,
org.junit
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Import-Package: javax.inject;version="1.0.0"
@@ -11,8 +11,6 @@
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.PlatformUI;
import org.json.JSONArray;
import org.json.JSONObject;
@@ -39,14 +37,14 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
try {
new RemoveMarkers().execute(event);
String json = getcoalaJSON(file, "CheckstyleBear");
processJSON(json);
processJSON(json, file);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public void processJSON(String json) throws IOException {
public void processJSON(String json, IFile file) throws IOException {
JSONObject obj = new JSONObject(json);
JSONArray arr = obj.getJSONObject("results").getJSONArray("default");
for (int i = 0; i < arr.length(); i++) {
@@ -56,17 +54,17 @@ public void processJSON(String json) throws IOException {
JSONArray arr2 = arr.getJSONObject(i).getJSONArray("affected_code");
for (int j = 0; j < arr2.length(); j++) {
int end_line = arr2.getJSONObject(j).getJSONObject("end").getInt("line");
createCoolMarker(end_line, 3 - severity, "( " + origin + " ): " + message);
createCoolMarker(file, end_line, 3 - severity, "( " + origin + " ): " + message);
}
}
}
public IMarker createCoolMarker(int line_num, int flag, String message) {
public IMarker createCoolMarker(IFile file, int line_num, int flag, String message) {
// :: param : flag = 1 for error
// flag = 2 for warning
IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
/*IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
IFileEditorInput input = (IFileEditorInput) editor.getEditorInput();
IFile file = input.getFile();
IFile file = input.getFile();*/
IResource resource = (IResource) file;
try {
@@ -17,6 +17,11 @@
public Object execute(ExecutionEvent event) throws ExecutionException {
IFile file = ((IFileEditorInput) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
.getActiveEditor().getEditorInput()).getFile();
removeAllMarkers(file);
return null;
}
public void removeAllMarkers(IFile file) {
IMarker[] problems = null;
IResource resource = (IResource) file;
int depth = IResource.DEPTH_INFINITE;
@@ -34,6 +39,5 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
e.printStackTrace();
}
}
return null;
}
}
@@ -0,0 +1,73 @@
package com.coala.tests;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.coala.core.handlers.Plugin;
import com.coala.core.handlers.RemoveMarkers;
import junit.framework.TestCase;
public class MarkerTest extends TestCase {
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("com.test_proj");
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();
}
@Test
public void test() throws IOException, CoreException, ExecutionException {
removeMarkers.removeAllMarkers(file);
String json = Plugin.getcoalaJSON(file, "CheckstyleBear");
plugin.processJSON(json, file);
IMarker[] markers = file.findMarkers("com.coala.core.coolproblem", true, IResource.DEPTH_INFINITE);
assertEquals(markers.length, 3);
}
@After
public void tearDown() throws CoreException {
project.close(null);
IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
for (IProject project : projects) {
project.delete(true, true, null);
}
ResourcesPlugin.getWorkspace().save(true, null);
}
}

0 comments on commit 68758db

Please sign in to comment.