package org.mockito.usage;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

import java.util.List;

import org.junit.*;
import org.mockito.exceptions.*;
import org.mockito.internal.StateResetter;

/**
 * invalid state happens if:
 *    -unfinished stubbing
 *    -unfinished stubVoid
 *    -stubbing without actual method call
 *    -verify without actual method call
 *    
 * we should aim to detect invalid state in following scenarios:
 *    -on method call on mock
 *    -on verify
 *    -on verifyZeroInteractions
 *    -on verifyNoMoreInteractions
 *    -on stub
 *    -on stubVoid
 *    
 * obviously we should consider if it is really important to cover all those naughty usage
 */
@SuppressWarnings("unchecked")
public class InvalidUsageTest {
    
    private List mock;

    @Before
    @After
    public void resetState() {
        StateResetter.reset();
        mock = mock(List.class);
    }
    
    @Test
    public void shouldDetectStubbingWithoutMethodCallOnMock() {
        try {
            stub("blah".contains("blah"));
            fail();
        } catch (MissingMethodInvocationException e) {
            //cool
        }
    }
    
    @Ignore
    @Test
    public void unfinishedStubbingDetectedOnVerify() {
        stub(mock.add("test"));
        
        try {
            verifyZeroInteractions(mock);
            fail();
        } catch (UnfinishedStubbingException e) {}
    }
    
    @Ignore
    @Test
    public void unfinishedStubbingDetectedWhenAnotherStubbingIsStarted() {
        stub(mock.add("test"));
        
        try {
            stub(mock.add("test")).andThrows(new Exception("ssdf"));
            fail();
        } catch (UnfinishedStubbingException e) {}
    }
    
    @Ignore
    @Test
    public void unfinishedStubbingDetectedMockCalled() {
        stub(mock.add("test"));
        
        try {
            mock.clear();
            fail();
        } catch (UnfinishedStubbingException e) {}
    }
    
    @Ignore
    @Test
    public void unfinishedStubbingVoid() {
        stubVoid(mock);
        
        try {
            mock.clear();
            fail();
        } catch (UnfinishedStubbingException e) {}
    }
}

@@ -387,21 +387,17 @@
  call%0A *
-    
 %0A
+
  * we sh
@@ -604,20 +604,16 @@
 bVoid%0A *
-    
 %0A * obvi
@@ -763,21 +763,17 @@
 eTest %7B%0A
-    
 %0A
+
     priv
@@ -908,28 +908,24 @@
 ass);%0A    %7D%0A
-    
 %0A    @Test%0A 
@@ -914,32 +914,81 @@
     %7D%0A%0A    @Test
+(expected=MissingMethodInvocationException.class)
 %0A    public void
@@ -1036,34 +1036,16 @@
 ock() %7B%0A
-        try %7B%0A    
         
@@ -1079,123 +1079,15 @@
 ));%0A
-            fail();%0A        %7D catch (MissingMethodInvocationException e) %7B%0A            //cool%0A        %7D%0A
     %7D%0A
-    
 %0A
+
     
@@ -1187,33 +1187,25 @@
 d(%22test%22));%0A
-        
 %0A
+
         try 
@@ -1317,37 +1317,33 @@
 ion e) %7B%7D%0A    %7D%0A
-    
 %0A
+
     @Ignore%0A    
@@ -1451,33 +1451,25 @@
 d(%22test%22));%0A
-        
 %0A
+
         try 
@@ -1608,37 +1608,33 @@
 ion e) %7B%7D%0A    %7D%0A
-    
 %0A
+
     @Ignore%0A    
@@ -1728,25 +1728,17 @@
 est%22));%0A
-        
 %0A
+
         
@@ -1846,21 +1846,17 @@
 %7D%0A    %7D%0A
-    
 %0A
+
     @Ign
@@ -1936,24 +1936,16 @@
 (mock);%0A
-        
 %0A       

use of @Test(expected) instead of try, fail, catch