1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.extension.test

File RepositoryUtils.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

4
47
13
1
188
122
15
0.32
3.62
13
1.15

Classes

Class Line # Actions
RepositoryUtils 42 47 0% 15 5
0.92187592.2%
 

Contributing tests

This file is covered by 17 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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 $Id: 8ac00d99803866574baf9499b0a9a481460f1a49 $
41    */
 
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   
 
64  162 toggle 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   
 
91  160 toggle public File getPermanentDirectory()
92    {
93  160 return this.permanentDirectory;
94    }
95   
 
96  160 toggle public File getTemporaryDirectory()
97    {
98  160 return this.temporaryDirectory;
99    }
100   
 
101  0 toggle public File getExtensionDirectory()
102    {
103  0 return this.extensionDirectory;
104    }
105   
 
106  326 toggle public File getLocalRepository()
107    {
108  326 return this.localRepositoryRoot;
109    }
110   
 
111  622 toggle public File getRemoteRepository()
112    {
113  622 return this.remoteRepositoryRoot;
114    }
115   
 
116  367 toggle public File getMavenRepository()
117    {
118  367 return this.mavenRepositoryRoot;
119    }
120   
 
121  344 toggle public File getMaven2Repository()
122    {
123  344 return this.maven2RepositoryRoot;
124    }
125   
 
126  6 toggle public String getMavenRepositoryId()
127    {
128  6 return MAVENREPOSITORY_ID;
129    }
130   
 
131  0 toggle public String getMaven2RepositoryId()
132    {
133  0 return MAVEN2REPOSITORY_ID;
134    }
135   
 
136  24 toggle public ExtensionPackager getExtensionPackager()
137    {
138  24 return this.extensionPackager;
139    }
140   
 
141  164 toggle public void setup() throws Exception
142    {
143    // copy
144   
145  164 copyResourceFolder(getLocalRepository(), "repository.local");
146  164 copyResourceFolder(getMavenRepository(), "repository.maven");
147  164 copyResourceFolder(getMaven2Repository(), "repository.maven2");
148   
149    // generated extensions
150   
151  164 this.extensionPackager.generateExtensions();
152    }
153   
 
154  652 toggle 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    }