|
1 |
| package com.agical.rmock.core.find.iterator; |
|
2 |
| |
|
3 |
| import java.io.File; |
|
4 |
| import java.net.URISyntaxException; |
|
5 |
| import java.net.URL; |
|
6 |
| import java.util.StringTokenizer; |
|
7 |
| |
|
8 |
| import com.agical.rmock.core.find.UnsupportedClassLocationException; |
|
9 |
| import com.agical.rmock.core.util.StringUtils; |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| public class ClassBaseDirExtractor { |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
11
| public File getDir(Class clazz) {
|
|
25 |
| |
|
26 |
11
| String className = clazz.getName();
|
|
27 |
11
| int dotIndex = className.lastIndexOf('.');
|
|
28 |
11
| if (dotIndex > -1) {
|
|
29 |
10
| className = className.substring(dotIndex+1)+".class";
|
|
30 |
| } else { |
|
31 |
1
| className = className + ".class";
|
|
32 |
| } |
|
33 |
| |
|
34 |
| |
|
35 |
11
| URL resource = clazz.getResource(className);
|
|
36 |
11
| if(!resource.getProtocol().equals("file")) {
|
|
37 |
1
| throw new UnsupportedClassLocationException("The class location ("+resource+") is not supported. Currently tests can only be discovered on the filesystem!");
|
|
38 |
| } |
|
39 |
10
| String file = resource.getFile();
|
|
40 |
| |
|
41 |
| |
|
42 |
10
| String fileNameWithNormalSpaces = StringUtils.replace( file, "%20", " " );
|
|
43 |
10
| File classDirectory = new File(fileNameWithNormalSpaces);
|
|
44 |
10
| String name = clazz.getPackage()==null?"":clazz.getPackage().getName();
|
|
45 |
| |
|
46 |
10
| StringTokenizer stringTokenizer = new StringTokenizer(name, ".");
|
|
47 |
10
| int tokens = stringTokenizer.countTokens();
|
|
48 |
| |
|
49 |
| |
|
50 |
10
| for (int i = 0; i <= tokens; i++){
|
|
51 |
60
| classDirectory = classDirectory.getParentFile();
|
|
52 |
| } |
|
53 |
10
| return classDirectory;
|
|
54 |
| } |
|
55 |
| |
|
56 |
| } |