package org.junit.internal;

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

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

public class MethodSorterTest {
    private static final String ALPHA = "java.lang.Object alpha(int,double,java.lang.Thread)";
    private static final String BETA = "void beta(int[][])";
    private static final String GAMMA_VOID = "int gamma()";
    private static final String GAMMA_BOOLEAN = "void gamma(boolean)";
    private static final String DELTA = "void delta()";
    private static final String EPSILON = "void epsilon()";
    private static final String SUPER_METHOD = "void superMario()";
    private static final String SUB_METHOD = "void subBowser()";

    static class DummySortWithoutAnnotation {
        Object alpha(int i, double d, Thread t) {
            return null;
        }

        void beta(int[][] x) {
        }

        int gamma() {
            return 0;
        }

        void gamma(boolean b) {
        }

        void delta() {
        }

        void epsilon() {
        }
    }

    static class Super {
        void superMario() {
        }
    }

    static class Sub extends Super {
        void subBowser() {
        }
    }

    private List<String> getDeclaredMethodNames(Class<?> clazz) {
        Method[] actualMethods = MethodSorter.getDeclaredMethods(clazz);

        // Obtain just the names instead of the full methods.
        List<String> names = new ArrayList<String>();
        for (Method m : actualMethods) {
            // Filter out synthetic methods from, e.g., coverage tools.
            if (!m.isSynthetic()) {
                names.add(m.toString().replace(clazz.getName() + '.', ""));
        	}
        }
        
        return names;
    }

    @Test
    public void testMethodsNullSorterSelf() {
        List<String> expected = Arrays.asList(EPSILON, BETA, ALPHA, DELTA, GAMMA_VOID, GAMMA_BOOLEAN);
        List<String> actual = getDeclaredMethodNames(DummySortWithoutAnnotation.class);
        assertEquals(expected, actual);
    }
    
    @Test
    public void testMethodsNullSorterSuper() {
        List<String> expected = Arrays.asList(SUPER_METHOD);
        List<String> actual = getDeclaredMethodNames(Super.class);
        assertEquals(expected, actual);
    }
    
    @Test
    public void testMethodsNullSorterSub() {
        List<String> expected = Arrays.asList(SUB_METHOD);
        List<String> actual = getDeclaredMethodNames(Sub.class);
        assertEquals(expected, actual);
    }

    @FixMethodOrder(MethodSorters.DEFAULT)
    static class DummySortWithDefault {
        Object alpha(int i, double d, Thread t) {
            return null;
        }

        void beta(int[][] x) {
        }

        int gamma() {
            return 0;
        }

        void gamma(boolean b) {
        }

        void delta() {
        }

        void epsilon() {
        }
    }

    @Test
    public void testDefaultMethodSorter() {
        List<String> expected = Arrays.asList(EPSILON, BETA, ALPHA, DELTA, GAMMA_VOID, GAMMA_BOOLEAN);
        List<String> actual = getDeclaredMethodNames(DummySortWithDefault.class);
        assertEquals(expected, actual);
    }

    @FixMethodOrder(MethodSorters.JVM)
    static class DummySortJvm {
        Object alpha(int i, double d, Thread t) {
            return null;
        }

        void beta(int[][] x) {
        }

        int gamma() {
            return 0;
        }

        void gamma(boolean b) {
        }

        void delta() {
        }

        void epsilon() {
        }
    }

    @Test
    public void testJvmMethodSorter() {
        Method[] fromJvmWithSynthetics = DummySortJvm.class.getDeclaredMethods();
        Method[] sorted = MethodSorter.getDeclaredMethods(DummySortJvm.class);
        assertArrayEquals(fromJvmWithSynthetics, sorted);
    }

    @FixMethodOrder(MethodSorters.NAME_ASCENDING)
    static class DummySortWithNameAsc {
        Object alpha(int i, double d, Thread t) {
            return null;
        }

        void beta(int[][] x) {
        }

        int gamma() {
            return 0;
        }

        void gamma(boolean b) {
        }

        void delta() {
        }

        void epsilon() {
        }
    }

    @Test
    public void testAscendingMethodSorter() {
        List<String> expected = Arrays.asList(ALPHA, BETA, DELTA, EPSILON, GAMMA_VOID, GAMMA_BOOLEAN);
        List<String> actual = getDeclaredMethodNames(DummySortWithNameAsc.class);
        assertEquals(expected, actual);
    }

	static class SubWithoutOwnMethods extends Super {
	}

	@Test
	public void providesNoDeclaredMethodsIfAllMethodsAreDeclaredBySuperClass() {
		Method[] declaredMethods = MethodSorter
				.getDeclaredMethods(SubWithoutOwnMethods.class);
		assertEquals("Wrong numer of declared methods.", 0,
				declaredMethods.length);
	}
}

@@ -4697,63 +4697,8 @@
  %7D%0A%0A
-%09static class SubWithoutOwnMethods extends Super %7B%0A%09%7D%0A%0A
 %09@Te
@@ -4742,20 +4742,13 @@
 hods
-IfAllMethods
+Which
 AreD
@@ -4841,28 +4841,18 @@
 ods(
-SubWithoutOwnMethods
+foobar.Sub
 .cla
@@ -4907,17 +4907,17 @@
 hods.%22, 
-0
+1
 ,%0A%09%09%09%09de

Add providesNoDeclaredMethodsWhichAreDeclaredBySuperClass.