1111import org .schabi .newpipe .extractor .subscription .SubscriptionItem ;
1212
1313import java .io .ByteArrayInputStream ;
14- import java .io .File ;
1514import java .io .FileInputStream ;
15+ import java .nio .charset .StandardCharsets ;
1616import java .util .Arrays ;
1717import java .util .List ;
1818
19- import static org .junit .Assert .*;
19+ import static org .junit .Assert .assertEquals ;
20+ import static org .junit .Assert .assertNotNull ;
21+ import static org .junit .Assert .assertTrue ;
22+ import static org .junit .Assert .fail ;
23+ import static org .schabi .newpipe .FileUtils .resolveTestResource ;
2024
2125/**
2226 * Test for {@link YoutubeSubscriptionExtractor}
@@ -34,81 +38,75 @@ public static void setupClass() {
3438
3539 @ Test
3640 public void testFromInputStream () throws Exception {
37- File testFile = new File ("extractor/src/test/resources/youtube_export_test.xml" );
38- if (!testFile .exists ()) testFile = new File ("src/test/resources/youtube_export_test.xml" );
41+ final List <SubscriptionItem > subscriptionItems = subscriptionExtractor .fromInputStream (
42+ new FileInputStream (resolveTestResource ("youtube_takeout_import_test.json" )));
43+ assertEquals (7 , subscriptionItems .size ());
3944
40- List <SubscriptionItem > subscriptionItems = subscriptionExtractor .fromInputStream (new FileInputStream (testFile ));
41- assertTrue ("List doesn't have exactly 8 items (had " + subscriptionItems .size () + ")" , subscriptionItems .size () == 8 );
42-
43- for (SubscriptionItem item : subscriptionItems ) {
45+ for (final SubscriptionItem item : subscriptionItems ) {
4446 assertNotNull (item .getName ());
4547 assertNotNull (item .getUrl ());
4648 assertTrue (urlHandler .acceptUrl (item .getUrl ()));
47- assertFalse ( item . getServiceId () == - 1 );
49+ assertEquals ( ServiceList . YouTube . getServiceId (), item . getServiceId () );
4850 }
4951 }
5052
5153 @ Test
5254 public void testEmptySourceException () throws Exception {
53- String emptySource = "<opml version=\" 1.1\" ><body>" +
54- "<outline text=\" Testing\" title=\" 123\" />" +
55- "</body></opml>" ;
56-
57- List <SubscriptionItem > items = subscriptionExtractor .fromInputStream (new ByteArrayInputStream (emptySource .getBytes ("UTF-8" )));
55+ final List <SubscriptionItem > items = subscriptionExtractor .fromInputStream (
56+ new ByteArrayInputStream ("[]" .getBytes (StandardCharsets .UTF_8 )));
5857 assertTrue (items .isEmpty ());
5958 }
6059
6160 @ Test
6261 public void testSubscriptionWithEmptyTitleInSource () throws Exception {
63- String channelId = "AA0AaAa0AaaaAAAAAA0aa0AA" ;
64- String source = "<opml version=\" 1.1\" ><body><outline text=\" YouTube Subscriptions\" title=\" YouTube Subscriptions\" >" +
65- "<outline text=\" \" title=\" \" type=\" rss\" xmlUrl=\" https://www.youtube.com/feeds/videos.xml?channel_id=" + channelId + "\" />" +
66- "</outline></body></opml>" ;
67-
68- List <SubscriptionItem > items = subscriptionExtractor .fromInputStream (new ByteArrayInputStream (source .getBytes ("UTF-8" )));
69- assertTrue ("List doesn't have exactly 1 item (had " + items .size () + ")" , items .size () == 1 );
70- assertTrue ("Item does not have an empty title (had \" " + items .get (0 ).getName () + "\" )" , items .get (0 ).getName ().isEmpty ());
71- assertTrue ("Item does not have the right channel id \" " + channelId + "\" (the whole url is \" " + items .get (0 ).getUrl () + "\" )" , items .get (0 ).getUrl ().endsWith (channelId ));
62+ final String source = "[{\" snippet\" :{\" resourceId\" :{\" channelId\" :\" UCEOXxzW2vU0P-0THehuIIeg\" }}}]" ;
63+ final List <SubscriptionItem > items = subscriptionExtractor .fromInputStream (
64+ new ByteArrayInputStream (source .getBytes (StandardCharsets .UTF_8 )));
65+
66+ assertEquals (1 , items .size ());
67+ assertEquals (ServiceList .YouTube .getServiceId (), items .get (0 ).getServiceId ());
68+ assertEquals ("https://www.youtube.com/channel/UCEOXxzW2vU0P-0THehuIIeg" , items .get (0 ).getUrl ());
69+ assertEquals ("" , items .get (0 ).getName ());
7270 }
7371
7472 @ Test
7573 public void testSubscriptionWithInvalidUrlInSource () throws Exception {
76- String source = "<opml version= \" 1.1 \" ><body><outline text= \" YouTube Subscriptions \" title= \" YouTube Subscriptions \" > " +
77- "<outline text= \" invalid \" title= \" url \" type= \" rss \" xmlUrl= \" https://www.youtube.com/feeds/videos.xml?channel_not_id=||||||| \" />" +
78- "<outline text= \" fail \" title= \" fail \" type= \" rss \" xmlUgrl= \" invalidTag \" />" +
79- "<outline text= \" invalid \" title= \" url \" type= \" rss \" xmlUrl= \" \" />" +
80- "<outline text= \" \" title= \" \" type= \" rss \" xmlUrl= \" \" />" +
81- "</outline></body></opml>" ;
82-
83- List < SubscriptionItem > items = subscriptionExtractor . fromInputStream ( new ByteArrayInputStream ( source . getBytes ( "UTF-8" ) ));
84- assertTrue ( items .isEmpty ());
74+ final String source = "[{ \" snippet \" :{ \" resourceId \" :{ \" channelId \" : \" gibberish \" }, \" title\" : \" name1 \" }}, " +
75+ "{ \" snippet \" :{ \" resourceId \" :{ \" channelId \" : \" UCEOXxzW2vU0P-0THehuIIeg \" }, \" title \" : \" name2 \" }}]" ;
76+ final List < SubscriptionItem > items = subscriptionExtractor . fromInputStream (
77+ new ByteArrayInputStream ( source . getBytes ( StandardCharsets . UTF_8 )));
78+
79+ assertEquals ( 1 , items . size ()) ;
80+ assertEquals ( ServiceList . YouTube . getServiceId (), items . get ( 0 ). getServiceId ());
81+ assertEquals ( "https://www.youtube.com/channel/UCEOXxzW2vU0P-0THehuIIeg" , items . get ( 0 ). getUrl ( ));
82+ assertEquals ( "name2" , items .get ( 0 ). getName ());
8583 }
8684
8785 @ Test
8886 public void testInvalidSourceException () {
8987 List <String > invalidList = Arrays .asList (
9088 "<xml><notvalid></notvalid></xml>" ,
9189 "<opml><notvalid></notvalid></opml>" ,
92- "<opml><body></body></opml>" ,
90+ "{\" a\" :\" b\" }" ,
91+ "[{}]" ,
92+ "[\" \" , 5]" ,
93+ "[{\" snippet\" :{\" title\" :\" name\" }}]" ,
94+ "[{\" snippet\" :{\" resourceId\" :{\" channelId\" :\" gibberish\" }}}]" ,
9395 "" ,
94- null ,
9596 "\uD83D \uDC28 \uD83D \uDC28 \uD83D \uDC28 " ,
9697 "gibberish" );
9798
9899 for (String invalidContent : invalidList ) {
99100 try {
100- if ( invalidContent != null ) {
101- byte [] bytes = invalidContent . getBytes ( "UTF-8" );
102- subscriptionExtractor . fromInputStream ( new ByteArrayInputStream ( bytes ) );
103- fail ( "Extracting from \" " + invalidContent + " \" didn't throw an exception" );
104- } else {
105- subscriptionExtractor . fromInputStream ( null );
106- fail ( "Extracting from null String didn't throw an exception" );
101+ byte [] bytes = invalidContent . getBytes ( StandardCharsets . UTF_8 );
102+ subscriptionExtractor . fromInputStream ( new ByteArrayInputStream ( bytes ) );
103+ fail ( "Extracting from \" " + invalidContent + " \" didn't throw an exception" );
104+ } catch ( final Exception e ) {
105+ boolean correctType = e instanceof SubscriptionExtractor . InvalidSourceException ;
106+ if (! correctType ) {
107+ e . printStackTrace ( );
107108 }
108- } catch (Exception e ) {
109- // System.out.println(" -> " + e);
110- boolean isExpectedException = e instanceof SubscriptionExtractor .InvalidSourceException ;
111- assertTrue ("\" " + e .getClass ().getSimpleName () + "\" is not the expected exception" , isExpectedException );
109+ assertTrue (e .getClass ().getSimpleName () + " is not InvalidSourceException" , correctType );
112110 }
113111 }
114112 }
0 commit comments