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