/* 
 * Mutability Detector
 *
 * Copyright 2009 Graham Allan
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * 		http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.mutabilitydetector;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

import org.mutabilitydetector.IAnalysisSession.AnalysisError;
import org.mutabilitydetector.IAnalysisSession.IsImmutable;

public class ImmutableAssert {

	public static void assertImmutable(IsImmutable result) {
		String failString = "Expected Immutable result.%n";
		assertEquals(failString, IsImmutable.DEFINITELY, result);
	}
	
	public static void assertImmutable(AnalysisResult result) {
		String failString = "Expected Immutable result.%n";
		failString += result.reasons.toString();
		assertEquals(failString, IsImmutable.DEFINITELY, result.isImmutable);
	}
	
	public static void assertDefinitelyNotImmutable(IsImmutable result) {
		assertEquals("Expected Not Immutable result.", IsImmutable.DEFINITELY_NOT, result);
	}

	public static void assertDefinitelyNotImmutable(AnalysisResult result) {
		assertEquals("Expected Not Immutable result.", IsImmutable.DEFINITELY_NOT, result.isImmutable);
	}
	
	public static void assertNotImmutable(IsImmutable result) {
		String error = "Expected any result but Immutable. %nActual: " + result.name();
		assertFalse(error, IsImmutable.DEFINITELY.equals(result));
		
	}
	
	public static void assertIsImmutableResult(IsImmutable expected, IsImmutable actual) {
		assertEquals(expected, actual);
	}
	
	public static void assertIsImmutableResult(IsImmutable expected, AnalysisResult actualResult) {
		String error = actualResult.reasons.toString();
		assertEquals(error, expected, actualResult.isImmutable);
		
	}
	
	public static void assertNotImmutable(Class<?> toAnalyse) {
		doAssertion(toAnalyse, IsImmutable.DEFINITELY_NOT, true);
	}

	public static void assertImmutable(Class<?> toAnalyse) {
		doAssertion(toAnalyse, IsImmutable.DEFINITELY, true);
	}

	public static void assertMaybeImmutable(Class<?> toAnalyse) {
		doAssertion(toAnalyse, IsImmutable.MAYBE, true);
	}

	private static void doAssertion(Class<?> toAnalyse, IsImmutable expected, boolean printReasons) {
		IAnalysisSession session = AnalysisSession.createWithCurrentClassPath();
		AnalysisResult result = session.resultFor(toAnalyse.getName());
		
		if(printReasons) {
			for (AnalysisError error : session.getErrors()) {
				System.err.printf("Analysis error running checker=[%s] on class=[%s.class]:%n%s%n", 
								      error.checkerName, error.onClass, error.description);
			}
		}
		String failure = "Exception " + toAnalyse.getName() + " is expected to be " + expected + " immutable.";
		if (printReasons) {
			failure += TestUtil.formatReasons(result.reasons);
		}
		assertEquals(failure, expected, result.isImmutable);

	}
}

@@ -738,16 +738,176 @@
 rtFalse;
+%0Aimport static org.mutabilitydetector.IAnalysisSession.IsImmutable.DEFINITELY;%0Aimport static org.mutabilitydetector.IAnalysisSession.IsImmutable.DEFINITELY_NOT;
 %0A%0Aimport
@@ -1299,142 +1299,44 @@
 %7B%0A%09%09
-String failString = %22Expected Immutable result.%25n%22;%0A%09%09failString += result.reasons.toString();%0A%09%09assertEquals(failString, IsImmutable.
+doAssertion(result.dottedClassName, 
 DEFI
@@ -1345,35 +1345,29 @@
 TELY, result
-.isImmutabl
+, tru
 e);%0A%09%7D%0A%09%0A%09pu
@@ -1604,67 +1604,44 @@
 %7B%0A%09%09
-a
+doA
 ssert
-Equals(%22Expected Not Immutable result.%22, IsImmutable.
+ion(result.dottedClassName, 
 DEFI
@@ -1654,35 +1654,29 @@
 _NOT, result
-.isImmutabl
+, tru
 e);%0A%09%7D%0A%09%0A%09pu
@@ -2760,16 +2760,58 @@
 Path();%0A
+%09%09String className = toAnalyse.getName();%0A
 %09%09Analys
@@ -2846,35 +2846,25 @@
 sultFor(
-toAnalyse.get
+class
 Name
-()
 );%0A%09%09%0A%09%09
@@ -3101,16 +3101,202 @@
 %09%09%7D%0A%09%09%7D%0A
+%09%09%0A%09%09doAssertion(className, expected, result, printReasons);%0A%0A%09%7D%0A%0A%09private static void doAssertion(String className, IsImmutable expected, AnalysisResult actual, boolean printReasons) %7B%0A
 %09%09String
@@ -3321,35 +3321,25 @@
 ion %22 + 
-toAnalyse.get
+class
 Name
-()
  + %22 is 
@@ -3418,16 +3418,23 @@
 ilure +=
+ %22%5Cn%22 +
  TestUti
@@ -3449,22 +3449,22 @@
 Reasons(
-result
+actual
 .reasons
@@ -3504,22 +3504,22 @@
 pected, 
-result
+actual
 .isImmut
@@ -3525,14 +3525,17 @@
 table);%0A
-%0A
 %09%7D
+%0A%09%0A%09
 %0A%7D%0A

Make assertion calls more consistent.