| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.test.suitebuilder; |
| 18 |
|
| 19 |
import com.android.internal.util.Predicate; |
| 20 |
import junit.framework.TestCase; |
| 21 |
|
| 22 |
import java.lang.annotation.ElementType; |
| 23 |
import java.lang.annotation.Retention; |
| 24 |
import java.lang.annotation.RetentionPolicy; |
| 25 |
import java.lang.annotation.Target; |
| 26 |
import java.lang.reflect.Method; |
| 27 |
|
| 28 |
public class TestPredicatesTest extends TestCase { |
| 29 |
|
| 30 |
public void testThatMethodWithAnnotationIsReportedAsBeingAnnotated() throws Exception { |
| 31 |
assertTrue(hasExampleAnnotation(ClassWithAnnotation.class, "testWithAnnotation")); |
| 32 |
assertTrue(hasExampleAnnotation(ClassWithoutAnnotation.class, "testWithAnnotation")); |
| 33 |
} |
| 34 |
|
| 35 |
public void testThatMethodWithOutAnnotationIsNotReportedAsBeingAnnotated() throws Exception { |
| 36 |
assertFalse(hasExampleAnnotation(ClassWithoutAnnotation.class, "testWithoutAnnotation")); |
| 37 |
} |
| 38 |
|
| 39 |
public void testThatClassAnnotatioCausesAllMethodsToBeReportedAsBeingAnnotated() |
| 40 |
throws Exception { |
| 41 |
assertTrue(hasExampleAnnotation(ClassWithAnnotation.class, "testWithoutAnnotation")); |
| 42 |
} |
| 43 |
|
| 44 |
private boolean hasExampleAnnotation(Class<? extends TestCase> aClass, String methodName) |
| 45 |
throws NoSuchMethodException { |
| 46 |
Method method = aClass.getMethod(methodName); |
| 47 |
TestMethod testMethod = new TestMethod(method, aClass); |
| 48 |
return TestPredicates.hasAnnotation(Example.class).apply(testMethod); |
| 49 |
} |
| 50 |
|
| 51 |
@Retention(RetentionPolicy.RUNTIME) |
| 52 |
@Target({ElementType.TYPE, ElementType.METHOD}) |
| 53 |
public @interface Example { |
| 54 |
} |
| 55 |
|
| 56 |
@Example |
| 57 |
static class ClassWithAnnotation extends TestCase { |
| 58 |
|
| 59 |
@Example |
| 60 |
public void testWithAnnotation() { |
| 61 |
} |
| 62 |
|
| 63 |
public void testWithoutAnnotation() { |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
static class ClassWithoutAnnotation extends TestCase { |
| 68 |
|
| 69 |
@Example |
| 70 |
public void testWithAnnotation() { |
| 71 |
} |
| 72 |
|
| 73 |
public void testWithoutAnnotation() { |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
private static final Predicate<Object> TRUE = new Predicate<Object>() { |
| 78 |
public boolean apply(Object o) { |
| 79 |
return true; |
| 80 |
} |
| 81 |
}; |
| 82 |
|
| 83 |
private static final Predicate<Object> FALSE = new Predicate<Object>() { |
| 84 |
public boolean apply(Object o) { |
| 85 |
return false; |
| 86 |
} |
| 87 |
}; |
| 88 |
|
| 89 |
public void testNotPredicate() throws Exception { |
| 90 |
assertTrue(TestPredicates.not(FALSE).apply(null)); |
| 91 |
assertFalse(TestPredicates.not(TRUE).apply(null)); |
| 92 |
} |
| 93 |
} |