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