Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor IntrospectionContextTestImpl in SuppressPropertiesBeanIntros… #96

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,13 @@
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.9.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
import java.util.Set;

import junit.framework.TestCase;
import org.mockito.Mockito;
import org.mockito.ArgumentCaptor;
import static org.mockito.Mockito.*;
import org.apache.commons.beanutils2.IntrospectionContext;

/**
* Test class for {@code SuppressPropertiesBeanIntrospector}.
Expand All @@ -49,14 +53,35 @@ public void testRemovePropertiesDuringIntrospection() throws IntrospectionExcept
final String[] properties = { "test", "other", "oneMore" };
final SuppressPropertiesBeanIntrospector introspector = new SuppressPropertiesBeanIntrospector(
Arrays.asList(properties));
final IntrospectionContextTestImpl context = new IntrospectionContextTestImpl();
// Create variables for tracking behaviors of mock object
Set<String> contextRemovedProperties = new HashSet<>();
// Construct mock object
final IntrospectionContext context = mock(IntrospectionContext.class);
// Method Stubs
doThrow(new UnsupportedOperationException("Unexpected method call!")).when(context)
.addPropertyDescriptors(any(PropertyDescriptor[].class));
doThrow(new UnsupportedOperationException("Unexpected method call!")).when(context)
.addPropertyDescriptor(any(PropertyDescriptor.class));
doAnswer((stubInvo) -> {
String name = stubInvo.getArgument(0);
contextRemovedProperties.add(name);
return null;
}).when(context).removePropertyDescriptor(any(String.class));
when(context.getTargetClass()).thenAnswer((stubInvo) -> {
throw new UnsupportedOperationException("Unexpected method call!");
});
when(context.hasProperty(any(String.class)))
.thenThrow(new UnsupportedOperationException("Unexpected method call!"));
when(context.getPropertyDescriptor(any(String.class)))
.thenThrow(new UnsupportedOperationException("Unexpected method call!"));
when(context.propertyNames()).thenAnswer((stubInvo) -> {
throw new UnsupportedOperationException("Unexpected method call!");
});

introspector.introspect(context);
assertEquals("Wrong number of removed properties", properties.length, context
.getRemovedProperties().size());
assertEquals("Wrong number of removed properties", properties.length, contextRemovedProperties.size());
for (final String property : properties) {
assertTrue("Property not removed: " + property, context
.getRemovedProperties().contains(property));
assertTrue("Property not removed: " + property, contextRemovedProperties.contains(property));
}
}

Expand All @@ -70,13 +95,35 @@ public void testPropertyNamesDefensiveCopy() throws IntrospectionException {
final SuppressPropertiesBeanIntrospector introspector = new SuppressPropertiesBeanIntrospector(
properties);
properties.add("prop2");
final IntrospectionContextTestImpl context = new IntrospectionContextTestImpl();
// Create variables for tracking behaviors of mock object
Set<String> contextRemovedProperties = new HashSet<>();
// Construct mock object
final IntrospectionContext context = mock(IntrospectionContext.class);
// Method Stubs
doThrow(new UnsupportedOperationException("Unexpected method call!")).when(context)
.addPropertyDescriptors(any(PropertyDescriptor[].class));
doThrow(new UnsupportedOperationException("Unexpected method call!")).when(context)
.addPropertyDescriptor(any(PropertyDescriptor.class));
doAnswer((stubInvo) -> {
String name = stubInvo.getArgument(0);
contextRemovedProperties.add(name);
return null;
}).when(context).removePropertyDescriptor(any(String.class));
when(context.getTargetClass()).thenAnswer((stubInvo) -> {
throw new UnsupportedOperationException("Unexpected method call!");
});
when(context.hasProperty(any(String.class)))
.thenThrow(new UnsupportedOperationException("Unexpected method call!"));
when(context.getPropertyDescriptor(any(String.class)))
.thenThrow(new UnsupportedOperationException("Unexpected method call!"));
when(context.propertyNames()).thenAnswer((stubInvo) -> {
throw new UnsupportedOperationException("Unexpected method call!");
});

introspector.introspect(context);
assertEquals("Wrong number of removed properties", 1, context
.getRemovedProperties().size());
assertEquals("Wrong number of removed properties", 1, contextRemovedProperties.size());
assertTrue("Wrong removed property",
context.getRemovedProperties().contains("prop1"));
contextRemovedProperties.contains("prop1"));
}

/**
Expand All @@ -93,57 +140,4 @@ public void testGetSuppressedPropertiesModify() {
// ok
}
}

/**
* A test implementation of IntrospectionContext which collects the properties which
* have been removed.
*/
private static class IntrospectionContextTestImpl implements IntrospectionContext {
/** Stores the names of properties which have been removed. */
private final Set<String> removedProperties = new HashSet<>();

/**
* Returns the names of properties which have been removed.
*
* @return the set with removed properties
*/
public Set<String> getRemovedProperties() {
return removedProperties;
}

@Override
public Class<?> getTargetClass() {
throw new UnsupportedOperationException("Unexpected method call!");
}

@Override
public void addPropertyDescriptor(final PropertyDescriptor desc) {
throw new UnsupportedOperationException("Unexpected method call!");
}

@Override
public void addPropertyDescriptors(final PropertyDescriptor[] descriptors) {
throw new UnsupportedOperationException("Unexpected method call!");
}

@Override
public boolean hasProperty(final String name) {
throw new UnsupportedOperationException("Unexpected method call!");
}

@Override
public PropertyDescriptor getPropertyDescriptor(final String name) {
throw new UnsupportedOperationException("Unexpected method call!");
}

@Override
public void removePropertyDescriptor(final String name) {
removedProperties.add(name);
}

@Override
public Set<String> propertyNames() {
throw new UnsupportedOperationException("Unexpected method call!");
}
}
}