1 package be
.nikiroo
.fanfix
.test
;
4 import java
.util
.ArrayList
;
5 import java
.util
.Arrays
;
6 import java
.util
.Collections
;
9 import be
.nikiroo
.fanfix
.data
.Chapter
;
10 import be
.nikiroo
.fanfix
.data
.MetaData
;
11 import be
.nikiroo
.fanfix
.data
.Paragraph
;
12 import be
.nikiroo
.fanfix
.data
.Story
;
13 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
14 import be
.nikiroo
.fanfix
.library
.LocalLibrary
;
15 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
16 import be
.nikiroo
.utils
.IOUtils
;
17 import be
.nikiroo
.utils
.test
.TestCase
;
18 import be
.nikiroo
.utils
.test
.TestLauncher
;
20 class LibraryTest
extends TestLauncher
{
21 private BasicLibrary lib
;
24 public LibraryTest(String
[] args
) {
25 super("Library", args
);
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";
39 final String errMess
= "The resulting stories in the list are not what we expected";
41 addSeries(new TestLauncher("Local", args
) {
43 addTest(new TestCase("getList") {
45 public void test() throws Exception
{
46 List
<MetaData
> metas
= lib
.getList();
47 assertEquals(errMess
, Arrays
.asList(),
52 addTest(new TestCase("save") {
54 public void test() throws Exception
{
55 lib
.save(story(luid1
, name1
, source1
, author1
), luid1
,
58 List
<MetaData
> metas
= lib
.getList();
59 assertEquals(errMess
, Arrays
.asList(name1
),
64 addTest(new TestCase("save more") {
66 public void test() throws Exception
{
67 List
<MetaData
> metas
= null;
69 lib
.save(story(luid2
, name2
, source2
, author1
), luid2
,
72 metas
= lib
.getList();
73 assertEquals(errMess
, Arrays
.asList(name1
, name2
),
76 lib
.save(story(luid3
, name3
, source2
, author1
), luid3
,
79 metas
= lib
.getList();
81 Arrays
.asList(name1
, name2
, name3
),
86 addTest(new TestCase("save override luid (change author)") {
88 public void test() throws Exception
{
89 // same luid as a previous one
90 lib
.save(story(luid3
, name3ex
, source2
, author2
),
93 List
<MetaData
> metas
= lib
.getList();
95 Arrays
.asList(name1
, name2
, name3ex
),
100 addTest(new TestCase("getList with results") {
102 public void test() throws Exception
{
103 List
<MetaData
> metas
= lib
.getList();
104 assertEquals(3, metas
.size());
108 addTest(new TestCase("getList by source") {
110 public void test() throws Exception
{
111 List
<MetaData
> metas
= null;
113 metas
= lib
.getListBySource(source1
);
114 assertEquals(1, metas
.size());
116 metas
= lib
.getListBySource(source2
);
117 assertEquals(2, metas
.size());
119 metas
= lib
.getListBySource(null);
120 assertEquals(3, metas
.size());
124 addTest(new TestCase("getList by author") {
126 public void test() throws Exception
{
127 List
<MetaData
> metas
= null;
129 metas
= lib
.getListByAuthor(author1
);
130 assertEquals(2, metas
.size());
132 metas
= lib
.getListByAuthor(author2
);
133 assertEquals(1, metas
.size());
135 metas
= lib
.getListByAuthor(null);
136 assertEquals(3, metas
.size());
140 addTest(new TestCase("changeType") {
142 public void test() throws Exception
{
143 List
<MetaData
> metas
= null;
145 lib
.changeSource(luid3
, source1
, null);
147 metas
= lib
.getListBySource(source1
);
148 assertEquals(2, metas
.size());
150 metas
= lib
.getListBySource(source2
);
151 assertEquals(1, metas
.size());
153 metas
= lib
.getListBySource(null);
154 assertEquals(3, metas
.size());
158 addTest(new TestCase("save override luid (change source)") {
160 public void test() throws Exception
{
161 List
<MetaData
> metas
= null;
163 // same luid as a previous one
164 lib
.save(story(luid3
, "My story 3", source2
, author2
),
167 metas
= lib
.getListBySource(source1
);
168 assertEquals(1, metas
.size());
170 metas
= lib
.getListBySource(source2
);
171 assertEquals(2, metas
.size());
173 metas
= lib
.getListBySource(null);
174 assertEquals(3, metas
.size());
182 protected void start() throws Exception
{
183 tmp
= File
.createTempFile(".test-fanfix", ".library");
187 lib
= new LocalLibrary(tmp
, OutputType
.INFO_TEXT
, OutputType
.CBZ
);
191 protected void stop() throws Exception
{
192 IOUtils
.deltree(tmp
);
196 * Return the (sorted) list of titles present in this list of
202 * @return the sorted list
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());
210 Collections
.sort(list
);
214 private Story
story(String luid
, String title
, String source
, String author
) {
215 Story story
= new Story();
217 MetaData meta
= new MetaData();
219 meta
.setTitle(title
);
220 meta
.setSource(source
);
221 meta
.setAuthor(author
);
224 Chapter resume
= chapter(0, "Resume");
225 meta
.setResume(resume
);
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
);
233 for (Chapter chap
: story
.getChapters()) {
234 words
+= chap
.getWords();
236 meta
.setWords(words
);
241 private Chapter
chapter(int number
, String name
) {
242 Chapter chapter
= new Chapter(number
, name
);
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));
248 chapter
.setParagraphs(paragraphs
);
251 for (Paragraph para
: chapter
.getParagraphs()) {
252 words
+= para
.getWords();
254 chapter
.setWords(words
);