[ 
https://issues.apache.org/jira/browse/SLING-5294?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Thierry Ygé updated SLING-5294:
-------------------------------
    Attachment: ClientSideTeleporter.java_patch_v2

[~bdelacretaz] adding another patch solution which works as you suggested it.

I have added a new dependency in the pom.xml to use common-io 2.4
mainly to use the FileUtils for listing files in a folder.

Apart from the "code reformatting" which may shows some additional differences, 
you should find it all in the added methods as follow.

{code}
    private final Set<String> embeddedResources = new HashSet<String>();
...
    private InputStream buildTestBundle(Class<?> c, Collection<Class<?>> 
embeddedClasses, String bundleSymbolicName) throws FileNotFoundException {
        final TinyBundle b = TinyBundles.bundle()
...
        if (!embeddedResources.isEmpty()) addEmbeddedResourcesToBundle(b);
...
    }

    private void addEmbeddedResourcesToBundle(TinyBundle b) throws 
FileNotFoundException {
        URL baseURL = 
ClientSideTeleporter.class.getClassLoader().getResource("");
        for (String rsc : embeddedResources) {
            if (rsc.endsWith("*")) {
                // add all the resources below that path
                String folderPath = baseURL.getPath() + rsc.substring(0, 
rsc.length() - 1);
                File folder = new File(folderPath);
                for (File file : FileUtils.listFiles(folder, null, true)) {
                    String embeddedResourcePath = 
file.getPath().substring(baseURL.getPath().length());
                    b.add(embeddedResourcePath, new FileInputStream(file));
                }
            } else {
                // single resource added , check if that exist and it's not a 
folder
                URL resourceURL = 
ClientSideTeleporter.class.getClassLoader().getResource(rsc);
                File resourceFile = new File(resourceURL.getFile());
                if (resourceFile.exists() && !resourceFile.isDirectory()) {
                    String embeddedResourcePath = 
resourceURL.getPath().substring(baseURL.getPath().length());
                    b.add(embeddedResourcePath, resourceURL);
                }
            }
        }
    }

    /**
     * Indicate that a specific resource must be embedded in the test bundle.
     * It support suffix with * for all resource in a specific folder
     * i.e "/resources/*", "/resource/a/b/c.txt" are allowed
     */
    public ClientSideTeleporter withResources(String r) {
        embeddedResources.add(r);
        return this;
    }
{code}

That's basically it. It works on our local tests which embed some resources 
used during the tests.

Let me know if you need also some junit tests to be added in the teleporter , 
as currently there are none in that project.

> Make test resources available to teleported tests
> -------------------------------------------------
>
>                 Key: SLING-5294
>                 URL: https://issues.apache.org/jira/browse/SLING-5294
>             Project: Sling
>          Issue Type: Improvement
>          Components: Testing
>    Affects Versions: JUnit Tests Teleporter 1.0.2
>            Reporter: Thierry Ygé
>         Attachments: ClientSideTeleporter.java_patch, 
> ClientSideTeleporter.java_patch_v2
>
>
> Currently you can add classes to the bundle, but not resources.
> It would be nice to simply add the following code (see attached diff).



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to