1 /*
   2  * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation. Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  */
  24 
  25 package client.test.runner;
  26 
  27 import client.test.CheckUI;
  28 import client.test.Keywords;
  29 import client.test.RunUI;
  30 import java.io.File;
  31 import java.io.IOException;
  32 import java.lang.annotation.Annotation;
  33 import java.lang.reflect.Method;
  34 import java.util.ArrayList;
  35 import java.util.HashMap;
  36 import java.util.List;
  37 import java.util.regex.Matcher;
  38 import java.util.regex.Pattern;
  39 
  40 /**
  41  * @author shura
  42  */
  43 public class RunUITestFinder extends AbstractTestFinder {
  44 
  45     static final String TEST_NAME = "unit.test.testname";
  46     static final String UNIT_TEST_CLASS_NAME = "unit.test.classname";
  47     static final String NO_DESCRIPTION = "unit.test.nodescription";
  48     static final String HAS_CHECK_UI = "unit.test.has.check.ui";
  49     static final String TYPE_JUNIT = "unit.test.type.junit";
  50     private static boolean excludeNoDesc = false;
  51     private static boolean excludeDesc = false;
  52     protected final static String testRoot;
  53 
  54     static {
  55         String tr = null;
  56         try {
  57             tr = new File(System.getProperty("client.test.root")).getCanonicalPath() + File.separator;
  58         } catch (IOException ex) {
  59             ex.printStackTrace();
  60         }
  61         testRoot = tr;
  62         excludeNoDesc = Boolean.getBoolean("client.exclude.nodesc");
  63         excludeDesc = Boolean.getBoolean("client.exclude.desc");
  64     }
  65 
  66     /**
  67      *
  68      */
  69     public RunUITestFinder() {
  70         super(JAVA_EXT);
  71     }
  72 
  73     @Override
  74     protected void addAttributes(HashMap tagValues) {
  75     }
  76 
  77     private static String calcHtmlFileName(String javaFileName, String htmlRelName) {
  78         return new File(javaFileName).getParent() + File.separator + htmlRelName;
  79     }
  80 
  81     @Override
  82     public void scan(File file) {
  83         if (file.isDirectory()) {
  84             super.scan(file);
  85         } else {
  86             try {
  87                 Class testClass = fileToClass(file);
  88                 if (excludeTestClass(testClass)) {
  89                     return;
  90                 }
  91                 List<RunUI> runUIs = findAnnotations(testClass, RunUI.class);
  92                 boolean hasCheckUI = findAnnotations(testClass, CheckUI.class).size() > 0;
  93                 List<String> resources = new ArrayList<String>(runUIs.size());
  94                 List<Keywords> keys = findAnnotations(testClass, Keywords.class);
  95                 if (!runUIs.isEmpty()) {
  96                     boolean nodescription = false;
  97                     for (RunUI runUI : runUIs) {
  98                         if (runUI.noDescription()) {
  99                             nodescription = true;
 100                             break;
 101                         }
 102                         String value = runUI.value();
 103                         if (value != null) {
 104                             if (value.length() == 0) {
 105                                 value = testClass.getSimpleName() + ".html";
 106                             }
 107                             resources.add(value);
 108                         }
 109                     }
 110 
 111                     if (nodescription && !excludeNoDesc
 112                             || !nodescription && !excludeDesc) {
 113                         String rootDir = getRootDir().getAbsolutePath();
 114                         String filePath = file.getAbsolutePath().substring(rootDir.length() + 1, file.getAbsolutePath().length());
 115                         String path = file.getPath();
 116                         HashMap tagValues = new HashMap();
 117 
 118                         // Keywords processing
 119                         String key_str = null;
 120                         if (!keys.isEmpty())
 121                         {
 122                             for (Keywords key : keys) {
 123                                 key_str = (key_str==null?key.keywords():" " + key.keywords());
 124                             }
 125                             tagValues.put("keywords", key_str);
 126                             System.out.println("The following keywords \"" + key_str + "\" were found in the test " + file.getName());
 127                         }
 128 
 129                         if (nodescription) {
 130                             tagValues.put("testName", removeDotJavaExtensionFromFilePath(filePath));
 131                             tagValues.put(FILE_PATH_PROP, path);
 132                             tagValues.put(UNIT_TEST_CLASS_NAME, testClass.getName());
 133                             tagValues.put(NO_DESCRIPTION, Boolean.TRUE.toString());
 134                             tagValues.put(HAS_CHECK_UI, "false");
 135 
 136                             foundTestDescription(tagValues, new File(filePath), 0);
 137 
 138                         } else {
 139                             for (String html : resources) {
 140                                 String htmlFile = calcHtmlFileName(path, html);
 141                                 if (!new File(htmlFile).exists()) {
 142                                     error("Html resource '" + htmlFile + "' doesn't exists.");
 143                                     continue;
 144                                 }
 145 
 146                                 if (resources.size() > 1) {
 147                                     tagValues.put("id", html);
 148                                 }
 149                                 tagValues.put("testName", removeDotJavaExtensionFromFilePath(filePath));
 150                                 tagValues.put(FILE_PATH_PROP, path);
 151 
 152                                 tagValues.put(TEST_NAME, html);
 153                                 tagValues.put(INSTR_FILE_PATH_PROP, htmlFile);
 154                                 tagValues.put(UNIT_TEST_CLASS_NAME, testClass.getName());
 155                                 tagValues.put(NO_DESCRIPTION, Boolean.FALSE.toString());
 156                                 tagValues.put(HAS_CHECK_UI, Boolean.toString(hasCheckUI));
 157 
 158                                 foundTestDescription(tagValues, new File(filePath), 0);
 159                             }
 160                         }
 161                     }
 162                 }
 163             } catch (ClassNotFoundException e) {
 164                 error(e.getMessage());
 165                 e.printStackTrace();
 166             }
 167         }
 168     }
 169 
 170     @Override
 171     protected boolean acceptFile(File file) {
 172         throw new UnsupportedOperationException("acceptFile() shouldn't be called");
 173     }
 174     static final Pattern SLASHES = Pattern.compile("[\\/\\\\]");
 175 
 176     /**
 177      *
 178      * @param fl
 179      * @return
 180      * @throws ClassNotFoundException
 181      */
 182     public static Class fileToClass(File fl) throws ClassNotFoundException {
 183         try {
 184             String flPath = fl.getCanonicalPath();
 185 
 186             String clazz = flPath.substring(testRoot.length(), flPath.length() - JAVA_EXT.length());
 187             Matcher matcher = SLASHES.matcher(clazz);
 188             if (matcher.find()) {
 189                 clazz = matcher.replaceAll("\\.");
 190             }
 191             return Class.forName(clazz);
 192         } catch (IOException ex) {
 193             ex.printStackTrace();
 194         }
 195         return null;
 196     }
 197 
 198     static <T extends Annotation> List<T> findAnnotations(Class testClass, Class<T> type) {
 199         List<T> list = new ArrayList<T>();
 200         for (Method m : testClass.getMethods()) {
 201             //used to be m.isAnnotationPresent(type) instead of m.getAnnotation(type) != null
 202             if (m.getAnnotation(type) != null) {
 203                 list.add(m.getAnnotation(type));
 204             }
 205         }
 206         return list;
 207     }
 208 
 209     private void error(String st) {
 210         ErrorHandler eh = getErrorHandler();
 211         if (eh != null) {
 212             eh.error(st);
 213         } else {
 214             // being run in standalone mode
 215             System.err.println(st);
 216         }
 217     }
 218 }