@Override public Map<String, String> toMetadataRecord() { ImmutableMap.Builder<String, String> builder = ImmutableMap.builder(); if (getDisplayName() != null) builder.put("displayName", getDisplayName()); if (getParent() != null && getParent().getDisplayName() != null) { builder.put("parentDisplayName", getParent().getDisplayName()); } return builder.build(); } }
@Test public void testSettingParentLocation() { AbstractLocation location = new ConcreteLocation(); AbstractLocation locationSub = new ConcreteLocation(); locationSub.setParent(location); assertEquals(ImmutableList.copyOf(location.getChildren()), ImmutableList.of(locationSub)); assertEquals(locationSub.getParent(), location); }
@Test public void nullNameAndParentLocationIsAcceptable() { AbstractLocation location = new ConcreteLocation(MutableMap.of("name", null, "parentLocation", null)); assertEquals(location.getDisplayName(), null); assertEquals(location.getParent(), null); }
@Test public void testClearingParentLocation() { AbstractLocation location = new ConcreteLocation(); AbstractLocation locationSub = new ConcreteLocation(); locationSub.setParent(location); locationSub.setParent(null); assertEquals(ImmutableList.copyOf(location.getChildren()), Collections.emptyList()); assertEquals(locationSub.getParent(), null); }
@Test public void constructorParentLocationReturnsExpectedLocation() { AbstractLocation parent = new ConcreteLocation(MutableMap.of("name", "Middle Earth")); AbstractLocation child = new ConcreteLocation(MutableMap.of("name", "The Shire", "parentLocation", parent)); assertEquals(child.getParent(), parent); assertEquals(ImmutableList.copyOf(parent.getChildren()), ImmutableList.of(child)); }
@Test public void testAddChildToParentLocationReturnsExpectedLocation() { AbstractLocation parent = new ConcreteLocation(MutableMap.of("id", "1")); AbstractLocation child = new ConcreteLocation(MutableMap.of("id", "2")); parent.addChild(child); assertEquals(child.getParent(), parent); assertEquals(ImmutableList.copyOf(parent.getChildren()), ImmutableList.of(child)); }
@Test public void setParentLocationReturnsExpectedLocation() { AbstractLocation parent = new ConcreteLocation(MutableMap.of("name", "Middle Earth")); AbstractLocation child = new ConcreteLocation(MutableMap.of("name", "The Shire")); child.setParent(parent); assertEquals(child.getParent(), parent); assertEquals(ImmutableList.copyOf(parent.getChildren()), ImmutableList.of(child)); }
@SuppressWarnings({ "rawtypes", "unchecked" }) private synchronized void unmanageNonRecursiveClearItsFields(Location loc, ManagementTransitionMode mode) { if (mode.isDestroying()) { ((AbstractLocation)loc).setParent(null, true); Location parent = ((AbstractLocation)loc).getParent(); if (parent instanceof ProvisioningLocation<?>) { try { ((ProvisioningLocation)parent).release(loc); } catch (Exception e) { Exceptions.propagateIfFatal(e); log.debug("Error releasing "+loc+" in its parent "+parent+": "+e); } } } else { // if not destroying, don't change the parent's children list ((AbstractLocation)loc).setParent(null, false); } // clear config to help with GC; everyone says you're not supposed to, but this really seems to help, // else config bag is littered with refs to entities etc and some JVMs seem to keep them around much longer ((AbstractLocation)loc).config().removeAllLocalConfig(); }