@Test public void shouldUseAnnotationsOnClassFieldsForProperties() throws Exception { SomeStructure structure = new SomeStructure(); structure.setCount(33); structure.setIdentifier("This is the identifier value"); Reflection reflection = new Reflection(SomeStructure.class); List<Property> props = reflection.getAllPropertiesOn(structure); Map<String, Property> propsByName = reflection.getAllPropertiesByNameOn(structure); assertThat(props.size(), is(3)); assertThat(propsByName.size(), is(3)); Property property = propsByName.remove("identifier"); assertThat(property.getName(), is("identifier")); assertThat(property.getLabel(), is(CommonI18n.noMoreContent.text())); assertThat(property.getDescription(), is(CommonI18n.nullActivityMonitorTaskName.text())); assertThat(property.getCategory(), is(CommonI18n.noMoreContent.text())); assertThat(property.getType().equals(String.class), is(true)); assertThat(property.isReadOnly(), is(false)); assertThat(property, is(findProperty(property.getName(), props))); assertValue(reflection, structure, property, structure.getIdentifier()); property = propsByName.remove("count"); assertThat(property.getName(), is("count")); assertThat(property.getLabel(), is("Count")); assertThat(property.getDescription(), is("This is the count")); assertThat(property.getCategory(), is("")); assertThat(property.getType().equals(Integer.TYPE), is(true)); assertThat(property.isReadOnly(), is(false)); assertValue(reflection, structure, property, structure.getCount()); property = propsByName.remove("onFire"); assertThat(property.getName(), is("onFire")); assertThat(property.getLabel(), is("On Fire")); assertThat(property.getDescription(), is("")); assertThat(property.getCategory(), is("")); assertThat(property.getType().equals(Boolean.TYPE), is(true)); assertThat(property.isReadOnly(), is(true)); assertValue(reflection, structure, property, structure.isOnFire()); }
@Test public void shouldGetAllPropertiesOnJavaBean() throws Exception { Status status = Status.INFO; int code = 121; I18n msg = CommonI18n.argumentMayNotBeEmpty; Object[] params = new Object[] {"argName"}; String resource = "The source"; String location = "The place to be"; Throwable throwable = null; Problem problem = new Problem(status, code, msg, params, resource, location, throwable); Reflection reflection = new Reflection(Problem.class); List<Property> props = reflection.getAllPropertiesOn(problem); Map<String, Property> propsByName = reflection.getAllPropertiesByNameOn(problem); assertThat(props.size(), is(8)); assertThat(propsByName.size(), is(8)); assertThat(props.containsAll(propsByName.values()), is(true)); Property property = propsByName.remove("status"); assertThat(property.getName(), is("status")); assertThat(property.getLabel(), is("Status")); assertThat(property.getType().equals(Status.class), is(true)); assertThat(property.isReadOnly(), is(true)); assertThat(property, is(findProperty(property.getName(), props))); assertValue(reflection, problem, property, status); property = propsByName.remove("code"); assertThat(property.getName(), is("code")); assertThat(property.getLabel(), is("Code")); assertThat(property.getType().equals(Integer.TYPE), is(true)); assertThat(property.isReadOnly(), is(true)); assertValue(reflection, problem, property, code); property = propsByName.remove("message"); assertThat(property.getName(), is("message")); assertThat(property.getLabel(), is("Message")); assertThat(property.getType().equals(I18n.class), is(true)); assertThat(property.isReadOnly(), is(true)); assertValue(reflection, problem, property, msg); property = propsByName.remove("messageString"); assertThat(property.getName(), is("messageString")); assertThat(property.getLabel(), is("Message String")); assertThat(property.getType().equals(String.class), is(true)); assertThat(property.isReadOnly(), is(true)); assertValue(reflection, problem, property, msg.text(params)); property = propsByName.remove("parameters"); assertThat(property.getName(), is("parameters")); assertThat(property.getLabel(), is("Parameters")); assertThat(property.getType().equals(Object[].class), is(true)); assertThat(property.isReadOnly(), is(true)); assertValue(reflection, problem, property, params); property = propsByName.remove("resource"); assertThat(property.getName(), is("resource")); assertThat(property.getLabel(), is("Resource")); assertThat(property.getType().equals(String.class), is(true)); assertThat(property.isReadOnly(), is(true)); assertValue(reflection, problem, property, resource); property = propsByName.remove("location"); assertThat(property.getName(), is("location")); assertThat(property.getLabel(), is("Location")); assertThat(property.getType().equals(String.class), is(true)); assertThat(property.isReadOnly(), is(true)); assertValue(reflection, problem, property, location); property = propsByName.remove("throwable"); assertThat(property.getName(), is("throwable")); assertThat(property.getLabel(), is("Throwable")); assertThat(property.getType().equals(Throwable.class), is(true)); assertThat(property.isReadOnly(), is(true)); assertValue(reflection, problem, property, throwable); assertThat(propsByName.isEmpty(), is(true)); }