Skip to content

Commit

Permalink
use more modern style unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
psiegman committed Aug 9, 2014
1 parent 3d42d50 commit aa090f3
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 33 deletions.
@@ -1,15 +1,16 @@
package nl.siegmann.epublib.browsersupport;

import static org.junit.Assert.assertEquals;

import java.util.HashMap;
import java.util.Map;

import junit.framework.TestCase;


import nl.siegmann.epublib.domain.Book;
import nl.siegmann.epublib.domain.Resource;

public class NavigationHistoryTest extends TestCase {
import org.junit.Test;

public class NavigationHistoryTest {

private static final Resource mockResource = new Resource("mockResource.html");

Expand Down Expand Up @@ -85,7 +86,8 @@ public Resource getMockResource() {
return mockResource;
}
}


@Test
public void test1() {
MockSectionWalker navigator = new MockSectionWalker(new MockBook());
NavigationHistory browserHistory = new NavigationHistory(navigator);
Expand Down Expand Up @@ -135,7 +137,7 @@ public void test1() {
assertEquals(2, browserHistory.getCurrentSize());
}


@Test
public void test2() {
MockSectionWalker navigator = new MockSectionWalker(new MockBook());
NavigationHistory browserHistory = new NavigationHistory(navigator);
Expand Down Expand Up @@ -173,6 +175,7 @@ public void test2() {

}

@Test
public void test3() {
MockSectionWalker navigator = new MockSectionWalker(new MockBook());
NavigationHistory browserHistory = new NavigationHistory(navigator);
Expand Down
@@ -1,37 +1,43 @@
package nl.siegmann.epublib.domain;

import nl.siegmann.epublib.service.MediatypeService;
import junit.framework.TestCase;

public class BookTest extends TestCase {
import org.junit.Assert;
import org.junit.Test;

public class BookTest {

@Test
public void testGetContents1() {
Book book = new Book();
Resource resource1 = new Resource("id1", "Hello, world !".getBytes(), "chapter1.html", MediatypeService.XHTML);
book.getSpine().addResource(resource1);
book.getTableOfContents().addSection(resource1, "My first chapter");
assertEquals(1, book.getContents().size());
Assert.assertEquals(1, book.getContents().size());
}

@Test
public void testGetContents2() {
Book book = new Book();
Resource resource1 = new Resource("id1", "Hello, world !".getBytes(), "chapter1.html", MediatypeService.XHTML);
book.getSpine().addResource(resource1);
Resource resource2 = new Resource("id1", "Hello, world !".getBytes(), "chapter2.html", MediatypeService.XHTML);
book.getTableOfContents().addSection(resource2, "My first chapter");
assertEquals(2, book.getContents().size());
Assert.assertEquals(2, book.getContents().size());
}

@Test
public void testGetContents3() {
Book book = new Book();
Resource resource1 = new Resource("id1", "Hello, world !".getBytes(), "chapter1.html", MediatypeService.XHTML);
book.getSpine().addResource(resource1);
Resource resource2 = new Resource("id1", "Hello, world !".getBytes(), "chapter2.html", MediatypeService.XHTML);
book.getTableOfContents().addSection(resource2, "My first chapter");
book.getGuide().addReference(new GuideReference(resource2, GuideReference.FOREWORD, "The Foreword"));
assertEquals(2, book.getContents().size());
Assert.assertEquals(2, book.getContents().size());
}

@Test
public void testGetContents4() {
Book book = new Book();

Expand All @@ -44,6 +50,6 @@ public void testGetContents4() {
Resource resource3 = new Resource("id1", "Hello, world !".getBytes(), "foreword.html", MediatypeService.XHTML);
book.getGuide().addReference(new GuideReference(resource3, GuideReference.FOREWORD, "The Foreword"));

assertEquals(3, book.getContents().size());
Assert.assertEquals(3, book.getContents().size());
}
}
@@ -1,28 +1,32 @@
package nl.siegmann.epublib.domain;

import junit.framework.TestCase;
import nl.siegmann.epublib.service.MediatypeService;

public class ResourcesTest extends TestCase {
import org.junit.Assert;
import org.junit.Test;

public class ResourcesTest {

@Test
public void testGetResourcesByMediaType1() {
Resources resources = new Resources();
resources.add(new Resource("foo".getBytes(), MediatypeService.XHTML));
resources.add(new Resource("bar".getBytes(), MediatypeService.XHTML));
assertEquals(0, resources.getResourcesByMediaType(MediatypeService.PNG).size());
assertEquals(2, resources.getResourcesByMediaType(MediatypeService.XHTML).size());
assertEquals(2, resources.getResourcesByMediaTypes(new MediaType[] {MediatypeService.XHTML}).size());
Assert.assertEquals(0, resources.getResourcesByMediaType(MediatypeService.PNG).size());
Assert.assertEquals(2, resources.getResourcesByMediaType(MediatypeService.XHTML).size());
Assert.assertEquals(2, resources.getResourcesByMediaTypes(new MediaType[] {MediatypeService.XHTML}).size());
}

@Test
public void testGetResourcesByMediaType2() {
Resources resources = new Resources();
resources.add(new Resource("foo".getBytes(), MediatypeService.XHTML));
resources.add(new Resource("bar".getBytes(), MediatypeService.PNG));
resources.add(new Resource("baz".getBytes(), MediatypeService.PNG));
assertEquals(2, resources.getResourcesByMediaType(MediatypeService.PNG).size());
assertEquals(1, resources.getResourcesByMediaType(MediatypeService.XHTML).size());
assertEquals(1, resources.getResourcesByMediaTypes(new MediaType[] {MediatypeService.XHTML}).size());
assertEquals(3, resources.getResourcesByMediaTypes(new MediaType[] {MediatypeService.XHTML, MediatypeService.PNG}).size());
assertEquals(3, resources.getResourcesByMediaTypes(new MediaType[] {MediatypeService.CSS, MediatypeService.XHTML, MediatypeService.PNG}).size());
Assert.assertEquals(2, resources.getResourcesByMediaType(MediatypeService.PNG).size());
Assert.assertEquals(1, resources.getResourcesByMediaType(MediatypeService.XHTML).size());
Assert.assertEquals(1, resources.getResourcesByMediaTypes(new MediaType[] {MediatypeService.XHTML}).size());
Assert.assertEquals(3, resources.getResourcesByMediaTypes(new MediaType[] {MediatypeService.XHTML, MediatypeService.PNG}).size());
Assert.assertEquals(3, resources.getResourcesByMediaTypes(new MediaType[] {MediatypeService.CSS, MediatypeService.XHTML, MediatypeService.PNG}).size());
}
}
@@ -1,20 +1,26 @@
package nl.siegmann.epublib.domain;

import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class TableOfContentsTest extends TestCase {
import org.junit.Test;

public class TableOfContentsTest{

@Test
public void testCalculateDepth_simple1() {
TableOfContents tableOfContents = new TableOfContents();
assertEquals(0, tableOfContents.calculateDepth());
}

@Test
public void testCalculateDepth_simple2() {
TableOfContents tableOfContents = new TableOfContents();
tableOfContents.addTOCReference(new TOCReference());
assertEquals(1, tableOfContents.calculateDepth());
}

@Test
public void testCalculateDepth_simple3() {
TableOfContents tableOfContents = new TableOfContents();
tableOfContents.addTOCReference(new TOCReference());
Expand All @@ -25,6 +31,7 @@ public void testCalculateDepth_simple3() {
assertEquals(2, tableOfContents.calculateDepth());
}

@Test
public void testAddResource1() {
Resource resource = new Resource("foo");
TableOfContents toc = new TableOfContents();
Expand All @@ -35,6 +42,7 @@ public void testAddResource1() {
assertEquals("pear", tocReference.getTitle());
}

@Test
public void testAddResource2() {
Resource resource = new Resource("foo");
TableOfContents toc = new TableOfContents();
Expand All @@ -57,6 +65,7 @@ public void testAddResource2() {
assertEquals("apple", tocReference3.getTitle());
}

@Test
public void testAddResource3() {
Resource resource = new Resource("foo");
TableOfContents toc = new TableOfContents();
Expand All @@ -69,6 +78,7 @@ public void testAddResource3() {
assertEquals("pear", tocReference.getTitle());
}

@Test
public void testAddResourceWithIndexes() {
Resource resource = new Resource("foo");
TableOfContents toc = new TableOfContents();
Expand Down
@@ -1,5 +1,9 @@
package nl.siegmann.epublib.epub;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand Down
@@ -1,5 +1,10 @@
package nl.siegmann.epublib.epub;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
Expand Down
@@ -1,19 +1,23 @@
package nl.siegmann.epublib.epub;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.io.StringReader;

import junit.framework.TestCase;
import nl.siegmann.epublib.domain.Identifier;
import nl.siegmann.epublib.domain.Metadata;

import org.junit.Assert;
import org.junit.Test;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class PackageDocumentMetadataReaderTest extends TestCase {
public class PackageDocumentMetadataReaderTest {

@Test
public void test1() {
try {
Document document = EpubProcessorSupport.createDocumentBuilder().parse(PackageDocumentMetadataReader.class.getResourceAsStream("/opf/test2.opf"));
Expand All @@ -25,11 +29,13 @@ public void test1() {
}
}

@Test
public void testReadsLanguage() {
Metadata metadata = getMetadata("/opf/test_language.opf");
assertEquals("fi", metadata.getLanguage());
}

@Test
public void testDefaultsToEnglish() {
Metadata metadata = getMetadata("/opf/test_default_language.opf");
assertEquals("en", metadata.getLanguage());
Expand All @@ -48,6 +54,7 @@ private Metadata getMetadata(String file) {
}
}

@Test
public void test2() throws SAXException, IOException {
// given
String input = "<package version=\"2.0\" xmlns=\"http://www.idpf.org/2007/opf\" unique-identifier=\"BookId\">"
Expand Down
@@ -1,13 +1,16 @@
package nl.siegmann.epublib.epub;

import java.util.Collection;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import junit.framework.TestCase;
import java.util.Collection;

import org.junit.Test;
import org.w3c.dom.Document;

public class PackageDocumentReaderTest extends TestCase {
public class PackageDocumentReaderTest {

@Test
public void testFindCoverHref_content1() {
EpubReader epubReader = new EpubReader();
Document packageDocument;
Expand Down
@@ -1,15 +1,19 @@
package nl.siegmann.epublib.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;

import junit.framework.TestCase;
import org.junit.Test;

public class IOUtilTest extends TestCase {
public class IOUtilTest {

@Test
public void testToByteArray1() {
byte[] testArray = new byte[Byte.MAX_VALUE - Byte.MIN_VALUE];
for (int i = Byte.MIN_VALUE; i < Byte.MAX_VALUE; i++) {
Expand All @@ -24,6 +28,7 @@ public void testToByteArray1() {
}
}

@Test
public void testToByteArray2() {
byte[] testArray = new byte[IOUtil.IO_COPY_BUFFER_SIZE + 1];
Random random = new Random();
Expand All @@ -37,6 +42,7 @@ public void testToByteArray2() {
}
}

@Test
public void testCopyInputStream1() {
byte[] testArray = new byte[(IOUtil.IO_COPY_BUFFER_SIZE * 3) + 10];
Random random = new Random();
Expand All @@ -52,6 +58,7 @@ public void testCopyInputStream1() {
}
}

@Test
public void testCalcNrRead() {
Integer[] testData = new Integer[] {
// nrRead, totalNrRead, reault
Expand Down

0 comments on commit aa090f3

Please sign in to comment.