Skip to content

Commit

Permalink
Merge pull request #7 from lyncodev/master
Browse files Browse the repository at this point in the history
Adding some test coverage
  • Loading branch information
jmelo-lyncode authored Sep 10, 2017
2 parents ec45d56 + f55425a commit 2f5b535
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 21 deletions.
2 changes: 1 addition & 1 deletion gradle/jacoco.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jacocoTestReport {
reports {
xml.enabled true
xml.destination "${buildDir}/reports/jacoco/report.xml"
html.enabled false
html.enabled true
csv.enabled false
}
}
11 changes: 0 additions & 11 deletions src/main/java/org/jtwig/xliff/exceptions/XliffException.java

This file was deleted.

4 changes: 0 additions & 4 deletions src/main/java/org/jtwig/xliff/parser/xml/XmlReaderEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ public XmlReaderEvent(XmlStartElementFactory xmlStartElementFactory, XMLEvent ev
this.event = event;
}

public boolean isStartElement() {
return event.isStartElement();
}

public XmlStartElement asStartElement() {
return xmlStartElementFactory.create(event);
}
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/org/jtwig/xliff/parser/xml/XmlStartElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ public XmlStartElement(XmlAttributeFactory xmlAttributeFactory, StartElement eve
this.event = event;
}

public boolean hasName(String name) {
return this.event.getName() != null && name.equals(this.event.getName().getLocalPart());
}

public Optional<XmlAttribute> getAttribute(String name) {
Iterator<Attribute> attributeIterator = this.event.getAttributes();
while (attributeIterator.hasNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public XmlStartElementFactory(XmlAttributeFactory xmlAttributeFactory) {
}

public XmlStartElement create (XMLEvent event) {
assert event.isStartElement();
return new XmlStartElement(xmlAttributeFactory, event.asStartElement());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.jtwig.xliff.i18n;

import org.jtwig.environment.EnvironmentConfigurationBuilder;
import org.jtwig.environment.EnvironmentFactory;
import org.jtwig.translate.message.source.MessageSource;
import org.jtwig.translate.message.source.factory.MessageSourceFactory;
import org.junit.Test;

import static org.hamcrest.core.IsNull.notNullValue;
import static org.junit.Assert.assertThat;

public class XliffMessageSourceFactoryBuilderTest {
@Test
public void builder() throws Exception {
MessageSourceFactory factory = new XliffMessageSourceFactoryBuilder(XmlFileFilter.xmlFileFilter())
.withLookupClasspath("test")
.withLookupClasspathRecursively("test")
.withLookupDirectory("test")
.withLookupDirectoryRecursively("test")
.build();

MessageSource result = factory.create(new EnvironmentFactory().create(EnvironmentConfigurationBuilder.configuration().build()));

assertThat(result, notNullValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.jtwig.xliff.parser.xml.XmlReader;
import org.jtwig.xliff.parser.xml.XmlReaderEvent;
import org.jtwig.xliff.parser.xml.XmlStartElement;
import org.jtwig.xliff.parser.xml.exceptions.XmlReaderException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
Expand Down Expand Up @@ -34,4 +35,15 @@ public void parseNoTargetAttribute() throws Exception {

underTest.parse(xmlReader);
}

@Test
public void innerException() throws Exception {
XmlReader xmlReader = mock(XmlReader.class);

when(xmlReader.currentEvent()).thenThrow(new XmlReaderException("test"));

expectedException.expect(XliffParsingException.class);

underTest.parse(xmlReader);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import org.jtwig.xliff.model.TranslationUnit;
import org.jtwig.xliff.parser.xml.XmlReader;
import org.jtwig.xliff.parser.xml.XmlStartElement;
import org.jtwig.xliff.parser.xml.exceptions.XmlReaderException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -54,4 +56,15 @@ public void parseBothExist() throws Exception {
assertThat(result.getSource(), is("one"));
assertThat(result.getTarget(), is("two"));
}

@Test
public void parseExtension() throws Exception {
XmlReader reader = mock(XmlReader.class);

when(reader.nextStartElement(anyString())).thenThrow(new XmlReaderException("test"));

expectedException.expect(XliffParsingException.class);

underTest.parse(reader);
}
}
40 changes: 40 additions & 0 deletions src/test/java/org/jtwig/xliff/parser/XliffParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.jtwig.xliff.parser;

import com.google.common.base.Optional;
import org.jtwig.xliff.exceptions.XliffParsingException;
import org.jtwig.xliff.parser.xml.XmlReader;
import org.jtwig.xliff.parser.xml.XmlReaderFactory;
import org.jtwig.xliff.parser.xml.XmlStartElement;
import org.jtwig.xliff.parser.xml.exceptions.XmlReaderException;
import org.junit.Test;

import java.io.ByteArrayInputStream;

import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;

public class XliffParserTest {
private final XmlReaderFactory xmlReaderFactory = mock(XmlReaderFactory.class);
private final TranslationFileParser translationFileParser = mock(TranslationFileParser.class);
private XliffParser underTest = new XliffParser(xmlReaderFactory, translationFileParser);

@Test(expected = XliffParsingException.class)
public void readerException() throws Exception {
ByteArrayInputStream inputStream = new ByteArrayInputStream("test".getBytes());

given(xmlReaderFactory.create(inputStream)).willThrow(new XmlReaderException("test"));

underTest.parse(inputStream);
}

@Test(expected = XliffParsingException.class)
public void noStart() throws Exception {
ByteArrayInputStream inputStream = new ByteArrayInputStream("test".getBytes());
XmlReader reader = mock(XmlReader.class);

given(xmlReaderFactory.create(inputStream)).willReturn(reader);
given(reader.nextStartElement("file")).willReturn(Optional.<XmlStartElement>absent());

underTest.parse(inputStream);
}
}
43 changes: 43 additions & 0 deletions src/test/java/org/jtwig/xliff/parser/xml/XmlReaderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.jtwig.xliff.parser.xml;

import org.jtwig.xliff.parser.xml.exceptions.XmlReaderException;
import org.junit.Test;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLStreamException;

import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;

public class XmlReaderTest {
private final XmlStartElementFactory xmlStartElementFactory = mock(XmlStartElementFactory.class);
private final XmlReaderEventFactory xmlReaderEventFactory = mock(XmlReaderEventFactory.class);
private final XMLEventReader xmlEventReader = mock(XMLEventReader.class);

private XmlReader underTest = new XmlReader(
xmlStartElementFactory,
xmlReaderEventFactory,
xmlEventReader
);

@Test(expected = XmlReaderException.class)
public void next() throws Exception {
given(xmlEventReader.hasNext()).willReturn(true);
given(xmlEventReader.nextEvent()).willThrow(new XMLStreamException());

underTest.nextStartElement("test");
}

@Test(expected = XmlReaderException.class)
public void text() throws Exception {
given(xmlEventReader.hasNext()).willReturn(true);
given(xmlEventReader.nextEvent()).willThrow(new XMLStreamException());

underTest.textInsideCurrentElement();
}

@Test(expected = XmlReaderException.class)
public void current() throws Exception {
XmlReaderEvent result = underTest.currentEvent();
}
}
39 changes: 39 additions & 0 deletions src/test/java/org/jtwig/xliff/parser/xml/XmlStartElementTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.jtwig.xliff.parser.xml;

import com.google.common.base.Optional;
import org.junit.Test;

import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.StartElement;

import static java.util.Arrays.asList;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;

public class XmlStartElementTest {
XmlAttributeFactory xmlAttributeFactory = mock(XmlAttributeFactory.class);
StartElement startElement = mock(StartElement.class);

XmlStartElement underTest = new XmlStartElement(xmlAttributeFactory, startElement);

@Test
public void xmlStartNoAttributes() throws Exception {
given(startElement.getAttributes()).willReturn(asList().iterator());

Optional<XmlAttribute> result = underTest.getAttribute("test");

assertThat(result.isPresent(), is(false));
}
@Test
public void xmlStartNullNameAttribute() throws Exception {
Attribute attribute = mock(Attribute.class);
given(startElement.getAttributes()).willReturn(asList(attribute).iterator());
given(attribute.getName()).willReturn(null);

Optional<XmlAttribute> result = underTest.getAttribute("test");

assertThat(result.isPresent(), is(false));
}
}

0 comments on commit 2f5b535

Please sign in to comment.