mirror of
https://github.com/itplr-kosit/validator.git
synced 2026-05-25 16:55:39 +00:00
some more tests
This commit is contained in:
parent
46ddea57d6
commit
2c479eded5
8 changed files with 197 additions and 30 deletions
|
|
@ -82,6 +82,7 @@ public class Helper {
|
|||
return new ContentRepository(strategy, Simple.REPOSITORY_URI);
|
||||
}
|
||||
|
||||
|
||||
public static URI getSchemaLocation() {
|
||||
return ROOT.resolve("repository/simple.xsd");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,86 @@
|
|||
package de.kosit.validationtool.impl.tasks;
|
||||
|
||||
import static de.kosit.validationtool.config.TestScenarioFactory.createScenario;
|
||||
import static de.kosit.validationtool.impl.Helper.serialize;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import javax.xml.transform.Source;
|
||||
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import de.kosit.validationtool.api.InputFactory;
|
||||
import de.kosit.validationtool.impl.ContentRepository;
|
||||
import de.kosit.validationtool.impl.ConversionService;
|
||||
import de.kosit.validationtool.impl.Helper.Simple;
|
||||
import de.kosit.validationtool.impl.Scenario;
|
||||
import de.kosit.validationtool.impl.model.Result;
|
||||
import de.kosit.validationtool.impl.tasks.CheckAction.Bag;
|
||||
|
||||
import net.sf.saxon.s9api.DocumentBuilder;
|
||||
import net.sf.saxon.s9api.Processor;
|
||||
import net.sf.saxon.s9api.SaxonApiException;
|
||||
|
||||
/**
|
||||
* Test for {@link CreateReportAction}.
|
||||
*
|
||||
* @author Andreas Penski
|
||||
*/
|
||||
public class CreateReportActionTest {
|
||||
|
||||
private CreateReportAction action;
|
||||
|
||||
private ContentRepository repository;
|
||||
|
||||
@Rule
|
||||
public ExpectedException expectedException = ExpectedException.none();
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.repository = Simple.createContentRepository();
|
||||
this.action = new CreateReportAction(this.repository.getProcessor(), new ConversionService(), this.repository.getResolver());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleCreate() {
|
||||
final Bag bag = TestBagBuilder.createBag(true, true);
|
||||
final Scenario scenario = createScenario().build(this.repository).getObject();
|
||||
bag.setScenarioSelectionResult(new Result<>(scenario));
|
||||
bag.setReport(null);
|
||||
this.action.check(bag);
|
||||
assertThat(bag.getReport()).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoValidParseResult() {
|
||||
// e.g. no valid xml file specified
|
||||
final Bag bag = TestBagBuilder.createBag(InputFactory.read("someBytes".getBytes(), "invalid"), true);
|
||||
final Scenario scenario = createScenario().build(this.repository).getObject();
|
||||
bag.setScenarioSelectionResult(new Result<>(scenario));
|
||||
assertThat(bag.getReport()).isNull();
|
||||
this.action.check(bag);
|
||||
assertThat(bag.getReport()).isNotNull();
|
||||
final String reportString = serialize(bag.getReport());
|
||||
assertThat(reportString).contains("SAXParseException");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecutionException() throws SaxonApiException {
|
||||
this.expectedException.expect(IllegalStateException.class);
|
||||
this.expectedException.expectMessage(Matchers.containsString("Can not create final report"));
|
||||
final Processor p = mock(Processor.class);
|
||||
final DocumentBuilder documentBuilder = mock(DocumentBuilder.class);
|
||||
this.action = new CreateReportAction(p, new ConversionService(), null);
|
||||
|
||||
when(p.newDocumentBuilder()).thenReturn(documentBuilder);
|
||||
when(documentBuilder.build(any(Source.class))).thenThrow(new SaxonApiException("mocked"));
|
||||
this.action.check(TestBagBuilder.createBag(InputFactory.read(Simple.SIMPLE_VALID), true));
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -17,7 +17,7 @@
|
|||
* under the License.
|
||||
*/
|
||||
|
||||
package de.kosit.validationtool.impl;
|
||||
package de.kosit.validationtool.impl.tasks;
|
||||
|
||||
import static de.kosit.validationtool.api.InputFactory.read;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
|
@ -27,9 +27,9 @@ import org.junit.Rule;
|
|||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import de.kosit.validationtool.impl.Helper;
|
||||
import de.kosit.validationtool.impl.Helper.Simple;
|
||||
import de.kosit.validationtool.impl.model.Result;
|
||||
import de.kosit.validationtool.impl.tasks.DocumentParseAction;
|
||||
import de.kosit.validationtool.model.reportInput.XMLSyntaxError;
|
||||
|
||||
import net.sf.saxon.s9api.XdmNode;
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
package de.kosit.validationtool.impl.xml;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import javax.xml.XMLConstants;
|
||||
import javax.xml.validation.SchemaFactory;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.xml.sax.SAXNotRecognizedException;
|
||||
import org.xml.sax.SAXNotSupportedException;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
*
|
||||
* Tests the internal functions used to create a secure resolver
|
||||
*
|
||||
* @author Andreas Penski
|
||||
*/
|
||||
public class BaseResolverTest {
|
||||
|
||||
@RequiredArgsConstructor
|
||||
private class TestResolvingStrategy extends StrictRelativeResolvingStrategy {
|
||||
|
||||
void setInternalProperty(final SchemaFactory factory, final boolean lenient) {
|
||||
allowExternalSchema(factory, lenient, "quatsch");
|
||||
}
|
||||
}
|
||||
|
||||
@Rule
|
||||
public ExpectedException expectedException = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public void testIgnoreUnsupportedProperty() throws SAXNotRecognizedException, SAXNotSupportedException {
|
||||
final SchemaFactory sf = mock(SchemaFactory.class);
|
||||
final TestResolvingStrategy s = new TestResolvingStrategy();
|
||||
doThrow(new SAXNotRecognizedException("not supported")).when(sf).setProperty(any(), any());
|
||||
s.setInternalProperty(sf, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFailOnUnsupportedProperty() throws SAXNotRecognizedException, SAXNotSupportedException {
|
||||
this.expectedException.expect(IllegalStateException.class);
|
||||
final SchemaFactory sf = mock(SchemaFactory.class);
|
||||
final TestResolvingStrategy s = new TestResolvingStrategy();
|
||||
doThrow(new SAXNotRecognizedException("not supported")).when(sf).setProperty(any(), any());
|
||||
s.setInternalProperty(sf, false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleSuccess() throws SAXNotRecognizedException, SAXNotSupportedException {
|
||||
final SchemaFactory sf = mock(SchemaFactory.class);
|
||||
final TestResolvingStrategy s = new TestResolvingStrategy();
|
||||
s.setInternalProperty(sf, true);
|
||||
s.setInternalProperty(sf, false);
|
||||
verify(sf, times(2)).setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "quatsch");
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue