1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.extension.test; |
21 |
|
|
22 |
|
import java.io.File; |
23 |
|
import java.io.IOException; |
24 |
|
import java.io.InputStream; |
25 |
|
import java.net.URL; |
26 |
|
import java.util.Collection; |
27 |
|
import java.util.Date; |
28 |
|
import java.util.HashMap; |
29 |
|
import java.util.Map; |
30 |
|
import java.util.regex.Pattern; |
31 |
|
|
32 |
|
import org.apache.commons.io.FileUtils; |
33 |
|
import org.reflections.Reflections; |
34 |
|
import org.reflections.scanners.ResourcesScanner; |
35 |
|
import org.reflections.util.ClasspathHelper; |
36 |
|
import org.reflections.util.ConfigurationBuilder; |
37 |
|
import org.reflections.util.FilterBuilder; |
38 |
|
|
39 |
|
|
40 |
|
@version |
41 |
|
|
|
|
| 92.2% |
Uncovered Elements: 5 (64) |
Complexity: 15 |
Complexity Density: 0.32 |
|
42 |
|
public class RepositoryUtils |
43 |
|
{ |
44 |
|
protected static final String MAVENREPOSITORY_ID = "test-maven"; |
45 |
|
|
46 |
|
protected static final String MAVEN2REPOSITORY_ID = "test-maven2"; |
47 |
|
|
48 |
|
protected final File permanentDirectory; |
49 |
|
|
50 |
|
protected final File temporaryDirectory; |
51 |
|
|
52 |
|
protected final File extensionDirectory; |
53 |
|
|
54 |
|
protected final File localRepositoryRoot; |
55 |
|
|
56 |
|
protected final File mavenRepositoryRoot; |
57 |
|
|
58 |
|
protected final File maven2RepositoryRoot; |
59 |
|
|
60 |
|
protected final File remoteRepositoryRoot; |
61 |
|
|
62 |
|
protected final ExtensionPackager extensionPackager; |
63 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 1 |
Complexity Density: 0.06 |
|
64 |
162 |
public RepositoryUtils()... |
65 |
|
{ |
66 |
162 |
File testDirectory = new File("target/test-" + new Date().getTime()); |
67 |
|
|
68 |
162 |
this.temporaryDirectory = new File(testDirectory, "temporary-dir"); |
69 |
|
|
70 |
162 |
this.permanentDirectory = new File(testDirectory, "permanent-dir"); |
71 |
162 |
this.extensionDirectory = new File(this.permanentDirectory, "extension/"); |
72 |
162 |
this.localRepositoryRoot = new File(this.extensionDirectory, "repository/"); |
73 |
|
|
74 |
162 |
this.mavenRepositoryRoot = new File(testDirectory, "maven/"); |
75 |
162 |
this.maven2RepositoryRoot = new File(testDirectory, "maven2/"); |
76 |
162 |
this.remoteRepositoryRoot = new File(testDirectory, "remote/"); |
77 |
|
|
78 |
162 |
Map<String, File> repositories = new HashMap<String, File>(); |
79 |
162 |
repositories.put(null, getRemoteRepository()); |
80 |
162 |
repositories.put("remote", getRemoteRepository()); |
81 |
162 |
repositories.put("local", getLocalRepository()); |
82 |
|
|
83 |
162 |
this.extensionPackager = new ExtensionPackager(this.permanentDirectory, repositories); |
84 |
|
|
85 |
162 |
System.setProperty("extension.repository.local", this.localRepositoryRoot.getAbsolutePath()); |
86 |
162 |
System.setProperty("extension.repository.maven", this.mavenRepositoryRoot.getAbsolutePath()); |
87 |
162 |
System.setProperty("extension.repository.maven2", this.maven2RepositoryRoot.getAbsolutePath()); |
88 |
162 |
System.setProperty("extension.repository.remote", this.remoteRepositoryRoot.getAbsolutePath()); |
89 |
|
} |
90 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
91 |
160 |
public File getPermanentDirectory()... |
92 |
|
{ |
93 |
160 |
return this.permanentDirectory; |
94 |
|
} |
95 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
96 |
160 |
public File getTemporaryDirectory()... |
97 |
|
{ |
98 |
160 |
return this.temporaryDirectory; |
99 |
|
} |
100 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
101 |
0 |
public File getExtensionDirectory()... |
102 |
|
{ |
103 |
0 |
return this.extensionDirectory; |
104 |
|
} |
105 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
106 |
326 |
public File getLocalRepository()... |
107 |
|
{ |
108 |
326 |
return this.localRepositoryRoot; |
109 |
|
} |
110 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
111 |
622 |
public File getRemoteRepository()... |
112 |
|
{ |
113 |
622 |
return this.remoteRepositoryRoot; |
114 |
|
} |
115 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
116 |
367 |
public File getMavenRepository()... |
117 |
|
{ |
118 |
367 |
return this.mavenRepositoryRoot; |
119 |
|
} |
120 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
121 |
344 |
public File getMaven2Repository()... |
122 |
|
{ |
123 |
344 |
return this.maven2RepositoryRoot; |
124 |
|
} |
125 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
126 |
6 |
public String getMavenRepositoryId()... |
127 |
|
{ |
128 |
6 |
return MAVENREPOSITORY_ID; |
129 |
|
} |
130 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
131 |
0 |
public String getMaven2RepositoryId()... |
132 |
|
{ |
133 |
0 |
return MAVEN2REPOSITORY_ID; |
134 |
|
} |
135 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
136 |
24 |
public ExtensionPackager getExtensionPackager()... |
137 |
|
{ |
138 |
24 |
return this.extensionPackager; |
139 |
|
} |
140 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
141 |
164 |
public void setup() throws Exception... |
142 |
|
{ |
143 |
|
|
144 |
|
|
145 |
164 |
copyResourceFolder(getLocalRepository(), "repository.local"); |
146 |
164 |
copyResourceFolder(getMavenRepository(), "repository.maven"); |
147 |
164 |
copyResourceFolder(getMaven2Repository(), "repository.maven2"); |
148 |
|
|
149 |
|
|
150 |
|
|
151 |
164 |
this.extensionPackager.generateExtensions(); |
152 |
|
} |
153 |
|
|
|
|
| 95% |
Uncovered Elements: 1 (20) |
Complexity: 3 |
Complexity Density: 0.19 |
|
154 |
652 |
public int copyResourceFolder(File targetFolder, String resourcePackage) throws IOException... |
155 |
|
{ |
156 |
652 |
int nb = 0; |
157 |
|
|
158 |
652 |
Collection<URL> urls = ClasspathHelper.forPackage(resourcePackage); |
159 |
|
|
160 |
652 |
if (!urls.isEmpty()) { |
161 |
320 |
String prefix = resourcePackage; |
162 |
320 |
if (!prefix.endsWith(".")) { |
163 |
320 |
prefix = prefix + '.'; |
164 |
|
} |
165 |
|
|
166 |
320 |
Reflections reflections = |
167 |
|
new Reflections(new ConfigurationBuilder().setScanners(new ResourcesScanner()).setUrls(urls) |
168 |
|
.filterInputsBy(new FilterBuilder.Include(FilterBuilder.prefix(prefix)))); |
169 |
|
|
170 |
320 |
for (String resource : reflections.getResources(Pattern.compile(".*"))) { |
171 |
7811 |
targetFolder.mkdirs(); |
172 |
|
|
173 |
7811 |
File targetFile = new File(targetFolder, resource.substring(prefix.length())); |
174 |
|
|
175 |
7811 |
InputStream resourceStream = getClass().getResourceAsStream("/" + resource); |
176 |
|
|
177 |
7811 |
try { |
178 |
7811 |
FileUtils.copyInputStreamToFile(resourceStream, targetFile); |
179 |
7811 |
++nb; |
180 |
|
} finally { |
181 |
7811 |
resourceStream.close(); |
182 |
|
} |
183 |
|
} |
184 |
|
} |
185 |
|
|
186 |
652 |
return nb; |
187 |
|
} |
188 |
|
} |