Bugfix:Type convert error in JsonPathSelector #129
parent
95bdb30296
commit
b165090434
|
@ -46,9 +46,12 @@ public class JsonPathSelector implements Selector {
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
if (object instanceof List) {
|
if (object instanceof List) {
|
||||||
return (List<String>) object;
|
List<Object> items = (List<Object>) object;
|
||||||
|
for (Object item : items) {
|
||||||
|
list.add(String.valueOf(item));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
list.add(object.toString());
|
list.add(String.valueOf(object));
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
package us.codecraft.webmagic.selector;
|
package us.codecraft.webmagic.selector;
|
||||||
|
|
||||||
import junit.framework.Assert;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static us.codecraft.webmagic.selector.Selectors.*;
|
import static us.codecraft.webmagic.selector.Selectors.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -16,19 +16,19 @@ public class ExtractorsTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEach() {
|
public void testEach() {
|
||||||
Assert.assertEquals("<a href=\"xxx\">aabbcc</a>", $("div h1 a").select(html));
|
assertThat($("div h1 a").select(html)).isEqualTo("<a href=\"xxx\">aabbcc</a>");
|
||||||
Assert.assertEquals("xxx", $("div h1 a", "href").select(html));
|
assertThat($("div h1 a", "href").select(html)).isEqualTo("xxx");
|
||||||
Assert.assertEquals("aabbcc", $("div h1 a", "innerHtml").select(html));
|
assertThat($("div h1 a", "innerHtml").select(html)).isEqualTo("aabbcc");
|
||||||
Assert.assertEquals("xxx", xpath("//a/@href").select(html));
|
assertThat(xpath("//a/@href").select(html)).isEqualTo("xxx");
|
||||||
Assert.assertEquals("xxx", regex("a href=\"(.*)\"").select(html));
|
assertThat(regex("a href=\"(.*)\"").select(html)).isEqualTo("xxx");
|
||||||
Assert.assertEquals("xxx", regex("(a href)=\"(.*)\"", 2).select(html));
|
assertThat(regex("(a href)=\"(.*)\"", 2).select(html)).isEqualTo("xxx");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCombo() {
|
public void testCombo() {
|
||||||
Assert.assertEquals("bb", and($("title"), regex("aa(bb)cc")).select(html2));
|
assertThat(and($("title"), regex("aa(bb)cc")).select(html2)).isEqualTo("bb");
|
||||||
OrSelector or = or($("div h1 a", "innerHtml"), xpath("//title"));
|
OrSelector or = or($("div h1 a", "innerHtml"), xpath("//title"));
|
||||||
Assert.assertEquals("aabbcc", or.select(html));
|
assertThat(or.select(html)).isEqualTo("aabbcc");
|
||||||
Assert.assertEquals("<title>aabbcc</title>", or.select(html2));
|
assertThat(or.select(html2)).isEqualTo("<title>aabbcc</title>");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
package us.codecraft.webmagic.selector;
|
package us.codecraft.webmagic.selector;
|
||||||
|
|
||||||
import junit.framework.Assert;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author code4crafter@gmai.com <br>
|
* @author code4crafter@gmai.com <br>
|
||||||
*/
|
*/
|
||||||
|
@ -32,16 +33,16 @@ public class JsonPathSelectorTest {
|
||||||
"}";
|
"}";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() {
|
public void testJsonPath() {
|
||||||
JsonPathSelector jsonPathSelector = new JsonPathSelector("$.store.book[*].author");
|
JsonPathSelector jsonPathSelector = new JsonPathSelector("$.store.book[*].author");
|
||||||
String select = jsonPathSelector.select(text);
|
String select = jsonPathSelector.select(text);
|
||||||
List<String> list = jsonPathSelector.selectList(text);
|
List<String> list = jsonPathSelector.selectList(text);
|
||||||
Assert.assertNotNull(select);
|
assertThat(select).isEqualTo("Nigel Rees");
|
||||||
Assert.assertNotNull(list);
|
assertThat(list).contains("Nigel Rees","Evelyn Waugh");
|
||||||
jsonPathSelector = new JsonPathSelector("$.store.book[?(@.category == 'reference')]");
|
jsonPathSelector = new JsonPathSelector("$.store.book[?(@.category == 'reference')]");
|
||||||
list = jsonPathSelector.selectList(text);
|
list = jsonPathSelector.selectList(text);
|
||||||
select = jsonPathSelector.select(text);
|
select = jsonPathSelector.select(text);
|
||||||
Assert.assertNotNull(list);
|
assertThat(select).isEqualTo("{\"author\":\"Nigel Rees\",\"title\":\"Sayings of the Century\",\"category\":\"reference\",\"price\":8.95}");
|
||||||
Assert.assertNotNull(select);
|
assertThat(list).contains("{\"author\":\"Nigel Rees\",\"title\":\"Sayings of the Century\",\"category\":\"reference\",\"price\":8.95}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package us.codecraft.webmagic.selector;
|
package us.codecraft.webmagic.selector;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.assertj.core.api.Assertions;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -20,6 +20,6 @@ public class RegexSelectorTest {
|
||||||
String source = "(hello world";
|
String source = "(hello world";
|
||||||
RegexSelector regexSelector = new RegexSelector(regex);
|
RegexSelector regexSelector = new RegexSelector(regex);
|
||||||
String select = regexSelector.select(source);
|
String select = regexSelector.select(source);
|
||||||
Assert.assertEquals(source,select);
|
Assertions.assertThat(select).isEqualTo(source);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,11 +49,6 @@ public class UrlUtilsTest {
|
||||||
assertThat(replacedHtml).isEqualTo("<a href=\"http://www.dianping.com/start\" tag>");
|
assertThat(replacedHtml).isEqualTo("<a href=\"http://www.dianping.com/start\" tag>");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void test(){
|
|
||||||
UrlUtils.canonicalizeUrl("start tag", "http://www.dianping.com/");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetDomain(){
|
public void testGetDomain(){
|
||||||
String url = "http://www.dianping.com/aa/";
|
String url = "http://www.dianping.com/aa/";
|
||||||
|
|
Loading…
Reference in New Issue