⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testjarclassloader.java

📁 模块化您的应用系统
💻 JAVA
字号:
package com.opensymphony.tonic.loaders.classloading;

import com.opensymphony.tonic.mock.MockBear;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;

public class TestJarClassLoader extends AbstractTestClassLoader
{
    /**
     * Here we try to load a specific JAR (paddington-test-tonic.jar) and get the descriptor out of it.
     */
    public void testLoader() throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException
    {
        // hacky way of getting to the paddington-test-tonic.jar (assume it's two levels below src/test/etc/test-disabled-tonic.xml)
        File pluginsDirectory = getPluginsDirectory();
        File pluginJar = new File(pluginsDirectory, "paddington-test-plugin.jar");

        // make the JAR loader
        JarClassLoader loader = new JarClassLoader(pluginJar, this.getClass().getClassLoader());

        // now make sure we only got one descriptor back
        Enumeration descriptors = loader.findResources("tonic-plugin.xml");
        assertTrue(descriptors.hasMoreElements());
        URL descriptor = (URL) descriptors.nextElement();
        assertTrue(descriptor.toExternalForm().endsWith("paddington-test-plugin.jar!tonic-plugin.xml"));
        assertFalse(descriptors.hasMoreElements());

        // now try to load the tonic class and cast it to something in the global class path
        Class paddingtonClass = loader.findClass("com.opensymphony.tonic.mock.MockPaddington");
        MockBear paddington = (MockBear) paddingtonClass.newInstance();

        // check we really did get the right class
        assertEquals("com.opensymphony.tonic.mock.MockPaddington", paddington.getClass().getName());
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -