ExtensionLoader { @SuppressWarnings("unchecked") public static <T> ExtensionLoader<T> getExtensionLoader(Class<T> type) { if (type == null) throw new IllegalArgumentException("Extension type == null"); if (!type.isInterface()) { throw new IllegalArgumentException("Extension type(" + type.getName() + ") is not interface!"); } if (!withExtensionAnnotation(type)) { throw new IllegalArgumentException("type(" + type.getName() + ") is not a extension, because WITHOUT @Extension Annotation!"); } ExtensionLoader<T> loader = (ExtensionLoader<T>) EXTENSION_LOADERS.get(type); if (loader == null) { EXTENSION_LOADERS.putIfAbsent(type, new ExtensionLoader<T>(type)); loader = (ExtensionLoader<T>) EXTENSION_LOADERS.get(type); } return loader; } private  ExtensionLoader(Class<T> type); @SuppressWarnings("unchecked") static ExtensionLoader<T> getExtensionLoader(Class<T> type); void registerExtension(String name, T extension); void registerExtension(String name, T extension, boolean override); T getExtension(String name); T getExtension(String name, Map<String, String> properties); T getExtension(Map<String, String> properties); T getExtension(String name, List<String> wrappers); T getExtension(String name, Map<String, String> properties, List<String> wrappers); T getDefaultExtension(); T getDefaultExtension(List<String> wrappers); boolean hasExtension(String name); boolean hasDefaultExtension(); Set<String> getSupportedExtensions(); String getDefaultExtensionName(); Map<String, Map<String, String>> getExtensionAttribute(); Map<String, String> getExtensionAttribute(String name); @Override String toString();  }
@Test public void test_getExtensionLoader_Null() throws Exception { try { ExtensionLoader.getExtensionLoader(null); fail(); } catch (IllegalArgumentException expected) { assertThat(expected.getMessage(), containsString("Extension type == null")); } }
@Test public void test_getExtensionLoader_NotInterface() throws Exception { try { ExtensionLoader.getExtensionLoader(ExtensionLoaderTest.class); fail(); } catch (IllegalArgumentException expected) { assertThat(expected.getMessage(), containsString("type(com.alibaba.cooma.ExtensionLoaderTest) is not interface")); } }
@Test public void test_getExtensionLoader_NotSpiAnnotation() throws Exception { try { ExtensionLoader.getExtensionLoader(NoExtensionExt.class); fail(); } catch (IllegalArgumentException expected) { assertThat(expected.getMessage(), allOf(containsString(NoExtensionExt.class.getName()), containsString("is not a extension"), containsString("WITHOUT @Extension Annotation"))); } }
@Test public void test_InvalidExtName() throws Exception { try { ExtensionLoader.getExtensionLoader(InvalidNameExt.class); fail(); } catch (IllegalStateException expected) { assertThat(expected.getMessage(), containsString("default name(invalid-name&) of extension com.alibaba.cooma.ext8.InvalidNameExt is invalid")); } }
@Test public void test_InvalidExtName2() throws Exception { try { ExtensionLoader.getExtensionLoader(InvalidNameExt2.class); fail(); } catch (IllegalStateException expected) { assertThat(expected.getMessage(), containsString("default name(invalidName]) of extension com.alibaba.cooma.ext8.InvalidNameExt2 is invalid")); } }