OSDN Git Service

4b425ce8eca8e2317e237b3c7f44393c66212fdf
[coroid/NicoBrowser.git] / test / nicobrowser / NicoHttpClientTest.java
1 /*$Id$*/
2 package nicobrowser;
3
4 import java.io.File;
5 import java.io.IOException;
6 import java.net.URISyntaxException;
7 import java.net.URL;
8 import java.util.ArrayList;
9 import java.util.logging.Level;
10 import java.util.logging.Logger;
11 import nicobrowser.entity.NicoContent;
12 import java.util.List;
13 import javax.persistence.EntityManager;
14 import javax.persistence.EntityManagerFactory;
15 import javax.persistence.EntityTransaction;
16 import javax.persistence.Persistence;
17 import nicobrowser.entity.NicoContent.Status;
18 import nicobrowser.search.SearchKind;
19 import nicobrowser.search.SortKind;
20 import nicobrowser.search.SortOrder;
21 import nicobrowser.search.SearchResult;
22 import org.apache.http.HttpException;
23 import org.junit.After;
24 import org.junit.AfterClass;
25 import org.junit.Before;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28 import static org.junit.Assert.*;
29
30 /**
31  *
32  * @author yuki
33  */
34 public class NicoHttpClientTest {
35
36     static final String OK_MAIL = "niconico.senyou@live.jp";
37     static final String OK_PASS = "piyopiyo";
38     static final String OK_LIST_NO = "1802157";
39     static final String NORMAL_VIDEO = "sm1359820";
40     static final String OFFICIAL_VIDEO = "so8799877";
41     private NicoHttpClient instance;
42
43     public NicoHttpClientTest() {
44     }
45
46     @BeforeClass
47     public static void setUpClass() throws Exception {
48     }
49
50     @AfterClass
51     public static void tearDownClass() throws Exception {
52     }
53
54     @Before
55     public void setUp() {
56         instance = new NicoHttpClient();
57     }
58
59     @After
60     public void tearDown() {
61     }
62
63     /**
64      * Test of login method, of class NicoHttpClient.
65      */
66     @Test
67     public void login() throws HttpException, URISyntaxException, InterruptedException {
68         System.out.println("login");
69
70         // ログイン失敗ケース
71         instance.logout();
72         String mail = "test@example.com";
73         String password = "xxxx";
74         boolean result = instance.login(mail, password);
75         assertEquals(false, result);
76
77         // ログイン成功ケース
78         instance.logout();
79         mail = OK_MAIL;
80         password = OK_PASS;
81         result = instance.login(mail, password);
82         assertEquals(true, result);
83     }
84
85     public void logout() throws URISyntaxException, HttpException, InterruptedException {
86         System.out.println("logout");
87
88         boolean result;
89         result = instance.logout();
90         assertEquals(true, result);
91
92         result = instance.logout();
93         assertEquals(true, result);
94     }
95
96     @Test
97     public void loadMyList() throws URISyntaxException, HttpException, InterruptedException {
98         System.out.println("loadMyList");
99
100         List list;
101
102         list = instance.loadMyList(OK_LIST_NO);
103         assertNotNull(list);
104         assertNotSame(0, list.size());
105 //        for (Object o : list) {
106 //            System.out.println(o.toString());
107 //        }
108
109         list = instance.loadMyList("XXXX");
110         assertNotNull(list);
111         assertSame(0, list.size());
112     }
113
114 //
115     public void loadMyListDaily() throws URISyntaxException, HttpException, InterruptedException {
116         System.out.println("loadMyListDaily");
117         List<NicoContent> list = instance.loadMyListDaily();
118         assertNotNull(list);
119         assertNotSame(0, list.size());
120 //        System.out.println(list.size());
121 //        for (Object o : list) {
122 //            System.out.println(o.toString());
123 //        }
124
125         EntityManagerFactory factory;
126         EntityManager manager;
127
128         factory = Persistence.createEntityManagerFactory("NicoBrowserPU");
129         manager = factory.createEntityManager();
130
131         EntityTransaction transaction = manager.getTransaction();
132
133         transaction.begin();
134         try {
135             for (NicoContent c : list) {
136                 manager.persist(c);
137             }
138
139             transaction.commit();
140         } catch (Exception ex) {
141             ex.printStackTrace();
142             transaction.rollback();
143         } finally {
144             manager.close();
145             factory.close();
146         }
147
148     }
149
150     @Test
151     public void getVideoInfo() throws URISyntaxException, HttpException, InterruptedException, IOException {
152         System.out.println("getFlv");
153
154         instance.login(OK_MAIL, OK_PASS);
155         try {
156             URL str = instance.getVideoInfo(NORMAL_VIDEO).getVideoUrl();
157             System.out.println(str);
158         } catch (IOException ex) {
159             fail();
160             Logger.getLogger(NicoHttpClientTest.class.getName()).log(Level.SEVERE, null, ex);
161         }
162
163     }
164
165     public void getVideoInfo_Official() throws URISyntaxException, HttpException, InterruptedException, IOException {
166         System.out.println("getVideoInfo_Official");
167         instance.login(OK_MAIL, OK_PASS);
168         VideoInfo vi = instance.getVideoInfo(OFFICIAL_VIDEO);
169         assertNotSame(OFFICIAL_VIDEO, vi.getRealVideoId());
170     }
171
172     public void downLoad() throws URISyntaxException, IOException, HttpException, InterruptedException {
173         System.out.println("downLoad");
174
175         instance.login(OK_MAIL, OK_PASS);
176
177         try {
178             VideoInfo vi = instance.getVideoInfo("sm183036");
179             instance.getFlvFile(vi, "sm183036", NicoContent.Status.GET_INFO, false, ProgressListener.EMPTY_LISTENER);
180         } catch (IOException ex) {
181             Logger.getLogger(NicoHttpClientTest.class.getName()).log(Level.SEVERE, null, ex);
182             fail();
183         }
184     }
185
186     /**
187      * Test of getFlvFile method, of class NicoHttpClient.
188      */
189     public void getFlvFile_String() throws HttpException, InterruptedException, URISyntaxException {
190         System.out.println("getFlvFile");
191         String videoID = "sm1097445";
192         instance.login(OK_MAIL, OK_PASS);
193         GetFlvResult result;
194         try {
195             VideoInfo vi = instance.getVideoInfo(videoID);
196             result = instance.getFlvFile(vi);
197             System.out.println(result);
198             assertNotSame(Status.GET_INFO, result);
199         } catch (Exception ex) {
200             Logger.getLogger(NicoHttpClientTest.class.getName()).log(Level.SEVERE, null, ex);
201             fail();
202         }
203
204         // 権限が無い動画?の取得
205         // http://www.nicovideo.jp/watch/1231042440
206         // 【亡き王女の為のセプテット・ツェペシュの幼き末裔】Priere -プリエール-
207         try {
208             VideoInfo vi = instance.getVideoInfo("1231042440");
209             result = instance.getFlvFile(vi);
210             fail("権限が無い動画を取得しようとした場合は例外が送出される");
211         } catch (IOException ex) {
212             Logger.getLogger(NicoHttpClientTest.class.getName()).log(Level.SEVERE, null, ex);
213         }
214     }
215
216     @Test
217     public void getFlvFile_Official() throws IOException, URISyntaxException, HttpException, InterruptedException {
218         // 公式動画(ビデオIDが実際と異なる)のダウンロード
219         instance.login(OK_MAIL, OK_PASS);
220         VideoInfo vi = instance.getVideoInfo(OFFICIAL_VIDEO);
221         GetFlvResult result = instance.getFlvFile(vi);
222         assertNotSame(Status.GET_INFO, result.getStatus());
223         assertNull("公式動画からは著者は取得できない", result.getAuthor());
224     }
225
226     @Test
227     public void search() throws URISyntaxException, InterruptedException, HttpException, IOException {
228         System.out.println("search");
229         instance.login(OK_MAIL, OK_PASS);
230
231         SearchResult result = instance.search(SearchKind.KeyWord, "初音ミク", SortKind.PlayTimes, SortOrder.Descending, 1);
232         ArrayList<NicoContent> conts = result.getContents();
233         System.out.println("検索結果件数: " + conts.size());
234         assertEquals("1ページの上限32件がヒット", 32, conts.size());
235         assertTrue(result.getPages().size() > 0);
236     }
237
238     public void loadMyMovie() throws URISyntaxException, InterruptedException, HttpException, IOException {
239         System.out.println("search");
240         instance.login(OK_MAIL, OK_PASS);
241
242         NicoContent cont;
243
244         // 通常の動画
245         cont = instance.loadMyMovie("sm9");
246         assertNotNull(cont);
247         assertEquals("sm9", cont.getNicoId());
248
249         // チャンネル動画はnull(statusがfailなので)
250         cont = instance.loadMyMovie("1228201771");
251         assertNull(cont);
252
253         // 削除済み
254         cont = instance.loadMyMovie("sm1");
255         assertNull(cont);
256
257         // 存在しない
258         cont = instance.loadMyMovie("xxx");
259         assertNull(cont);
260     }
261
262     /**
263      * マイリスト登録するテストケースだが, 不完全. 登録できなかった場合もテストケースは正常終了する.
264      */
265     @Test
266     public void addMyList() throws URISyntaxException, HttpException, InterruptedException, IOException {
267         instance.login(OK_MAIL, OK_PASS);
268
269         instance.addMyList("5681871", "sm9477049");
270     }
271
272     @Test
273     public void getCommentFile() throws URISyntaxException, HttpException, InterruptedException, Exception {
274         final String comm = NORMAL_VIDEO + "comment";
275         final String tcomm = NORMAL_VIDEO + "tcomment.xml";
276         File commFile = new File(comm + ".xml");
277         File tcommFile = new File(tcomm);
278
279         assertFalse("ファイルを削除してください", commFile.exists());
280         assertFalse("ファイルを削除してください", tcommFile.exists());
281
282         instance.login(OK_MAIL, OK_PASS);
283         VideoInfo vi = instance.getVideoInfo(NORMAL_VIDEO);
284         instance.getCommentFile(vi, comm, null);
285         instance.getTCommentFile(vi, tcomm);
286
287         assertTrue(commFile.exists());
288         assertTrue(tcommFile.exists());
289     }
290
291     @Test
292     public void getCommentFile_Official() throws URISyntaxException, HttpException, InterruptedException, Exception {
293         final String comm = OFFICIAL_VIDEO + "comment";
294         final String tcomm = OFFICIAL_VIDEO + "tcomment.xml";
295         File commFile = new File(comm + ".xml");
296         File tcommFile = new File(tcomm);
297
298         assertFalse("ファイルを削除してください", commFile.exists());
299         assertFalse("ファイルを削除してください", tcommFile.exists());
300
301         instance.login(OK_MAIL, OK_PASS);
302         VideoInfo vi = instance.getVideoInfo(OFFICIAL_VIDEO);
303         instance.getCommentFile(vi, comm, null);
304         instance.getTCommentFile(vi, tcomm);
305
306         assertTrue(commFile.exists());
307         assertTrue(tcommFile.exists());
308     }
309 }