public void testSanitizeName() throws Exception { IMetaStore metaStore = new MemoryMetaStore(); MetaStoreFactory<MyOtherElement> factory = new MetaStoreFactory<MyOtherElement>(MyOtherElement.class, metaStore, "custom"); MyOtherElement element = new MyOtherElement(null, ATTR); try { factory.saveElement(element); fail("Saved illegal element (name == null)"); } catch (MetaStoreException e) { assertNotNull(e); } try { element.setName(""); factory.saveElement(element); fail("Saved illegal element (name.isEmpty())"); } catch (MetaStoreException e) { assertNotNull(e); } try { element.setName(" "); factory.saveElement(element); fail("Saved illegal element (name.isEmpty())"); } catch (MetaStoreException e) { assertNotNull(e); } element.setName(NAME); factory.saveElement(element); assertEquals(Arrays.asList(NAME), factory.getElementNames()); }
@Test public void testMyElement() throws Exception { IMetaStore metaStore = new MemoryMetaStore(); // List of named elements... // List<MyNameElement> nameList = new ArrayList<MyNameElement>(); for (int i = 0; i < NR_NAME; i++) { nameList.add(new MyNameElement("name" + i, "description" + i, "color" + i)); } List<MyFilenameElement> filenameList = new ArrayList<MyFilenameElement>(); for (int i = 0; i < NR_FILENAME; i++) { filenameList.add(new MyFilenameElement("filename" + i, "size" + i, "gender" + i)); } // Construct our test element... // MyElement me = new MyElement(NAME, ATTR, ANOTHER, PASSWORD, INT, LONG, BOOL, DATE); for (int i = 0; i < NR_ATTR; i++) { me.getSubAttributes().add(new MyElementAttr("key" + i, "value" + i, "desc" + i)); } me.setNameElement(nameList.get(NR_NAME - 1)); me.setFilenameElement(filenameList.get(NR_FILENAME - 1)); List<String> stringList = Arrays.asList("a", "b", "c", "d"); me.setStringList(stringList); MyOtherElement myOtherElement = new MyOtherElement("other", "other attribute"); me.setMyOtherElement(myOtherElement); MetaStoreFactory<MyOtherElement> otherFactory = new MetaStoreFactory<MyOtherElement>(MyOtherElement.class, metaStore, "custom"); MetaStoreFactory<MyElement> factory = new MetaStoreFactory<MyElement>(MyElement.class, metaStore, "custom"); // For loading, specify the name, filename lists or factory that we're referencing... // factory.addNameList(MyElement.LIST_KEY_MY_NAMES, nameList); factory.addFilenameList(MyElement.LIST_KEY_MY_FILENAMES, filenameList); factory.addNameFactory(MyElement.FACTORY_OTHER_ELEMENT, otherFactory); // Store the class in the meta store // factory.saveElement(me); // Load the class from the meta store // MyElement verify = factory.loadElement(NAME); // Verify list element details... // IMetaStoreElement element = metaStore.getElementByName("custom", factory.getElementType(), NAME); assertNotNull(element); // Verify the general idea // assertNotNull(verify); assertEquals(ATTR, verify.getMyAttribute()); assertEquals(ANOTHER, verify.getAnotherAttribute()); assertEquals(PASSWORD, verify.getPasswordAttribute()); assertEquals(INT, verify.getIntAttribute()); assertEquals(LONG, verify.getLongAttribute()); assertEquals(BOOL, verify.isBoolAttribute()); assertEquals(DATE, verify.getDateAttribute()); assertEquals(me.getSubAttributes().size(), verify.getSubAttributes().size()); assertEquals(me.getNameElement(), verify.getNameElement()); assertEquals(me.getFilenameElement(), verify.getFilenameElement()); // verify the details... // assertTrue(metaStore.namespaceExists("custom")); IMetaStoreElementType elementType = factory.getElementType(); assertNotNull(elementType); assertEquals("My element type", elementType.getName()); assertEquals("This is my element type", elementType.getDescription()); assertNotNull(element); IMetaStoreAttribute child = element.getChild("my_attribute"); assertNotNull(child); assertEquals(ATTR, MetaStoreUtil.getAttributeString(child)); child = element.getChild("passwordAttribute"); assertNotNull(child); assertNotSame( "Password needs to be encoded", PASSWORD, MetaStoreUtil.getAttributeString(child)); child = element.getChild("anotherAttribute"); assertNotNull(child); assertEquals(ANOTHER, MetaStoreUtil.getAttributeString(child)); // Verify the child attributes as well... // This also verifies that the attributes are in the right order. // The list can't be re-ordered after loading. // for (int i = 0; i < NR_ATTR; i++) { MyElementAttr attr = verify.getSubAttributes().get(i); assertEquals("key" + i, attr.getKey()); assertEquals("value" + i, attr.getValue()); assertEquals("desc" + i, attr.getDescription()); } // Verify the referenced MyOtherElement // MyOtherElement verifyOtherElement = verify.getMyOtherElement(); assertNotNull(verifyOtherElement); assertEquals(myOtherElement.getName(), verifyOtherElement.getName()); assertEquals(myOtherElement.getSomeAttribute(), verifyOtherElement.getSomeAttribute()); // verify that the String list is loaded... List<String> verifyList = verify.getStringList(); assertEquals(stringList.size(), verifyList.size()); for (int i = 0; i < stringList.size(); i++) { assertEquals(stringList.get(i), verifyList.get(i)); } List<String> names = factory.getElementNames(); assertEquals(1, names.size()); assertEquals(NAME, names.get(0)); List<MyElement> list = factory.getElements(); assertEquals(1, list.size()); assertEquals(NAME, list.get(0).getName()); factory.deleteElement(NAME); assertEquals(0, factory.getElementNames().size()); assertEquals(0, factory.getElements().size()); }