1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 package org.openoffice.test;
22 
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Properties;
26 import java.util.logging.Level;
27 
28 import org.junit.runner.JUnitCore;
29 import org.junit.runner.Result;
30 import org.junit.runner.notification.RunListener;
31 import org.junit.runner.notification.Failure;
32 import org.openoffice.test.common.FileUtil;
33 import org.openoffice.test.common.Logger;
34 import org.openoffice.test.common.NamedRequest;
35 
36 public class Run {
37 
addRequest(List<NamedRequest> requests, NamedRequest request)38 	private static void addRequest(List<NamedRequest> requests, NamedRequest request) {
39 		for (NamedRequest r : requests) {
40 			if (r.getName().equals(request.getName())) {
41 				r.merge(request);
42 				return;
43 			}
44 		}
45 
46 		requests.add(request);
47 	}
48 
printUsage(String msg, int code)49 	private static void printUsage(String msg, int code) {
50 		if (msg != null)
51 			System.out.println(msg);
52 		System.out.print(FileUtil.readStreamAsString(Run.class.getResourceAsStream("Run.help"), "utf-8"));
53 		System.exit(code);
54 	}
55 
main(String... args)56 	public static void main(String... args) {
57 		ArrayList<String> runnableClasses = new ArrayList<String>();
58 		ArrayList<String> listenerClasses = new ArrayList<String>();
59 
60 		ArrayList<String> tcs = new ArrayList<String>();
61 		ArrayList<String> tps = new ArrayList<String>();
62 		ArrayList<String> tms = new ArrayList<String>();
63 
64 		List<NamedRequest> requests = new ArrayList<NamedRequest>();
65 
66 		for (int i = 0; i < args.length; i++) {
67 			String arg = args[i];
68 			if ("-help".equals(arg)) {
69 				printUsage(null, 0);
70 			} else if (arg.startsWith("-D")) {
71 				String propEntry = arg.substring(2);
72 				String key = propEntry;
73 				String value = null;
74 				int in = propEntry.indexOf("=");
75 				if (in >= 0) {
76 					key = propEntry.substring(0, in);
77 					value = propEntry.substring(++in);
78 				}
79 				System.setProperty(key, value);
80 			} else if (arg.equals("-propertyfile")) {
81 				if (++i >= args.length)
82 					printUsage("Invalid arguments", 1);
83 				Properties props = FileUtil.loadProperties(args[i]);
84 				System.getProperties().putAll(props);
85 			} else if (arg.equals("-r")) {
86 				if (++i >= args.length)
87 					printUsage("Invalid arguments", 1);
88 				runnableClasses.add(args[i]);
89 			} else if (arg.equals("-l")) {
90 				if (++i >= args.length)
91 					printUsage("Invalid arguments", 1);
92 				listenerClasses.add(args[i]);
93 			} else if (arg.equals("-tc")) {
94 				if (++i >= args.length)
95 					printUsage("Invalid arguments", 1);
96 				tcs.add(args[i]);
97 			} else if (arg.equals("-tp")) {
98 				if (++i >= args.length)
99 					printUsage("Invalid arguments", 1);
100 				tps.add(args[i]);
101 			} else if (arg.equals("-tm")) {
102 				if (++i >= args.length)
103 					printUsage("Invalid arguments", 1);
104 				tms.add(args[i]);
105 			}
106 		}
107 
108 		Logger log = Logger.getLogger(Run.class);
109 		for (String r : runnableClasses) {
110 			try {
111 				((Runnable) Class.forName(r).newInstance()).run();
112 			} catch (Exception e) {
113 				log.log(Level.SEVERE, "Runnable is failed!", e);
114 				System.exit(2);
115 			}
116 		}
117 
118 		JUnitCore core = new JUnitCore();
119 		for (String l : listenerClasses)  {
120 			try {
121 				core.addListener((RunListener) Class.forName(l).newInstance());
122 			} catch (Exception e) {
123 				// Ignore
124 				log.log(Level.WARNING, "Listener is not added!", e);
125 			}
126 		}
127 
128 		for (String s : tcs) {
129 			NamedRequest request = NamedRequest.tc(s);
130 			addRequest(requests, request);
131 		}
132 
133 		for (String s : tps) {
134 			NamedRequest request = NamedRequest.tp(s);
135 			addRequest(requests, request);
136 		}
137 
138 		for (String s : tms) {
139 			NamedRequest request = NamedRequest.tm(s);
140 			addRequest(requests, request);
141 		}
142 
143 		int code = 0;
144 		for (NamedRequest request : requests) {
145 			Result result = core.run(request.getRunner());
146 			if (!result.wasSuccessful()) {
147 				code = 1;
148 				java.util.List<Failure> failureList = result.getFailures();
149 				for( Failure f: failureList)
150 					log.log( Level.SEVERE, "Failure in "+request.getName()+" :"+ f.toString(), f.getException());
151 			}
152 		}
153 
154 		System.exit(code);
155 	}
156 }
157 
158