package us.codecraft.webmagic;

import org.junit.Test;
import us.codecraft.webmagic.model.AfterExtractor;
import us.codecraft.webmagic.model.annotation.ExtractBy;

/**
 * @author code4crafter@gmail.com
 *         Date: 2017/6/3
 *         Time: 下午2:54
 */
public class SimpleHttpClientTest {

    public static class Weather implements AfterExtractor {

        private String location;

        @ExtractBy(notNull = true, value = "//div[@id='7d']//ul[@class='t']/li[2]/p[@class='tem']/i/regex('([\\-\\d]+)',1)")
        private Integer lowTemperature;

        @ExtractBy(notNull = true, value = "//div[@id='7d']//ul[@class='t']/li[2]/p[@class='tem']/span/regex('([\\-\\d]+)',1)")
        private Integer highTemperature;

        @ExtractBy(notNull = true, value = "//div[@id='7d']//ul[@class='t']/li[2]/p[@class='wea']/text()")
        private String desc;

        @Override
        public void afterProcess(Page page) {
            if (lowTemperature > highTemperature) {
                int temp = lowTemperature;
                lowTemperature = highTemperature;
                highTemperature = temp;
            }
        }

        public String getLocation() {
            return location;
        }

        public void setLocation(String location) {
            this.location = location;
        }

        public Integer getLowTemperature() {
            return lowTemperature;
        }

        public void setLowTemperature(Integer lowTemperature) {
            this.lowTemperature = lowTemperature;
        }

        public Integer getHighTemperature() {
            return highTemperature;
        }

        public void setHighTemperature(Integer highTemperature) {
            this.highTemperature = highTemperature;
        }

        public String getDesc() {
            return desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }

        @Override
        public String toString() {
            return "Weather{" +
                    "location='" + location + '\'' +
                    ", lowTemperature=" + lowTemperature +
                    ", highTemperature=" + highTemperature +
                    ", desc='" + desc + '\'' +
                    '}';
        }
    }

    @Test
    public void test() throws Exception {
        SimpleHttpClient simpleClient = new SimpleHttpClient(Site.me().setUserAgent(
                "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36"));
        Weather weather = simpleClient.get("http://www.weather.com.cn/weather/101020100.shtml", Weather.class);
        System.out.println(weather);

    }
}

@@ -25,16 +25,41 @@
 magic;%0A%0A
+import org.junit.Ignore;%0A
 import o
@@ -182,16 +182,75 @@
 actBy;%0A%0A
+import static org.assertj.core.api.Assertions.assertThat;%0A%0A
 /**%0A * @
@@ -2325,24 +2325,36 @@
    %7D%0A    %7D%0A%0A
+    @Ignore%0A
     @Test%0A  
@@ -2745,34 +2745,38 @@
     
-System.out.println(weather
+assertThat(weather).isNotNull(
 );%0A%0A

complete SimpleHttpClientTest