rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/LaunchSetup.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 24 Feb 2015 11:12:53 +0100
changeset 1787 ea12a3bb4b33
parent 1537 897d7f70c5a5
permissions -rw-r--r--
Using year range 2012-2015 in copyright header
jaroslav@356
     1
/**
jaroslav@356
     2
 * Back 2 Browser Bytecode Translator
jaroslav@1787
     3
 * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@356
     4
 *
jaroslav@356
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@356
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@356
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@356
     8
 *
jaroslav@356
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@356
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@356
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@356
    12
 * GNU General Public License for more details.
jaroslav@356
    13
 *
jaroslav@356
    14
 * You should have received a copy of the GNU General Public License
jaroslav@356
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@356
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@356
    17
 */
jaroslav@372
    18
package org.apidesign.bck2brwsr.vmtest.impl;
jaroslav@356
    19
jaroslav@370
    20
import java.io.IOException;
jaroslav@1537
    21
import java.util.HashSet;
jaroslav@383
    22
import java.util.LinkedHashMap;
jaroslav@383
    23
import java.util.Map;
jaroslav@1537
    24
import java.util.Set;
jaroslav@382
    25
import org.apidesign.bck2brwsr.launcher.Launcher;
jaroslav@372
    26
import org.testng.annotations.AfterGroups;
jaroslav@372
    27
import org.testng.annotations.BeforeGroups;
jaroslav@356
    28
jaroslav@356
    29
/**
jaroslav@356
    30
 *
jaroslav@356
    31
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@356
    32
 */
jaroslav@383
    33
public final class LaunchSetup {
jaroslav@384
    34
    static LaunchSetup INSTANCE = new LaunchSetup();
jaroslav@356
    35
    
jaroslav@681
    36
    private Launcher js;
jaroslav@384
    37
    private final Map<String,Launcher> brwsrs = new LinkedHashMap<>();
jaroslav@372
    38
    
jaroslav@384
    39
    private LaunchSetup() {
jaroslav@383
    40
    }
jaroslav@383
    41
    
jaroslav@681
    42
    public Launcher javaScript() {
jaroslav@681
    43
        return js(true);
jaroslav@681
    44
    } 
jaroslav@681
    45
    private synchronized  Launcher js(boolean create) {
jaroslav@681
    46
        if (js == null && create) {
jaroslav@803
    47
            final String p = System.getProperty("vmtest.js", "script"); // NOI18N
jaroslav@800
    48
            switch (p) {
jaroslav@1537
    49
                case "brwsr": // NOI18N
jaroslav@1537
    50
                    String cmd = null;
jaroslav@1537
    51
                    String pb = System.getProperty("vmtest.brwsrs"); // NOI18N
jaroslav@1537
    52
                    if (pb != null) {
jaroslav@1537
    53
                        cmd = pb.split(",")[0]; // NOI18N
jaroslav@1537
    54
                    }
jaroslav@1537
    55
                    js = brwsr(cmd);
jaroslav@1537
    56
                    break;
jaroslav@803
    57
                case "script": js = Launcher.createJavaScript(); break; // NOI18N
jaroslav@800
    58
                default: throw new IllegalArgumentException(
jaroslav@803
    59
                    "Unknown value of vmtest.js property: " + p
jaroslav@800
    60
                );
jaroslav@800
    61
            }
jaroslav@681
    62
        }
jaroslav@384
    63
        return js;
jaroslav@383
    64
    } 
jaroslav@383
    65
    
jaroslav@384
    66
    public synchronized Launcher brwsr(String cmd) {
jaroslav@384
    67
        Launcher s = brwsrs.get(cmd);
jaroslav@383
    68
        if (s == null) {
jaroslav@384
    69
            s = Launcher.createBrowser(cmd);
jaroslav@384
    70
            brwsrs.put(cmd, s);
jaroslav@383
    71
        }
jaroslav@383
    72
        return s;
jaroslav@356
    73
    }
jaroslav@356
    74
jaroslav@372
    75
    @BeforeGroups("run")
jaroslav@372
    76
    public void initializeLauncher() throws IOException {
jaroslav@1537
    77
        Set<Launcher> all = new HashSet<>(brwsrs.values());
jaroslav@681
    78
        if (js(false) != null) {
jaroslav@1537
    79
            all.add(js(true));
jaroslav@681
    80
        }
jaroslav@1537
    81
        for (Launcher launcher : all) {
jaroslav@384
    82
            launcher.initialize();
jaroslav@384
    83
        }
jaroslav@356
    84
    }
jaroslav@356
    85
jaroslav@372
    86
    @AfterGroups("run")
jaroslav@372
    87
    public void shutDownLauncher() throws IOException, InterruptedException {
jaroslav@1537
    88
        Set<Launcher> all = new HashSet<>(brwsrs.values());
jaroslav@681
    89
        if (js(false) != null) {
jaroslav@1537
    90
            all.add(js(true));
jaroslav@681
    91
        }
jaroslav@1537
    92
        for (Launcher launcher : all) {
jaroslav@384
    93
            launcher.shutdown();
jaroslav@384
    94
        }
jaroslav@356
    95
    }
jaroslav@356
    96
}