Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jferard committed Apr 23, 2022
1 parent b476173 commit 8e46b79
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,9 @@ public void saveAs(final Path path) throws IOException {
out.flush();
out.close();
}
} catch (final FileNotFoundException e) {
} catch (final IOException e) {
this.logger.log(Level.SEVERE, "Can't open " + path, e);
throw new IOException(e);
} catch (final NullPointerException e) {
this.logger.log(Level.SEVERE, "No file", e);
throw new IOException(e);
throw e;
}
}

Expand Down Expand Up @@ -192,7 +189,7 @@ public void saveAs(final File file, final ZipUTF8WriterBuilder builder) throws I
this.saveAs(file.toPath(), builder);
}

private void saveAs(final Path path, final ZipUTF8WriterBuilder builder) throws IOException {
public void saveAs(final Path path, final ZipUTF8WriterBuilder builder) throws IOException {
try {
final OutputStream out = Files.newOutputStream(path);
final ZipUTF8Writer writer = builder.build(out);
Expand All @@ -202,9 +199,9 @@ private void saveAs(final Path path, final ZipUTF8WriterBuilder builder) throws
writer.finish();
writer.close();
}
} catch (final FileNotFoundException e) {
} catch (final IOException e) {
this.logger.log(Level.SEVERE, "Can't open " + path, e);
throw new IOException(e);
throw e;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@

import com.github.jferard.fastods.odselement.OdsElements;
import com.github.jferard.fastods.odselement.StandardOdsEntry;
import com.github.jferard.fastods.style.MasterPageStyle;
import com.github.jferard.fastods.style.PageLayoutStyle;
import com.github.jferard.fastods.style.TableCellStyle;
import com.github.jferard.fastods.style.TableColumnStyle;
import com.github.jferard.fastods.style.TableRowStyle;
import com.github.jferard.fastods.style.TableStyle;
import com.github.jferard.fastods.util.XMLUtil;
import com.github.jferard.fastods.util.ZipUTF8Writer;
import com.github.jferard.fastods.util.ZipUTF8WriterBuilderImpl;
Expand All @@ -35,7 +41,10 @@
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand All @@ -47,6 +56,11 @@
import java.nio.channels.Channels;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.spi.FileSystemProvider;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
Expand Down Expand Up @@ -351,6 +365,19 @@ public final void testSaveAsNullFile() {
PowerMock.verifyAll();
}

@Test
public final void testSaveAsNullFileBuilder() {
PowerMock.resetAll();
TestHelper.initMockDocument(this.odsElements);

PowerMock.replayAll();
final AnonymousOdsDocument document = this.getAnonymousDocument();
Assert.assertThrows(IOException.class,
() -> new AnonymousOdsFileWriter(this.logger, document).saveAs((File) null, this.builder));

PowerMock.verifyAll();
}

@Test
public final void testSaveWithWriter() throws IOException {
final OutputStream outputStream = PowerMock.createMock(OutputStream.class);
Expand Down Expand Up @@ -385,6 +412,142 @@ public final void testSaveWithWriter() throws IOException {
PowerMock.verifyAll();
}

@Test
public final void testSaveAsPath() throws IOException {
final OutputStream outputStream = PowerMock.createMock(OutputStream.class);
final Path path = PowerMock.createMock(Path.class);
final FileSystem fs = PowerMock.createMock(FileSystem.class);
final FileSystemProvider p = PowerMock.createMock(FileSystemProvider.class);

PowerMock.resetAll();
TestHelper.initMockDocument(this.odsElements);
EasyMock.expect(path.getFileSystem()).andReturn(fs);
EasyMock.expect(fs.provider()).andReturn(p);
EasyMock.expect(p.newOutputStream(path)).andReturn(outputStream);
outputStream.flush();
outputStream.close();
this.odsElements
.createEmptyElements(EasyMock.eq(this.xmlUtil), EasyMock.isA(ZipUTF8Writer.class));
this.odsElements
.writeMimeType(EasyMock.eq(this.xmlUtil), EasyMock.isA(ZipUTF8Writer.class));
this.odsElements.writeMeta(EasyMock.eq(this.xmlUtil), EasyMock.isA(ZipUTF8Writer.class));
this.odsElements.writeStyles(EasyMock.eq(this.xmlUtil), EasyMock.isA(ZipUTF8Writer.class));
this.odsElements.writeContent(EasyMock.eq(this.xmlUtil), EasyMock.isA(ZipUTF8Writer.class));
this.odsElements
.writeSettings(EasyMock.eq(this.xmlUtil), EasyMock.isA(ZipUTF8Writer.class));
this.odsElements.writeExtras(EasyMock.eq(this.xmlUtil), EasyMock.isA(ZipUTF8Writer.class));
outputStream.write(EasyMock.isA(byte[].class), EasyMock.anyInt(), EasyMock.anyInt());
EasyMock.expectLastCall().anyTimes();
outputStream.flush();
EasyMock.expectLastCall().anyTimes();

PowerMock.replayAll();
final AnonymousOdsDocument document = this.getAnonymousDocument();
new AnonymousOdsFileWriter(this.logger, document).saveAs(path);

PowerMock.verifyAll();
}

@Test
public final void testSaveAsFile() throws IOException {
final OutputStream outputStream = PowerMock.createMock(OutputStream.class);
final File file = PowerMock.createMock(File.class);
final Path path = PowerMock.createMock(Path.class);
final FileSystem fs = PowerMock.createMock(FileSystem.class);
final FileSystemProvider p = PowerMock.createMock(FileSystemProvider.class);

PowerMock.resetAll();
TestHelper.initMockDocument(this.odsElements);
EasyMock.expect(file.toPath()).andReturn(path);
EasyMock.expect(path.getFileSystem()).andReturn(fs);
EasyMock.expect(fs.provider()).andReturn(p);
EasyMock.expect(p.newOutputStream(path)).andReturn(outputStream);
outputStream.flush();
outputStream.close();
this.odsElements
.createEmptyElements(EasyMock.eq(this.xmlUtil), EasyMock.isA(ZipUTF8Writer.class));
this.odsElements
.writeMimeType(EasyMock.eq(this.xmlUtil), EasyMock.isA(ZipUTF8Writer.class));
this.odsElements.writeMeta(EasyMock.eq(this.xmlUtil), EasyMock.isA(ZipUTF8Writer.class));
this.odsElements.writeStyles(EasyMock.eq(this.xmlUtil), EasyMock.isA(ZipUTF8Writer.class));
this.odsElements.writeContent(EasyMock.eq(this.xmlUtil), EasyMock.isA(ZipUTF8Writer.class));
this.odsElements
.writeSettings(EasyMock.eq(this.xmlUtil), EasyMock.isA(ZipUTF8Writer.class));
this.odsElements.writeExtras(EasyMock.eq(this.xmlUtil), EasyMock.isA(ZipUTF8Writer.class));
outputStream.write(EasyMock.isA(byte[].class), EasyMock.anyInt(), EasyMock.anyInt());
EasyMock.expectLastCall().anyTimes();
outputStream.flush();
EasyMock.expectLastCall().anyTimes();

PowerMock.replayAll();
final AnonymousOdsDocument document = this.getAnonymousDocument();
new AnonymousOdsFileWriter(this.logger, document).saveAs(file);

PowerMock.verifyAll();
}

@Test
public final void testSaveAsPathException() throws IOException {
final Path path = PowerMock.createMock(Path.class);
final FileSystem fs = PowerMock.createMock(FileSystem.class);
final FileSystemProvider p = PowerMock.createMock(FileSystemProvider.class);

PowerMock.resetAll();
TestHelper.initMockDocument(this.odsElements);
EasyMock.expect(path.getFileSystem()).andReturn(fs);
EasyMock.expect(fs.provider()).andReturn(p);
EasyMock.expect(p.newOutputStream(path)).andThrow(new NoSuchFileException("e"));

PowerMock.replayAll();
final AnonymousOdsDocument document = this.getAnonymousDocument();
final AnonymousOdsFileWriter writer = new AnonymousOdsFileWriter(this.logger, document);
Assert.assertThrows(NoSuchFileException.class, () -> {
writer.saveAs(path);
});

PowerMock.verifyAll();
}

@Test
public final void testSaveAsPathBuilderException() throws IOException {
final Path path = PowerMock.createMock(Path.class);
final FileSystem fs = PowerMock.createMock(FileSystem.class);
final FileSystemProvider p = PowerMock.createMock(FileSystemProvider.class);

PowerMock.resetAll();
TestHelper.initMockDocument(this.odsElements);
EasyMock.expect(path.getFileSystem()).andReturn(fs);
EasyMock.expect(fs.provider()).andReturn(p);
EasyMock.expect(p.newOutputStream(path)).andThrow(new NoSuchFileException("e"));

PowerMock.replayAll();
final AnonymousOdsDocument document = this.getAnonymousDocument();
final AnonymousOdsFileWriter writer = new AnonymousOdsFileWriter(this.logger, document);
Assert.assertThrows(NoSuchFileException.class, () -> {
writer.saveAs(path, this.builder);
});

PowerMock.verifyAll();
}

@Test
public final void testDocument() throws IOException {
PowerMock.resetAll();
EasyMock.expect(this.odsElements.addContentStyle(EasyMock.isA(TableStyle.class))).andReturn(true);
EasyMock.expect(this.odsElements.addContentStyle(EasyMock.isA(TableRowStyle.class))).andReturn(true);
EasyMock.expect(this.odsElements.addContentStyle(EasyMock.isA(TableColumnStyle.class))).andReturn(true);
EasyMock.expect(this.odsElements.addContentStyle(EasyMock.isA(TableCellStyle.class))).andReturn(true);
EasyMock.expect(this.odsElements.addMasterPageStyle(EasyMock.isA(MasterPageStyle.class))).andReturn(true).times(2);
EasyMock.expect(this.odsElements.addPageLayoutStyle(EasyMock.isA(PageLayoutStyle.class))).andReturn(true).times(2);

PowerMock.replayAll();
final AnonymousOdsDocument document = this.getAnonymousDocument();
final AnonymousOdsFileWriter writer = new AnonymousOdsFileWriter(this.logger, document);

PowerMock.verifyAll();
Assert.assertEquals(document, writer.document());
}

@Test
public final void testSaveWithWriterDir() {
PowerMock.resetAll();
Expand Down

0 comments on commit 8e46b79

Please sign in to comment.