Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 36fa58633ed5a99de965faf7ef466a9f6e5ca838 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*******************************************************************************
 * Copyright (c) 2011 Torkild U. Resheim.
 * 
 * All rights reserved. This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: Torkild U. Resheim - initial API and implementation
 *******************************************************************************/
package org.eclipse.mylyn.docs.epub.tests.api;

import java.io.File;
import java.io.IOException;

import junit.framework.Assert;

import org.eclipse.emf.common.util.EList;
import org.eclipse.mylyn.docs.epub.core.EPUB;
import org.eclipse.mylyn.docs.epub.core.OPS2Publication;
import org.eclipse.mylyn.docs.epub.core.OPSPublication;
import org.eclipse.mylyn.docs.epub.ocf.Container;
import org.eclipse.mylyn.docs.epub.ocf.RootFile;
import org.eclipse.mylyn.docs.epub.ocf.RootFiles;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * @author Torkild U. Resheim
 * 
 */
public class TestEPUB extends AbstractTest {

	private final File epubFile = new File("test" + File.separator + "test.epub");

	private final File epubFolder = new File("test" + File.separator + "epub");

	/**
	 * @throws java.lang.Exception
	 */
	@Override
	@Before
	public void setUp() throws Exception {
		if (epubFile.exists()) {
			epubFile.delete();
		}
		if (epubFolder.exists()) {
			deleteFolder(epubFolder);
		}
		epubFolder.mkdirs();
	}

	private boolean deleteFolder(File folder) {
		if (folder.isDirectory()) {
			String[] children = folder.list();
			for (int i = 0; i < children.length; i++) {
				boolean ok = deleteFolder(new File(folder, children[i]));
				if (!ok) {
					return false;
				}
			}
		}
		return folder.delete();
	}

	/**
	 * @throws java.lang.Exception
	 */
	@Override
	@After
	public void tearDown() throws Exception {
		if (epubFolder.exists()) {
			deleteFolder(epubFolder);
		}
		if (epubFile.exists()) {
			epubFile.delete();
		}
	}

	/**
	 * Test method for {@link org.eclipse.mylyn.docs.epub.core.EPUB#EPUB()}.
	 * <p>
	 * It must be possible to obtain the publication list which must be empty.
	 * </p>
	 */
	@Test
	public final void testEPUB() {
		EPUB epub = new EPUB();
		Assert.assertEquals(true, epub.getOPSPublications().isEmpty());

	}

	/**
	 * Test method for
	 * {@link org.eclipse.mylyn.docs.epub.core.EPUB#add(java.io.File, java.lang.String)}
	 * .
	 * <ul>
	 * <li>Publication MIME-type shall be correct</li>
	 * <li>Rootfile path shall be correct</li>
	 * <li>Rootfile object shall be correct.</li>
	 * </ul>
	 * 
	 * @throws Exception
	 */
	@Test
	public final void testAddFileString() throws Exception {
		EPUB epub = new EPUB();
		File drawing = new File("testdata/drawing-100x100.svg");
		epub.add(drawing, "image/svg+xml");
		Container container = epub.getContainer();
		RootFiles rootfiles = container.getRootfiles();
		EList<RootFile> files = rootfiles.getRootfiles();
		Assert.assertEquals(true, files.get(0).getFullPath().equals("SVG+XML/drawing-100x100.svg"));
		Assert.assertEquals(true, files.get(0).getMediaType().equals("image/svg+xml"));
		Assert.assertEquals(true, files.get(0).getPublication() == drawing);
	}

	/**
	 * Test method for
	 * {@link org.eclipse.mylyn.docs.epub.core.EPUB#pack(java.io.File)}.
	 * <ul>
	 * <li>Shall throw exception when unknown publication type is added.</li>
	 * </ul>
	 * 
	 * @throws Exception
	 */
	@Test
	public final void testPackFail() throws Exception {
		EPUB epub = new EPUB();
		File drawing = new File("testdata/drawing-100x100.svg");
		epub.add(drawing, "image/svg+xml");
		Container container = epub.getContainer();
		RootFiles rootfiles = container.getRootfiles();
		EList<RootFile> files = rootfiles.getRootfiles();
		files.get(0).setPublication(null);
		try {
			epub.pack(epubFile);
			fail();
		} catch (Exception e) {
		}
	}

	/**
	 * Test method for
	 * {@link org.eclipse.mylyn.docs.epub.core.EPUB#add(org.eclipse.mylyn.docs.epub.core.OPSPublication)}
	 * .
	 * <ul>
	 * <li>Container shall hold more than one OPS publication</li>
	 * <li>OPS structures shall follow naming conventions.</li>
	 * <li>OPS MIME-type shall be correct</li>
	 * <li>Rootfile object shall be correct.</li>
	 * </ul>
	 * 
	 * @throws Exception
	 */
	@Test
	public final void testAddOPSPublication() throws Exception {
		EPUB epub = new EPUB();
		OPSPublication oebps1 = new OPS2Publication();
		OPSPublication oebps2 = new OPS2Publication();
		epub.add(oebps1);
		epub.add(oebps2);
		Container container = epub.getContainer();
		RootFiles rootfiles = container.getRootfiles();
		EList<RootFile> files = rootfiles.getRootfiles();
		Assert.assertEquals(true, files.get(0).getFullPath().equals("OEBPS/content.opf"));
		Assert.assertEquals(true, files.get(1).getFullPath().equals("OEBPS_1/content.opf"));
		Assert.assertEquals(true, files.get(0).getMediaType().equals("application/oebps-package+xml"));
		Assert.assertEquals(true, files.get(1).getMediaType().equals("application/oebps-package+xml"));
		Assert.assertEquals(true, files.get(0).getPublication() == oebps1);
		Assert.assertEquals(true, files.get(1).getPublication() == oebps2);
	}

	/**
	 * Test method for
	 * {@link org.eclipse.mylyn.docs.epub.core.EPUB#getOPSPublications()}.
	 * <p>
	 * One OPS-publication and one SVG drawing are added. Only the
	 * OPS-publication shall be returned.
	 * </p>
	 * 
	 * @throws Exception
	 */
	@Test
	public final void testGetOPSPublications() throws Exception {
		EPUB epub = new EPUB();
		OPSPublication oebps = new OPS2Publication();
		epub.add(oebps);
		File drawing = new File("testdata/drawing-100x100.svg");
		epub.add(drawing, "image/svg+xml");
		Assert.assertEquals(1, epub.getOPSPublications().size());
	}

	/**
	 * Test method for
	 * {@link org.eclipse.mylyn.docs.epub.core.EPUB#pack(java.io.File)}.
	 * <ul>
	 * <li>Temporary folder shall not exist when job is done.</li>
	 * </ul>
	 * 
	 * @throws Exception
	 */
	@Test
	public final void testPackFile() throws Exception {
		EPUB epub = new EPUB();
		OPSPublication oebps = new OPS2Publication();
		oebps.addItem(new File("testdata/plain-page.xhtml"));
		epub.add(oebps);
		File drawing = new File("testdata/drawing-100x100.svg");
		epub.add(drawing, "image/svg+xml");
		File tempFolder = epub.pack(epubFile);
		Assert.assertEquals(false, tempFolder.exists());
	}

	/**
	 * Test method for
	 * {@link org.eclipse.mylyn.docs.epub.core.EPUB#pack(java.io.File, java.io.File)}
	 * .
	 * <ul>
	 * <li>Work folder shall exist when job is done.</li>
	 * <li>Work folder shall contain EPUB artifacts.</li>
	 * <li>Exception shall be thrown if working folder already exist.</li>
	 * </ul>
	 * 
	 * @throws Exception
	 */
	@Test
	public final void testPackFileFile() throws Exception {
		EPUB epub = new EPUB();
		OPSPublication oebps = new OPS2Publication();
		oebps.addItem(new File("testdata/plain-page.xhtml"));
		epub.add(oebps);
		epub.pack(epubFile, epubFolder);
		// Make sure all required files are present
		File metaFolder = new File(epubFolder.getAbsolutePath() + File.separator + "META-INF");
		Assert.assertEquals(true, metaFolder.exists());
		Assert.assertEquals(true, metaFolder.isDirectory());
		File containerFile = new File(epubFolder.getAbsolutePath() + File.separator + "META-INF" + File.separator
				+ "container.xml");
		Assert.assertEquals(true, containerFile.exists());
		Assert.assertEquals(false, containerFile.isDirectory());
		File oebpsFolder = new File(epubFolder.getAbsolutePath() + File.separator + "OEBPS");
		Assert.assertEquals(true, oebpsFolder.exists());
		Assert.assertEquals(true, oebpsFolder.isDirectory());
	}

	/**
	 * Test method for
	 * {@link org.eclipse.mylyn.docs.epub.core.EPUB#pack(java.io.File, java.io.File)}
	 * .
	 * <ul>
	 * <li>Exception shall be thrown if working folder already exist.</li>
	 * </ul>
	 * 
	 * @throws Exception
	 */
	@Test
	public final void testPackFileFileExists() throws Exception {
		File workingFolder = File.createTempFile("epub_", null);
		EPUB epub = new EPUB();
		OPSPublication oebps = new OPS2Publication();
		epub.add(oebps);
		try {
			epub.pack(epubFile, workingFolder);
			fail();
		} catch (Exception e) {
		}
	}

	/**
	 * Test method for
	 * {@link org.eclipse.mylyn.docs.epub.core.EPUB#pack(java.io.File)} .
	 * <ul>
	 * <li>Exception shall be thrown if the EPUB is empty.</li>
	 * </ul>
	 * 
	 * @throws Exception
	 */
	@Test
	public final void testPackMissingPublication() throws Exception {
		EPUB epub = new EPUB();
		try {
			epub.pack(epubFile);
			fail();
		} catch (Exception e) {
		}
	}

	/**
	 * Test method for
	 * {@link org.eclipse.mylyn.docs.epub.core.EPUB#unpack(java.io.File)}.
	 * <ul>
	 * <li>Unpacked EPUB shall have the same contents as the packed one.</li>
	 * </ul>
	 * 
	 * @throws Exception
	 */
	@Test
	public final void testUnpackFile() throws Exception {
		EPUB epub = new EPUB();
		OPSPublication oebps1 = new OPS2Publication();
		OPSPublication oebps2 = new OPS2Publication();
		oebps1.addItem(new File("testdata/plain-page.xhtml"));
		oebps2.addItem(new File("testdata/plain-page.xhtml"));
		epub.add(oebps1);
		epub.add(oebps2);
		epub.pack(epubFile, epubFolder);
		EPUB epub2 = new EPUB();
		epub2.unpack(epubFile);
		Assert.assertEquals(2, epub2.getOPSPublications().size());
	}

	/**
	 * Test method for
	 * {@link org.eclipse.mylyn.docs.epub.core.EPUB#unpack(java.io.File, java.io.File)}
	 * .
	 * 
	 * @throws Exception
	 */
	@Test
	public final void testUnpackFileFile() throws Exception {
		// First pack the EPUB
		EPUB epub = new EPUB();
		OPSPublication oebps = new OPS2Publication();
		oebps.addItem(new File("testdata/plain-page.xhtml"));
		epub.add(oebps);
		epub.pack(epubFile, epubFolder);
		deleteFolder(epubFolder);

		// Then check for some contents when unpacked
		EPUB epub2 = new EPUB();
		epub2.unpack(epubFile, epubFolder);

		// Make sure all required files are present
		File metaFolder = new File(epubFolder.getAbsolutePath() + File.separator + "META-INF");
		Assert.assertEquals(true, metaFolder.exists());
		Assert.assertEquals(true, metaFolder.isDirectory());
		File containerFile = new File(epubFolder.getAbsolutePath() + File.separator + "META-INF" + File.separator
				+ "container.xml");
		Assert.assertEquals(true, containerFile.exists());
		Assert.assertEquals(false, containerFile.isDirectory());
		File oebpsFolder = new File(epubFolder.getAbsolutePath() + File.separator + "OEBPS");
		Assert.assertEquals(true, oebpsFolder.exists());
		Assert.assertEquals(true, oebpsFolder.isDirectory());
	}

	private class EPUB_OCF_Test extends EPUB {
		public void testReadOCF(File workingFolder) throws IOException {
			readOCF(workingFolder);
		}
	}

	/**
	 * See if the OCF file generated by this tooling can be read.
	 * 
	 * @throws Exception
	 */
	@Test
	public final void testReadOCF_Dogfood() throws Exception {
		File workingFolder = new File("testdata/OCF-Tests/Dogfood");
		EPUB_OCF_Test epub = new EPUB_OCF_Test();
		epub.testReadOCF(workingFolder);
	}

	/**
	 * This case was discovered when testing an EPUB file generated by DocBook
	 * Reading the OCF fails with a java.net.SocketException: Unexpected end of
	 * file from server. On closer inspection we can see that the file is
	 * declared as XHTML (which it of course is not). This is probably due to an
	 * issue in DocBook XSL 1.76.1
	 * 
	 * @see http://sourceforge.net/tracker/index.php?func=detail&aid=3353537
	 *      &group_id=21935&atid=373747.
	 * 
	 * @throws Exception
	 */
	@Test
	public final void testReadOCF_SocketException() throws Exception {
		File workingFolder = new File("testdata/OCF-Tests/SocketException");
		EPUB_OCF_Test epub = new EPUB_OCF_Test();
		epub.testReadOCF(workingFolder);
	}
}

Back to the top