001/** 002 * Powerunit - A JDK1.8 test framework 003 * Copyright (C) 2014 Mathieu Boretti. 004 * 005 * This file is part of Powerunit 006 * 007 * Powerunit is free software: you can redistribute it and/or modify 008 * it under the terms of the GNU General Public License as published by 009 * the Free Software Foundation, either version 3 of the License, or 010 * (at your option) any later version. 011 * 012 * Powerunit is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 015 * GNU General Public License for more details. 016 * 017 * You should have received a copy of the GNU General Public License 018 * along with Powerunit. If not, see <http://www.gnu.org/licenses/>. 019 */ 020package ch.powerunit; 021 022import java.io.PrintStream; 023import java.lang.management.ManagementFactory; 024import java.lang.reflect.Method; 025import java.util.Arrays; 026 027import javax.management.InstanceAlreadyExistsException; 028import javax.management.MBeanRegistrationException; 029import javax.management.MBeanServer; 030import javax.management.MalformedObjectNameException; 031import javax.management.NotCompliantMBeanException; 032import javax.management.ObjectName; 033 034import ch.powerunit.impl.PowerUnit; 035import ch.powerunit.impl.DefaultPowerUnitRunnerImpl; 036import ch.powerunit.impl.DefaultTestResultListener; 037 038/** 039 * Main class that can be used to run test. 040 * 041 * @author borettim 042 * @since 0.1.0 043 * @see #main(String[]) The main method to have the description of the 044 * parameters. 045 */ 046public class PowerUnitMainRunner { 047 048 private PowerUnitMainRunner() { 049 } 050 051 public static final PrintStream DEFAULT_OUT = System.out; 052 053 /** 054 * The main method. 055 * 056 * @param args 057 * The argument syntax is the following : 058 * <ul> 059 * <li><code>path className[:methodName],...</code></li> 060 * </ul> 061 */ 062 public static void main(String[] args) { 063 if (args.length != 2) { 064 DEFAULT_OUT.printf("The received argument is not valid : %1$s%n", 065 Arrays.toString(args)); 066 System.exit(-1);// NOSONAR 067 } 068 PowerUnit mbean = new PowerUnit(); 069 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); 070 ObjectName name; 071 try { 072 name = new ObjectName("ch.powerunit.jmx:type=PowerUnit"); 073 mbs.registerMBean(mbean, name); 074 } catch (MalformedObjectNameException | InstanceAlreadyExistsException 075 | MBeanRegistrationException | NotCompliantMBeanException e1) { 076 e1.printStackTrace(); 077 } 078 String outputPath = args[0]; 079 String classes[] = args[1].split("\\s*,\\s*"); 080 StringBuilder resumedSucess = new StringBuilder(); 081 StringBuilder resumedFailure = new StringBuilder(); 082 StringBuilder resumedSkipped = new StringBuilder(); 083 084 boolean success = true; 085 for (String c : classes) { 086 DEFAULT_OUT.printf("Running test for %1$s%n", c); 087 try { 088 PowerUnitRunner r = null; 089 if (c.contains(":")) { 090 String param[] = c.split("\\s*:\\s*"); 091 Class<?> cls = Class.forName(param[0]); 092 Method m = cls.getMethod(param[1]); 093 r = new DefaultPowerUnitRunnerImpl(cls, m); 094 } else { 095 Class<?> cls = Class.forName(c); 096 r = new DefaultPowerUnitRunnerImpl(cls); 097 } 098 DefaultTestResultListener def = new DefaultTestResultListener( 099 outputPath, DEFAULT_OUT, mbean); 100 r.addListener(def); 101 r.run();// NOSONAR 102 success &= !def.isError(); 103 resumedSucess.append(def.getResumedSucess()); 104 resumedFailure.append(def.getResumedFailure()); 105 resumedSkipped.append(def.getResumedSkipped()); 106 } catch (ClassNotFoundException e) { 107 DEFAULT_OUT.printf("Unable to create the class %1$s%n", c); 108 } catch (NoSuchMethodException e) { 109 DEFAULT_OUT.printf( 110 "Unable to create the find the method %1$s%n", c); 111 } catch (SecurityException e) { 112 DEFAULT_OUT 113 .printf("Unable to create the test because of security issue %1$s%n", 114 c); 115 } finally { 116 DEFAULT_OUT.printf("End running test for %1$s%n", c); 117 } 118 } 119 DEFAULT_OUT.print("\n\nSuccess tests:\n" + resumedSucess.toString() 120 + "\n\nSkipped tests:\n" + resumedSkipped.toString() 121 + "\n\nFailed tests:\n" + resumedFailure.toString() + "\n"); 122 if (!success) { 123 System.exit(-1);// NOSONAR 124 } 125 System.exit(0);// NOSONAR 126 } 127 128}