OSDN Git Service

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