package org.conscrypt.metrics;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class OptionalMethodTest {

    @Test
    public void workingMethod() {
        OptionalMethod substring =
                new OptionalMethod(String.class, "substring", int.class, int.class);
        assertNotNull(substring);

        assertEquals("put", substring.invoke("input", 2, 5));
    }

    @Test
    public void nullClass() {
        OptionalMethod substring =
                new OptionalMethod(null, "substring", int.class, int.class);
        assertNotNull(substring);

        assertNull(substring.invoke("input", 2, 5));
    }

    @Test(expected = NullPointerException.class)
    public void nullMethodName() {
        new OptionalMethod(String.class, null, int.class, int.class);
    }

    @Test
    public void nullArgumentClasses() {
        OptionalMethod substring = new OptionalMethod(String.class, "substring", int.class, null);
        assertNotNull(substring);

        assertNull(substring.invoke("input", 2, 5));
    }

    @Test
    public void noSuchMethodName() {
        OptionalMethod subwrong =
                new OptionalMethod(null, "subwrong", int.class, int.class);
        assertNotNull(subwrong);

        assertNull(subwrong.invoke("input", 2, 5));
    }

    @Test
    public void noSuchMethodArgs() {
        OptionalMethod subwrong =
                new OptionalMethod(null, "substring", long.class, byte[].class);
        assertNotNull(subwrong);

        assertNull(subwrong.invoke("input", 2, 5));
    }
}

@@ -158,16 +158,61 @@
 ertNull;
+%0Aimport static org.junit.Assert.assertThrows;
 %0A%0Aimport
@@ -881,19 +881,77 @@
 Test
-(expected =
+%0A    public void nullMethodName() %7B%0A        assertThrows(%0A           
  Nul
@@ -977,52 +977,27 @@
 lass
-)
+,
 %0A    
-public void nullMethodName() %7B%0A
        
+ () -%3E
  new
@@ -1049,24 +1049,25 @@
 , int.class)
+)
 ;%0A    %7D%0A%0A   

Use assertThrows() in OptionalMethodTest (#1058)