31 package com.google.protobuf;
36 import java.net.URLClassLoader;
37 import java.util.Arrays;
38 import java.util.Collections;
39 import java.util.HashSet;
41 import junit.framework.Test;
42 import junit.framework.TestCase;
43 import junit.framework.TestSuite;
62 static interface RegistryTests {
67 void testIsFullRegistry();
71 void testAdd_immutable();
75 public static class InnerTest implements RegistryTests {
95 assertTrue(ExtensionRegistryFactory.isFullRegistry(registry));
101 NonNestedExtensionLite.registerAllExtensions(registry1);
102 registry1.
add(NonNestedExtensionLite.nonNestedExtensionLite);
106 registry2.
add(NonNestedExtension.nonNestedExtension);
112 "Test is using a non-lite extension",
114 NonNestedExtensionLite.nonNestedExtensionLite.getClass()));
116 "Extension is not registered in masqueraded full registry",
120 registry1.findLiteExtensionByNumber(
121 NonNestedExtensionLite.MessageLiteToBeExtended.getDefaultInstance(), 1);
122 assertNotNull(
"Extension registered in lite registry",
extension);
125 "Test is using a non-lite extension",
126 Extension.class.isAssignableFrom(NonNestedExtension.nonNestedExtension.getClass()));
128 "Extension is registered in masqueraded full registry",
136 NonNestedExtensionLite.registerAllExtensions(registry1);
138 }
catch (UnsupportedOperationException expected) {
141 registry1.
add(NonNestedExtensionLite.nonNestedExtensionLite);
143 }
catch (UnsupportedOperationException expected) {
150 }
catch (IllegalArgumentException expected) {
153 registry2.
add(NonNestedExtension.nonNestedExtension);
155 }
catch (IllegalArgumentException expected) {
181 assertFalse(ExtensionRegistryFactory.isFullRegistry(registry));
187 NonNestedExtensionLite.registerAllExtensions(registry);
190 registry.findLiteExtensionByNumber(
191 NonNestedExtensionLite.MessageLiteToBeExtended.getDefaultInstance(), 1);
192 assertNotNull(
"Extension is registered in Lite registry",
extension);
199 NonNestedExtensionLite.registerAllExtensions(registry);
201 }
catch (UnsupportedOperationException expected) {
208 TestSuite
suite =
new TestSuite();
209 for (
Method method : RegistryTests.class.getMethods()) {
221 ClassLoader storedClassLoader =
Thread.currentThread().getContextClassLoader();
226 Thread.currentThread().setContextClassLoader(storedClassLoader);
231 Thread.currentThread().setContextClassLoader(storedClassLoader);
235 private void runTestMethod(ClassLoader classLoader, Class<? extends RegistryTests> testClass)
237 classLoader.loadClass(ExtensionRegistryFactory.class.getName());
238 Class<?>
test = classLoader.loadClass(testClass.getName());
240 test.getMethod(testName).invoke(
test.getDeclaredConstructor().newInstance());
249 final Set<String> classNamesNotInLite =
250 Collections.unmodifiableSet(
253 ExtensionRegistryFactory.FULL_REGISTRY_CLASS_NAME,
259 return new URLClassLoader(
260 ((URLClassLoader) testClassLoader).getURLs(), ClassLoader.getSystemClassLoader()) {
262 public Class<?> loadClass(String
name,
boolean resolve)
throws ClassNotFoundException {
263 if (classNamesNotInLite.contains(
name)) {
264 throw new ClassNotFoundException(
"Class deliberately blacklisted by test.");
266 Class<?> loadedClass =
null;
268 loadedClass = findLoadedClass(
name);
269 if (loadedClass ==
null) {
270 loadedClass = findClass(
name);
272 resolveClass(loadedClass);
275 }
catch (ClassNotFoundException | SecurityException e) {