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(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 			String className = m.substring(0, i);
127 			String methodName = m.substring(++i);
128 			try {
129 				namedRequest.addRunner(Request.method(Class.forName(className), methodName).getRunner());
130 			} catch (Throwable e) {
131 				log.log(Level.WARNING, MessageFormat.format("Test method {0} can not be tested!", m), e);
132 			}
133 
134 		}
135 		return namedRequest;
136 	}
137 
merge(NamedRequest other)138 	public void merge(NamedRequest other) {
139 		suite.getChildren().addAll(other.suite.getChildren());
140 	}
141 
getName()142 	public String getName() {
143 		return suite.getName();
144 	}
145 
146 	@Override
getRunner()147 	public Runner getRunner() {
148 		return suite;
149 	}
150 
151 	public static class Suite extends ParentRunner<Runner> {
152 
153 		protected final List<Runner> fRunners = new ArrayList<Runner>();
154 
155 		protected String name;
156 
Suite(String name)157 		protected Suite(String name) throws InitializationError {
158 			super(null);
159 			this.name = name;
160 		}
161 
getName()162 		protected String getName() {
163 			return name;
164 		}
165 
166 		@Override
getChildren()167 		protected List<Runner> getChildren() {
168 			return fRunners;
169 		}
170 
171 		@Override
describeChild(Runner child)172 		protected Description describeChild(Runner child) {
173 			return child.getDescription();
174 		}
175 
176 		@Override
runChild(Runner runner, final RunNotifier notifier)177 		protected void runChild(Runner runner, final RunNotifier notifier) {
178 			runner.run(notifier);
179 		}
180 	}
181 
182 }