-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
[MNG-6729] StringSearchModelInterpolator introspects objects from Java API #275
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -250,7 +250,13 @@ private static class CacheItem | |
|
||
private boolean isQualifiedForInterpolation( Class<?> cls ) | ||
{ | ||
return !cls.getName().startsWith( "java" ); | ||
Package pkg = cls.getPackage(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As you have the Class object. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @eolivelli Given objects which are passed into these routines can be complex objects with delegates and inheritance as well. This algorithm is traversing all the hierarchy of given object, and the leaf of this structure is anyway supposed to be the String. Finally, the string with the expression is overridden by interpolated string. This works fine without my change. My change only guarantees that the internal JDK fields of e.g. There are much more troubles with this class. As for instance we are also reading |
||
if ( pkg == null ) | ||
{ | ||
return true; | ||
} | ||
String pkgName = pkg.getName(); | ||
return !pkgName.startsWith( "java." ) && !pkgName.startsWith( "javax." ); | ||
} | ||
|
||
private boolean isQualifiedForInterpolation( Field field, Class<?> fieldType ) | ||
|
@@ -278,33 +284,37 @@ private boolean isQualifiedForInterpolation( Field field, Class<?> fieldType ) | |
this.isQualifiedForInterpolation = isQualifiedForInterpolation( clazz ); | ||
this.isArray = clazz.isArray(); | ||
List<CacheField> fields = new ArrayList<>(); | ||
for ( Field currentField : clazz.getDeclaredFields() ) | ||
if ( isQualifiedForInterpolation ) | ||
{ | ||
Class<?> type = currentField.getType(); | ||
if ( isQualifiedForInterpolation( currentField, type ) ) | ||
for ( Field currentField : clazz.getDeclaredFields() ) | ||
{ | ||
if ( String.class == type ) | ||
Class<?> type = currentField.getType(); | ||
if ( isQualifiedForInterpolation( currentField, type ) ) | ||
{ | ||
if ( !Modifier.isFinal( currentField.getModifiers() ) ) | ||
if ( String.class == type ) | ||
{ | ||
fields.add( new StringField( currentField ) ); | ||
if ( !Modifier.isFinal( currentField.getModifiers() ) ) | ||
{ | ||
fields.add( new StringField( currentField ) ); | ||
} | ||
} | ||
else if ( List.class.isAssignableFrom( type ) ) | ||
{ | ||
fields.add( new ListField( currentField ) ); | ||
} | ||
else if ( Collection.class.isAssignableFrom( type ) ) | ||
{ | ||
throw new RuntimeException( | ||
"We dont interpolate into collections, use a list instead" ); | ||
} | ||
else if ( Map.class.isAssignableFrom( type ) ) | ||
{ | ||
fields.add( new MapField( currentField ) ); | ||
} | ||
else | ||
{ | ||
fields.add( new ObjectField( currentField ) ); | ||
} | ||
} | ||
else if ( List.class.isAssignableFrom( type ) ) | ||
{ | ||
fields.add( new ListField( currentField ) ); | ||
} | ||
else if ( Collection.class.isAssignableFrom( type ) ) | ||
{ | ||
throw new RuntimeException( "We dont interpolate into collections, use a list instead" ); | ||
} | ||
else if ( Map.class.isAssignableFrom( type ) ) | ||
{ | ||
fields.add( new MapField( currentField ) ); | ||
} | ||
else | ||
{ | ||
fields.add( new ObjectField( currentField ) ); | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's not use powermock to get access to Fields. I don't want to seduce people to use powermock, in general it means your code is not good testable, but that should be fixed with better code, not with powermock power.
I'm pretty sure we're already using some code for this, e.g. Reflector from Plexus Utils.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rfscholte
Why Robert? Java developers are glad to use PowerMock all around the world. As for instance, I would not be able to grow the coverage in Surefire without using PowerMock.
I am not arguing against. I am just claiming how it is in the Java world.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case there's no reason to introduce powermock, so let's just not do so.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rfscholte
Can you show me a concrete example?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based
org.apache.commons.lang3.reflect.FieldUtils;
on commons-lang3There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Developers know that test code uses a specific set of libraries which are extremely specialized and some of them are Domain Specific in test area.
I have quite a lot of commercial experiences with writing Java tests but we always have used Mockito, Powermock, AssertJ, Spock, etc. But we never mix the domains.
Also we should use modern libraries for testing purposes as the same as Java 8 is juicy for a lot of contributors and me too! There's no difference with the libs, and even more important to make the Maven code attractive for new contributors. This means we wouldn't change the world with our internal coding principals; instead we should adapt to the world, the sooner the better!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tibor, this is becoming a useless discussion. The only thing you wanted to do is verify a private Field, that's pure reflection. You don't need a library for that, but if you already have a library supporting that, just use that one.
Mavens reputation of downloading the internet is because Maven made it maybe too easy too add dependencies. One should be critical for every dependency, hence in this case there's no need for powermock.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We started with mocking MOJO and we improved the JaCoCo coverage of the class which normally can be tested only in the integration tests. So we used PowerMock and the coverage grew; wrong contributions can be easily found within a minute. So this was, I think, extremly good experience. And what is fantastic on such unit tests is the
showcase of purpose of the entire class and expected behavior
because good unit tests becomes documentation. So but complex MOJOs with private methods and injection would not be testable without mocking in unit tests.