Test with a 'catalog sandwich'. The purpose of this test is to illustrate that the full stack of nested catalog items is not searched for classloaders.
Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/39dc1ccd Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/39dc1ccd Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/39dc1ccd Branch: refs/heads/master Commit: 39dc1ccdf539a4453889f91ad1d3d6cde11abaf7 Parents: a9e9618 Author: Geoff Macartney <[email protected]> Authored: Wed Sep 21 11:46:35 2016 +0100 Committer: Geoff Macartney <[email protected]> Committed: Thu Apr 20 11:18:42 2017 +0100 ---------------------------------------------------------------------- .../brooklyn/catalog/CatalogYamlEntityTest.java | 100 ++++++++++++++----- .../core/mgmt/osgi/OsgiStandaloneTest.java | 2 + .../brooklyn/util/osgi/OsgiTestResources.java | 2 + 3 files changed, 80 insertions(+), 24 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/39dc1ccd/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/catalog/CatalogYamlEntityTest.java ---------------------------------------------------------------------- diff --git a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/catalog/CatalogYamlEntityTest.java b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/catalog/CatalogYamlEntityTest.java index 4e6cfb8..a617250 100644 --- a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/catalog/CatalogYamlEntityTest.java +++ b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/catalog/CatalogYamlEntityTest.java @@ -18,6 +18,9 @@ */ package org.apache.brooklyn.camp.brooklyn.catalog; +import static org.apache.brooklyn.util.osgi.OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_MESSAGE_RESOURCE; +import static org.apache.commons.io.FileUtils.getFile; +import static org.apache.commons.io.FileUtils.readFileToString; import static com.google.common.base.Preconditions.checkNotNull; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; @@ -34,13 +37,20 @@ import org.apache.brooklyn.camp.brooklyn.AbstractYamlTest; import org.apache.brooklyn.config.ConfigKey; import org.apache.brooklyn.core.catalog.internal.CatalogUtils; import org.apache.brooklyn.core.config.ConfigKeys; +import org.apache.brooklyn.core.entity.BrooklynConfigKeys; +import org.apache.brooklyn.core.mgmt.osgi.OsgiStandaloneTest; import org.apache.brooklyn.core.test.entity.TestEntity; import org.apache.brooklyn.core.test.entity.TestEntityImpl; import org.apache.brooklyn.core.typereg.RegisteredTypes; import org.apache.brooklyn.entity.software.base.SoftwareProcess; +import org.apache.brooklyn.entity.software.base.VanillaSoftwareProcess; import org.apache.brooklyn.entity.stock.BasicApplication; import org.apache.brooklyn.entity.stock.BasicEntity; +import org.apache.brooklyn.test.support.TestResourceUnavailableException; +import org.apache.brooklyn.util.core.ResourceUtils; import org.apache.brooklyn.util.exceptions.Exceptions; +import org.apache.brooklyn.util.osgi.OsgiTestResources; +import org.apache.commons.io.FileUtils; import org.testng.Assert; import org.testng.annotations.Test; @@ -50,6 +60,15 @@ import com.google.common.collect.Iterables; public class CatalogYamlEntityTest extends AbstractYamlTest { + private static final String SIMPLE_ENTITY_TYPE = OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_ENTITY; + private static final String MORE_ENTITIES_POM_PROPERTIES_PATH = + "META-INF/maven/org.apache.brooklyn.test.resources.osgi/brooklyn-test-osgi-more-entities/pom.properties"; + + @Override + protected boolean disableOsgi() { + return false; + } + @Test public void testAddCatalogItemVerySimple() throws Exception { String symbolicName = "my.catalog.app.id.load"; @@ -57,7 +76,7 @@ public class CatalogYamlEntityTest extends AbstractYamlTest { RegisteredType item = mgmt().getTypeRegistry().get(symbolicName, TEST_VERSION); String planYaml = RegisteredTypes.getImplementationDataStringForSpec(item); - assertTrue(planYaml.indexOf("services:")>=0, "expected 'services:' block: "+item+"\n"+planYaml); + assertTrue(planYaml.contains("services:"), "expected 'services:' block: "+item+"\n"+planYaml); deleteCatalogEntity(symbolicName); } @@ -178,7 +197,7 @@ public class CatalogYamlEntityTest extends AbstractYamlTest { public void testLaunchApplicationWithCatalogReferencingOtherCatalog() throws Exception { String referencedSymbolicName = "my.catalog.app.id.referenced"; String referrerSymbolicName = "my.catalog.app.id.referring"; - + addCatalogItems( "brooklyn.catalog:", " name: My Catalog App", @@ -192,10 +211,10 @@ public class CatalogYamlEntityTest extends AbstractYamlTest { " - id: " + referrerSymbolicName, " item:", " type: " + ver(referencedSymbolicName, TEST_VERSION)); - + RegisteredType referrer = mgmt().getTypeRegistry().get(referrerSymbolicName, TEST_VERSION); String planYaml = RegisteredTypes.getImplementationDataStringForSpec(referrer); - Assert.assertTrue(planYaml.indexOf("services")>=0, "expected services in: "+planYaml); + Assert.assertTrue(planYaml.contains("services"), "expected services in: "+planYaml); Entity app = createAndStartApplication("services:", "- type: " + ver(referrerSymbolicName, TEST_VERSION)); @@ -229,7 +248,7 @@ public class CatalogYamlEntityTest extends AbstractYamlTest { public void testLaunchApplicationChildWithCatalogReferencingOtherCatalog() throws Exception { String referencedSymbolicName = "my.catalog.app.id.child.referenced"; String referrerSymbolicName = "my.catalog.app.id.child.referring"; - + addCatalogEntity(IdAndVersion.of(referencedSymbolicName, TEST_VERSION), TestEntity.class.getName()); addCatalogItems( @@ -242,7 +261,7 @@ public class CatalogYamlEntityTest extends AbstractYamlTest { " - type: " + BasicEntity.class.getName(), " brooklyn.children:", " - type: " + ver(referencedSymbolicName, TEST_VERSION)); - + Entity app = createAndStartApplication( "services:", "- type: "+BasicEntity.class.getName(), @@ -358,7 +377,7 @@ public class CatalogYamlEntityTest extends AbstractYamlTest { public void testCreateSpecFromCatalogItem() { String id = "my.catalog.app.id.create_spec"; addCatalogEntity(IdAndVersion.of(id, TEST_VERSION), TestEntity.class.getName()); - + BrooklynTypeRegistry catalog = mgmt().getTypeRegistry(); RegisteredType item = catalog.get(id, TEST_VERSION); EntitySpec<?> spec = catalog.createSpec(item, null, EntitySpec.class); @@ -366,7 +385,7 @@ public class CatalogYamlEntityTest extends AbstractYamlTest { AbstractBrooklynObjectSpec<?,?> spec2 = catalog.createSpec(item, null, null); Assert.assertNotNull(spec2); } - + @Test public void testMissingTypeDoesNotRecurse() { String symbolicName = "my.catalog.app.id.basic"; @@ -382,8 +401,8 @@ public class CatalogYamlEntityTest extends AbstractYamlTest { @Test public void testVersionedTypeDoesNotRecurse() throws Exception { - // Alternatively, we could change this to tell foo:v2 reference foo:v1, but that feels - // like a bad idea! + // Alternatively, we could change this to tell foo:v2 reference foo:v1, but that feels + // like a bad idea! String symbolicName = "my.catalog.app.id.basic"; addCatalogEntity(IdAndVersion.of(symbolicName, TEST_VERSION), TestEntity.class.getName()); @@ -400,7 +419,7 @@ public class CatalogYamlEntityTest extends AbstractYamlTest { public void testIndirectRecursionFails() throws Exception { String callerSymbolicName = "my.catalog.app.id.caller"; String calleeSymbolicName = "my.catalog.app.id.callee"; - + // Need to have a stand alone caller first so we can create an item to depend on it. // After that replace it/insert a new version which completes the cycle addCatalogEntity(IdAndVersion.of(callerSymbolicName, TEST_VERSION + "-pre"), TestEntity.class.getName()); @@ -422,7 +441,7 @@ public class CatalogYamlEntityTest extends AbstractYamlTest { // Need to have a stand alone caller first so we can create an item to depend on it. // After that replace it/insert a new version which completes the cycle - + addCatalogEntity(IdAndVersion.of(callerSymbolicName, TEST_VERSION + "-pre"), TestEntity.class.getName()); addCatalogEntity(IdAndVersion.of(calleeSymbolicName, TEST_VERSION), callerSymbolicName); @@ -498,7 +517,7 @@ public class CatalogYamlEntityTest extends AbstractYamlTest { Entity testEntity = Iterables.getOnlyElement(app.getChildren()); assertEquals(testEntity.config().get(TestEntity.CONF_NAME), val); } - + @Test public void testConfigAppliedToCatalogItemImpl() throws Exception { addCatalogEntity("test", TestEntityImpl.class.getName()); @@ -613,11 +632,45 @@ public class CatalogYamlEntityTest extends AbstractYamlTest { mgmt().getCatalog().deleteCatalogItem(id, version); } - // The test is disabled as it fails. The entity will get assigned the outer-most catalog - // item which doesn't have the necessary libraries with visibility to the entity's classpath - // When loading resources from inside the entity then we will use the wrong BCLCS. A workaround - // has been implemented which explicitly adds the entity's class loader to the fallbacks. - @Test(groups="WIP") + @Test + public void testDeepCatalogItemCanLoadResources() throws Exception { + TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH); + + String symbolicNameInner = "my.catalog.app.id.inner"; + String symbolicNameFiller = "my.catalog.app.id.filler"; + String symbolicNameOuter = "my.catalog.app.id.outer"; + addCatalogItems( + "brooklyn.catalog:", + " version: " + TEST_VERSION, + " items:", + " - id: " + symbolicNameInner, + " name: My Catalog App", + " brooklyn.libraries:", + " - url: " + OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_URL, + " item: " + SIMPLE_ENTITY_TYPE, + " - id: " + symbolicNameFiller, + " name: Filler App", + " brooklyn.libraries:", + " - url: " + OsgiStandaloneTest.BROOKLYN_TEST_OSGI_MORE_ENTITIES_0_1_0_URL, + " item: " + symbolicNameInner, + " - id: " + symbolicNameOuter, + " item: " + symbolicNameFiller); + + String yaml = "name: " + symbolicNameOuter + "\n" + + "services: \n" + + " - serviceType: "+ver(symbolicNameOuter); + Entity app = createAndStartApplication(yaml); + Entity entity = app.getChildren().iterator().next(); + + final String catalogBom = ResourceUtils.create(entity).getResourceAsString("classpath://" + MORE_ENTITIES_POM_PROPERTIES_PATH); + assertTrue(catalogBom.contains("artifactId=brooklyn-test-osgi-more-entities")); + + deleteCatalogEntity(symbolicNameOuter); + deleteCatalogEntity(symbolicNameFiller); + deleteCatalogEntity(symbolicNameInner); + } + + @Test public void testCatalogItemIdInReferencedItems() throws Exception { String symbolicNameInner = "my.catalog.app.id.inner"; String symbolicNameOuter = "my.catalog.app.id.outer"; @@ -637,7 +690,6 @@ public class CatalogYamlEntityTest extends AbstractYamlTest { Entity app = createAndStartApplication(yaml); Entity entity = app.getChildren().iterator().next(); - assertEquals(entity.getCatalogItemId(), ver(symbolicNameOuter)); assertEquals(entity.getCatalogItemSuperIds().size(), 2); assertEquals(entity.getCatalogItemSuperIds().get(0), ver(symbolicNameOuter)); @@ -650,10 +702,10 @@ public class CatalogYamlEntityTest extends AbstractYamlTest { private void registerAndLaunchAndAssertSimpleEntity(String symbolicName, String serviceType) throws Exception { registerAndLaunchAndAssertSimpleEntity(symbolicName, serviceType, serviceType); } - + private void registerAndLaunchAndAssertSimpleEntity(String symbolicName, String serviceType, String expectedType) throws Exception { addCatalogEntity(IdAndVersion.of(symbolicName, TEST_VERSION), serviceType); - + Entity app = createAndStartApplication( "services:", "- type: "+ver(symbolicName, TEST_VERSION)); @@ -667,11 +719,11 @@ public class CatalogYamlEntityTest extends AbstractYamlTest { public static class IdAndVersion { public final String id; public final String version; - + public static IdAndVersion of(String id, String version) { return new IdAndVersion(id, version); } - + public IdAndVersion(String id, String version) { this.id = checkNotNull(id, "id"); this.version = checkNotNull(version, "version"); @@ -681,7 +733,7 @@ public class CatalogYamlEntityTest extends AbstractYamlTest { private void addCatalogEntity(String symbolicName, String entityType) { addCatalogEntity(IdAndVersion.of(symbolicName, TEST_VERSION), entityType); } - + private void addCatalogEntity(IdAndVersion idAndVersion, String serviceType) { addCatalogItems( "brooklyn.catalog:", http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/39dc1ccd/core/src/test/java/org/apache/brooklyn/core/mgmt/osgi/OsgiStandaloneTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/core/mgmt/osgi/OsgiStandaloneTest.java b/core/src/test/java/org/apache/brooklyn/core/mgmt/osgi/OsgiStandaloneTest.java index 35429f1..15b9ae9 100644 --- a/core/src/test/java/org/apache/brooklyn/core/mgmt/osgi/OsgiStandaloneTest.java +++ b/core/src/test/java/org/apache/brooklyn/core/mgmt/osgi/OsgiStandaloneTest.java @@ -50,7 +50,9 @@ public class OsgiStandaloneTest extends OsgiTestBase { public static final String BROOKLYN_TEST_OSGI_ENTITIES_PATH = OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_PATH; public static final String BROOKLYN_TEST_OSGI_ENTITIES_SYMBOLIC_NAME_FULL = OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_SYMBOLIC_NAME_FULL; + public static final String BROOKLYN_TEST_OSGI_MORE_ENTITIES_0_1_0_PATH = OsgiTestResources.BROOKLYN_TEST_OSGI_MORE_ENTITIES_0_1_0_PATH; public static final String BROOKLYN_TEST_OSGI_ENTITIES_URL = "classpath:"+BROOKLYN_TEST_OSGI_ENTITIES_PATH; + public static final String BROOKLYN_TEST_OSGI_MORE_ENTITIES_0_1_0_URL = "classpath:"+BROOKLYN_TEST_OSGI_MORE_ENTITIES_0_1_0_PATH; public static final String BROOKLYN_TEST_OSGI_ENTITIES_NAME = "org.apache.brooklyn.test.resources.osgi.brooklyn-test-osgi-entities"; public static final String BROOKLYN_TEST_OSGI_ENTITIES_VERSION = "0.1.0"; http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/39dc1ccd/utils/common/src/test/java/org/apache/brooklyn/util/osgi/OsgiTestResources.java ---------------------------------------------------------------------- diff --git a/utils/common/src/test/java/org/apache/brooklyn/util/osgi/OsgiTestResources.java b/utils/common/src/test/java/org/apache/brooklyn/util/osgi/OsgiTestResources.java index 39ae99c..b778dc9 100644 --- a/utils/common/src/test/java/org/apache/brooklyn/util/osgi/OsgiTestResources.java +++ b/utils/common/src/test/java/org/apache/brooklyn/util/osgi/OsgiTestResources.java @@ -44,6 +44,8 @@ public class OsgiTestResources { public static final String BROOKLYN_TEST_OSGI_ENTITIES_SYMBOLIC_NAME_FULL = "org.apache.brooklyn.test.resources.osgi."+BROOKLYN_TEST_OSGI_ENTITIES_SYMBOLIC_NAME_FINAL_PART; public static final String BROOKLYN_TEST_OSGI_ENTITIES_PATH = "/brooklyn/osgi/brooklyn-test-osgi-entities.jar"; + public static final String BROOKLYN_TEST_OSGI_MORE_ENTITIES_0_1_0_PATH = "/brooklyn/osgi/brooklyn-test-osgi-more-entities_0.1.0.jar"; + public static final String BROOKLYN_TEST_OSGI_ENTITIES_MESSAGE_RESOURCE = "/org/apache/brooklyn/test/osgi/resources/message.txt"; public static final String BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_APPLICATION = "org.apache.brooklyn.test.osgi.entities.SimpleApplication"; public static final String BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_ENTITY = "org.apache.brooklyn.test.osgi.entities.SimpleEntity"; public static final String BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_POLICY = "org.apache.brooklyn.test.osgi.entities.SimplePolicy";
