Skip to content

Commit

Permalink
Also find getters that's capitalization does not match the field name (
Browse files Browse the repository at this point in the history
  • Loading branch information
yankee42 authored and lanwen committed Jun 11, 2017
1 parent 6f85602 commit 53cbd50
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ru.yandex.qatools.proc.test;

import ru.yandex.qatools.processors.matcher.gen.annotations.GenerateMatcher;

@GenerateMatcher
public class Capitalization {
private String id;

public String getID() {
return id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ru.yandex.qatools.proc.test;

import org.hamcrest.Matcher;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;

/**
* @author Vladislav Bauer
*/

public class CapitalizationTest {

@Test
public void shouldFindMatcher() throws Exception {
assertThat(CapitalizationMatchers.class.getDeclaredMethod("withId", Matcher.class), notNullValue());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
Expand Down Expand Up @@ -141,14 +142,28 @@ public static Function<Element, MethodSpec> asMethodSpec() {
.addParameter(ownerType, "actual")
.returns(fieldType)
.addStatement(
"return $L.get$L()",
"return $L.$L()",
"actual",
Naming.normalize(field.getSimpleName())
getGetterName(field)
)
.build()
).build()
)
.build();
};
}

private static String getGetterName(Element field) {
String caseInsensitiveGetterName = "get" + field.getSimpleName();

return field.getEnclosingElement().getEnclosedElements().stream()
.filter(elem -> elem.getSimpleName().toString().equalsIgnoreCase(caseInsensitiveGetterName))
.findAny()
.orElseThrow(() -> new NoSuchElementException(
"cannot find getter for " + field.getSimpleName() + " on class " +
field.getEnclosingElement().getSimpleName())
)
.getSimpleName()
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@
import com.google.testing.compile.CompilationRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import ru.yandex.qatools.processors.matcher.gen.bean.ClassSpecDescription;
import ru.yandex.qatools.processors.matcher.gen.testclasses.WithSomeFields;
import ru.yandex.qatools.processors.matcher.gen.testclasses.WithoutGetter;

import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.function.Predicate;

import static java.util.stream.Collectors.toCollection;
import static java.util.stream.Collectors.toList;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;
import static ru.yandex.qatools.processors.matcher.gen.processing.MethodsCollector.collectingMethods;

Expand All @@ -27,26 +28,38 @@ public class MethodsCollectorTest {

@Rule
public CompilationRule compilation = new CompilationRule();
@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void shouldCollectFields() throws Exception {
TypeElement withFields = compilation.getElements().getTypeElement(WithSomeFields.class.getCanonicalName());
LinkedList<Element> fields = new LinkedList<>(compilation.getElements().getAllMembers(withFields).stream()
.filter(ofKind(ElementKind.FIELD))
.map(elem -> (Element) elem)
.collect(toList()));
LinkedList<Element> fields = findFields(WithSomeFields.class);

ClassSpecDescription apply = collectingMethods().finisher().apply(fields);

assertThat(apply.getClassElement().getSimpleName().toString(), equalTo(WithSomeFields.class.getSimpleName()));
assertThat(apply.getSpec().methodSpecs, hasSize(fields.size() + 1));
assertThat(
apply.getSpec().methodSpecs.stream().map(methodSpec -> methodSpec.name).collect(toList()),
hasItems("withValue", "withBytes", "withStr", "withInteger")
hasItems("withValue", "withBytes", "withStr", "withInteger", "withLower")
);
}

@Test
public void shouldThrowException_ifNoGetterPresent() throws Exception {
thrown.expect(NoSuchElementException.class);
thrown.expectMessage(allOf(containsString("WithoutGetter"), containsString("prop")));

collectingMethods().finisher().apply(findFields(WithoutGetter.class));
}

private LinkedList<Element> findFields(final Class<?> withSomeFieldsClass) {
TypeElement withFields = compilation.getElements().getTypeElement(withSomeFieldsClass.getCanonicalName());
return new LinkedList<>(compilation.getElements().getAllMembers(withFields).stream()
.filter(ofKind(ElementKind.FIELD))
.map(elem -> (Element) elem)
.collect(toList()));
}

public static Predicate<Element> ofKind(ElementKind kind) {
return elem -> elem.getKind() == kind;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,25 @@ public class WithSomeFields {
byte[] bytes;
String str;
Integer integer;
String lower;

int getValue() {
return value;
}

byte[] getBytes() {
return bytes;
}

String getStr() {
return str;
}

Integer getInteger() {
return integer;
}

String getLOWER() {
return lower;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package ru.yandex.qatools.processors.matcher.gen.testclasses;

public class WithoutGetter {
private String prop;
}

0 comments on commit 53cbd50

Please sign in to comment.