package com.coala.core.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);
	}

}

@@ -1473,367 +1473,8 @@
 ;%0A%09%7D
-%0A%0A%09@Test%0A%09public void test() throws IOException, CoreException, ExecutionException %7B%0A%09%09removeMarkers.removeAllMarkers(file);%0A%09%09String json = Plugin.getcoalaJSON(file, %22CheckstyleBear%22);%0A%09%09plugin.processJSON(json, file);%0A%09%09IMarker%5B%5D markers = file.findMarkers(%22com.coala.core.coolproblem%22, true, IResource.DEPTH_INFINITE);%0A%09%09assertEquals(markers.length, 3);%0A%09%7D
 %0A%09%0A%09
@@ -1630,19 +1630,16 @@
 ects();%0A
-%09%09%0A
 %09%09for (I
@@ -1713,11 +1713,8 @@
 %09%09%7D%0A
-%09%09%0A
 %09%09Re
@@ -1764,10 +1764,384 @@
 l);%0A%09%7D%0A%0A
+%09@Test%0A%09public void test() throws IOException, CoreException, ExecutionException %7B%0A%09%09removeMarkers.removeAllMarkers(file);%0A%09%09plugin.createCoolMarker(file, 0, 0, %22First marker%22);%0A%09%09plugin.createCoolMarker(file, 0, 0, %22Second marker%22);%0A%09%09IMarker%5B%5D markers = file.findMarkers(%22com.coala.core.coolproblem%22, true, IResource.DEPTH_INFINITE);%0A%09%09assertEquals(markers.length, 2);%0A%09%7D%0A
 %7D%0A

Improve test