1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package testprogress2; 18 19 import com.sun.javadoc.AnnotationDesc; 20 import com.sun.javadoc.AnnotationValue; 21 import com.sun.javadoc.ClassDoc; 22 import com.sun.javadoc.AnnotationDesc.ElementValuePair; 23 24 import java.util.ArrayList; 25 import java.util.List; 26 27 /** 28 * represents a list of testtargets annotations all belonging to one test method 29 * / one testclass together with its processed information. 30 */ 31 public class TestMethodInformation { 32 private boolean annotationExists = false; 33 34 private List<TestTargetNew> targets = new ArrayList<TestTargetNew>(); 35 36 private String error = null; 37 38 private Color color = Color.RED; 39 40 public enum Level { 41 TODO, PARTIAL, PARTIAL_COMPLETE, COMPLETE, ADDITIONAL, NOT_NECESSARY, NOT_FEASIBLE, SUFFICIENT 42 } 43 44 public enum Color { 45 GREEN /* ready */, YELLOW /* work */, RED 46 /* missing essential stuff */ 47 } 48 49 public TestMethodInformation(Originator originator, 50 AnnotationDesc[] annots, ClassDoc targetClass) { 51 // System.out.println("looking at "+testMethodDoc); 52 if (targetClass == null) { 53 addError("target class annotation missing!"); 54 return; 55 } 56 57 for (AnnotationDesc annot : annots) { 58 if (annot.annotationType().qualifiedName().equals( 59 "dalvik.annotation.TestTargets")) { 60 // multi target case 61 annotationExists = true; 62 ElementValuePair[] pairs = annot.elementValues(); 63 if (pairs.length != 1 64 && !pairs[0].element().qualifiedName().equals( 65 "dalvik.annotation.TestTargets.value")) { 66 throw new RuntimeException("TestTargets has mismatched " 67 + "attributes"); 68 } 69 AnnotationValue[] targets = (AnnotationValue[])pairs[0].value() 70 .value(); 71 for (AnnotationValue ttn : targets) { 72 // the test targets must be annotations themselves 73 AnnotationDesc ttnd = (AnnotationDesc)ttn.value(); 74 handleTestTargetNew(originator, ttnd, targetClass); 75 } 76 } else if (annot.annotationType().qualifiedName().equals( 77 "dalvik.annotation.TestTargetNew")) { 78 // singular case 79 annotationExists = true; 80 handleTestTargetNew(originator, annot, targetClass); 81 } // else some other annotation - ignore 82 } 83 84 boolean targetsCorrect = true; 85 for (TestTargetNew ttn : targets) { 86 targetsCorrect &= (ttn.getTargetMethod() != null || ttn 87 .getTargetClass() != null); 88 } 89 90 // calculate color of test method 91 if (annotationExists) { 92 if (targetsCorrect) { 93 color = Color.GREEN; 94 } // else incorrect targets 95 } else { 96 addError("no annotation!"); 97 } 98 } 99 100 private void handleTestTargetNew(Originator originator, AnnotationDesc ttn, 101 ClassDoc targetClass) { 102 TestTargetNew testTarget = new TestTargetNew(originator, ttn, 103 targetClass); 104 if (testTarget.isHavingProblems()) { 105 // add to overall message 106 addError(testTarget.getNotes()); 107 } 108 targets.add(testTarget); 109 } 110 111 private void addError(String err) { 112 if (error == null) 113 error = ""; 114 error += err + " ; "; 115 } 116 117 /** 118 * @return the error 119 */ 120 public String getError() { 121 return error; 122 } 123 124 public List<TestTargetNew> getTargets() { 125 return targets; 126 } 127 128 public Color getColor() { 129 return color; 130 } 131 132 } 133