首页 > 其他 > 详细

main方法执行junit测试

时间:2014-03-01 03:32:39      阅读:1209      评论:0      收藏:0      [点我收藏+]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package com.xzm.util.task;
 
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
 
import org.junit.Test;
import org.junit.runner.JUnitCore;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.StringUtils;
 
import cn.os.util.QuietCssErrorHandler;
import cn.os.util.SilentIncorrectnessListener;
 
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.WebResponse;
import com.gargoylesoftware.htmlunit.html.DomNode;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.util.FalsifyingWebConnection;
import com.sun.jmx.snmp.tasks.Task;
import com.xzm.entity.Qiushibaike;
import com.xzm.entity.QiushibaikeAnswer;
import com.xzm.service.QiuShiService;
 
 
 
@Service
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/com/xzm/config/spring/applicationContext.xml" })
public class InviteEvaluate {
    private static Logger logger = LoggerFactory
    .getLogger(InviteEvaluate.class);
 
    @Autowired
    private QiuShiService qiuShiService;
     
    public HtmlPage htmlPage;
 
 
    public void getContent() {
        try {
             new Thread() {
                    public void run() {
                        JUnitCore.runClasses(new Class[] { InviteEvaluate.class });
                   }
                }.start();
        } catch (Exception e) {
        }
    }
     
    public void getContentTest(){
        try {
            htmlPage = getPage();
            getQiuContent(htmlPage);
             
        } catch (Exception e) {
            // TODO: handle exception
        }
         
    }
    /**
     * 获取正文
     *
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public void getQiuContent(HtmlPage htmlPage) {
        Qiushibaike qiushibaike = null;
        List<DomNode> page = (List<DomNode>) htmlPage
                .getByXPath("//*[@class=‘block untagged mb15 bs2‘]");
 
        List<Qiushibaike> qiushibaikes = new ArrayList<Qiushibaike>();
 
        for (int i = 0; i < page.size(); i++) {
            try {
                qiushibaike = new Qiushibaike();
                 
                 String str = page.get(i).asXml().split("\\n")[0].split("\"")[3];//qiushi_tag_62569450
                 String id = str.substring(str.lastIndexOf("_")+1, str.length());
                 if(!StringUtils.isEmpty(id)){
                     qiushibaike.setQiushiId(Integer.valueOf(id));
                 }
                 
                List<DomNode> content = (List<DomNode>) page.get(i).getByXPath(
                        "*[@class=‘content‘]");
                if (content.size() > 0) {
                    qiushibaike.setContent(content.get(0).asText());
                }
                List<DomNode> image = (List<DomNode>) page.get(i).getByXPath(
                        "*[@class=‘thumb‘]/a/img");
                if (image.size() > 0) {
                    qiushibaike.setPicUrl(image.get(0).asXml().split("\"")[1]);
                    qiushibaike.setTitle(image.get(0).asXml().split("\"")[3]);
                }
 
                List<DomNode> down = (List<DomNode>) page.get(i).getByXPath(
                        "*[@class=‘bar‘]/ul/li[2]/a");
                if (down.size() > 0) {
                    qiushibaike.setDown(Integer.valueOf(down.get(0).asText()));
                }
                List<DomNode> up = (List<DomNode>) page.get(i).getByXPath(
                        "*[@class=‘bar‘]/ul/li/a"); //
                if (up.size() > 0) {
                    qiushibaike.setUp(Integer.valueOf(up.get(0).asText()));
                }
 
                List<DomNode> author = (List<DomNode>) page.get(i).getByXPath(
                        "*[@class=‘author‘]"); //
                if (author.size() > 0) {
                    qiushibaike.setAuthor(author.get(0).asText());
                }
                qiushibaike.setCreateTime(new Date());
                qiushibaikes.add(qiushibaike);
            } catch (Exception e) {
                logger.info("qiushi 正文 error : " ,e);
                continue ;
            }
        }
        int count = qiuShiService.addQiushi(qiushibaikes);
        logger.info("当期时间是: " + new Date() + " 正文内容数量是 : " + count);
        //获取完正文,获取评论
        getQiuAnswers(htmlPage);
 
    }
 
     
    /**
     * 糗事回复信息入库
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public void getQiuAnswers(HtmlPage htmlPage){
         List<HtmlElement> elements = (List<HtmlElement>)
         htmlPage.getByXPath("//*[@class=‘qiushi_comments‘]");
         for (HtmlElement htmlElement : elements) {
             try {
                 HtmlPage resultPage = htmlElement.click();
                 List<QiushibaikeAnswer> answers = getQiuAnswer(resultPage);
                 int count = qiuShiService.addQiushiAnswer(answers);
                 logger.info("当期时间是: " + new Date() + " 回复内容数量是 : " + count);
            } catch (Exception e) {
                continue ;
            }
         }
          
    }
     
     
    /**
     * 获取糗事的id
     * @param resultPage
     * @return
     */
    public String getQiuId(HtmlPage resultPage){
         DomNode head = (DomNode) resultPage.getByXPath("//*[@class=‘block untagged noline mb15 bs2‘]").get(0);
         String str = head.asXml().split("\\n")[0].split("\"")[3];//qiushi_tag_62569450
         return str.substring(str.lastIndexOf("_")+1, str.length());
    }
    /**
     * 获取糗事的回复内容
     * @param resultPage
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<QiushibaikeAnswer> getQiuAnswer(HtmlPage resultPage){
         
         List<DomNode> replays = (List<DomNode>) resultPage.getByXPath("//*[@class=‘replay‘]");
         List<QiushibaikeAnswer> answers = new ArrayList<QiushibaikeAnswer>();
         QiushibaikeAnswer answer = null;
         for (DomNode replay : replays) {
             try {
                 answer = new QiushibaikeAnswer();
                 if(!StringUtils.isEmpty(getQiuId(resultPage))){
                     answer.setContentId(Integer.valueOf(getQiuId(resultPage)));
                 }
                 DomNode ansNameDom = (DomNode) replay.getByXPath("a").get(0);
                 answer.setAnsName(ansNameDom.asText());
                 DomNode ansContentDom = (DomNode) replay.getByXPath("span").get(0);
                 answer.setAnsContent(ansContentDom.asText());
                 answers.add(answer);
            } catch (Exception e) {
                logger.info("qiushi 回复 error : " ,e);
                continue ;
            }
        }
        return answers;
    }
     
     
     
     
    /**
     * 获取主页信息
     *
     * @return
     * @throws Exception
     */
    public HtmlPage getPage() {
        try {
            WebClient client = new WebClient();
             
             
            //处理异常等信息
            client.setIncorrectnessListener(new SilentIncorrectnessListener());
            client.getOptions().setJavaScriptEnabled(false);
            client.setCssErrorHandler(new QuietCssErrorHandler());
            client.getOptions().setThrowExceptionOnScriptError(false);
            client.getOptions().setThrowExceptionOnFailingStatusCode(false);
            java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
            HtmlPage htmlPage = client.getPage("http://www.qiushibaike.com");
            return htmlPage;//
        } catch (Exception e) {
            return null;
        }
    }
     
     
    @Test
    public void testTransfer() {
        try {
            getContentTest();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
     
    public static void main(String[] args) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
             
            @Override
            public void run() {
                InviteEvaluate evaluate = new InviteEvaluate();
                evaluate.getContent();
            }
        },0, 1000*60*5);
         
         
    }
     
}

main方法执行junit测试,布布扣,bubuko.com

main方法执行junit测试

原文:http://www.cnblogs.com/xuzhenmin/p/3573875.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!