Skip to content

Commit

Permalink
Add a test for FieldDiffer
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmiray committed Mar 24, 2016
1 parent b3f79dc commit e825e8f
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 2 deletions.
2 changes: 1 addition & 1 deletion subprojects/naum-core/naum-core.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ dependencies {
compileOnly 'org.projectlombok:lombok:1.16.8'

compile 'org.ow2.asm:asm-all:5.1'
compile 'com.google.code.findbugs:jsr305:3.0.1'

testCompile 'javax.inject:javax.inject:1'
testCompile 'com.google.code.findbugs:jsr305:3.0.1'
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ protected <M extends MemberInfo> void checkModifiers(M previous, M next, Collect
.severity(Diff.Severity.ERROR)
.type(Diff.Type.MODIFIED)
.messageKey(keyPrefix + ".modifiers.modified")
.messageArg(previous.getName())
.messageArg(modifiersAsString(previous.getModifiers()))
.messageArg(previous.getModifiers())
.messageArg(modifiersAsString(next.getModifiers()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ private void checkVersion(Collection<Diff> list) {
.severity(Diff.Severity.WARNING)
.type(Diff.Type.MODIFIED)
.messageKey(KEY_CLASS_VERSION_MODIFIED)
.messageArg(previous.getName())
.messageArg(previous.getVersion())
.messageArg(next.getVersion())
.build());
Expand All @@ -106,6 +107,7 @@ private void checkSuperclass(Collection<Diff> list) {
.severity(Diff.Severity.ERROR)
.type(Diff.Type.MODIFIED)
.messageKey(KEY_CLASS_SUPERCLASS_MODIFIED)
.messageArg(previous.getName())
.messageArg(previous.getSuperclass())
.messageArg(next.getSuperclass())
.build());
Expand Down Expand Up @@ -134,6 +136,7 @@ private void checkInterfaces(Collection<Diff> list) {
.severity(Diff.Severity.ERROR)
.type(Diff.Type.REMOVED)
.messageKey(KEY_CLASS_INTERFACE_REMOVED)
.messageArg(previous.getName())
.messageArg(i)
.build());
}
Expand All @@ -144,6 +147,7 @@ private void checkInterfaces(Collection<Diff> list) {
.severity(Diff.Severity.ERROR)
.type(Diff.Type.ADDED)
.messageKey(KEY_CLASS_INTERFACE_ADDED)
.messageArg(previous.getName())
.messageArg(i)
.build());
}
Expand Down Expand Up @@ -185,6 +189,7 @@ private void checkInnerClasses(final Collection<Diff> list) {
.severity(Diff.Severity.ERROR)
.type(Diff.Type.REMOVED)
.messageKey(KEY_CLASS_INNERCLASS_REMOVED)
.messageArg(previous.getName())
.messageArg(c)
.build());
}
Expand All @@ -195,6 +200,7 @@ private void checkInnerClasses(final Collection<Diff> list) {
.severity(Diff.Severity.ERROR)
.type(Diff.Type.ADDED)
.messageKey(KEY_CLASS_INNERCLASS_ADDED)
.messageArg(previous.getName())
.messageArg(c)
.build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
@Data(staticConstructor = "fieldDiffer")
@EqualsAndHashCode(callSuper = true)
public class FieldDiffer extends AbstractMemberDiffer<FieldInfo> {
public static final String KEY_FIELD_MODIFIERS_MODIFIED = "field.modifiers.modified";
public static final String KEY_FIELD_TYPE_MODIFIED = "field.type.modified";

private final FieldInfo previous;
private final FieldInfo next;

Expand Down Expand Up @@ -60,7 +63,8 @@ private void checkType(List<Diff> list) {
Diff.builder()
.severity(Diff.Severity.ERROR)
.type(Diff.Type.MODIFIED)
.messageKey("method.type.modified")
.messageKey(KEY_FIELD_TYPE_MODIFIED)
.messageArg(previous.getName())
.messageArg(previous.getType())
.messageArg(next.getType())
.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ private void checkReturnType(List<Diff> list) {
.severity(Diff.Severity.ERROR)
.type(Diff.Type.MODIFIED)
.messageKey("method.type.modified")
.messageArg(previous.getName())
.messageArg(previous.getReturnType())
.messageArg(next.getReturnType())
.build());
Expand Down Expand Up @@ -97,6 +98,7 @@ private void checkExceptions(Collection<Diff> list) {
.severity(Diff.Severity.ERROR)
.type(Diff.Type.REMOVED)
.messageKey("method.exception.removed")
.messageArg(previous.getName())
.messageArg(e)
.build());
}
Expand All @@ -107,6 +109,7 @@ private void checkExceptions(Collection<Diff> list) {
.severity(Diff.Severity.ERROR)
.type(Diff.Type.ADDED)
.messageKey("method.exception.added")
.messageArg(previous.getName())
.messageArg(e)
.build());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Class
class.version.modified=Class version of {0} changed from {1} to {2}
class.superclass.modified=Superclass of {0} changed from {1} to {2}
class.interface.removed=Interface {1} was removed from {0}
class.interface.added=Interface {1} was added to {0}
class.innerclass.removed=Inner class {1} was removed from {0}
class.innerclass.added=Inner class {1} was added to {0}
class.modifiers.modified=Modifiers of class {0} changed from {1} ({2}) to {3} ({4})
# Field
field.modifiers.modified=Modifiers of field {0} changed from {1} ({2}) to {3} ({4})
field.type.modified=Type of field {0} changed from {1} to {2}
# Constructor
constructor.exception.removed=Exception {0} removed from throws clause of constructor
constructor.exception.added=Exception {0} added to throws clause of constructor
constructor.modifiers.modified=Modifiers of constructor for class {0} changed from {1} ({2}) to {3} ({4})
# Method
method.type.modified=Type of method {0} changed from {1} to {2}
method.exception.removed=Exception {1} removed from throws clause of method {0}
method.exception.added=Exception {1} added to throws clause of method {0}
method.modifiers.modified=Modifiers of method {0} changed from {1} ({2}) to {3} ({4})
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ private Object[] classStructure() {
.severity(Diff.Severity.WARNING)
.type(Diff.Type.MODIFIED)
.messageKey(KEY_CLASS_VERSION_MODIFIED)
.messageArg(CLASSNAME)
.messageArg(V1_7)
.messageArg(V1_8)
.build()
Expand All @@ -101,6 +102,7 @@ private Object[] classStructure() {
.severity(Diff.Severity.ERROR)
.type(Diff.Type.MODIFIED)
.messageKey(KEY_CLASS_SUPERCLASS_MODIFIED)
.messageArg(CLASSNAME)
.messageArg(JAVA_LANG_OBJECT)
.messageArg(SUPER_CLASSNAME)
.build()
Expand All @@ -122,6 +124,7 @@ private Object[] classStructure() {
.severity(Diff.Severity.ERROR)
.type(Diff.Type.MODIFIED)
.messageKey(KEY_CLASS_MODIFIERS_MODIFIED)
.messageArg(CLASSNAME)
.messageArg("public")
.messageArg(ACC_PUBLIC)
.messageArg("private")
Expand All @@ -144,6 +147,7 @@ private Object[] classStructure() {
.severity(Diff.Severity.ERROR)
.type(Diff.Type.REMOVED)
.messageKey(KEY_CLASS_INTERFACE_REMOVED)
.messageArg(CLASSNAME)
.messageArg(JAVA_IO_SERIALIZABLE)
.build()
)
Expand All @@ -163,6 +167,7 @@ private Object[] classStructure() {
.severity(Diff.Severity.ERROR)
.type(Diff.Type.ADDED)
.messageKey(KEY_CLASS_INTERFACE_ADDED)
.messageArg(CLASSNAME)
.messageArg(JAVA_IO_SERIALIZABLE)
.build()
)
Expand All @@ -185,12 +190,14 @@ private Object[] classStructure() {
.severity(Diff.Severity.ERROR)
.type(Diff.Type.REMOVED)
.messageKey(KEY_CLASS_INTERFACE_REMOVED)
.messageArg(CLASSNAME)
.messageArg(JAVA_IO_CLONEABLE)
.build(),
Diff.builder()
.severity(Diff.Severity.ERROR)
.type(Diff.Type.ADDED)
.messageKey(KEY_CLASS_INTERFACE_ADDED)
.messageArg(CLASSNAME)
.messageArg(JAVA_IO_CLOSEABLE)
.build()
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kordamp.naum.diff;

import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import junitparams.naming.TestCaseName;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.kordamp.naum.model.ClassInfo;
import org.kordamp.naum.model.FieldInfo;

import java.util.Collection;

import static java.util.Arrays.asList;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.kordamp.naum.diff.ClassDiffer.KEY_CLASS_SUPERCLASS_MODIFIED;
import static org.kordamp.naum.diff.FieldDiffer.KEY_FIELD_MODIFIERS_MODIFIED;
import static org.kordamp.naum.diff.FieldDiffer.KEY_FIELD_TYPE_MODIFIED;
import static org.kordamp.naum.diff.FieldDiffer.fieldDiffer;
import static org.objectweb.asm.Opcodes.ACC_PRIVATE;
import static org.objectweb.asm.Opcodes.ACC_PUBLIC;

/**
* @author Andres Almiray
*/
@RunWith(JUnitParamsRunner.class)
public class FieldDifferTest {
private static final String FIELDNAME = "var";
private static final String JAVA_LANG_OBJECT = "java.lang.Object";
private static final String JAVA_LANG_INTEGER = "java.lang.Integer";

@Test
@Parameters(method = "parameters")
@TestCaseName("{method}[{index}] - {0}")
public void fieldsDiffer(String testName, FieldInfo previous, FieldInfo next, Collection<Diff> expected) {
FieldDiffer differ = fieldDiffer(previous, next);
Collection<Diff> actual = differ.diff();
assertThat(actual, hasSize(greaterThan(0)));
assertThat(actual, equalTo(expected));
}

private Object[] parameters() {
return new Object[]{
new Object[]{
"modifiers",
FieldInfo.builder()
.name(FIELDNAME)
.modifiers(ACC_PUBLIC)
.type(JAVA_LANG_OBJECT)
.build(),
FieldInfo.builder()
.name(FIELDNAME)
.modifiers(ACC_PRIVATE)
.type(JAVA_LANG_OBJECT)
.build(),
asList(
Diff.builder()
.severity(Diff.Severity.ERROR)
.type(Diff.Type.MODIFIED)
.messageKey(KEY_FIELD_MODIFIERS_MODIFIED)
.messageArg(FIELDNAME)
.messageArg("public")
.messageArg(ACC_PUBLIC)
.messageArg("private")
.messageArg(ACC_PRIVATE)
.build()
)
},

new Object[]{
"type",
FieldInfo.builder()
.name(FIELDNAME)
.type(JAVA_LANG_OBJECT)
.build(),
FieldInfo.builder()
.name(FIELDNAME)
.type(JAVA_LANG_INTEGER)
.build(),
asList(
Diff.builder()
.severity(Diff.Severity.ERROR)
.type(Diff.Type.MODIFIED)
.messageKey(KEY_FIELD_TYPE_MODIFIED)
.messageArg(FIELDNAME)
.messageArg(JAVA_LANG_OBJECT)
.messageArg(JAVA_LANG_INTEGER)
.build()
)
},
};
}
}

0 comments on commit e825e8f

Please sign in to comment.