rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/VMTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 28 Apr 2013 22:38:29 +0200
branchmodel
changeset 1050 6415da7d00b6
parent 1018 49eb825c87b7
child 1054 fef28d5bee88
permissions -rw-r--r--
Turning VMTest into a builder pattern
     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.util.ArrayList;
    21 import java.util.Arrays;
    22 import java.util.List;
    23 import org.apidesign.bck2brwsr.launcher.Launcher;
    24 import org.apidesign.bck2brwsr.vmtest.impl.CompareCase;
    25 import org.testng.annotations.Factory;
    26 
    27 /** A TestNG {@link Factory} that seeks for {@link Compare} and {@link BrwsrTest} annotations
    28  * in provided class and builds set of tests that verify the functionality of <b>Bck2Brwsr</b> 
    29  * based system. Use as:
    30  * <pre>
    31  * {@code @}{@link Factory} public static create() {
    32  *   return @{link VMTest}.{@link #create(java.lang.Class) create}(YourClass.class);
    33  * }</pre>
    34  * where <code>YourClass</code> contains methods annotated with
    35  * {@link Compare} and {@link BrwsrTest} annotations.
    36  * 
    37  * @author Jaroslav Tulach <jtulach@netbeans.org>
    38  */
    39 public final class VMTest {
    40     private final List<Class> classes = new ArrayList<>();
    41     private final List<String> launcher = new ArrayList<>();
    42     
    43     private VMTest() {
    44     }
    45     
    46     /** Inspects <code>clazz</code> and for each method annotated by
    47      * {@link Compare} or {@link BrwsrTest} creates
    48      * instances of tests. 
    49      * <p>
    50      * Each {@link Compare} instance runs the test in different virtual
    51      * machine and at the end they compare the results.
    52      * <p>
    53      * Each {@link BrwsrTest} annotated method is executed once in {@link Launcher started
    54      * browser}.
    55      * 
    56      * @param clazz the class (or classes) to inspect
    57      * @return the set of created tests
    58      */
    59     public static Object[] create(Class clazz) {
    60         return newTests().addClass(clazz).build();
    61     }
    62     
    63     /** Creates new builder for test execution. Continue with methods
    64      * like {@link #addClass(java.lang.Class[])} or {@link #addLauncher(java.lang.String[])}.
    65      * Finish the process by calling {@link #build()}.
    66      * 
    67      * @return new instance of a builder
    68      * @since 0.7
    69      */
    70     public static VMTest newTests() {
    71         return new VMTest();
    72     }
    73     
    74     /** Adds class (or classes) to the test execution. The classes are inspected
    75      * to contain methods annotated by
    76      * {@link Compare} or {@link BrwsrTest}. Appropriate set of TestNG test
    77      * cases is then created.
    78      * <p>
    79      * Each {@link Compare} instance runs the test in different virtual
    80      * machine and at the end they compare the results.
    81      * <p>
    82      * Each {@link BrwsrTest} annotated method is executed once in {@link Launcher started
    83      * browser}.
    84      * 
    85      * @param classes one or more classes to inspect
    86      * @since 0.7
    87      */
    88     public final VMTest addClass(Class... classes) {
    89         this.classes.addAll(Arrays.asList(classes));
    90         return this;
    91     }
    92 
    93     /** Adds list of launchers that should be used to execute tests defined
    94      * by {@link Compare} and {@link BrwsrTest} annotations. This value 
    95      * can be overrided by using <code>vmtest.brwsrs</code> property.
    96      * List of supported launchers is available in the documentation of
    97      * {@link Launcher}.
    98      * 
    99      * @param launcher names of one or more launchers to use for the execution
   100      *   of tests
   101      * @since 0.7
   102      */
   103     public final VMTest addLauncher(String... launcher) {
   104         this.launcher.addAll(Arrays.asList(launcher));
   105         return this;
   106     }
   107     
   108     /** Assembles the provided information into the final array of tests.
   109      * @return array of TestNG tests
   110      * @since 0.7
   111      */
   112     public final Object[] build() {
   113         return CompareCase.create(
   114             launcher.toArray(new String[0]), 
   115             classes.toArray(new Class[0])
   116         );
   117     }
   118 }