/*******************************************************************************
 * Copyright (c) 2013 itemis AG (http://www.itemis.eu) 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
 *******************************************************************************/
package org.eclipse.xtext.junit4.internal;

import static org.eclipse.xtext.xbase.lib.IterableExtensions.*;
import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.ui.texteditor.MarkerUtilities;
import org.junit.Test;

/**
 * @author dhuebner - Initial contribution and API
 */
public class InternalBuilderTest {

	@Test
	public void test() throws CoreException {
		long maxMem = Runtime.getRuntime().maxMemory() / (1024 * 1024);
		long free = Runtime.getRuntime().freeMemory() / (1024 * 1024);
		long used = Runtime.getRuntime().totalMemory() / (1024 * 1024);
		System.out.println("Starting build. Memory max=" + maxMem + "m, total=" + used + "m, free=" + free + "m");
		ResourcesPlugin.getWorkspace().build(IncrementalProjectBuilder.FULL_BUILD, new NullProgressMonitor());

		final IMarker[] markers = ResourcesPlugin.getWorkspace().getRoot()
				.findMarkers(IMarker.PROBLEM, true, IResource.DEPTH_INFINITE);
		List<String> errors = new ArrayList<String>();
		for (IMarker marker : markers) {
			String msg = MarkerUtilities.getMessage(marker);
			if (MarkerUtilities.getSeverity(marker) == IMarker.SEVERITY_ERROR) {
				errors.add(msg);
			}
		}

		List<String> top10;
		if (errors.size() > 10) {
			top10 = toList(take(errors, 10));
		} else {
			top10 = errors;
		}
		maxMem = Runtime.getRuntime().maxMemory() / (1024 * 1024);
		free = Runtime.getRuntime().freeMemory() / (1024 * 1024);
		used = Runtime.getRuntime().totalMemory() / (1024 * 1024);
		System.out.println("Finished build. Memory max=" + maxMem + "m, total=" + used + "m, free=" + free + "m");
		assertTrue("Problems found (" + top10.size() + " from " + errors.size() + "): " + join(errors, ", "),
				errors.isEmpty());
	}

}

@@ -612,16 +612,75 @@
 ert.*;%0A%0A
+import java.io.File;%0Aimport java.io.FileNotFoundException;%0A
 import j
@@ -1023,16 +1023,54 @@
 onitor;%0A
+import org.eclipse.jdt.core.JavaCore;%0A
 import o
@@ -1111,16 +1111,53 @@
 lities;%0A
+import org.eclipse.xtext.util.Files;%0A
 import o
@@ -1318,317 +1318,192 @@
 tion
- %7B%0A%09%09long maxMem = Runtime.getRuntime().maxMemory() / (1024 * 1024
+, FileNotFoundException %7B%0A%0A%09%09reportMemoryState(%22Starting build.%22
 );%0A
+%0A
 %09%09
-long free = Runtime.getRuntime().freeMemory() / (1024 * 1024);%0A%09%09long used = Runtime.getRuntime().totalMemory() / (1024 * 1024
+try %7B%0A%09%09%09File jdtMetadata = JavaCore.getPlugin().getStateLocation().toFile(
 );%0A%09%09
+%09
-System.out.println(%22Starting build. Memory max=%22 + maxMem + %22m, total=%22 + used + %22m, free=%22 + free + %22m%22
+Files.sweepFolder(jdtMetadata
 );%0A
+%09
 %09%09Re
@@ -1603,16 +1603,74 @@
 tor());%0A
+%09%09%7D finally %7B%0A%09%09%09reportMemoryState(%22Finished build.%22);%0A%09%09%7D
 %0A%09%09final
@@ -2162,77 +2162,243 @@
 %09%09%7D%0A
+%0A
 %09%09
-maxMem = Runtime.getRuntime().maxMemory() / (1024 * 1024);%0A%09%09free =
+assertTrue(%22Problems found (%22 + top10.size() + %22 from %22 + errors.size() + %22): %22 + join(errors, %22, %22),%0A%09%09%09%09errors.isEmpty());%0A%09%7D%0A%0A%09private void reportMemoryState(String reportName) %7B%0A%09%09System.out.println(reportName + %22 Memory max=%22 +
  Run
@@ -2415,20 +2415,19 @@
 ntime().
-free
+max
 Memory()
@@ -2446,18 +2446,28 @@
 024)
-;%0A%09%09used =
+ + %22m, total=%22%0A%09%09%09%09+
  Run
@@ -2517,243 +2517,83 @@
 024)
-;%0A%09%09System.out.println(%22Finished build. Memory max=%22 + maxMem + %22m, total=%22 + used + %22m, free=%22 + free + %22m%22);%0A%09%09assertTrue(%22Problems found (%22 + top10.size() + %22 from %22 + errors.size() + %22): %22 + join(errors, %22, %22),%0A%09%09%09%09errors.isEmpty()
+ + %22m, free=%22 + Runtime.getRuntime().freeMemory()%0A%09%09%09%09/ (1024 * 1024) + %22m%22
 );%0A%09

reset jdt index before running the test build