cn.javass.spring.chapter4.ResourcePatternTest.java Source code

Java tutorial

Introduction

Here is the source code for cn.javass.spring.chapter4.ResourcePatternTest.java

Source

package cn.javass.spring.chapter4;

import java.io.File;
import java.io.IOException;

import junit.framework.Assert;

import org.jboss.vfs.VFS;
import org.jboss.vfs.VirtualFile;
import org.jboss.vfs.spi.RealFileSystem;
import org.junit.Test;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

public class ResourcePatternTest {

    @Test
    public void testClasspathPrefix() throws IOException {
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        //???ResourceResourceLoader.getResource
        Resource[] resources = resolver.getResources("classpath:META-INF/INDEX.LIST");
        Assert.assertEquals(1, resources.length);

        //??ResourceResourceLoader.getResource
        resources = resolver.getResources("classpath:META-INF/*.LIST");
        Assert.assertTrue(resources.length == 1);

        //???ResourceResourceLoader.getResource
        resources = resolver.getResources("classpath:META-INF/MANIFEST.MF");
        Assert.assertEquals(1, resources.length);
    }

    @Test
    public void testClasspathAsteriskPrefix() throws IOException {
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        //??Resource
        //ClassLoader.getResources("META-INF")??
        //?????
        Resource[] resources = resolver.getResources("classpath*:META-INF/INDEX.LIST");
        Assert.assertTrue(resources.length > 1);

        //??Resource
        resources = resolver.getResources("classpath*:META-INF/*.LIST");
        Assert.assertTrue(resources.length > 1);

    }

    @Test
    public void testClasspathAsteriskPrefixLimit() throws IOException {
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        //ClassLoader.getResources("")
        //??jar
        //?????
        Resource[] resources = resolver.getResources("classpath*:asm-*.txt");
        Assert.assertTrue(resources.length == 0);

        //ClassLoader.getResources("asm-license.txt")
        //asm-license.txtcom.springsource.net.sf.cglib-2.2.0.jar
        resources = resolver.getResources("classpath*:asm-license.txt");
        Assert.assertTrue(resources.length > 0);

        //??Resource
        resources = resolver.getResources("classpath*:LICENS*");
        Assert.assertTrue(resources.length == 1);
    }

    @Test
    public void testFilekPrefix() throws IOException {
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = resolver.getResources("file:D:/*.txt");
        Assert.assertTrue(resources.length > 0);
    }

    @Test
    public void testVfsPrefix() throws IOException {
        //1.
        VirtualFile home = VFS.getChild("/home");
        //2.?
        VFS.mount(home, new RealFileSystem(new File("d:")));
        //3.??
        VirtualFile testFile = home.getChild("test.txt");

        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = resolver.getResources("/home/test.txt");
        Assert.assertTrue(resources.length > 0);
        System.out.println(resources[0].getClass());

    }

}