Skip to content

Commit

Permalink
Merge pull request #17 from Soulcramer/master
Browse files Browse the repository at this point in the history
Let the user use an Implementation of List.
  • Loading branch information
burnoo authored Oct 9, 2017
2 parents ac57dfe + f695c14 commit c0f1365
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
4 changes: 2 additions & 2 deletions jspoon/src/main/java/pl/droidsonroids/jspoon/HtmlAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class HtmlAdapter<T> {
}

// Not annotated field - List of annotated type
if (selector == null && fieldClass.equals(List.class)) {
if (selector == null && List.class.isAssignableFrom(fieldClass)) {
selector = getSelectorFromListType(field);
}

Expand Down Expand Up @@ -67,7 +67,7 @@ private Selector getSelectorFromListType(Field field) {

private void addCachedHtmlField(Field field, Selector selector, Class<?> fieldClass) {
HtmlField<T> htmlField;
if (fieldClass.equals(List.class)) {
if (List.class.isAssignableFrom(fieldClass)) {
htmlField = new HtmlListField<>(field, selector);
} else if (Utils.isSimple(fieldClass)) {
htmlField = new HtmlSimpleField<>(field, selector);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private static class AnnotatedPost extends Post {
format = "HH:mm dd.MM.yyyy") Date created;
@Selector("h2") String header;
@Selector("p") String content;
@Selector("li") List<String> tags;
@Selector("li") ArrayList<String> tags;

AnnotatedPost() {
//no-op
Expand All @@ -178,7 +178,7 @@ private static class AnnotatedPost extends Post {
this.created = created;
this.header = header;
this.content = content;
this.tags = Arrays.asList(tags);
this.tags = new ArrayList<>(Arrays.asList(tags));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package pl.droidsonroids.jspoon;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Before;
Expand Down Expand Up @@ -29,31 +30,37 @@ public void setUp() {

private static class BooleanModel {
@Selector(".boolean") List<Boolean> booleanList;
@Selector(".boolean") ArrayList<Boolean> booleanArrayList;
}

private static class IntModel {
@Selector(".int") List<Integer> integerList;
@Selector(".int") ArrayList<Integer> integerArrayList;
}
private static class StringModel {
@Selector(".string") List<String> stringList;
@Selector(".string") ArrayList<String> stringArrayList;
}

@Test
public void booleanList() {
public void booleanLists() {
BooleanModel booleanModel = createObjectFromHtml(BooleanModel.class);
assertEquals(booleanModel.booleanList, Arrays.asList(true, false, false));
assertEquals(booleanModel.booleanArrayList, new ArrayList<>(Arrays.asList(true, false, false)));
}

@Test
public void integerList() {
public void integerLists() {
IntModel intModel = createObjectFromHtml(IntModel.class);
assertEquals(intModel.integerList, Arrays.asList(-200, 4, 32000));
assertEquals(intModel.integerArrayList, new ArrayList<>(Arrays.asList(-200, 4, 32000)));
}

@Test
public void stringList() {
public void stringLists() {
StringModel stringModel = createObjectFromHtml(StringModel.class);
assertEquals(stringModel.stringList, Arrays.asList("Test1", "", "Test2 ".trim()));
assertEquals(stringModel.stringArrayList, new ArrayList<>(Arrays.asList("Test1", "", "Test2 ".trim())));
}

private <T> T createObjectFromHtml(Class<T> className) {
Expand Down

0 comments on commit c0f1365

Please sign in to comment.