OSDN Git Service

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