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

File ResourceExtensionRepository.java

 

Coverage histogram

../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

2
23
9
1
130
92
13
0.57
2.56
9
1.44

Classes

Class Line # Actions
ResourceExtensionRepository 45 23 0% 13 34
0.00%
 

Contributing tests

No tests hitting this source file were found.

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.InputStream;
23    import java.io.UnsupportedEncodingException;
24    import java.net.URL;
25    import java.net.URLEncoder;
26    import java.util.Collections;
27   
28    import org.xwiki.component.manager.ComponentLookupException;
29    import org.xwiki.component.manager.ComponentManager;
30    import org.xwiki.extension.Extension;
31    import org.xwiki.extension.ExtensionDependency;
32    import org.xwiki.extension.ExtensionId;
33    import org.xwiki.extension.ExtensionNotFoundException;
34    import org.xwiki.extension.ResolveException;
35    import org.xwiki.extension.repository.AbstractExtensionRepository;
36    import org.xwiki.extension.repository.DefaultExtensionRepositoryDescriptor;
37    import org.xwiki.extension.repository.ExtensionRepository;
38    import org.xwiki.extension.repository.internal.ExtensionSerializer;
39    import org.xwiki.extension.repository.internal.local.DefaultLocalExtension;
40    import org.xwiki.extension.repository.result.CollectionIterableResult;
41    import org.xwiki.extension.repository.result.IterableResult;
42    import org.xwiki.extension.version.Version;
43    import org.xwiki.extension.version.internal.DefaultVersion;
44   
 
45    public class ResourceExtensionRepository extends AbstractExtensionRepository implements ExtensionRepository
46    {
47    private ExtensionSerializer extensionSerializer;
48   
49    private ClassLoader classLoader;
50   
51    private String baseResource;
52   
 
53  0 toggle public ResourceExtensionRepository(ClassLoader classLoader, String baseResource, ComponentManager componentManager)
54    throws ComponentLookupException
55    {
56  0 super(new DefaultExtensionRepositoryDescriptor("test-resources", "resources", null));
57   
58  0 this.extensionSerializer = componentManager.getInstance(ExtensionSerializer.class);
59   
60  0 this.classLoader = classLoader;
61  0 this.baseResource = baseResource;
62    }
63   
 
64  0 toggle InputStream getResourceAsStream(ExtensionId extensionId, String type) throws UnsupportedEncodingException
65    {
66  0 return this.classLoader.getResourceAsStream(getEncodedPath(extensionId, type));
67    }
68   
 
69  0 toggle URL getResource(ExtensionId extensionId, String type) throws UnsupportedEncodingException
70    {
71  0 return this.classLoader.getResource(getEncodedPath(extensionId, type));
72    }
73   
 
74  0 toggle String getEncodedPath(ExtensionId extensionId, String type) throws UnsupportedEncodingException
75    {
76  0 return this.baseResource + URLEncoder.encode(getPathSuffix(extensionId, type), "UTF-8");
77    }
78   
 
79  0 toggle String getPathSuffix(ExtensionId extensionId, String type)
80    {
81  0 return extensionId.getId() + '-' + extensionId.getVersion().getValue() + '.' + type;
82    }
83   
 
84  0 toggle @Override
85    public Extension resolve(ExtensionId extensionId) throws ResolveException
86    {
87  0 InputStream descriptor;
88  0 try {
89  0 descriptor = getResourceAsStream(extensionId, "xed");
90    } catch (UnsupportedEncodingException e) {
91  0 throw new ResolveException("Invalid extension id [" + extensionId + "]", e);
92    }
93   
94  0 if (descriptor == null) {
95  0 throw new ExtensionNotFoundException("Extension [" + extensionId + "] not found");
96    }
97   
98  0 try {
99  0 DefaultLocalExtension localExtension =
100    this.extensionSerializer.loadLocalExtensionDescriptor(null, descriptor);
101   
102  0 return new ResourceExtension(this, localExtension);
103    } catch (Exception e) {
104  0 throw new ResolveException("Failed to parse descriptor for extension [" + extensionId + "]", e);
105    }
106    }
107   
 
108  0 toggle @Override
109    public Extension resolve(ExtensionDependency extensionDependency) throws ResolveException
110    {
111  0 return resolve(new ExtensionId(extensionDependency.getId(),
112    new DefaultVersion(extensionDependency.getVersionConstraint().getValue())));
113    }
114   
 
115  0 toggle @Override
116    public boolean exists(ExtensionId extensionId)
117    {
118  0 try {
119  0 return getResource(extensionId, "xed") != null;
120    } catch (UnsupportedEncodingException e) {
121  0 return false;
122    }
123    }
124   
 
125  0 toggle @Override
126    public IterableResult<Version> resolveVersions(String id, int offset, int nb) throws ResolveException
127    {
128  0 return new CollectionIterableResult<Version>(0, offset, Collections.<Version>emptyList());
129    }
130    }