Search in sources :

Example 11 with MCREditorSession

use of org.mycore.frontend.xeditor.MCREditorSession in project mycore by MyCoRe-Org.

the class MCRXEditorValidatorTest method testMatchesRule.

@Test
public void testMatchesRule() throws JaxenException, JDOMException {
    MCREditorSession session = buildSession("document[isbn]");
    addRule(session, "/document", "xpath", "//isbn", "matches", "^(97(8|9))?\\d{9}(\\d|X)$");
    assertTrue(session.getValidator().isValid());
    assertEquals("false", session.getVariables().get(MCRXEditorValidator.XED_VALIDATION_FAILED));
    checkResult(session, "/document/isbn", MCRValidationResults.MARKER_DEFAULT);
    session = buildSession("document[isbn='9780672317248']");
    addRule(session, "/document", "xpath", "//isbn", "matches", "^(97(8|9))?\\d{9}(\\d|X)$");
    assertTrue(session.getValidator().isValid());
    assertEquals("false", session.getVariables().get(MCRXEditorValidator.XED_VALIDATION_FAILED));
    checkResult(session, "/document/isbn", MCRValidationResults.MARKER_SUCCESS);
    session = buildSession("document[isbn='0-672-31724-9']");
    addRule(session, "/document", "xpath", "//isbn", "matches", "^(97(8|9))?\\d{9}(\\d|X)$");
    assertFalse(session.getValidator().isValid());
    assertEquals("true", session.getVariables().get(MCRXEditorValidator.XED_VALIDATION_FAILED));
    checkResult(session, "/document/isbn", MCRValidationResults.MARKER_ERROR);
}
Also used : MCREditorSession(org.mycore.frontend.xeditor.MCREditorSession) Test(org.junit.Test)

Example 12 with MCREditorSession

use of org.mycore.frontend.xeditor.MCREditorSession in project mycore by MyCoRe-Org.

the class MCRXEditorValidatorTest method buildSession.

private MCREditorSession buildSession(String template) throws JaxenException, JDOMException {
    MCREditorSession session = new MCREditorSession();
    Document editedXML = new Document(new MCRNodeBuilder().buildElement(template, null, null));
    session.setEditedXML(editedXML);
    return session;
}
Also used : MCREditorSession(org.mycore.frontend.xeditor.MCREditorSession) MCRNodeBuilder(org.mycore.common.xml.MCRNodeBuilder) Document(org.jdom2.Document)

Example 13 with MCREditorSession

use of org.mycore.frontend.xeditor.MCREditorSession in project mycore by MyCoRe-Org.

the class MCRXEditorValidatorTest method testExternalMethodRule.

@Test
public void testExternalMethodRule() throws JaxenException, JDOMException {
    MCREditorSession session = buildSession("document[author='Jim'][author[2]='Charles'][author[3]]");
    addRule(session, "/document/author", "class", getClass().getName(), "method", "nameStartsWithJ");
    assertFalse(session.getValidator().isValid());
    assertEquals("true", session.getVariables().get(MCRXEditorValidator.XED_VALIDATION_FAILED));
    checkResult(session, "/document/author[1]", MCRValidationResults.MARKER_SUCCESS);
    checkResult(session, "/document/author[2]", MCRValidationResults.MARKER_ERROR);
    checkResult(session, "/document/author[3]", MCRValidationResults.MARKER_DEFAULT);
    session = buildSession("document[author[1][first='John'][last='Doe']][author[2][first='James'][last='Watt']][author[3]]");
    addRule(session, "/document/author", "class", getClass().getName(), "method", "authorIsJohnDoe");
    assertFalse(session.getValidator().isValid());
    assertEquals("true", session.getVariables().get(MCRXEditorValidator.XED_VALIDATION_FAILED));
    checkResult(session, "/document/author[1]", MCRValidationResults.MARKER_SUCCESS);
    checkResult(session, "/document/author[2]", MCRValidationResults.MARKER_ERROR);
    checkResult(session, "/document/author[3]", MCRValidationResults.MARKER_SUCCESS);
}
Also used : MCREditorSession(org.mycore.frontend.xeditor.MCREditorSession) Test(org.junit.Test)

Aggregations

MCREditorSession (org.mycore.frontend.xeditor.MCREditorSession)13 Test (org.junit.Test)12 Document (org.jdom2.Document)1 MCRNodeBuilder (org.mycore.common.xml.MCRNodeBuilder)1