XmlMementoSerializer extends XmlSerializer<T> implements MementoSerializer<T> { @Override public void setLookupContext(LookupContext lookupContext) { this.lookupContext = checkNotNull(lookupContext, "lookupContext"); delegatingClassLoader.setManagementContext(lookupContext.lookupManagementContext()); } XmlMementoSerializer(ClassLoader classLoader);  XmlMementoSerializer(ClassLoader classLoader, Map<String, String> deserializingClassRenames); @Override void serialize(Object object, Writer writer); @Override void setLookupContext(LookupContext lookupContext); @Override void unsetLookupContext();  }
@Test public void testEntity() throws Exception { final TestApplication app = TestApplication.Factory.newManagedInstanceForTests(); ManagementContext managementContext = app.getManagementContext(); try { serializer.setLookupContext(newEmptyLookupManagementContext(managementContext, true).add(app)); assertSerializeAndDeserialize(app); } finally { Entities.destroyAll(managementContext); } }
@Test public void testLocation() throws Exception { final TestApplication app = TestApplication.Factory.newManagedInstanceForTests(); ManagementContext managementContext = app.getManagementContext(); try { final Location loc = managementContext.getLocationManager().createLocation(LocationSpec.create(SimulatedLocation.class)); serializer.setLookupContext(newEmptyLookupManagementContext(managementContext, true).add(loc)); assertSerializeAndDeserialize(loc); } finally { Entities.destroyAll(managementContext); } }
@Test public void testCatalogItem() throws Exception { final TestApplication app = TestApplication.Factory.newManagedInstanceForTests(); ManagementContext managementContext = app.getManagementContext(); try { CatalogItem<?, ?> catalogItem = CatalogItemBuilder.newEntity("symbolicName", "0.0.1") .displayName("test catalog item") .description("description") .plan("yaml plan") .iconUrl("iconUrl") .libraries(CatalogItemDtoAbstract.parseLibraries(ImmutableList.of("library-url"))) .build(); serializer.setLookupContext(newEmptyLookupManagementContext(managementContext, true).add(catalogItem)); assertSerializeAndDeserialize(catalogItem); } finally { Entities.destroyAll(managementContext); } }
@Test public void testEntitySpecFromOsgi() throws Exception { TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_V1_PATH); mgmt = LocalManagementContextForTests.builder(true).enableOsgiReusable().build(); RegisteredType ci = OsgiVersionMoreEntityTest.addMoreEntityV1(mgmt, "1.0"); EntitySpec<DynamicCluster> spec = EntitySpec.create(DynamicCluster.class) .configure(DynamicCluster.INITIAL_SIZE, 1) .configure(DynamicCluster.MEMBER_SPEC, mgmt.getTypeRegistry().createSpec(ci, null, EntitySpec.class)); serializer.setLookupContext(newEmptyLookupManagementContext(mgmt, true)); assertSerializeAndDeserialize(spec); }
@Test public void testOsgiBundleNameNotIncludedForWhiteListed() throws Exception { mgmt = LocalManagementContextForTests.builder(true).enableOsgiReusable().build(); serializer.setLookupContext(newEmptyLookupManagementContext(mgmt, true)); Object obj = PersistMode.AUTO; assertSerializeAndDeserialize(obj); String expectedForm = "<"+PersistMode.class.getName()+">AUTO</"+PersistMode.class.getName()+">"; String serializedForm = serializer.toString(obj); assertEquals(serializedForm.trim(), expectedForm.trim()); }
@Test public void testOsgiBundleNamePrefixIncluded() throws Exception { String bundlePath = OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH; String bundleUrl = OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_URL; TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), bundlePath); mgmt = LocalManagementContextForTests.builder(true).enableOsgiReusable().build(); serializer.setLookupContext(newEmptyLookupManagementContext(mgmt, true)); Bundle bundle = installBundle(mgmt, bundleUrl); String classname = OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_OBJECT; Class<?> osgiObjectClazz = bundle.loadClass(classname); Object obj = Reflections.invokeConstructorFromArgs(osgiObjectClazz, "myval").get(); assertSerializeAndDeserialize(obj); String expectedForm = Joiner.on("\n").join( "<"+bundle.getSymbolicName()+":"+classname+">", " <val>myval</val>", "</"+bundle.getSymbolicName()+":"+classname+">"); String WHITE_LIST_KEY = "org.apache.brooklyn.classloader.fallback.bundles"; String origWhiteList = System.getProperty(WHITE_LIST_KEY); System.setProperty(WHITE_LIST_KEY, "some.none.existend.whitelist:1.0.0"); try { String serializedForm = serializer.toString(obj); assertEquals(serializedForm.trim(), expectedForm.trim()); } finally { if (origWhiteList != null) { System.setProperty(WHITE_LIST_KEY, origWhiteList); } else { System.clearProperty(WHITE_LIST_KEY); } } }
@Test public void testNoRenameOsgiClass() throws Exception { String bundlePath = OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_COM_EXAMPLE_PATH; String bundleUrl = "classpath:" + bundlePath; String classname = OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_COM_EXAMPLE_OBJECT; TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), bundlePath); mgmt = LocalManagementContextForTests.builder(true).enableOsgiReusable().build(); Bundle bundle = installBundle(mgmt, bundleUrl); String bundlePrefix = bundle.getSymbolicName(); Class<?> osgiObjectClazz = bundle.loadClass(classname); Object obj = Reflections.invokeConstructorFromArgs(osgiObjectClazz, "myval").get(); serializer = new XmlMementoSerializer<Object>(mgmt.getCatalogClassLoader(), ImmutableMap.<String,String>of()); serializer.setLookupContext(newEmptyLookupManagementContext(mgmt, true)); String serializedForm = Joiner.on("\n").join( "<"+bundlePrefix+":"+classname+">", " <val>myval</val>", "</"+bundlePrefix+":"+classname+">"); runRenamed(serializedForm, obj, ImmutableMap.<String, String>of()); }
@Test public void testRenamedOsgiClassMovedBundle() throws Exception { String bundlePath = OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_COM_EXAMPLE_PATH; String bundleUrl = "classpath:" + bundlePath; String classname = OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_COM_EXAMPLE_OBJECT; TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), bundlePath); mgmt = LocalManagementContextForTests.builder(true).enableOsgiReusable().build(); Bundle bundle = installBundle(mgmt, bundleUrl); String oldBundlePrefix = "com.old.symbolicname"; String bundlePrefix = bundle.getSymbolicName(); Class<?> osgiObjectClazz = bundle.loadClass(classname); Object obj = Reflections.invokeConstructorFromArgs(osgiObjectClazz, "myval").get(); serializer = new XmlMementoSerializer<Object>(mgmt.getCatalogClassLoader(), ImmutableMap.of(oldBundlePrefix + ":" + classname, bundlePrefix + ":" + classname)); serializer.setLookupContext(newEmptyLookupManagementContext(mgmt, true)); String serializedForm = Joiner.on("\n").join( "<"+bundlePrefix+":"+classname+">", " <val>myval</val>", "</"+bundlePrefix+":"+classname+">"); runRenamed(serializedForm, obj, ImmutableMap.<String, String>of( bundlePrefix + ":" + classname, oldBundlePrefix + ":" + classname)); }
@Test public void testRenamedOsgiClassWithoutBundlePrefixInRename() throws Exception { String bundlePath = OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_COM_EXAMPLE_PATH; String bundleUrl = "classpath:" + bundlePath; String classname = OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_COM_EXAMPLE_OBJECT; String oldClassname = "com.old.package.name.OldClassName"; TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), bundlePath); mgmt = LocalManagementContextForTests.builder(true).enableOsgiReusable().build(); Bundle bundle = installBundle(mgmt, bundleUrl); String bundlePrefix = bundle.getSymbolicName(); Class<?> osgiObjectClazz = bundle.loadClass(classname); Object obj = Reflections.invokeConstructorFromArgs(osgiObjectClazz, "myval").get(); serializer = new XmlMementoSerializer<Object>(mgmt.getCatalogClassLoader(), ImmutableMap.of(oldClassname, classname)); serializer.setLookupContext(newEmptyLookupManagementContext(mgmt, true)); String serializedForm = Joiner.on("\n").join( "<"+bundlePrefix+":"+classname+">", " <val>myval</val>", "</"+bundlePrefix+":"+classname+">"); runRenamed(serializedForm, obj, ImmutableMap.<String, String>of( bundlePrefix + ":" + classname, bundlePrefix + ":" + oldClassname)); }
@Test public void testOsgiBundleNamePrefixIncludedForDownstreamDependency() throws Exception { mgmt = LocalManagementContextForTests.builder(true).enableOsgiReusable().build(); serializer.setLookupContext(newEmptyLookupManagementContext(mgmt, true)); String bundleName = "com.goole.guava"; String classname = "com.google.common.base.Predicates_-ObjectPredicate"; Object obj = Predicates.alwaysTrue(); assertSerializeAndDeserialize(obj); String expectedFormInFelix = "<"+classname+">ALWAYS_TRUE</"+classname+">"; String expectedFormInKaraf = "<"+bundleName+":"+classname+">ALWAYS_TRUE</"+bundleName+":"+classname+">"; String serializedForm = serializer.toString(obj); Assert.assertTrue(serializedForm.trim().equals(expectedFormInFelix) || serializedForm.trim().equals(expectedFormInKaraf), "Should have matched either the karaf or the felix form, but was "+serializedForm); }
@Test public void testImmutableCollectionsWithDanglingEntityRef() throws Exception { final TestApplication app = TestApplication.Factory.newManagedInstanceForTests(); final TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class)); ManagementContext managementContext = app.getManagementContext(); try { serializer.setLookupContext(newEmptyLookupManagementContext(managementContext, false).add(app)); List<?> resultList = serializeAndDeserialize(ImmutableList.of(app, entity)); assertEquals(resultList, ImmutableList.of(app)); Set<?> resultSet = serializeAndDeserialize(ImmutableSet.of(app, entity)); assertEquals(resultSet, ImmutableSet.of(app)); Map<?, ?> resultMap = serializeAndDeserialize(ImmutableMap.of(app, "appval", "appkey", app, entity, "entityval", "entityKey", entity)); assertEquals(resultMap, ImmutableMap.of(app, "appval", "appkey", app)); } finally { Entities.destroyAll(managementContext); } }
@Test public void testFieldReffingEntity() throws Exception { final TestApplication app = TestApplication.Factory.newManagedInstanceForTests(); ReffingEntity reffer = new ReffingEntity(app); ManagementContext managementContext = app.getManagementContext(); try { serializer.setLookupContext(newEmptyLookupManagementContext(managementContext, true).add(app)); ReffingEntity reffer2 = assertSerializeAndDeserialize(reffer); assertEquals(reffer2.entity, app); } finally { Entities.destroyAll(managementContext); } }
@Test public void testUntypedFieldReffingEntity() throws Exception { final TestApplication app = TestApplication.Factory.newManagedInstanceForTests(); ReffingEntity reffer = new ReffingEntity((Object)app); ManagementContext managementContext = app.getManagementContext(); try { serializer.setLookupContext(newEmptyLookupManagementContext(managementContext, true).add(app)); ReffingEntity reffer2 = assertSerializeAndDeserialize(reffer); assertEquals(reffer2.obj, app); } finally { Entities.destroyAll(managementContext); } }