// Protocol Buffers - Google's data interchange format // Copyright 2008 Google Inc. All rights reserved. // https://developers.google.com/protocol-buffers/ // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following disclaimer // in the documentation and/or other materials provided with the // distribution. // * Neither the name of Google Inc. nor the names of its // contributors may be used to endorse or promote products derived from // this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. package com.google.protobuf; import protobuf_unittest.NonNestedExtension; import protobuf_unittest.NonNestedExtensionLite; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; import java.lang.reflect.Method; import java.net.URLClassLoader; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.Set; /** * Tests for {@link ExtensionRegistryFactory} and the {@link ExtensionRegistry} instances it * creates. * *
This test simulates the runtime behaviour of the ExtensionRegistryFactory by delegating test * definitions to two inner classes {@link InnerTest} and {@link InnerLiteTest}, the latter of * which is executed using a custom ClassLoader, simulating the ProtoLite environment. * *
The test mechanism employed here is based on the pattern in
* {@code com.google.common.util.concurrent.AbstractFutureFallbackAtomicHelperTest}
*/
public class ExtensionRegistryFactoryTest extends TestCase {
// A classloader which blacklists some non-Lite classes.
private static final ClassLoader LITE_CLASS_LOADER = getLiteOnlyClassLoader();
/**
* Defines the set of test methods which will be run.
*/
static interface RegistryTests {
void testCreate();
void testEmpty();
void testIsFullRegistry();
void testAdd();
}
/**
* Test implementations for the non-Lite usage of ExtensionRegistryFactory.
*/
public static class InnerTest implements RegistryTests {
@Override
public void testCreate() {
ExtensionRegistryLite registry = ExtensionRegistryFactory.create();
assertEquals(registry.getClass(), ExtensionRegistry.class);
}
@Override
public void testEmpty() {
ExtensionRegistryLite emptyRegistry = ExtensionRegistryFactory.createEmpty();
assertEquals(emptyRegistry.getClass(), ExtensionRegistry.class);
assertEquals(emptyRegistry, ExtensionRegistry.EMPTY_REGISTRY);
}
@Override
public void testIsFullRegistry() {
ExtensionRegistryLite registry = ExtensionRegistryFactory.create();
assertTrue(ExtensionRegistryFactory.isFullRegistry(registry));
}
@Override
public void testAdd() {
ExtensionRegistryLite registry1 = ExtensionRegistryLite.newInstance();
NonNestedExtensionLite.registerAllExtensions(registry1);
registry1.add(NonNestedExtensionLite.nonNestedExtensionLite);
ExtensionRegistryLite registry2 = ExtensionRegistryLite.newInstance();
NonNestedExtension.registerAllExtensions((ExtensionRegistry) registry2);
registry2.add(NonNestedExtension.nonNestedExtension);
ExtensionRegistry fullRegistry1 = (ExtensionRegistry) registry1;
ExtensionRegistry fullRegistry2 = (ExtensionRegistry) registry2;
assertTrue("Test is using a non-lite extension",
GeneratedMessageLite.GeneratedExtension.class.isAssignableFrom(
NonNestedExtensionLite.nonNestedExtensionLite.getClass()));
assertNull("Extension is not registered in masqueraded full registry",
fullRegistry1.findImmutableExtensionByName("protobuf_unittest.nonNestedExtension"));
GeneratedMessageLite.GeneratedExtension