1 package be
.nikiroo
.fanfix
.test
;
4 import java
.util
.ArrayList
;
7 import be
.nikiroo
.fanfix
.data
.Chapter
;
8 import be
.nikiroo
.fanfix
.data
.MetaData
;
9 import be
.nikiroo
.fanfix
.data
.Paragraph
;
10 import be
.nikiroo
.fanfix
.data
.Story
;
11 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
12 import be
.nikiroo
.fanfix
.library
.LocalLibrary
;
13 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
14 import be
.nikiroo
.utils
.IOUtils
;
15 import be
.nikiroo
.utils
.test
.TestCase
;
16 import be
.nikiroo
.utils
.test
.TestLauncher
;
18 public class LibraryTest
extends TestLauncher
{
19 private BasicLibrary lib
;
22 public LibraryTest(String
[] args
) {
23 super("Library", args
);
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";
33 addSeries(new TestLauncher("Local", args
) {
35 addTest(new TestCase("getList") {
37 public void test() throws Exception
{
38 List
<MetaData
> metas
= lib
.getList();
39 assertEquals(0, metas
.size());
43 addTest(new TestCase("save") {
45 public void test() throws Exception
{
46 lib
.save(story(luid1
, "My story 1", source1
, author1
),
49 List
<MetaData
> metas
= lib
.getList();
50 assertEquals(1, metas
.size());
54 addTest(new TestCase("save more") {
56 public void test() throws Exception
{
57 List
<MetaData
> metas
= null;
59 lib
.save(story(luid2
, "My story 2", source2
, author1
),
62 metas
= lib
.getList();
63 assertEquals(2, metas
.size());
65 lib
.save(story(luid3
, "My story 3", source2
, author1
),
68 metas
= lib
.getList();
69 assertEquals(3, metas
.size());
73 addTest(new TestCase("save override luid (change author)") {
75 public void test() throws Exception
{
76 // same luid as a previous one
78 story(luid3
, "My story 3 [edited]", source2
,
79 author2
), luid3
, null);
81 List
<MetaData
> metas
= lib
.getList();
82 assertEquals(3, metas
.size());
86 addTest(new TestCase("getList with results") {
88 public void test() throws Exception
{
89 List
<MetaData
> metas
= lib
.getList();
90 assertEquals(3, metas
.size());
94 addTest(new TestCase("getList by source") {
96 public void test() throws Exception
{
97 List
<MetaData
> metas
= null;
99 metas
= lib
.getListBySource(source1
);
100 assertEquals(1, metas
.size());
102 metas
= lib
.getListBySource(source2
);
103 assertEquals(2, metas
.size());
105 metas
= lib
.getListBySource(null);
106 assertEquals(3, metas
.size());
110 addTest(new TestCase("getList by author") {
112 public void test() throws Exception
{
113 List
<MetaData
> metas
= null;
115 metas
= lib
.getListByAuthor(author1
);
116 assertEquals(2, metas
.size());
118 metas
= lib
.getListByAuthor(author2
);
119 assertEquals(1, metas
.size());
121 metas
= lib
.getListByAuthor(null);
122 assertEquals(3, metas
.size());
126 addTest(new TestCase("changeType") {
128 public void test() throws Exception
{
129 List
<MetaData
> metas
= null;
131 lib
.changeSource(luid3
, source1
, null);
133 metas
= lib
.getListBySource(source1
);
134 assertEquals(2, metas
.size());
136 metas
= lib
.getListBySource(source2
);
137 assertEquals(1, metas
.size());
139 metas
= lib
.getListBySource(null);
140 assertEquals(3, metas
.size());
144 addTest(new TestCase("save override luid (change source)") {
146 public void test() throws Exception
{
147 List
<MetaData
> metas
= null;
149 // same luid as a previous one
150 lib
.save(story(luid3
, "My story 3", source2
, author2
),
153 metas
= lib
.getListBySource(source1
);
154 assertEquals(1, metas
.size());
156 metas
= lib
.getListBySource(source2
);
157 assertEquals(2, metas
.size());
159 metas
= lib
.getListBySource(null);
160 assertEquals(3, metas
.size());
168 protected void start() throws Exception
{
169 tmp
= File
.createTempFile(".test-fanfix", ".library");
173 lib
= new LocalLibrary(tmp
, OutputType
.INFO_TEXT
, OutputType
.CBZ
);
177 protected void stop() throws Exception
{
178 IOUtils
.deltree(tmp
);
181 private Story
story(String luid
, String title
, String source
, String author
) {
182 Story story
= new Story();
184 MetaData meta
= new MetaData();
186 meta
.setTitle(title
);
187 meta
.setSource(source
);
188 meta
.setAuthor(author
);
191 Chapter resume
= chapter(0, "Resume");
192 meta
.setResume(resume
);
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
);
200 for (Chapter chap
: story
.getChapters()) {
201 words
+= chap
.getWords();
203 meta
.setWords(words
);
208 private Chapter
chapter(int number
, String name
) {
209 Chapter chapter
= new Chapter(number
, name
);
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));
215 chapter
.setParagraphs(paragraphs
);
218 for (Paragraph para
: chapter
.getParagraphs()) {
219 words
+= para
.getWords();
221 chapter
.setWords(words
);