17850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.com/*
27850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.com * Copyright (C) 2009 The Android Open Source Project
37850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.com *
47850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.com * Licensed under the Apache License, Version 2.0 (the "License");
57850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.com * you may not use this file except in compliance with the License.
67850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.com * You may obtain a copy of the License at
77850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.com *
87850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.com *      http://www.apache.org/licenses/LICENSE-2.0
97850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.com *
107850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.com * Unless required by applicable law or agreed to in writing, software
117850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.com * distributed under the License is distributed on an "AS IS" BASIS,
127850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.com * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.com * See the License for the specific language governing permissions and
147850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.com * limitations under the License.
157850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.com */
167850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.com
177850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.compackage vogar.target;
187850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.com
197850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.comimport java.lang.reflect.Method;
203cea2f55f3da60eb0c4bdd1616cbfa964ee2cd91jessewilson@google.comimport java.util.concurrent.atomic.AtomicReference;
21a7a8fe1efa0fb3299d68709d60701a045bcc72f3jsharpe@google.comimport vogar.ClassAnalyzer;
227850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.comimport vogar.Result;
2337f7c3a73d290eed1bf4cdff80e029eaa7620801jessewilson@google.comimport vogar.monitor.TargetMonitor;
247850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.com
257850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.com/**
26a7a8fe1efa0fb3299d68709d60701a045bcc72f3jsharpe@google.com * Runs a Java class with a main method. This includes jtreg tests.
277850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.com */
287850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.compublic final class MainRunner implements Runner {
297850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.com
307850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.com    private TargetMonitor monitor;
315f9e5b96aaea17c60c82f11b8933d00ff52b5a85jessewilson@google.com    private Class<?> mainClass;
327850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.com    private Method main;
337850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.com
3472c9585a2c7535a2a847b8c316d28d875582dc09jsharpe@google.com    public void init(TargetMonitor monitor, String actionName, String qualification,
353cea2f55f3da60eb0c4bdd1616cbfa964ee2cd91jessewilson@google.com            Class<?> mainClass, AtomicReference<String> skipPastReference,
363cea2f55f3da60eb0c4bdd1616cbfa964ee2cd91jessewilson@google.com            TestEnvironment testEnvironment, int timeoutSeconds, boolean profile) {
377850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.com        this.monitor = monitor;
385f9e5b96aaea17c60c82f11b8933d00ff52b5a85jessewilson@google.com        this.mainClass = mainClass;
39a7a8fe1efa0fb3299d68709d60701a045bcc72f3jsharpe@google.com        try {
405f9e5b96aaea17c60c82f11b8933d00ff52b5a85jessewilson@google.com            this.main = mainClass.getMethod("main", String[].class);
41a7a8fe1efa0fb3299d68709d60701a045bcc72f3jsharpe@google.com        } catch (NoSuchMethodException e) {
42a7a8fe1efa0fb3299d68709d60701a045bcc72f3jsharpe@google.com            // Don't create a MainRunner without first checking supports().
43a7a8fe1efa0fb3299d68709d60701a045bcc72f3jsharpe@google.com            throw new IllegalArgumentException(e);
44a7a8fe1efa0fb3299d68709d60701a045bcc72f3jsharpe@google.com        }
457850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.com    }
467850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.com
473cea2f55f3da60eb0c4bdd1616cbfa964ee2cd91jessewilson@google.com    public boolean run(String actionName, Profiler profiler, String[] args) {
485f9e5b96aaea17c60c82f11b8933d00ff52b5a85jessewilson@google.com        monitor.outcomeStarted(this, mainClass.getName(), actionName);
497850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.com        try {
506fa1029b5a60514952d707a51253f5929628a4ecbdc@google.com            if (profiler != null) {
516fa1029b5a60514952d707a51253f5929628a4ecbdc@google.com                profiler.start();
526fa1029b5a60514952d707a51253f5929628a4ecbdc@google.com            }
537850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.com            main.invoke(null, new Object[] { args });
54a7a8fe1efa0fb3299d68709d60701a045bcc72f3jsharpe@google.com            monitor.outcomeFinished(Result.SUCCESS);
557850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.com        } catch (Throwable ex) {
567850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.com            ex.printStackTrace();
57a7a8fe1efa0fb3299d68709d60701a045bcc72f3jsharpe@google.com            monitor.outcomeFinished(Result.EXEC_FAILED);
58f7e7f5bb8b41b02a8d749df5d1dfe2101342e5aabdc@google.com        } finally {
59f7e7f5bb8b41b02a8d749df5d1dfe2101342e5aabdc@google.com            if (profiler != null) {
60f7e7f5bb8b41b02a8d749df5d1dfe2101342e5aabdc@google.com                profiler.stop();
61f7e7f5bb8b41b02a8d749df5d1dfe2101342e5aabdc@google.com            }
627850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.com        }
633cc430f91313dab5074cffa6508c0b47cd9f2b50jessewilson@google.com        return true;
64a7a8fe1efa0fb3299d68709d60701a045bcc72f3jsharpe@google.com    }
65a7a8fe1efa0fb3299d68709d60701a045bcc72f3jsharpe@google.com
66a7a8fe1efa0fb3299d68709d60701a045bcc72f3jsharpe@google.com    public boolean supports(Class<?> klass) {
67a7a8fe1efa0fb3299d68709d60701a045bcc72f3jsharpe@google.com        // public static void main(String[] args)
68a7a8fe1efa0fb3299d68709d60701a045bcc72f3jsharpe@google.com        return new ClassAnalyzer(klass).hasMethod(true, void.class, "main", String[].class);
697850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.com    }
707850f3f3da0099b76f09ed64d23e0a43ba4a5c76jessewilson@google.com}
71