diff --git a/jspoon/src/main/java/pl/droidsonroids/jspoon/HtmlField.java b/jspoon/src/main/java/pl/droidsonroids/jspoon/HtmlField.java index 452a7a1..0d7671f 100644 --- a/jspoon/src/main/java/pl/droidsonroids/jspoon/HtmlField.java +++ b/jspoon/src/main/java/pl/droidsonroids/jspoon/HtmlField.java @@ -142,7 +142,10 @@ private String getValue(Element node, Class fieldType) { if (value == null || value.isEmpty()) { value = spec.getDefaultValue(); } + } else { + value = spec.getDefaultValue(); } + } return value; } @@ -191,4 +194,4 @@ private Float getFloat(String value) { throw new FloatParseException(value, spec.getLocale()); } } -} \ No newline at end of file +} diff --git a/jspoon/src/test/java/pl/droidsonroids/jspoon/RegexTest.java b/jspoon/src/test/java/pl/droidsonroids/jspoon/RegexTest.java index 5f4f26a..951bd84 100644 --- a/jspoon/src/test/java/pl/droidsonroids/jspoon/RegexTest.java +++ b/jspoon/src/test/java/pl/droidsonroids/jspoon/RegexTest.java @@ -19,13 +19,26 @@ public void setUp() { } private static class RegexModel { - @Selector(value = "div", format = "([a-z]+),") String number; + @Selector(value = "div", regex = "([a-z]+),") String number; } + private static class RegexModelDefault { + @Selector(value = "div", regex = "(\\d+)", defValue = "1") int number; + } + + @Test public void regexTest() { HtmlAdapter htmlAdapter = jspoon.adapter(RegexModel.class); RegexModel regexModel = htmlAdapter.fromHtml(HTML_CONTENT); assertEquals(regexModel.number, "three"); } + + + @Test + public void regexDefaultTest() { + HtmlAdapter htmlAdapter = jspoon.adapter(RegexModelDefault.class); + RegexModelDefault regexModelDefault = htmlAdapter.fromHtml(HTML_CONTENT); + assertEquals(regexModelDefault.number, 1); + } }