All Downloads are FREE. Search and download functionalities are using the official Maven repository.

link.jfire.testsupport.runner.JfireContextRunner Maven / Gradle / Ivy

The newest version!
package link.jfire.testsupport.runner;

import java.io.File;
import java.net.URISyntaxException;
import link.jfire.core.JfireContext;
import link.jfire.core.JfireContextImpl;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.InitializationError;

public class JfireContextRunner extends BlockJUnit4ClassRunner
{
    private Class    klass;
    private JfireContext beanContext;
    
    public JfireContextRunner(Class klass) throws InitializationError, URISyntaxException
    {
        super(klass);
        this.klass = klass;
        ConfigPath path = klass.getAnnotation(ConfigPath.class);
        String value = path.value();
        beanContext = new JfireContextImpl();
        File config = null;
        if (value.startsWith("classpath:"))
        {
            try
            {
                config = new File(this.getClass().getClassLoader().getResource(value.substring(10)).toURI());
            }
            catch (Exception e)
            {
                throw new RuntimeException("文件无法找到", e);
            }
        }
        else if (value.startsWith("file:"))
        {
            try
            {
                config = new File(value.substring(5));
            }
            catch (Exception e)
            {
                throw new RuntimeException("文件无法找到", e);
            }
        }
        beanContext.readConfig(config);
        beanContext.addBean(klass.getName(), false, klass);
        beanContext.initContext();
    }
    
    protected Object createTest()
    {
        return beanContext.getBean(klass);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy