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.AddExports;
  28 import client.test.CheckUI;
  29 import client.test.Keywords;
  30 import client.test.RunUI;
  31 import java.io.File;
  32 import java.io.IOException;
  33 import java.lang.annotation.Annotation;
  34 import java.lang.reflect.Method;
  35 import java.util.ArrayList;
  36 import java.util.HashMap;
  37 import java.util.List;
  38 import java.util.regex.Matcher;
  39 import java.util.regex.Pattern;
  40 import java.util.stream.Collectors;
  41 import java.util.stream.Stream;
  42 
  43 /**
  44  * @author shura
  45  */
  46 public class RunUITestFinder extends AbstractTestFinder {
  47 
  48     static final String ADD_EXPORTS = "unit.test.addExports";
  49     static final String TEST_NAME = "unit.test.testname";
  50     static final String UNIT_TEST_CLASS_NAME = "unit.test.classname";
  51     static final String NO_DESCRIPTION = "unit.test.nodescription";
  52     static final String HAS_CHECK_UI = "unit.test.has.check.ui";
  53     static final String TYPE_JUNIT = "unit.test.type.junit";
  54     private static boolean excludeNoDesc = false;
  55     private static boolean excludeDesc = false;
  56     protected final static String testRoot;
  57 
  58     static {
  59         String tr = null;
  60         try {
  61             tr = new File(System.getProperty("client.test.root")).getCanonicalPath() + File.separator;
  62         } catch (IOException ex) {
  63             ex.printStackTrace();
  64         }
  65         testRoot = tr;
  66         excludeNoDesc = Boolean.getBoolean("client.exclude.nodesc");
  67         excludeDesc = Boolean.getBoolean("client.exclude.desc");
  68     }
  69 
  70     /**
  71      *
  72      */
  73     public RunUITestFinder() {
  74         super(JAVA_EXT);
  75     }
  76 
  77     @Override
  78     protected void addAttributes(HashMap tagValues) {
  79     }
  80 
  81     private static String calcHtmlFileName(String javaFileName, String htmlRelName) {
  82         return new File(javaFileName).getParent() + File.separator + htmlRelName;
  83     }
  84 
  85     @Override
  86     public void scan(File file) {
  87         if (file.isDirectory()) {
  88             super.scan(file);
  89         } else {
  90             try {
  91                 Class testClass = fileToClass(file);
  92                 if (excludeTestClass(testClass)) {
  93                     return;
  94                 }
  95                 List<RunUI> runUIs = findAnnotations(testClass, RunUI.class);
  96                 boolean hasCheckUI = findAnnotations(testClass, CheckUI.class).size() > 0;
  97                 List<String> resources = new ArrayList<String>(runUIs.size());
  98                 List<Keywords> keys = findAnnotations(testClass, Keywords.class);
  99                 List<AddExports> addExportsList = findAnnotations(testClass, AddExports.class);
 100                 if (!runUIs.isEmpty()) {
 101                     boolean nodescription = false;
 102                     for (RunUI runUI : runUIs) {
 103                         if (runUI.noDescription()) {
 104                             nodescription = true;
 105                             break;
 106                         }
 107                         String value = runUI.value();
 108                         if (value != null) {
 109                             if (value.length() == 0) {
 110                                 value = testClass.getSimpleName() + ".html";
 111                             }
 112                             resources.add(value);
 113                         }
 114                     }
 115 
 116                     if (nodescription && !excludeNoDesc
 117                             || !nodescription && !excludeDesc) {
 118                         String rootDir = getRootDir().getAbsolutePath();
 119                         String filePath = file.getAbsolutePath().substring(rootDir.length() + 1, file.getAbsolutePath().length());
 120                         String path = file.getPath();
 121                         HashMap tagValues = new HashMap();
 122 
 123                         // Keywords processing
 124                         String key_str = null;
 125                         if (!keys.isEmpty())
 126                         {
 127                             for (Keywords key : keys) {
 128                                 key_str = (key_str==null?key.keywords():" " + key.keywords());
 129                             }
 130                             tagValues.put("keywords", key_str);
 131                             System.out.println("The following keywords \"" + key_str + "\" were found in the test " + file.getName());
 132                         }
 133 
 134                         // --add-exports value
 135                         if (!addExportsList.isEmpty()) {
 136                             String[] addExportsArray = new String[addExportsList.size()];
 137                             for (int i = 0; i < addExportsList.size(); i++) {
 138                                 addExportsArray[i] = addExportsList.get(i).value();
 139                             }
 140                             String addExports = Stream.of(TestScript.combineAddExports(addExportsArray)).collect(Collectors.joining(" "));
 141                             tagValues.put(ADD_EXPORTS, addExports);
 142                             System.out.println("The following addExports \"" + addExports + "\" were found in the test " + file.getName());
 143                         }
 144 
 145                         if (nodescription) {
 146                             tagValues.put("testName", removeDotJavaExtensionFromFilePath(filePath));
 147                             tagValues.put(FILE_PATH_PROP, path);
 148                             tagValues.put(UNIT_TEST_CLASS_NAME, testClass.getName());
 149                             tagValues.put(NO_DESCRIPTION, Boolean.TRUE.toString());
 150                             tagValues.put(HAS_CHECK_UI, "false");
 151 
 152                             foundTestDescription(tagValues, new File(filePath), 0);
 153 
 154                         } else {
 155                             for (String html : resources) {
 156                                 String htmlFile = calcHtmlFileName(path, html);
 157                                 if (!new File(htmlFile).exists()) {
 158                                     error("Html resource '" + htmlFile + "' doesn't exists.");
 159                                     continue;
 160                                 }
 161 
 162                                 if (resources.size() > 1) {
 163                                     tagValues.put("id", html);
 164                                 }
 165                                 tagValues.put("testName", removeDotJavaExtensionFromFilePath(filePath));
 166                                 tagValues.put(FILE_PATH_PROP, path);
 167 
 168                                 tagValues.put(TEST_NAME, html);
 169                                 tagValues.put(INSTR_FILE_PATH_PROP, htmlFile);
 170                                 tagValues.put(UNIT_TEST_CLASS_NAME, testClass.getName());
 171                                 tagValues.put(NO_DESCRIPTION, Boolean.FALSE.toString());
 172                                 tagValues.put(HAS_CHECK_UI, Boolean.toString(hasCheckUI));
 173 
 174                                 foundTestDescription(tagValues, new File(filePath), 0);
 175                             }
 176                         }
 177                     }
 178                 }
 179             } catch (ClassNotFoundException e) {
 180                 error(e.getMessage());
 181                 e.printStackTrace();
 182             }
 183         }
 184     }
 185 
 186     @Override
 187     protected boolean acceptFile(File file) {
 188         throw new UnsupportedOperationException("acceptFile() shouldn't be called");
 189     }
 190     static final Pattern SLASHES = Pattern.compile("[\\/\\\\]");
 191 
 192     /**
 193      *
 194      * @param fl
 195      * @return
 196      * @throws ClassNotFoundException
 197      */
 198     public static Class fileToClass(File fl) throws ClassNotFoundException {
 199         try {
 200             String flPath = fl.getCanonicalPath();
 201 
 202             String clazz = flPath.substring(testRoot.length(), flPath.length() - JAVA_EXT.length());
 203             Matcher matcher = SLASHES.matcher(clazz);
 204             if (matcher.find()) {
 205                 clazz = matcher.replaceAll("\\.");
 206             }
 207             return Class.forName(clazz);
 208         } catch (IOException ex) {
 209             ex.printStackTrace();
 210         }
 211         return null;
 212     }
 213 
 214     static <T extends Annotation> List<T> findAnnotations(Class testClass, Class<T> type) {
 215         List<T> list = new ArrayList<T>();
 216         for (Method m : testClass.getMethods()) {
 217             //used to be m.isAnnotationPresent(type) instead of m.getAnnotation(type) != null
 218             if (m.getAnnotation(type) != null) {
 219                 list.add(m.getAnnotation(type));
 220             }
 221         }
 222         return list;
 223     }
 224 
 225     private void error(String st) {
 226         ErrorHandler eh = getErrorHandler();
 227         if (eh != null) {
 228             eh.error(st);
 229         } else {
 230             // being run in standalone mode
 231             System.err.println(st);
 232         }
 233     }
 234 }
--- EOF ---