Fix configure.sh again (--tui=no + jar was not ok)
[fanfix.git] / src / be / nikiroo / fanfix / test / LibraryTest.java
CommitLineData
6cac0e45
NR
1package be.nikiroo.fanfix.test;
2
3import java.io.File;
4import java.util.ArrayList;
5import java.util.List;
6
68e2c6d2
NR
7import be.nikiroo.fanfix.BasicLibrary;
8import be.nikiroo.fanfix.LocalLibrary;
6cac0e45
NR
9import be.nikiroo.fanfix.data.Chapter;
10import be.nikiroo.fanfix.data.MetaData;
11import be.nikiroo.fanfix.data.Paragraph;
12import be.nikiroo.fanfix.data.Story;
13import be.nikiroo.fanfix.output.BasicOutput.OutputType;
14import be.nikiroo.utils.IOUtils;
15import be.nikiroo.utils.test.TestCase;
16import be.nikiroo.utils.test.TestLauncher;
17
18public class LibraryTest extends TestLauncher {
68e2c6d2 19 private BasicLibrary lib;
6cac0e45
NR
20 private File tmp;
21
22 public LibraryTest(String[] args) {
23 super("Library", args);
24
25 final String luid1 = "001"; // A
26 final String luid2 = "002"; // B
27 final String luid3 = "003"; // B then A, then B
28 final String source1 = "Source A";
29 final String source2 = "Source B";
30 final String author1 = "Unknown author";
31 final String author2 = "Another other otter author";
32
33 addSeries(new TestLauncher("Local", args) {
34 {
35 addTest(new TestCase("getList") {
36 @Override
37 public void test() throws Exception {
68e2c6d2 38 List<MetaData> metas = lib.getList();
6cac0e45
NR
39 assertEquals(0, metas.size());
40 }
41 });
42
43 addTest(new TestCase("save") {
44 @Override
45 public void test() throws Exception {
46 lib.save(story(luid1, "My story 1", source1, author1),
47 luid1, null);
48
68e2c6d2 49 List<MetaData> metas = lib.getList();
6cac0e45
NR
50 assertEquals(1, metas.size());
51 }
52 });
53
54 addTest(new TestCase("save more") {
55 @Override
56 public void test() throws Exception {
57 List<MetaData> metas = null;
58
59 lib.save(story(luid2, "My story 2", source2, author1),
60 luid2, null);
61
68e2c6d2 62 metas = lib.getList();
6cac0e45
NR
63 assertEquals(2, metas.size());
64
65 lib.save(story(luid3, "My story 3", source2, author1),
66 luid3, null);
67
68e2c6d2 68 metas = lib.getList();
6cac0e45
NR
69 assertEquals(3, metas.size());
70 }
71 });
72
73 addTest(new TestCase("save override luid (change author)") {
74 @Override
75 public void test() throws Exception {
76 // same luid as a previous one
68e2c6d2
NR
77 lib.save(
78 story(luid3, "My story 3 [edited]", source2,
79 author2), luid3, null);
6cac0e45 80
68e2c6d2 81 List<MetaData> metas = lib.getList();
6cac0e45
NR
82 assertEquals(3, metas.size());
83 }
84 });
85
86 addTest(new TestCase("getList with results") {
87 @Override
88 public void test() throws Exception {
68e2c6d2 89 List<MetaData> metas = lib.getList();
6cac0e45
NR
90 assertEquals(3, metas.size());
91 }
92 });
93
94 addTest(new TestCase("getList by source") {
95 @Override
96 public void test() throws Exception {
97 List<MetaData> metas = null;
98
99 metas = lib.getListBySource(source1);
100 assertEquals(1, metas.size());
101
102 metas = lib.getListBySource(source2);
103 assertEquals(2, metas.size());
104
105 metas = lib.getListBySource(null);
106 assertEquals(3, metas.size());
107 }
108 });
109
110 addTest(new TestCase("getList by author") {
111 @Override
112 public void test() throws Exception {
113 List<MetaData> metas = null;
114
115 metas = lib.getListByAuthor(author1);
116 assertEquals(2, metas.size());
117
118 metas = lib.getListByAuthor(author2);
119 assertEquals(1, metas.size());
120
121 metas = lib.getListByAuthor(null);
122 assertEquals(3, metas.size());
123 }
124 });
125
126 addTest(new TestCase("changeType") {
127 @Override
128 public void test() throws Exception {
129 List<MetaData> metas = null;
130
68e2c6d2 131 lib.changeSource(luid3, source1, null);
6cac0e45
NR
132
133 metas = lib.getListBySource(source1);
134 assertEquals(2, metas.size());
135
136 metas = lib.getListBySource(source2);
137 assertEquals(1, metas.size());
138
139 metas = lib.getListBySource(null);
140 assertEquals(3, metas.size());
141 }
142 });
143
144 addTest(new TestCase("save override luid (change source)") {
145 @Override
146 public void test() throws Exception {
147 List<MetaData> metas = null;
148
149 // same luid as a previous one
150 lib.save(story(luid3, "My story 3", source2, author2),
151 luid3, null);
152
153 metas = lib.getListBySource(source1);
154 assertEquals(1, metas.size());
155
156 metas = lib.getListBySource(source2);
157 assertEquals(2, metas.size());
158
159 metas = lib.getListBySource(null);
160 assertEquals(3, metas.size());
161 }
162 });
163 }
164 });
165 }
166
167 @Override
168 protected void start() throws Exception {
169 tmp = File.createTempFile(".test-fanfix", ".library");
170 tmp.delete();
171 tmp.mkdir();
172
68e2c6d2 173 lib = new LocalLibrary(tmp, OutputType.INFO_TEXT, OutputType.CBZ);
6cac0e45
NR
174 }
175
176 @Override
177 protected void stop() throws Exception {
178 IOUtils.deltree(tmp);
179 }
180
181 private Story story(String luid, String title, String source, String author) {
182 Story story = new Story();
183
184 MetaData meta = new MetaData();
185 meta.setLuid(luid);
186 meta.setTitle(title);
187 meta.setSource(source);
188 meta.setAuthor(author);
189 story.setMeta(meta);
190
191 Chapter resume = chapter(0, "Resume");
192 meta.setResume(resume);
193
194 List<Chapter> chapters = new ArrayList<Chapter>();
195 chapters.add(chapter(1, "Chap 1"));
196 chapters.add(chapter(2, "Chap 2"));
197 story.setChapters(chapters);
198
199 long words = 0;
200 for (Chapter chap : story.getChapters()) {
201 words += chap.getWords();
202 }
203 meta.setWords(words);
204
205 return story;
206 }
207
208 private Chapter chapter(int number, String name) {
209 Chapter chapter = new Chapter(number, name);
210
211 List<Paragraph> paragraphs = new ArrayList<Paragraph>();
212 paragraphs.add(new Paragraph(Paragraph.ParagraphType.NORMAL,
213 "some words in this paragraph please thank you", 8));
214
215 chapter.setParagraphs(paragraphs);
216
217 long words = 0;
218 for (Paragraph para : chapter.getParagraphs()) {
219 words += para.getWords();
220 }
221 chapter.setWords(words);
222
223 return chapter;
224 }
225}