rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/VMTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 09 Jun 2014 19:17:41 +0200
changeset 1623 d9cdfd8ef694
parent 1054 fef28d5bee88
child 1787 ea12a3bb4b33
permissions -rw-r--r--
Remove the dirty hacks with names. The fix with the compiler should be good enough.
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.vmtest;
    19 
    20 import java.lang.annotation.Annotation;
    21 import java.util.ArrayList;
    22 import java.util.Arrays;
    23 import java.util.List;
    24 import org.apidesign.bck2brwsr.launcher.Launcher;
    25 import org.apidesign.bck2brwsr.vmtest.impl.CompareCase;
    26 import org.testng.annotations.Factory;
    27 
    28 /** A TestNG {@link Factory} that seeks for {@link Compare} and {@link BrwsrTest} annotations
    29  * in provided class and builds set of tests that verify the functionality of <b>Bck2Brwsr</b> 
    30  * based system. Use as:
    31  * <pre>
    32  * {@code @}{@link Factory} public static create() {
    33  *   return @{link VMTest}.{@link #create(java.lang.Class) create}(YourClass.class);
    34  * }</pre>
    35  * where <code>YourClass</code> contains methods annotated with
    36  * {@link Compare} and {@link BrwsrTest} annotations.
    37  * 
    38  * @author Jaroslav Tulach <jtulach@netbeans.org>
    39  */
    40 public final class VMTest {
    41     private final List<Class> classes = new ArrayList<>();
    42     private final List<String> launcher = new ArrayList<>();
    43     private Class<? extends Annotation> annotation = BrwsrTest.class;
    44     
    45     private VMTest() {
    46     }
    47     
    48     /** Inspects <code>clazz</code> and for each method annotated by
    49      * {@link Compare} or {@link BrwsrTest} creates
    50      * instances of tests. 
    51      * <p>
    52      * Each {@link Compare} instance runs the test in different virtual
    53      * machine and at the end they compare the results.
    54      * <p>
    55      * Each {@link BrwsrTest} annotated method is executed once in {@link Launcher started
    56      * browser}.
    57      * 
    58      * @param clazz the class (or classes) to inspect
    59      * @return the set of created tests
    60      */
    61     public static Object[] create(Class clazz) {
    62         return newTests().withClasses(clazz).build();
    63     }
    64     
    65     /** Creates new builder for test execution. Continue with methods
    66      * like {@link #withClasses(java.lang.Class[])} or {@link #withLaunchers(java.lang.String[])}.
    67      * Finish the process by calling {@link #build()}.
    68      * 
    69      * @return new instance of a builder
    70      * @since 0.7
    71      */
    72     public static VMTest newTests() {
    73         return new VMTest();
    74     }
    75     
    76     /** Adds class (or classes) to the test execution. The classes are inspected
    77      * to contain methods annotated by
    78      * {@link Compare} or {@link BrwsrTest}. Appropriate set of TestNG test
    79      * cases is then created.
    80      * <p>
    81      * Each {@link Compare} instance runs the test in different virtual
    82      * machine and at the end they compare the results.
    83      * <p>
    84      * Each {@link BrwsrTest} annotated method is executed once in {@link Launcher started
    85      * browser}.
    86      * 
    87      * @param classes one or more classes to inspect
    88      * @since 0.7
    89      */
    90     public final VMTest withClasses(Class... classes) {
    91         this.classes.addAll(Arrays.asList(classes));
    92         return this;
    93     }
    94 
    95     /** Adds list of launchers that should be used to execute tests defined
    96      * by {@link Compare} and {@link BrwsrTest} annotations. This value 
    97      * can be overrided by using <code>vmtest.brwsrs</code> property.
    98      * List of supported launchers is available in the documentation of
    99      * {@link Launcher}.
   100      * 
   101      * @param launcher names of one or more launchers to use for the execution
   102      *   of tests
   103      * @since 0.7
   104      */
   105     public final VMTest withLaunchers(String... launcher) {
   106         this.launcher.addAll(Arrays.asList(launcher));
   107         return this;
   108     }
   109 
   110     /** Specifies which annotation annotates the test methods
   111      * to be executed. By 
   112      * default it is the {@link BrwsrTest} annotation. Methods in
   113      * {@link #withClasses(java.lang.Class[]) test classes} annotated by
   114      * this annotation will be executed.
   115      * 
   116      * @param aClass an annotation class 
   117      * @return this
   118      * @since 0.8
   119      */
   120     public final VMTest withTestAnnotation(Class<? extends Annotation> aClass) {
   121         if (!aClass.isAnnotation()) {
   122             throw new IllegalStateException();
   123         }
   124         this.annotation = aClass;
   125         return this;
   126     }
   127     
   128     /** Assembles the provided information into the final array of tests.
   129      * @return array of TestNG tests
   130      * @since 0.7
   131      */
   132     public final Object[] build() {
   133         return CompareCase.create(
   134             launcher.toArray(new String[0]), 
   135             classes.toArray(new Class[0]),
   136             annotation
   137         );
   138     }
   139 }