/*******************************************************************************
 * Copyright (c) 2009 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.validation;

import static org.eclipse.xtext.junit.validation.AssertableDiagnostics.*;

import java.util.Arrays;
import java.util.List;

import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.xtext.XtextStandaloneSetup;
import org.eclipse.xtext.junit.AbstractXtextTests;
import org.eclipse.xtext.junit.validation.ValidatorTester;

/**
 * @author Moritz Eysholdt - Initial contribution and API
 */
public class ValidatorTesterTest extends AbstractXtextTests {

	protected static class TestingValidator extends AbstractDeclarativeValidator {
		@Check
		public void someError(EAnnotation dummy) {
			error("someError message", dummy, EcorePackage.Literals.EANNOTATION__DETAILS, -1, "101");
		}

		@Check
		public void someWarning(EClass dummy) {
			warning("someWarning message", dummy, EcorePackage.Literals.ECLASS__ABSTRACT, -1, "102");
		}

		@Check
		public void manyMessages(EClass dummy) {
			error("ErrorOne message", dummy, EcorePackage.Literals.ECLASS__ABSTRACT, -1, "103");
			error("ErrorTwo message", dummy, EcorePackage.Literals.ECLASS__ABSTRACT, -1, "104");
			warning("WarningOne message", dummy, EcorePackage.Literals.ECLASS__ABSTRACT, -1, "105");
		}

		@Override
		protected List<EPackage> getEPackages() {
			return Arrays.<EPackage>asList(EcorePackage.eINSTANCE);
		}

		@Override
		public boolean isLanguageSpecific() {
			return false;
		}
	}

	private ValidatorTester<TestingValidator> tester;

	@Override
	public void setUp() throws Exception {
		super.setUp();
		with(XtextStandaloneSetup.class);
		TestingValidator validator = get(TestingValidator.class);
		tester = new ValidatorTester<TestingValidator>(validator, get(EValidatorRegistrar.class));
	}

	public void testError() {
		tester.validator().someError(EcoreFactory.eINSTANCE.createEAnnotation());
		tester.diagnose().assertError("101", "someError");
	}

	public void testError2() {
		tester.validate(EcoreFactory.eINSTANCE.createEAnnotation()).assertError("101", "someError");
	}

	public void testError2Fail() {
		boolean caught = false;
		try {
			tester.validate(EcoreFactory.eINSTANCE.createEAnnotation()).assertError("234242");
		}
		catch (AssertionError err) {
			caught = true;
		}
		assertTrue(caught);
	}

	public void testTwoErrors() {
		tester.validator().manyMessages(EcorePackage.eINSTANCE.getEClass());
		tester.diagnose().assertAll(warning("105", "WarningOne"), error("104", "ErrorTwo"), error("103", "ErrorOne"));
	}

	public void testFail() {
		tester.validator().someError(EcoreFactory.eINSTANCE.createEAnnotation());
		boolean caught = false;
		try {
			tester.diagnose().assertError("12345", "someError");
		}
		catch (AssertionError err) {
			caught = true;
		}
		assertTrue(caught);
	}

	public void testWarning() {
		tester.validator().someWarning(EcorePackage.eINSTANCE.getEClass());
		tester.diagnose().assertWarning("102", "someWarning");
	}

}

@@ -2011,20 +2011,19 @@
 %09return 
-fals
+tru
 e;%0A%09%09%7D%0A%09
@@ -2308,34 +2308,17 @@
  get
-(EValidatorRegistrar.class
+Injector(
 ));%0A

Allow to test language dependent validators with objects without a resource (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=335624)