1 /* 2 * Copyright (C) 2011 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 package com.android.cts.javascannerdoclet; 17 18 import java.io.BufferedWriter; 19 import java.io.File; 20 import java.io.FileNotFoundException; 21 import java.io.FileOutputStream; 22 import java.io.FileWriter; 23 import java.io.IOException; 24 import java.io.OutputStream; 25 import java.io.PrintWriter; 26 import java.util.ArrayList; 27 import java.util.Collection; 28 import java.util.Iterator; 29 30 import javax.xml.parsers.DocumentBuilderFactory; 31 import javax.xml.parsers.ParserConfigurationException; 32 import javax.xml.transform.Transformer; 33 import javax.xml.transform.TransformerException; 34 import javax.xml.transform.TransformerFactory; 35 import javax.xml.transform.TransformerFactoryConfigurationError; 36 import javax.xml.transform.dom.DOMSource; 37 import javax.xml.transform.stream.StreamResult; 38 39 import org.w3c.dom.Attr; 40 import org.w3c.dom.Document; 41 import org.w3c.dom.Node; 42 import org.w3c.dom.NodeList; 43 44 import com.sun.javadoc.AnnotationDesc; 45 import com.sun.javadoc.AnnotationTypeDoc; 46 import com.sun.javadoc.AnnotationValue; 47 import com.sun.javadoc.ClassDoc; 48 import com.sun.javadoc.Doclet; 49 import com.sun.javadoc.MethodDoc; 50 import com.sun.javadoc.RootDoc; 51 import com.sun.javadoc.AnnotationDesc.ElementValuePair; 52 import com.sun.javadoc.AnnotationTypeElementDoc; 53 54 /** 55 * Doclet that outputs in the following format: 56 * 57 * suite:android.holo.cts 58 * case:HoloTest 59 * test:testHolo 60 * test:testHoloDialog[:timeout_value] 61 */ 62 public class CtsJavaScannerDoclet extends Doclet { 63 64 static final String JUNIT_TEST_CASE_CLASS_NAME = "junit.framework.testcase"; 65 66 public static boolean start(RootDoc root) { 67 ClassDoc[] classes = root.classes(); 68 if (classes == null) { 69 return false; 70 } 71 72 PrintWriter writer = new PrintWriter(System.out); 73 74 for (ClassDoc clazz : classes) { 75 if (clazz.isAbstract() || !isValidJUnitTestCase(clazz)) { 76 continue; 77 } 78 writer.append("suite:").println(clazz.containingPackage().name()); 79 writer.append("case:").println(clazz.name()); 80 for (; clazz != null; clazz = clazz.superclass()) { 81 for (MethodDoc method : clazz.methods()) { 82 if (!method.name().startsWith("test")) { 83 continue; 84 } 85 int timeout = -1; 86 AnnotationDesc[] annotations = method.annotations(); 87 for (AnnotationDesc annot : annotations) { 88 AnnotationTypeDoc atype = annot.annotationType(); 89 if (atype.toString().equals("com.android.cts.util.TimeoutReq")) { 90 ElementValuePair[] cpairs = annot.elementValues(); 91 for (ElementValuePair pair: cpairs) { 92 AnnotationTypeElementDoc elem = pair.element(); 93 AnnotationValue value = pair.value(); 94 if (elem.name().equals("minutes")) { 95 timeout = ((Integer)value.value()); 96 } 97 } 98 } 99 } 100 writer.append("test:"); 101 if (timeout >= 0) { 102 writer.append(method.name()).println(":" + timeout); 103 } else { 104 writer.println(method.name()); 105 } 106 } 107 } 108 } 109 110 writer.close(); 111 return true; 112 } 113 114 private static boolean isValidJUnitTestCase(ClassDoc clazz) { 115 while((clazz = clazz.superclass()) != null) { 116 if (JUNIT_TEST_CASE_CLASS_NAME.equals(clazz.qualifiedName().toLowerCase())) { 117 return true; 118 } 119 } 120 return false; 121 } 122 } 123