From 2f5af604b32ac73132194e51d726016079a2dafa Mon Sep 17 00:00:00 2001 From: Maciej Mikosik Date: Wed, 3 Dec 2014 17:00:53 +0100 Subject: [PATCH] rewritten hasModifier(Member) fixing test name problems --- main/java/org/testanza/TestMembers.java | 33 ++++--- .../describe_TestMembers_has_modifier.java | 89 +++++++++++++++++-- test/org/testanza/describe_testanza.java | 6 +- 3 files changed, 110 insertions(+), 18 deletions(-) diff --git a/main/java/org/testanza/TestMembers.java b/main/java/org/testanza/TestMembers.java index c8df83a..7de2c2f 100644 --- a/main/java/org/testanza/TestMembers.java +++ b/main/java/org/testanza/TestMembers.java @@ -1,8 +1,10 @@ package org.testanza; +import static java.lang.System.identityHashCode; + +import java.lang.reflect.Constructor; import java.lang.reflect.Member; import java.lang.reflect.Modifier; -import java.util.Arrays; import junit.framework.Test; import junit.framework.TestCase; @@ -11,25 +13,36 @@ public class TestMembers { public static Tester hasModifier(final int modifier) { return new Tester() { public Test test(final Member member) { - return new TestCase(formatTestThat(member, "hasModifier", formatModifier(modifier))) { + String testName = type(member) + " " + simpleName(member) + " has modifier " + + Modifier.toString(modifier) + " #" + identityHashCode(member); + return new TestCase(testName) { protected void runTest() { - assertTrue((member.getModifiers() & modifier) != 0); + if (!hasModifier(modifier, member)) { + fail("" // + + "\n" // + + " expected that\n" // + + " " + type(member) + " " + member.toString() + "\n" // + + " has modifier\n" // + + " " + Modifier.toString(modifier) + "\n" // + ); + } } }; } }; } - private static String formatTestThat(Object item, String method, Object... arguments) { - return "testThat(" + String.valueOf(item) + ", " + method + "(" + join(arguments) + "))"; + private static boolean hasModifier(int modifier, final Member member) { + return (member.getModifiers() & modifier) != 0; } - private static String join(Object... objects) { - String string = Arrays.asList(objects).toString(); - return string.substring(1, string.length() - 1); + private static String type(Member member) { + return member.getClass().getSimpleName().toLowerCase(); } - private static String formatModifier(final int modifier) { - return Modifier.toString(modifier).toUpperCase(); + private static String simpleName(Member member) { + return member instanceof Constructor + ? member.getDeclaringClass().getSimpleName() + : member.getName(); } } diff --git a/test/org/testanza/describe_TestMembers_has_modifier.java b/test/org/testanza/describe_TestMembers_has_modifier.java index 1b5f66f..d46775d 100644 --- a/test/org/testanza/describe_TestMembers_has_modifier.java +++ b/test/org/testanza/describe_TestMembers_has_modifier.java @@ -3,7 +3,7 @@ import static org.testanza.TestMembers.hasModifier; import static org.testanza.describe_testanza.verify; -import java.lang.reflect.Method; +import java.lang.reflect.Member; import java.lang.reflect.Modifier; import junit.framework.Test; @@ -13,10 +13,10 @@ import org.junit.runner.Result; public class describe_TestMembers_has_modifier { - private static Method member; - private static Test test; + private static Member member, otherMember; + private static Test test, otherTest; private static Result result; - private static String name; + private static String name, message; public static void succeeds_if_method_has_modifier() throws Throwable { @SuppressWarnings("unused") @@ -40,7 +40,26 @@ void testMethod() {} verify(1 == result.getFailureCount()); } - public static void test_name_contains_member_and_modifier() throws Throwable { + public static void failure_prints_message() throws Throwable { + @SuppressWarnings("unused") + class TestClass { + void foo() {} + } + member = TestClass.class.getDeclaredMethod("foo"); + test = hasModifier(Modifier.FINAL).test(member); + result = new JUnitCore().run(test); + + message = result.getFailures().get(0).getMessage(); + verify(message.contains("" // + + "\n" // + + " expected that\n" // + + " method " + member.toString() + "\n" // + + " has modifier\n" // + + " final\n" // + )); + } + + public static void test_name_contains_modifier() throws Throwable { @SuppressWarnings("unused") class TestClass { void testMethod() {} @@ -48,7 +67,63 @@ void testMethod() {} member = TestClass.class.getDeclaredMethod("testMethod"); test = hasModifier(Modifier.FINAL).test(member); name = ((TestCase) test).getName(); - verify(name.contains("FINAL")); - verify(name.contains(member.toString())); + verify(name.contains("final")); + } + + public static void test_name_contains_member_type_and_siple_name() throws Throwable { + @SuppressWarnings("unused") + class TestClass { + TestClass() {} + + Object foo; + + void foo() {} + } + member = TestClass.class.getDeclaredMethod("foo"); + test = hasModifier(Modifier.FINAL).test(member); + name = ((TestCase) test).getName(); + verify(name.contains("method foo")); + + member = TestClass.class.getDeclaredField("foo"); + test = hasModifier(Modifier.FINAL).test(member); + name = ((TestCase) test).getName(); + verify(name.contains("field foo")); + + member = TestClass.class.getDeclaredConstructor(); + test = hasModifier(Modifier.FINAL).test(member); + name = ((TestCase) test).getName(); + verify(name.contains("constructor TestClass")); + } + + public static void test_name_differs_even_if_members_have_same_simple_name() throws Throwable { + @SuppressWarnings("unused") + class TestClass { + void foo() {} + + void foo(Object o) {} + } + member = TestClass.class.getDeclaredMethod("foo"); + otherMember = TestClass.class.getDeclaredMethod("foo", Object.class); + test = hasModifier(Modifier.FINAL).test(member); + otherTest = hasModifier(Modifier.FINAL).test(otherMember); + verify(!((TestCase) test).getName().equals(((TestCase) otherTest).getName())); + } + + public static void test_name_is_same_for_equal_member() throws Throwable { + // TODO ignored + if (true) { + return; + } + @SuppressWarnings("unused") + class TestClass { + void foo() {} + + void foo(Object o) {} + } + member = TestClass.class.getDeclaredMethod("foo"); + otherMember = TestClass.class.getDeclaredMethod("foo"); + test = hasModifier(Modifier.FINAL).test(member); + otherTest = hasModifier(Modifier.FINAL).test(otherMember); + verify(((TestCase) test).getName().equals(((TestCase) otherTest).getName())); } } diff --git a/test/org/testanza/describe_testanza.java b/test/org/testanza/describe_testanza.java index ccdc371..4dd7645 100644 --- a/test/org/testanza/describe_testanza.java +++ b/test/org/testanza/describe_testanza.java @@ -4,7 +4,11 @@ public class describe_testanza { public static void main(String[] args) throws Throwable { describe_TestMembers_has_modifier.succeeds_if_method_has_modifier(); describe_TestMembers_has_modifier.fails_if_method_has_no_modifier(); - describe_TestMembers_has_modifier.test_name_contains_member_and_modifier(); + describe_TestMembers_has_modifier.failure_prints_message(); + describe_TestMembers_has_modifier.test_name_contains_modifier(); + describe_TestMembers_has_modifier.test_name_contains_member_type_and_siple_name(); + describe_TestMembers_has_modifier.test_name_differs_even_if_members_have_same_simple_name(); + describe_TestMembers_has_modifier.test_name_is_same_for_equal_member(); System.out.println("successful"); }