public void testGetStoredFields() { assertStoredField(Item.class, FIELD_NAME, ITEM_NAME, String.class, true, false); assertStoredField(ItemParent.class, "_array", null, String.class, false, true); StoredClass itemStoredClass = itemStoredClass(); StoredField storedField = itemStoredClass.storedField(FIELD_NAME, null); StoredField sameStoredField = itemStoredClass.getStoredFields()[0]; StoredField otherStoredField = storedClass(ItemParent.class).getStoredFields()[0]; Assert.equalsAndHashcode(storedField, sameStoredField, otherStoredField); Assert.isNull(itemStoredClass.storedField("", null)); }
private void assertStoredField( Class objectClass, String fieldName, final Object expectedFieldValue, Class expectedFieldType, boolean hasIndex, boolean isArray) { StoredClass storedClass = storedClass(objectClass); StoredField[] storedFields = storedClass.getStoredFields(); Assert.areEqual(1, storedFields.length); final StoredField storedField = storedFields[0]; Assert.areEqual(fieldName, storedField.getName()); StoredField storedFieldByName = storedClass.storedField(fieldName, expectedFieldType); Assert.areEqual(storedField, storedFieldByName); Object item = retrieveOnlyInstance(objectClass); Assert.areEqual(expectedFieldValue, storedField.get(item)); ReflectClass fieldType = storedField.getStoredType(); Assert.areEqual(reflector().forClass(expectedFieldType), fieldType); Assert.areEqual(isArray, storedField.isArray()); if (isMultiSession()) { return; } Assert.areEqual(hasIndex, storedField.hasIndex()); // FIXME: test rename if (!hasIndex) { Assert.expect( RuntimeException.class, new CodeBlock() { public void run() throws Throwable { storedField.traverseValues( new Visitor4() { public void visit(Object obj) {} }); } }); } else { final IntByRef count = new IntByRef(); storedField.traverseValues( new Visitor4() { public void visit(Object obj) { count.value++; Assert.areEqual(expectedFieldValue, obj); } }); Assert.areEqual(1, count.value); } }