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.common;
22 
23 import java.text.MessageFormat;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.logging.Level;
27 
28 import org.junit.runner.Description;
29 import org.junit.runner.Request;
30 import org.junit.runner.Runner;
31 import org.junit.runner.notification.RunNotifier;
32 import org.junit.runners.ParentRunner;
33 import org.junit.runners.model.InitializationError;
34 import org.openoffice.test.Run;
35 
36 public class NamedRequest extends Request {
37 
38 	private static Logger log = Logger.getLogger(Run.class);
39 
40 	private Suite suite = null;
41 
NamedRequest()42 	protected NamedRequest() {
43 
44 	}
45 
init(String arg)46 	private String init(String arg) throws InitializationError {
47 		String name = null;
48 		int ci = arg.indexOf(":");
49 		if (ci >= 0) {
50 			name = arg.substring(0, ci);
51 			arg = arg.substring(++ci);
52 		} else {
53 			name = arg;
54 			if (name.length() > 128) {
55 				name = name.substring(0, 128) + "...";
56 			}
57 		}
58 		suite = new Suite(null, name);
59 		return arg;
60 	}
61 
addRunner(Runner runner)62 	private void addRunner(Runner runner) {
63 		suite.getChildren().add(runner);
64 	}
65 
tc(String arg)66 	public static NamedRequest tc(String arg) {
67 		NamedRequest namedRequest = new NamedRequest();
68 		try {
69 			arg = namedRequest.init(arg);
70 		} catch (InitializationError e1) {
71 			return null;
72 		}
73 
74 		String[] klasses = arg.split(",");
75 		List<Class<?>> classes = new ArrayList<Class<?>>();
76 		for (String k : klasses) {
77 			try {
78 				classes.add(Class.forName(k));
79 			} catch (Throwable e) {
80 				log.log(Level.WARNING, MessageFormat.format("Test class {0} can not be tested!", k), e);
81 			}
82 		}
83 
84 		namedRequest.addRunner(Request.classes(classes.toArray(new Class[0])).getRunner());
85 		return namedRequest;
86 	}
87 
tp(String arg)88 	public static NamedRequest tp(String arg) {
89 		NamedRequest namedRequest = new NamedRequest();
90 		try {
91 			arg = namedRequest.init(arg);
92 		} catch (InitializationError e1) {
93 			return null;
94 		}
95 
96 		String[] packages = arg.split(",");
97 		List<Class<?>> classes = new ArrayList<Class<?>>();
98 		for (String p : packages) {
99 			List<String> klasses = SystemUtil.getClassesInPackage(p);
100 			for (String k : klasses) {
101 				if (k.matches(".+[$]{1}.*"))
102 					continue;
103 				try {
104 					classes.add(Class.forName(k));
105 				} catch (Throwable e) {
106 					log.log(Level.WARNING, MessageFormat.format("Test class {0} can not be tested!", k), e);
107 				}
108 			}
109 		}
110 
111 		namedRequest.addRunner(Request.classes(classes.toArray(new Class[0])).getRunner());
112 		return namedRequest;
113 	}
114 
tm(String arg)115 	public static NamedRequest tm(String arg) {
116 		NamedRequest namedRequest = new NamedRequest();
117 		try {
118 			arg = namedRequest.init(arg);
119 		} catch (InitializationError e1) {
120 			return null;
121 		}
122 
123 		String[] methods = arg.split(",");
124 		for (String m : methods) {
125 			int i = m.lastIndexOf(".");
126 			if (i < 0) {
127 				throw new RuntimeException("-tm parameter needs to have the form className.methodName");
128 			}
129 			String className = m.substring(0, i);
130 			String methodName = m.substring(++i);
131 			try {
132 				namedRequest.addRunner(Request.method(Class.forName(className), methodName).getRunner());
133 			} catch (Throwable e) {
134 				log.log(Level.WARNING, MessageFormat.format("Test method {0} can not be tested!", m), e);
135 			}
136 
137 		}
138 		return namedRequest;
139 	}
140 
merge(NamedRequest other)141 	public void merge(NamedRequest other) {
142 		suite.getChildren().addAll(other.suite.getChildren());
143 	}
144 
getName()145 	public String getName() {
146 		return suite.getName();
147 	}
148 
149 	@Override
getRunner()150 	public Runner getRunner() {
151 		return suite;
152 	}
153 
154 	public static class Suite extends ParentRunner<Runner> {
155 
156 		protected final List<Runner> fRunners = new ArrayList<Runner>();
157 
158 		protected String name;
159 
Suite(Class<?> testClass, String name)160 		protected Suite(Class<?> testClass, String name) throws InitializationError {
161 			super(testClass);
162 			this.name = name;
163 		}
164 
getName()165 		protected String getName() {
166 			return name;
167 		}
168 
169 		@Override
getChildren()170 		protected List<Runner> getChildren() {
171 			return fRunners;
172 		}
173 
174 		@Override
describeChild(Runner child)175 		protected Description describeChild(Runner child) {
176 			return child.getDescription();
177 		}
178 
179 		@Override
runChild(Runner runner, final RunNotifier notifier)180 		protected void runChild(Runner runner, final RunNotifier notifier) {
181 			runner.run(notifier);
182 		}
183 	}
184 
185 }
186