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