public static Bean copy(Bean bean) { if (bean == null) { return null; } Bean copy = Bean.create(bean.getId()); for (String property : bean.getPropertyNames()) { Collection<String> values = bean.getValues(property); if (values == null) { continue; } copy.setProperty(property, bean.getValues(property)); } for (String property : bean.getReferenceNames()) { List<BeanId> ids = bean.getReference(property); if (ids == null) { continue; } for (BeanId id : ids) { copy.addReference(property, BeanId.create(id.getInstanceId(), id.getSchemaName())); } } return copy; }
public static List<Bean> generateBeans(int numBeans, int numProps) { ArrayList<Bean> beans = new ArrayList<>(); for (int i = 0; i < numBeans; i++) { String id = "beanId" + i; String type = "beanType" + i; Bean bean = Bean.create(BeanId.create(id, type)); for (int j = 0; j < numProps; j++) { String _name = "propName" + j; String _value = "propFieldName" + j; bean.addProperty(_name, _value); List<String> d = Arrays.asList("1", "2", "3"); bean.addProperty(_name, d); } beans.add(bean); } return beans; }
/** * Create a admin bean instance. * * @param id unique identification of the bean instance. * @return AdminBean */ public static Bean create(final BeanId id) { Preconditions.checkNotNull(id); return new Bean(id, false); }
private Map<BeanId, Bean> getDirectSuccessors(Bean bean) { Map<BeanId, Bean> successors = new HashMap<>(); for (Bean b : storage.all()) { List<BeanId> refs = b.getReferences(); if (refs.contains(bean.getId())) { successors.put(b.getId(), b); } } return successors; }
private void replace(Bean target, Bean replace) { if (target == null) { // bean did not exist in storage, create it. target = replace; } checkReferencesExist(replace, new ArrayList<Bean>()); for (String name : replace.getPropertyNames()) { List<String> values = replace.getValues(name); if (values == null || values.size() == 0) { // null/empty indicates a remove/reset-to-default op target.remove(name); } else { target.setProperty(name, replace.getValues(name)); } } for (String name : replace.getReferenceNames()) { List<BeanId> values = replace.getReference(name); if (values == null || values.size() == 0) { // null/empty indicates a remove/reset-to-default op target.remove(name); } else { target.setReferences(name, values); } } }
Bean child = Bean.create(BeanId.create("child", "java.lang.String")); child.addProperty("property1", "true"); child.addProperty("property2", "false"); manager.create(child); Bean parent = Bean.create(BeanId.create("parent", "java.lang.String")); parent.addReference("refName", BeanId.create("child", "java.lang.String")); parent.addProperty("property1", "prop1"); parent.addProperty("property2", "prop2"); manager.create(parent); Bean grandparent = Bean.create(grandParentId); grandparent.addReference("refName", BeanId.create("parent", "java.lang.String")); manager.create(grandparent); List<String> childs = grandpa.get().getReferenceNames(); assertThat(childs.size(), is(1)); BeanId childRef = grandpa.get().getReference(childs.get(0)).get(0); Bean childBean = childRef.getBean(); assertThat(childBean.getId(), is(childBean.getId())); assertEquals(childBean.getSingleValue("property1"), "prop1"); assertEquals(childBean.getSingleValue("property2"), "prop2"); childs = childBean.getReferenceNames(); assertThat(childs.size(), is(1)); childRef = childBean.getReference(childs.get(0)).get(0); childBean = childRef.getBean(); assertThat(childBean.getId(), is(childBean.getId()));
@Override public BeanId apply(Bean input) { return input.getId(); }
public JaxrsBean(Bean bean) { this.schemaName = bean.getId().getSchemaName(); this.id = bean.getId().getInstanceId(); for (String name : bean.getPropertyNames()) { List<String> values = bean.getValues(name); if (values == null || values.isEmpty()) { continue; } properties.put(name, values); } for (String name : bean.getReferenceNames()) { List<BeanId> refs = bean.getReferences(); if (refs == null || refs.isEmpty()) { continue; } List<String> values = new ArrayList<>(); for (BeanId id : refs) { values.add(id.getInstanceId()); } properties.put(name, values); } }
public Bean traverse(Set<Bean> beans, Bean parent) { Bean current = Bean.create(BeanId.create(value + "", "binarytree")); current.setProperty("value", value + ""); if (parent != null) { current.setReference("parent", parent.getId()); } if (left != null) { Bean leftBean = left.traverse(beans, current); current.setReference("left", leftBean.getId()); } if (right != null) { Bean rightBean = right.traverse(beans, current); current.setReference("right", rightBean.getId()); } beans.add(current); return current; }
/** * Returns the a list of property names of the target bean that have * references to the bean id. */ private static boolean hasReferences(Bean target, BeanId reference) { for (String name : target.getReferenceNames()) { List<BeanId> refs = target.getReference(name); if (refs != null) { for (BeanId ref : target.getReference(name)) { if (ref.equals(reference)) { return true; } } } } return false; }
private static void checkReferencesExist(final Bean bean, Collection<Bean> additional) { HashMap<BeanId, Bean> additionalMap = new HashMap<>(); for (Bean b : additional) { additionalMap.put(b.getId(), b); } ArrayList<BeanId> allRefs = new ArrayList<>(); for (String name : bean.getReferenceNames()) { if (bean.getReference(name) == null) { // the reference is about to be removed. continue; } for (BeanId beanId : bean.getReference(name)) { allRefs.add(beanId); } } Collection<BeanId> missingReferences = new ArrayList<>(); for (BeanId beanId : allRefs) { if (beanId.getInstanceId() == null) { continue; } if (storage.get(beanId) == null && additionalMap.get(beanId) == null) { missingReferences.add(beanId); } } if (missingReferences.size() > 0) { throw CFG301_MISSING_RUNTIME_REF(bean.getId(), missingReferences); } }
@Test public void test_set_empty_properties() { createThenGet(c1); Bean b = Bean.create(BeanId.create("c1", CHILD_SCHEMA_NAME)); admin.set(b); Bean result = admin.get(b.getId()).get(); assertThat(result.getPropertyNames().size(), is(0)); }
private void setSingletonReferences(Bean bean) { Schema s = bean.getSchema(); for (SchemaPropertyRef ref : s.get(SchemaPropertyRef.class)) { if (ref.isSingleton()) { Schema singletonSchema = schemaManager.getSchema(ref.getSchemaName()); Optional<Bean> singleton = beanManager.getSingleton(ref.getSchemaName()); if (!singleton.isPresent()) { initFile(null); Bean fileBean = FILE_CONFIG.get(BeanId.createSingleton(ref.getSchemaName())); if (fileBean != null) { singleton = Optional.of(fileBean); } } if (!singleton.isPresent()) { singleton = Optional.of(Bean.create(BeanId.createSingleton(ref.getSchemaName()))); } singleton.get().set(singletonSchema); BeanId singletonId = singleton.get().getId(); singletonId.setBean(singleton.get()); // recursive call. setSingletonReferences(singleton.get()); bean.setReference(ref.getName(), singletonId); } } }
private void constructBean(Config properties, Schema schema, Bean bean, Map<String, Schema> schemas) { bean.set(schemas.get(bean.getId().getSchemaName())); for (String propertyName : schema.getPropertyNames()) { Object value; bean.addProperty(propertyName, toStrings((Collection) value)); } else { bean.addProperty(propertyName, value.toString()); b.set(schemas.get(b.getId().getSchemaName())); id.setBean(b); bean.addReference(propertyName, id); b.set(schemas.get(b.getId().getSchemaName())); beanId.setBean(b); beanIds.add(beanId); bean.addReference(propertyName, beanIds); } else { BeanId id = BeanId.create(value.toString(), refSchema.getName()); b.set(schemas.get(b.getId().getSchemaName())); id.setBean(b); bean.addReference(propertyName, id);
BeanId dId = BeanId.create("d", personSchema); Bean a = Bean.create(aId); Bean b = Bean.create(bId); Bean c = Bean.create(cId); Bean d = Bean.create(dId); a.setReference("bestFriend", bId); b.setReference("bestFriend", aId); c.setReference("bestFriend", dId); d.setReference("bestFriend", cId); a.addReference("closeFriends", Arrays.asList(bId, cId, dId)); b.addReference("closeFriends", Arrays.asList(aId, cId, dId)); c.addReference("closeFriends", Arrays.asList(aId, bId, dId)); d.addReference("closeFriends", Arrays.asList(aId, bId, cId)); a.addReference("colleauges", Arrays.asList(bId, cId, dId)); b.addReference("colleauges", Arrays.asList(aId, cId, dId)); c.addReference("colleauges", Arrays.asList(aId, bId, dId)); d.addReference("colleauges", Arrays.asList(aId, bId, cId));
Bean child = Bean.create(BeanId.create("c1", ConfigTestData.CHILD_SCHEMA_NAME)); child.setProperty("prop8", "100000"); admin.set(child); fail("10000 does not fit java.lang.Byte"); child.addProperty("prop3", "2.2"); admin.merge(child); fail("2.2 does not fit a collection of java.lang.Integer"); Bean parent = Bean.create(BeanId.create("g1", ConfigTestData.GRANDFATHER_SCHEMA_NAME)); try { parent.setProperty("prop14", "not_a_enum"); admin.set(parent); fail("not_a_enum is not a value of TimeUnit"); parent = Bean.create(BeanId.create("p1", ConfigTestData.PARENT_SCHEMA_NAME)); try { parent.addProperty("prop19", "not_a_enum"); admin.merge(parent); fail("not_a_enum is not a value of TimeUnit"); Bean grandfather = Bean.create(BeanId.create("g1", ConfigTestData.GRANDFATHER_SCHEMA_NAME)); try { grandfather.addProperty("prop1", Arrays.asList("1", "2")); admin.merge(grandfather); fail("Cannot add mutiple values to a single valued property."); grandfather = Bean.create(BeanId.create("p1", ConfigTestData.PARENT_SCHEMA_NAME));
@Test public void test_create_set_merge_non_existing_property() { createDefault(); Bean bean = Bean.create(c1.getBeanId()); bean.addProperty("non_existing", "bogus"); try { admin.create(bean); bean = Bean.create(BeanId.create("c5", ConfigTestData.CHILD_SCHEMA_NAME)); bean.setReference("non_existing", c1.getBeanId()); admin.create(bean); fail("Not possible to set property names that does not exist in schema"); assertThat(e.getEvent().getCode(), is(CFG111)); bean = Bean.create(c1.getBeanId()); bean.addProperty("non_existing", "bogus");
public void addReferences(int parentnum, int childnum, ConfigClass child) { addReferenceList(child.classname, child.classname); Bean[] b = beans.toArray(new Bean[0]); for (int i = 0; i < parentnum; i++) { List<Integer> numbers = randomUniqueInt(0, beans.size() - 1); while (parentnum-- > 0) { if (parentnum > b.length) { throw new IllegalArgumentException( "MINILANG: too few instances available [" + b.length + "] to reference [" + parentnum + "] instances."); } Bean parent = b[numbers.get(parentnum)]; Set<Bean> children = child.getInstances(childnum); for (Bean bean : children) { // circular references to self is not allowed. if (!bean.equals(parent)) { parent.addReference(bean.getId().getSchemaName(), bean.getId()); } } } } }