add test #570
parent
bb0eb69acf
commit
f02f469c69
|
@ -1,10 +1,14 @@
|
||||||
package us.codecraft.webmagic.formatter;
|
package us.codecraft.webmagic.formatter;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.time.DateFormatUtils;
|
||||||
|
import org.apache.commons.lang3.time.DateUtils;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import us.codecraft.webmagic.model.formatter.DateFormatter;
|
import us.codecraft.webmagic.model.formatter.DateFormatter;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author code4crafter@gmail.com
|
* @author code4crafter@gmail.com
|
||||||
*/
|
*/
|
||||||
|
@ -13,8 +17,10 @@ public class DateFormatterTest {
|
||||||
@Test
|
@Test
|
||||||
public void testDateFormatter() throws Exception {
|
public void testDateFormatter() throws Exception {
|
||||||
DateFormatter dateFormatter = new DateFormatter();
|
DateFormatter dateFormatter = new DateFormatter();
|
||||||
dateFormatter.initParam(new String[]{"yyyy-MM-dd HH:mm"});
|
String pattern = "yyyy-MM-dd HH:mm";
|
||||||
Date format = dateFormatter.format("2013-09-10 22:11");
|
Date date = DateUtils.parseDate("2013-09-10 22:11", new String[]{pattern});
|
||||||
System.out.println(format);
|
dateFormatter.initParam(new String[]{pattern});
|
||||||
|
Date format = dateFormatter.format(DateFormatUtils.format(date, pattern));
|
||||||
|
assertThat(format).isEqualTo(date);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,18 @@
|
||||||
package us.codecraft.webmagic.model;
|
package us.codecraft.webmagic.model;
|
||||||
|
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
|
import org.apache.commons.lang3.time.DateFormatUtils;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import us.codecraft.webmagic.Page;
|
import us.codecraft.webmagic.Page;
|
||||||
import us.codecraft.webmagic.Request;
|
import us.codecraft.webmagic.Request;
|
||||||
import us.codecraft.webmagic.model.annotation.ExtractBy;
|
import us.codecraft.webmagic.model.annotation.ExtractBy;
|
||||||
|
import us.codecraft.webmagic.model.annotation.Formatter;
|
||||||
import us.codecraft.webmagic.model.annotation.TargetUrl;
|
import us.codecraft.webmagic.model.annotation.TargetUrl;
|
||||||
|
import us.codecraft.webmagic.model.formatter.DateFormatter;
|
||||||
import us.codecraft.webmagic.selector.PlainText;
|
import us.codecraft.webmagic.selector.PlainText;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
@ -34,6 +38,17 @@ public class ModelPageProcessorTest {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class ModelDate {
|
||||||
|
|
||||||
|
@Formatter(value = "yyyyMMdd", formatter = DateFormatter.class)
|
||||||
|
@ExtractBy(value = "//div[@class='date']/text()", notNull = true)
|
||||||
|
private Date date;
|
||||||
|
|
||||||
|
public Date getDate() {
|
||||||
|
return date;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMultiModel_should_not_skip_when_match() throws Exception {
|
public void testMultiModel_should_not_skip_when_match() throws Exception {
|
||||||
Page page = new Page();
|
Page page = new Page();
|
||||||
|
@ -54,6 +69,15 @@ public class ModelPageProcessorTest {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testExtractDate() throws Exception {
|
||||||
|
ModelPageProcessor modelPageProcessor = ModelPageProcessor.create(null, ModelDate.class);
|
||||||
|
Page page = getMockPage();
|
||||||
|
modelPageProcessor.process(page);
|
||||||
|
ModelDate modelDate = (ModelDate) page.getResultItems().get(ModelDate.class.getCanonicalName());
|
||||||
|
assertThat(DateFormatUtils.format(modelDate.getDate(),"yyyyMMdd")).isEqualTo("20170603");
|
||||||
|
}
|
||||||
|
|
||||||
private Page getMockPage() throws IOException {
|
private Page getMockPage() throws IOException {
|
||||||
Page page = new Page();
|
Page page = new Page();
|
||||||
page.setRawText(IOUtils.toString(getClass().getClassLoader().getResourceAsStream("html/mock-webmagic.html")));
|
page.setRawText(IOUtils.toString(getClass().getClassLoader().getResourceAsStream("html/mock-webmagic.html")));
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
<title></title>
|
<title></title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
<div class="date">20170603</div>
|
||||||
<ul>
|
<ul>
|
||||||
<li class="list"><a href="http://webmagic.io/list/1"></a></li>
|
<li class="list"><a href="http://webmagic.io/list/1"></a></li>
|
||||||
<li class="list"><a href="http://webmagic.io/list/2"></a></li>
|
<li class="list"><a href="http://webmagic.io/list/2"></a></li>
|
||||||
|
|
Loading…
Reference in New Issue