1-
21import java .io .*;
32import java .util .*;
43import org .junit .Test ;
54import static org .junit .Assert .*;
65import org .kiwix .kiwixlib .*;
76
87public class test {
9- static {
10- System .loadLibrary ("kiwix" );
11- }
12-
13- private static byte [] getFileContent (String path )
14- throws IOException
15- {
16- File file = new File (path );
17- DataInputStream in = new DataInputStream (
18- new BufferedInputStream (
19- new FileInputStream (file )));
20- byte [] data = new byte [(int )file .length ()];
21- in .read (data );
22- return data ;
23- }
24-
25- private static byte [] getFileContentPartial (String path , int offset , int size )
26- throws IOException
27- {
28- File file = new File (path );
29- DataInputStream in = new DataInputStream (
30- new BufferedInputStream (
31- new FileInputStream (file )));
32- byte [] data = new byte [size ];
33- in .skipBytes (offset );
34- in .read (data , 0 , size );
35- return data ;
36- }
37-
38- private static String getTextFileContent (String path )
39- throws IOException
40- {
41- return new String (getFileContent (path ));
42- }
43-
44- @ Test
45- public void testReader ()
46- throws JNIKiwixException , IOException
47- {
48- JNIKiwixReader reader = new JNIKiwixReader ("small.zim" );
49- assertEquals ("Test ZIM file" , reader .getTitle ());
50- assertEquals (45 , reader .getFileSize ()); // The file size is in KiB
51- assertEquals ("A/main.html" , reader .getMainPage ());
52- String s = getTextFileContent ("small_zimfile_data/main.html" );
53- byte [] c = reader .getContent (new JNIKiwixString ("A/main.html" ),
54- new JNIKiwixString (),
55- new JNIKiwixString (),
56- new JNIKiwixInt ());
57- assertEquals (s , new String (c ));
58-
59- byte [] faviconData = getFileContent ("small_zimfile_data/favicon.png" );
60- assertEquals (faviconData .length , reader .getArticleSize ("I/favicon.png" ));
61- c = reader .getContent (new JNIKiwixString ("I/favicon.png" ),
62- new JNIKiwixString (),
63- new JNIKiwixString (),
64- new JNIKiwixInt ());
65- assertTrue (Arrays .equals (faviconData , c ));
66-
67- DirectAccessInfo dai = reader .getDirectAccessInformation ("I/favicon.png" );
68- assertNotEquals ("" , dai .filename );
69- c = getFileContentPartial (dai .filename , (int )dai .offset , faviconData .length );
70- assertTrue (Arrays .equals (faviconData , c ));
71- }
72-
73- @ Test
74- public void testReaderByFd ()
75- throws JNIKiwixException , IOException
76- {
77- FileInputStream fis = new FileInputStream ("small.zim" );
78- JNIKiwixReader reader = new JNIKiwixReader (fis .getFD ());
79- assertEquals ("Test ZIM file" , reader .getTitle ());
80- assertEquals (45 , reader .getFileSize ()); // The file size is in KiB
81- assertEquals ("A/main.html" , reader .getMainPage ());
82- String s = getTextFileContent ("small_zimfile_data/main.html" );
83- byte [] c = reader .getContent (new JNIKiwixString ("A/main.html" ),
84- new JNIKiwixString (),
85- new JNIKiwixString (),
86- new JNIKiwixInt ());
87- assertEquals (s , new String (c ));
88-
89- byte [] faviconData = getFileContent ("small_zimfile_data/favicon.png" );
90- assertEquals (faviconData .length , reader .getArticleSize ("I/favicon.png" ));
91- c = reader .getContent (new JNIKiwixString ("I/favicon.png" ),
92- new JNIKiwixString (),
93- new JNIKiwixString (),
94- new JNIKiwixInt ());
95- assertTrue (Arrays .equals (faviconData , c ));
96-
97- DirectAccessInfo dai = reader .getDirectAccessInformation ("I/favicon.png" );
98- assertNotEquals ("" , dai .filename );
99- c = getFileContentPartial (dai .filename , (int )dai .offset , faviconData .length );
100- assertTrue (Arrays .equals (faviconData , c ));
101- }
102-
103- @ Test
104- public void testReaderWithAnEmbeddedArchive ()
105- throws JNIKiwixException , IOException
106- {
107- File plainArchive = new File ("small.zim" );
108- FileInputStream fis = new FileInputStream ("small.zim.embedded" );
109- JNIKiwixReader reader = new JNIKiwixReader (fis .getFD (), 8 , plainArchive .length ());
110- assertEquals ("Test ZIM file" , reader .getTitle ());
111- assertEquals (45 , reader .getFileSize ()); // The file size is in KiB
112- assertEquals ("A/main.html" , reader .getMainPage ());
113- String s = getTextFileContent ("small_zimfile_data/main.html" );
114- byte [] c = reader .getContent (new JNIKiwixString ("A/main.html" ),
115- new JNIKiwixString (),
116- new JNIKiwixString (),
117- new JNIKiwixInt ());
118- assertEquals (s , new String (c ));
119-
120- byte [] faviconData = getFileContent ("small_zimfile_data/favicon.png" );
121- assertEquals (faviconData .length , reader .getArticleSize ("I/favicon.png" ));
122- c = reader .getContent (new JNIKiwixString ("I/favicon.png" ),
123- new JNIKiwixString (),
124- new JNIKiwixString (),
125- new JNIKiwixInt ());
126- assertTrue (Arrays .equals (faviconData , c ));
127-
128- DirectAccessInfo dai = reader .getDirectAccessInformation ("I/favicon.png" );
129- assertNotEquals ("" , dai .filename );
130- c = getFileContentPartial (dai .filename , (int )dai .offset , faviconData .length );
131- assertTrue (Arrays .equals (faviconData , c ));
132- }
133-
134- @ Test
135- public void testLibrary ()
136- throws IOException
137- {
138- Library lib = new Library ();
139- Manager manager = new Manager (lib );
140- String content = getTextFileContent ("catalog.xml" );
141- manager .readOpds (content , "http://localhost" );
142- assertEquals (lib .getBookCount (true , true ), 1 );
143- String [] bookIds = lib .getBooksIds ();
144- assertEquals (bookIds .length , 1 );
145- Book book = lib .getBookById (bookIds [0 ]);
146- assertEquals (book .getTitle (), "Test ZIM file" );
147- assertEquals (book .getTags (), "unit;test" );
148- assertEquals (book .getFaviconUrl (), "http://localhost/meta?name=favicon&content=small" );
149- assertEquals (book .getUrl (), "http://localhost/small.zim" );
150- }
151-
152- static
153- public void main (String [] args ) {
154- Library lib = new Library ();
155- lib .getBookCount (true , true );
156- }
157-
158-
159-
160- }
8+ static {
9+ System .loadLibrary ("kiwix" );
10+ System .loadLibrary ("zim" );
11+ System .loadLibrary ("buildkiwix" );
12+ }
13+
14+ private static byte [] getFileContent (String path )
15+ throws IOException
16+ {
17+ File file = new File (path );
18+ DataInputStream in = new DataInputStream (
19+ new BufferedInputStream (
20+ new FileInputStream (file )));
21+ byte [] data = new byte [(int )file .length ()];
22+ in .read (data );
23+ return data ;
24+ }
25+
26+ private static byte [] getFileContentPartial (String path , int offset , int size )
27+ throws IOException
28+ {
29+ File file = new File (path );
30+ DataInputStream in = new DataInputStream (
31+ new BufferedInputStream (
32+ new FileInputStream (file )));
33+ byte [] data = new byte [size ];
34+ in .skipBytes (offset );
35+ in .read (data , 0 , size );
36+ return data ;
37+ }
38+
39+ private static String getTextFileContent (String path )
40+ throws IOException
41+ {
42+ return new String (getFileContent (path ));
43+ }
44+
45+ @ Test
46+ public void testReader ()
47+ throws JNIKiwixException , IOException
48+ {
49+ JNIKiwixReader reader = new JNIKiwixReader ("small.zim" );
50+ assertEquals ("Test ZIM file" , reader .getTitle ());
51+ assertEquals (3 , reader .getFileSize ()); // The file size is in KiB
52+ assertEquals ("A/main.html" , reader .getMainPage ());
53+ String s = getTextFileContent ("small_zimfile_data/main.html" );
54+ byte [] c = reader .getContent (new JNIKiwixString ("A/main.html" ),
55+ new JNIKiwixString (),
56+ new JNIKiwixString (),
57+ new JNIKiwixInt ());
58+ assertEquals (s , new String (c ));
59+
60+ byte [] faviconData = getFileContent ("small_zimfile_data/favicon.png" );
61+ assertEquals (faviconData .length , reader .getArticleSize ("I/favicon.png" ));
62+ c = reader .getContent (new JNIKiwixString ("I/favicon.png" ),
63+ new JNIKiwixString (),
64+ new JNIKiwixString (),
65+ new JNIKiwixInt ());
66+ assertTrue (Arrays .equals (faviconData , c ));
67+
68+ DirectAccessInfo dai = reader .getDirectAccessInformation ("I/favicon.png" );
69+ assertNotEquals ("" , dai .filename );
70+ c = getFileContentPartial (dai .filename , (int )dai .offset , faviconData .length );
71+ assertTrue (Arrays .equals (faviconData , c ));
72+ }
73+
74+ @ Test
75+ public void testReaderByFd ()
76+ throws JNIKiwixException , IOException
77+ {
78+ FileInputStream fis = new FileInputStream ("small.zim" );
79+ JNIKiwixReader reader = new JNIKiwixReader (fis .getFD ());
80+ assertEquals ("Test ZIM file" , reader .getTitle ());
81+ assertEquals (3 , reader .getFileSize ()); // The file size is in KiB
82+ assertEquals ("A/main.html" , reader .getMainPage ());
83+ String s = getTextFileContent ("small_zimfile_data/main.html" );
84+ byte [] c = reader .getContent (new JNIKiwixString ("A/main.html" ),
85+ new JNIKiwixString (),
86+ new JNIKiwixString (),
87+ new JNIKiwixInt ());
88+ assertEquals (s , new String (c ));
89+
90+ byte [] faviconData = getFileContent ("small_zimfile_data/favicon.png" );
91+ assertEquals (faviconData .length , reader .getArticleSize ("I/favicon.png" ));
92+ c = reader .getContent (new JNIKiwixString ("I/favicon.png" ),
93+ new JNIKiwixString (),
94+ new JNIKiwixString (),
95+ new JNIKiwixInt ());
96+ assertTrue (Arrays .equals (faviconData , c ));
97+
98+ DirectAccessInfo dai = reader .getDirectAccessInformation ("I/favicon.png" );
99+ assertNotEquals ("" , dai .filename );
100+ c = getFileContentPartial (dai .filename , (int )dai .offset , faviconData .length );
101+ assertTrue (Arrays .equals (faviconData , c ));
102+ }
103+
104+ @ Test
105+ public void testReaderWithAnEmbeddedArchive ()
106+ throws JNIKiwixException , IOException
107+ {
108+ File plainArchive = new File ("small.zim" );
109+ FileInputStream fis = new FileInputStream ("small.zim.embedded" );
110+ JNIKiwixReader reader = new JNIKiwixReader (fis .getFD (), 8 , plainArchive .length ());
111+ assertEquals ("Test ZIM file" , reader .getTitle ());
112+ assertEquals (3 , reader .getFileSize ()); // The file size is in KiB
113+ assertEquals ("A/main.html" , reader .getMainPage ());
114+ String s = getTextFileContent ("small_zimfile_data/main.html" );
115+ byte [] c = reader .getContent (new JNIKiwixString ("A/main.html" ),
116+ new JNIKiwixString (),
117+ new JNIKiwixString (),
118+ new JNIKiwixInt ());
119+ assertEquals (s , new String (c ));
120+
121+ byte [] faviconData = getFileContent ("small_zimfile_data/favicon.png" );
122+ assertEquals (faviconData .length , reader .getArticleSize ("I/favicon.png" ));
123+ c = reader .getContent (new JNIKiwixString ("I/favicon.png" ),
124+ new JNIKiwixString (),
125+ new JNIKiwixString (),
126+ new JNIKiwixInt ());
127+ assertTrue (Arrays .equals (faviconData , c ));
128+
129+ DirectAccessInfo dai = reader .getDirectAccessInformation ("I/favicon.png" );
130+ assertNotEquals ("" , dai .filename );
131+ c = getFileContentPartial (dai .filename , (int )dai .offset , faviconData .length );
132+ assertTrue (Arrays .equals (faviconData , c ));
133+ }
134+
135+ @ Test
136+ public void testLibrary ()
137+ throws IOException
138+ {
139+ Library lib = new Library ();
140+ Manager manager = new Manager (lib );
141+ String content = getTextFileContent ("catalog.xml" );
142+ manager .readOpds (content , "http://localhost" );
143+ assertEquals (lib .getBookCount (true , true ), 1 );
144+ String [] bookIds = lib .getBooksIds ();
145+ assertEquals (bookIds .length , 1 );
146+ Book book = lib .getBookById (bookIds [0 ]);
147+ assertEquals (book .getTitle (), "Test ZIM file" );
148+ assertEquals (book .getTags (), "unit;test" );
149+ assertEquals (book .getFaviconUrl (), "http://localhost/meta?name=favicon&content=small" );
150+ assertEquals (book .getUrl (), "http://localhost/small.zim" );
151+ }
152+
153+ static
154+ public void main (String [] args ) {
155+ Library lib = new Library ();
156+ lib .getBookCount (true , true );
157+ }
158+ }
0 commit comments