/* 
 * 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.benchmarks;

import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mutabilitydetector.IAnalysisSession.IsImmutable.DEFINITELY_NOT;
import static org.mutabilitydetector.ImmutableAssert.assertDefinitelyNotImmutable;
import static org.mutabilitydetector.ImmutableAssert.assertImmutable;
import static org.mutabilitydetector.ImmutableAssert.assertNotImmutable;

import org.junit.Before;
import org.junit.Test;
import org.mutabilitydetector.CheckerRunner;
import org.mutabilitydetector.IAnalysisSession;
import org.mutabilitydetector.checkers.MutableTypeToFieldChecker;




public class MutableTypeToFieldCheckerTest {

	private IAnalysisSession mockSession;
	private MutableTypeToFieldChecker checker;

	@Before
	public void setUp() {
		mockSession = mock(IAnalysisSession.class);
		checker = new MutableTypeToFieldChecker(mockSession);
		
	}
	
	@Test
	public void testCheckerRequestsMutableStatusOfPublishedField() throws Exception {
		when(mockSession.isImmutable(MutableExample.class.getCanonicalName())).thenReturn(DEFINITELY_NOT);
		new CheckerRunner(null).run(checker, MutableByHavingMutableFieldAssigned.class);
		
		verify(mockSession).isImmutable(MutableExample.class.getCanonicalName());
	}
	
	@Test
	public void testCheckerFailsCheckIfAnyFieldsHaveMutableAssignedToThem() throws Exception {
		when(mockSession.isImmutable(MutableExample.class.getCanonicalName())).thenReturn(DEFINITELY_NOT);

		new CheckerRunner(null).run(checker, MutableByHavingMutableFieldAssigned.class);
		
		assertNotImmutable(checker.result());
		assertTrue(checker.reasons().size() > 0);
	}
	
	@Test
	public void testInstanceFieldWhichHasAMutatedArrayIsMutable() throws Exception {
		CheckerRunner.createWithCurrentClasspath().run(checker, MutableByHavingArrayTypeAsField.class);
		assertDefinitelyNotImmutable(checker.result());
	}
	
	@Test
	public void testStaticFieldWhichHasAMutatedArrayIsImmutable() throws Exception {
		CheckerRunner.createWithCurrentClasspath().run(checker, ImmutableWhenArrayFieldIsStatic.class);
		assertImmutable(checker.result());
	}
	
}

@@ -1133,16 +1133,74 @@
 mutable;
+%0Aimport static org.mutabilitydetector.TestUtil.runChecker;
 %0A%0Aimport
@@ -1245,53 +1245,8 @@
 st;%0A
-import org.mutabilitydetector.CheckerRunner;%0A
 impo
@@ -1356,19 +1356,16 @@
 ecker;%0A%0A
-%0A%0A%0A
 public c
@@ -1494,18 +1494,17 @@
 %09@Before
-%0A%09
+ 
 public v
@@ -1619,19 +1619,16 @@
 ssion);%0A
-%09%09%0A
 %09%7D%0A%09%0A%09@T
@@ -1622,34 +1622,33 @@
 on);%0A%09%7D%0A%09%0A%09@Test
-%0A%09
+ 
 public void test
@@ -1647,20 +1647,9 @@
 oid 
-testCheckerR
+r
 eque
@@ -1809,35 +1809,18 @@
 ;%0A%09%09
-new 
+run
 Checker
-Runner(null).run
 (che
@@ -1951,34 +1951,33 @@
 ());%0A%09%7D%0A%09%0A%09@Test
-%0A%09
+ 
 public void test
@@ -1976,20 +1976,9 @@
 oid 
-testCheckerF
+f
 ails
@@ -2148,35 +2148,18 @@
 %0A%0A%09%09
-new 
+run
 Checker
-Runner(null).run
 (che
@@ -2298,34 +2298,33 @@
  0);%0A%09%7D%0A%09%0A%09@Test
-%0A%09
+ 
 public void test
@@ -2323,13 +2323,9 @@
 oid 
-testI
+i
 nsta
@@ -2390,54 +2390,18 @@
 %7B%0A%09%09
+run
 Checker
-Runner.createWithCurrentClasspath().run
 (che
@@ -2507,18 +2507,17 @@
 %09%0A%09@Test
-%0A%09
+ 
 public v
@@ -2524,13 +2524,9 @@
 oid 
-testS
+s
 tati
@@ -2591,54 +2591,18 @@
 %7B%0A%09%09
+run
 Checker
-Runner.createWithCurrentClasspath().run
 (che
@@ -2689,10 +2689,12 @@
 );%0A%09%7D%0A%09%0A
+%09%0A
 %7D%0A

Tidy up test class.