@Test(expectedExceptions = { PropertyAccessException.class }) public void nullStartPath() throws PropertyAccessException { propertyAccessor.getPropertyContent(null, null, null, null, false); }
@BeforeClass public void initTestClass() { propertyAccessor = new PropertyAccessor(); propertyAccessor.log = LoggerFactory.getLogger(PropertyAccessor.class); }
if (!isAcceptedMethod(methodName)) { throw new PropertyAccessException("Method " + methodName + " MAY not be called!"); if (object.getClass().isArray()) { // ensure that we are really return getPropertyContent(propertyPath.getPathToContinue(), Integer.valueOf(Array.getLength(object))); } else { log.error("Trying to access the lenght() method for a non array type"); return getPropertyContent(propertyPath.getPathToContinue(), result); } catch (IllegalArgumentException e) { log.error(e.getMessage()); field.setAccessible(true); Object fieldObject = field.get(object); return getPropertyContent(propertyPath.getPathToContinue(), fieldObject); } catch (SecurityException e) { log.error(e.getMessage());
List<ParameterContentData> parameterContentList = propertyAccessor.getParameterContentData(propertyAccessorList, peter, new Object[] { peter }, resultValueMock, false);
List<ParameterContentData> parameterContentList = propertyAccessor.getParameterContentData(propertyAccessorList, peter, new Object[] { peter }, null, false);
@Test public void nullNeededResultObjectForResultAccess() throws PropertyAccessException { PropertyPathStart start = new PropertyPathStart(); start.setName("this"); start.setContentType(ParameterContentType.RETURN); String result = propertyAccessor.getPropertyContent(start, null, null, null, false); assertThat(result, is(equalTo("null"))); }
@Test public void analyzeReturnValueString() throws PropertyAccessException { // valid PropertyPathStart start = new PropertyPathStart(); start.setName("returnName"); start.setContentType(ParameterContentType.RETURN); String result = propertyAccessor.getPropertyContent(start, null, null, "Peter", false); assertThat(result, is("Peter")); }
/** * {@inheritDoc} */ @Override public List<ParameterContentData> getParameterContentData(List<PropertyPathStart> propertyAccessorList, Object clazz, Object[] parameters, Object returnValue, boolean exception) { List<ParameterContentData> parameterContentData = new ArrayList<ParameterContentData>(); for (PropertyPathStart start : propertyAccessorList) { try { String content = this.getPropertyContent(start, clazz, parameters, returnValue, exception); ParameterContentData paramContentData = new ParameterContentData(); paramContentData.setContent(content); paramContentData.setContentType(start.getContentType()); paramContentData.setName(start.getName()); paramContentData.setSignaturePosition(start.getSignaturePosition()); parameterContentData.add(paramContentData); } catch (PropertyAccessException e) { if (log.isErrorEnabled()) { log.error("Cannot access the property: " + start + " for class " + clazz + ". Will be removed from the list to prevent further errors! (" + e.getMessage() + ")"); } propertyAccessorList.remove(start); // iterator.remove(); // Unsupported exception. Iterator can't make changes, since // iterating over a snapshot. } } return parameterContentData; }
@Test(expectedExceptions = { PropertyAccessException.class }) public void nullNeededClassObjectForFieldAccess() throws PropertyAccessException { PropertyPathStart start = new PropertyPathStart(); start.setName("this"); start.setContentType(ParameterContentType.FIELD); propertyAccessor.getPropertyContent(start, null, null, resultValueMock, false); Mockito.verifyZeroInteractions(resultValueMock); }
@Test public void analyzeReturnValueVoidMethod() throws PropertyAccessException { // create initial object relation Person peter = new Person("Peter"); // valid PropertyPathStart start = new PropertyPathStart(); start.setName("setName"); start.setContentType(ParameterContentType.RETURN); String result = propertyAccessor.getPropertyContent(start, null, null, peter, false); assertThat(result, is("Peter")); }
@Test(expectedExceptions = { PropertyAccessException.class }) public void missingContentType() throws PropertyAccessException { PropertyPathStart start = new PropertyPathStart(); start.setName("name"); start.setSignaturePosition(0); propertyAccessor.getPropertyContent(start, null, null, null, false); }
@Test public void readFieldPersonObject() throws PropertyAccessException { Person person = new Person(); person.setName("Dirk"); PropertyPathStart start = new PropertyPathStart(); start.setName("this"); start.setContentType(ParameterContentType.FIELD); String result = propertyAccessor.getPropertyContent(start, person, null, resultValueMock, false); assertThat(result, is("Dirk")); Mockito.verifyZeroInteractions(resultValueMock); }
@Test(expectedExceptions = { PropertyAccessException.class }) public void nullNeededParameterObject() throws PropertyAccessException { PropertyPathStart start = new PropertyPathStart(); start.setName("name"); start.setSignaturePosition(0); start.setContentType(ParameterContentType.PARAM); propertyAccessor.getPropertyContent(start, null, null, resultValueMock, false); Mockito.verifyZeroInteractions(resultValueMock); }
@Test(expectedExceptions = { PropertyAccessException.class }) public void parameterArrayOutOfRange() throws PropertyAccessException { PropertyPathStart start = new PropertyPathStart(); start.setName("name"); start.setSignaturePosition(0); start.setContentType(ParameterContentType.PARAM); propertyAccessor.getPropertyContent(start, null, new Object[0], resultValueMock, false); Mockito.verifyZeroInteractions(resultValueMock); }
throw new PropertyAccessException("Class reference cannot be null!"); return getPropertyContent(propertyPathStart.getPathToContinue(), clazz); case PARAM: if (null == parameters) { return getPropertyContent(propertyPathStart.getPathToContinue(), parameters[propertyPathStart.getSignaturePosition()]); case RETURN: return NULL_VALUE; } else { return getPropertyContent(propertyPathStart.getPathToContinue(), returnValue);
@Test public void analyzeReturnValueObjectException() throws PropertyAccessException { // create initial object relation Person peter = new Person("Peter"); Person juergen = new Person("Hans"); peter.setChild(juergen); List<PropertyPathStart> propertyAccessorList = new ArrayList<PropertyPathStart>(); // valid PropertyPathStart start = new PropertyPathStart(); start.setName("return"); start.setContentType(ParameterContentType.RETURN); PropertyPath pathOne = new PropertyPath("child"); start.setPathToContinue(pathOne); propertyAccessorList.add(start); String result = propertyAccessor.getPropertyContent(start, null, null, peter, true); assertThat(result, is("n/a")); }
@Test public void analyzeReturnValueObject() throws PropertyAccessException { // create initial object relation Person peter = new Person("Peter"); Person juergen = new Person("Hans"); peter.setChild(juergen); List<PropertyPathStart> propertyAccessorList = new ArrayList<PropertyPathStart>(); // valid PropertyPathStart start = new PropertyPathStart(); start.setName("return"); start.setContentType(ParameterContentType.RETURN); PropertyPath pathOne = new PropertyPath("child"); start.setPathToContinue(pathOne); propertyAccessorList.add(start); String result = propertyAccessor.getPropertyContent(start, null, null, peter, false); assertThat(result, is("Hans")); }
@Test public void readFieldPersonName() throws PropertyAccessException { Person person = new Person(); person.setName("Dirk"); PropertyPathStart start = new PropertyPathStart(); start.setName("this"); start.setContentType(ParameterContentType.FIELD); PropertyPath path = new PropertyPath(); path.setName("name"); start.setPathToContinue(path); String result = propertyAccessor.getPropertyContent(start, person, null, resultValueMock, false); assertThat(result, is("Dirk")); Mockito.verifyZeroInteractions(resultValueMock); }
@Test public void invokeListSizeMethod() throws PropertyAccessException { // create initial object relation Person peter = new Person("Peter"); List<String> foreNames = new ArrayList<String>(); foreNames.add("blub"); foreNames.add("blub2"); foreNames.add("blub3"); peter.setForeNamesAsList(foreNames); PropertyPathStart start = new PropertyPathStart(); start.setName("this"); start.setContentType(ParameterContentType.FIELD); PropertyPath path = new PropertyPath(); path.setName("foreNamesAsList"); start.setPathToContinue(path); PropertyPath path2 = new PropertyPath(); path2.setName("size()"); path.setPathToContinue(path2); String result = propertyAccessor.getPropertyContent(start, peter, null, resultValueMock, false); assertThat(Integer.parseInt(result), is(3)); Mockito.verifyZeroInteractions(resultValueMock); }
@Test(expectedExceptions = { PropertyAccessException.class }) public void invokeForbiddenMethod() throws PropertyAccessException { // create initial object relation Person peter = new Person("Peter"); PropertyPathStart start = new PropertyPathStart(); start.setName("this"); start.setContentType(ParameterContentType.FIELD); PropertyPath path = new PropertyPath(); path.setName("getName()"); start.setPathToContinue(path); propertyAccessor.getPropertyContent(start, peter, null, resultValueMock, false); Mockito.verifyZeroInteractions(resultValueMock); }