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

File FileExtensionRepository.java

 

Coverage histogram

../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

6
40
11
1
177
131
19
0.47
3.64
11
1.73

Classes

Class Line # Actions
FileExtensionRepository 50 40 0% 19 12
0.789473778.9%
 

Contributing tests

This file is covered by 91 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.FileInputStream;
24    import java.io.FileNotFoundException;
25    import java.io.FilenameFilter;
26    import java.io.InputStream;
27    import java.io.UnsupportedEncodingException;
28    import java.net.URLEncoder;
29    import java.util.Collections;
30    import java.util.LinkedList;
31    import java.util.List;
32   
33    import org.xwiki.component.manager.ComponentLookupException;
34    import org.xwiki.component.manager.ComponentManager;
35    import org.xwiki.extension.Extension;
36    import org.xwiki.extension.ExtensionDependency;
37    import org.xwiki.extension.ExtensionId;
38    import org.xwiki.extension.ExtensionNotFoundException;
39    import org.xwiki.extension.ResolveException;
40    import org.xwiki.extension.repository.AbstractExtensionRepository;
41    import org.xwiki.extension.repository.DefaultExtensionRepositoryDescriptor;
42    import org.xwiki.extension.repository.ExtensionRepository;
43    import org.xwiki.extension.repository.internal.ExtensionSerializer;
44    import org.xwiki.extension.repository.internal.local.DefaultLocalExtension;
45    import org.xwiki.extension.repository.result.CollectionIterableResult;
46    import org.xwiki.extension.repository.result.IterableResult;
47    import org.xwiki.extension.version.Version;
48    import org.xwiki.extension.version.internal.DefaultVersion;
49   
 
50    public class FileExtensionRepository extends AbstractExtensionRepository implements ExtensionRepository
51    {
52    private ExtensionSerializer extensionSerializer;
53   
54    private File directory;
55   
 
56  138 toggle public FileExtensionRepository(File directory, ComponentManager componentManager) throws ComponentLookupException
57    {
58  138 super(new DefaultExtensionRepositoryDescriptor("test-file", "file", null));
59   
60  138 this.extensionSerializer = componentManager.getInstance(ExtensionSerializer.class);
61   
62  138 this.directory = directory;
63    }
64   
 
65  0 toggle public File getDirectory()
66    {
67  0 return this.directory;
68    }
69   
 
70  176 toggle InputStream getFileAsStream(ExtensionId extensionId, String type)
71    throws FileNotFoundException, UnsupportedEncodingException
72    {
73  176 return new FileInputStream(getFile(extensionId, type));
74    }
75   
 
76  351 toggle public File getFile(ExtensionId extensionId, String type) throws UnsupportedEncodingException
77    {
78  351 File extensionFile = new File(this.directory, getEncodedPath(extensionId, type));
79   
80  351 return extensionFile;
81    }
82   
 
83  351 toggle String getEncodedPath(ExtensionId extensionId, String type) throws UnsupportedEncodingException
84    {
85  351 return URLEncoder.encode(getPathSuffix(extensionId, type), "UTF-8");
86    }
87   
 
88  351 toggle String getPathSuffix(ExtensionId extensionId, String type)
89    {
90  351 return extensionId.getId() + '-' + extensionId.getVersion().getValue() + '.' + type;
91    }
92   
 
93  176 toggle @Override
94    public Extension resolve(ExtensionId extensionId) throws ResolveException
95    {
96  176 InputStream descriptor;
97  176 try {
98  176 descriptor = getFileAsStream(extensionId, "xed");
99    } catch (FileNotFoundException e) {
100  1 throw new ExtensionNotFoundException("Could not find extension [" + extensionId + "]", e);
101    } catch (Exception e) {
102  0 throw new ResolveException("Invalid extension id [" + extensionId + "]", e);
103    }
104   
105  175 if (descriptor == null) {
106  0 throw new ExtensionNotFoundException("Extension [" + extensionId + "] not found");
107    }
108   
109  175 try {
110  175 DefaultLocalExtension localExtension =
111    this.extensionSerializer.loadLocalExtensionDescriptor(null, descriptor);
112   
113  175 return new FileExtension(this, localExtension);
114    } catch (Exception e) {
115  0 throw new ResolveException("Failed to parse descriptor for extension [" + extensionId + "]", e);
116    }
117    }
118   
 
119  51 toggle @Override
120    public Extension resolve(ExtensionDependency extensionDependency) throws ResolveException
121    {
122  51 return resolve(new ExtensionId(extensionDependency.getId(),
123    new DefaultVersion(extensionDependency.getVersionConstraint().getValue())));
124    }
125   
 
126  0 toggle @Override
127    public boolean exists(ExtensionId extensionId)
128    {
129  0 try {
130  0 return getFile(extensionId, "xed").exists();
131    } catch (Exception e) {
132  0 return false;
133    }
134    }
135   
 
136  82 toggle @Override
137    public IterableResult<Version> resolveVersions(final String id, int offset, int nb) throws ResolveException
138    {
139  82 List<Version> versions = new LinkedList<Version>();
140   
141  82 try {
142  82 for (File file : this.directory.listFiles(new FilenameFilter()
143    {
 
144  4264 toggle @Override
145    public boolean accept(File dir, String name)
146    {
147  4264 return name.startsWith(id + '-') && name.endsWith(".xed");
148    }
149    })) {
150  22 FileInputStream fis = null;
151  22 try {
152  22 fis = new FileInputStream(file);
153   
154  22 DefaultLocalExtension localExtension =
155    this.extensionSerializer.loadLocalExtensionDescriptor(null, fis);
156   
157  22 versions.add(localExtension.getId().getVersion());
158    } finally {
159  22 if (fis != null) {
160  22 fis.close();
161    }
162    }
163   
164    }
165    } catch (Exception e) {
166  0 throw new ResolveException("Failed to resolve versions for extenion [" + id + "]", e);
167    }
168   
169  82 if (versions.isEmpty()) {
170  77 throw new ExtensionNotFoundException("Extension [" + id + "] not found");
171    }
172   
173  5 Collections.sort(versions);
174   
175  5 return new CollectionIterableResult<Version>(0, offset, versions);
176    }
177    }