Merged HEAD-QA to HEAD (4.2) (including moving test classes into separate folders)

51903 to 54309 


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@54310 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Samuel Langlois
2013-08-20 17:17:31 +00:00
parent 5a8f6ee635
commit e60d57ea42
70 changed files with 7094 additions and 1988 deletions

View File

@@ -1,295 +0,0 @@
/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.web.forms;
import java.io.Serializable;
import java.util.Calendar;
import java.util.HashMap;
import org.alfresco.model.WCMAppModel;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.security.authentication.MutableAuthenticationDao;
import org.alfresco.service.cmr.model.FileFolderService;
import org.alfresco.service.cmr.model.FileInfo;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.security.MutableAuthenticationService;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.XMLUtil;
import org.alfresco.util.BaseSpringTest;
import org.alfresco.util.TestWithUserUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shale.test.mock.MockExternalContext;
import org.apache.shale.test.mock.MockFacesContext;
import org.apache.shale.test.mock.MockHttpServletRequest;
import org.apache.shale.test.mock.MockHttpServletResponse;
import org.apache.shale.test.mock.MockServletContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.StaticWebApplicationContext;
import org.w3c.dom.Document;
/**
* JUnit tests to exercise parts of the forms codebase
*
* @author ariel backenroth
*/
public class FormsTest
extends BaseSpringTest
{
/////////////////////////////////////////////////////////////////////////////
private static class MockForm
extends FormImpl
{
MockForm(final NodeRef folderNodeRef,
final FormsService formsService)
{
super(folderNodeRef, formsService);
}
public void setOutputPathPattern(final String opp)
{
final NodeService nodeService = this.getServiceRegistry().getNodeService();
nodeService.setProperty(this.getNodeRef(), WCMAppModel.PROP_OUTPUT_PATH_PATTERN, opp);
}
}
/////////////////////////////////////////////////////////////////////////////
private final static Log LOGGER = LogFactory.getLog(FormsTest.class);
private final static String WEB_CLIENT_APPLICATION_CONTEXT =
"classpath:alfresco/web-client-application-context.xml";
private NodeService nodeService;
private FormsService formsService;
private MockForm mockForm;
protected void onSetUpInTransaction()
throws Exception
{
System.err.println("onSetUpInTransaction");
super.onSetUpInTransaction();
this.nodeService = (NodeService)super.applicationContext.getBean("dbNodeService");
assertNotNull(this.nodeService);
final FileFolderService fileFolderService = (FileFolderService)
super.applicationContext.getBean("fileFolderService");
assertNotNull(fileFolderService);
this.formsService = (FormsService)super.applicationContext.getBean("FormsService");
assertNotNull(this.formsService);
final MutableAuthenticationService authenticationService = (MutableAuthenticationService)
applicationContext.getBean("authenticationService");
authenticationService.clearCurrentSecurityContext();
final MutableAuthenticationDao authenticationDAO = (MutableAuthenticationDao)
applicationContext.getBean("authenticationDao");
// Create a workspace that contains the 'live' nodes
final StoreRef testStoreRef = this.nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE,
"Test_" + System.currentTimeMillis());
// Get a reference to the root node
final NodeRef rootNodeRef = this.nodeService.getRootNode(testStoreRef);
// Create an authenticate the user
if(!authenticationDAO.userExists(AuthenticationUtil.getAdminUserName()))
{
authenticationService.createAuthentication(AuthenticationUtil.getAdminUserName(), "admin".toCharArray());
}
TestWithUserUtils.authenticateUser(AuthenticationUtil.getAdminUserName(),
"admin",
rootNodeRef,
authenticationService);
// set up a faces context
final MockExternalContext ec = new MockExternalContext(new MockServletContext(),
new MockHttpServletRequest(),
new MockHttpServletResponse());
final StaticWebApplicationContext ac = new StaticWebApplicationContext();
ac.setParent(this.applicationContext);
this.applicationContext = ac;
ec.getApplicationMap().put(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
this.applicationContext);
new MockFacesContext(ec);
final FileInfo folderInfo =
fileFolderService.create(rootNodeRef,
"test_form",
WCMAppModel.TYPE_FORMFOLDER);
final HashMap<QName, Serializable> props = new HashMap<QName, Serializable>();
this.nodeService.addAspect(folderInfo.getNodeRef(),
WCMAppModel.ASPECT_FORM,
props);
this.mockForm = new MockForm(folderInfo.getNodeRef(), this.formsService);
}
@Override
protected String[] getConfigLocations()
{
return (String[])ArrayUtils.add(super.getConfigLocations(),
WEB_CLIENT_APPLICATION_CONTEXT);
}
@Override
protected ConfigurableApplicationContext loadContext(Object key)
throws Exception
{
return new ClassPathXmlApplicationContext((String[])key);
}
public void testOutputPathPatternForFormInstanceData()
throws Exception
{
class OutputPathPatternTest
{
public final String expected;
public final String pattern;
public final Document xml;
public final String name;
public final String parentAVMPath;
public final String webapp;
public OutputPathPatternTest(final String expected,
final String pattern,
final Document xml,
final String name,
final String parentAVMPath,
final String webapp)
{
this.expected = expected;
this.pattern = pattern;
this.xml = xml;
this.name = name;
this.parentAVMPath = parentAVMPath;
this.webapp = webapp;
}
}
final OutputPathPatternTest[] opps = new OutputPathPatternTest[] {
new OutputPathPatternTest("avmstore:/www/avm_webapps/webapp/dir/foo.xml",
"${name}.xml",
XMLUtil.parse("<foo/>"),
"foo",
"avmstore:/www/avm_webapps/webapp/dir",
"webapp"),
new OutputPathPatternTest("avmstore:/www/avm_webapps/foo.xml",
"/${name}.xml",
XMLUtil.parse("<foo/>"),
"foo",
"avmstore:/www/avm_webapps/webapp/dir",
"webapp"),
new OutputPathPatternTest("avmstore:/www/avm_webapps/webapp/foo.xml",
"/${webapp}/${name}.xml",
XMLUtil.parse("<foo/>"),
"foo",
"avmstore:/www/avm_webapps/webapp/dir",
"webapp"),
new OutputPathPatternTest("avmstore:/www/avm_webapps/webapp/foo.xml",
"/${webapp}/${name}.xml",
XMLUtil.parse("<foo/>"),
"foo",
"avmstore:/www/avm_webapps/another_webapp/dir",
"webapp"),
new OutputPathPatternTest("avmstore:/www/avm_webapps/webapp/dir1/dir2/foo.xml",
"/${webapp}/${cwd}/${name}.xml",
XMLUtil.parse("<foo/>"),
"foo",
"avmstore:/www/avm_webapps/another_webapp/dir1/dir2",
"webapp"),
new OutputPathPatternTest("avmstore:/www/avm_webapps/webapp/dir/" + Calendar.getInstance().get(Calendar.YEAR) + "_foo.xml",
"${date?string('yyyy')}_${name}.xml",
XMLUtil.parse("<foo/>"),
"foo",
"avmstore:/www/avm_webapps/webapp/dir",
"webapp"),
new OutputPathPatternTest("avmstore:/www/avm_webapps/webapp/dir/foo.xml",
"${xml.root_tag.name}.xml",
XMLUtil.parse("<root_tag><name>foo</name></root_tag>"),
"foo",
"avmstore:/www/avm_webapps/webapp/dir",
"webapp"),
new OutputPathPatternTest("avmstore:/www/avm_webapps/webapp/dir/07.xml",
"${xml.root_tag.date?date('yyyy-MM-dd')?string('MM')}.xml",
XMLUtil.parse("<root_tag><date>1776-07-04</date></root_tag>"),
"foo",
"avmstore:/www/avm_webapps/webapp/dir",
"webapp"),
new OutputPathPatternTest("avmstore:/www/avm_webapps/webapp/dir/foo.xml",
"${xml['foons:root_tag/foons:name']}.xml",
XMLUtil.parse("<foons:root_tag xmlns:foons='bar'><foons:name>foo</foons:name></foons:root_tag>"),
"foo",
"avmstore:/www/avm_webapps/webapp/dir",
"webapp"),
new OutputPathPatternTest("avmstore:/www/avm_webapps/webapp/dir/foo.xml",
"${xml[\"/*[name()='foons:root_tag']/*[name()='foons:name']\"]}.xml",
XMLUtil.parse("<foons:root_tag xmlns:foons='bar'><foons:name>foo</foons:name></foons:root_tag>"),
"foo",
"avmstore:/www/avm_webapps/webapp/dir",
"webapp"),
new OutputPathPatternTest("avmstore:/www/avm_webapps/webapp/dir/foo.xml",
"${xml['/foons:root_tag/foons:name']}.xml",
XMLUtil.parse("<foons:root_tag xmlns:foons='bar'><foons:name>foo</foons:name></foons:root_tag>"),
"foo",
"avmstore:/www/avm_webapps/webapp/dir",
"webapp"),
new OutputPathPatternTest(null,
"${xml.root_tag.name}",
XMLUtil.parse("<foons:root_tag xmlns:foons='bar'><foons:name>foo</foons:name></foons:root_tag>"),
"foo",
"avmstore:/www/avm_webapps/webapp/dir",
"webapp")
};
for (final OutputPathPatternTest oppt : opps)
{
this.mockForm.setOutputPathPattern(oppt.pattern);
if (oppt.expected == null)
{
try
{
this.mockForm.getOutputPathForFormInstanceData(oppt.xml,
oppt.name,
oppt.parentAVMPath,
oppt.webapp);
fail("expected pattern " + oppt.pattern + " to fail");
}
catch (Exception e)
{
// expected failure
}
}
else
{
assertEquals(oppt.pattern + " failed",
oppt.expected,
this.mockForm.getOutputPathForFormInstanceData(oppt.xml,
oppt.name,
oppt.parentAVMPath,
oppt.webapp));
}
}
}
}

View File

@@ -1,143 +0,0 @@
/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.web.forms;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import junit.framework.TestCase;
import org.alfresco.util.XMLUtil;
/**
* Simple XMLUtil test
*/
public class XMLUtilTest extends TestCase
{
public static final String SOME_XML =
" <model name='test1:testModelOne' xmlns='http://www.alfresco.org/model/dictionary/1.0'>" +
" <description>Test model one</description>" +
" <author>Alfresco</author>" +
" <published>2008-01-01</published>" +
" <version>1.0</version>" +
" <imports>" +
" <import uri='http://www.alfresco.org/model/dictionary/1.0' prefix='d'/>" +
" </imports>" +
" <namespaces>" +
" <namespace uri='http://www.alfresco.org/test/testmodel1/1.0' prefix='test1'/>" +
" </namespaces>" +
" <types>" +
" <type name='test1:base'>" +
" <title>Base</title>" +
" <description>The Base Type</description>" +
" <properties>" +
" <property name='test1:prop1'>" +
" <type>d:text</type>" +
" </property>" +
" </properties>" +
" </type>" +
" </types>" +
" </model>";
private final static int threadCount = 5;
private final static int loopCount = 50;
private final static int randomNextInt = 100;
private Map<String, Throwable> errors = new HashMap<String, Throwable>();
protected void setUp() throws Exception
{
}
// https://issues.alfresco.com/browse/ETWOONE-241
public void testConcurrentParse()
{
ThreadGroup threadGroup = new ThreadGroup(getName());
Thread[] threads = new Thread[threadCount];
for (int i = 0; i < threadCount; i++)
{
threads[i] = new Thread(threadGroup, new TestRun(""+i), String.format("XMLUtilTest-%02d", i));
threads[i].start();
}
// join each thread so that we wait for them all to finish
for (int i = 0; i < threads.length; i++)
{
try
{
threads[i].join();
}
catch (InterruptedException e)
{
// ignore
}
}
if (errors.size() != 0)
{
fail();
}
}
class TestRun extends Thread
{
private String arg;
public TestRun(String arg)
{
this.arg = arg;
}
public String getArg()
{
return arg;
}
public void run()
{
Random random = new Random(System.currentTimeMillis());
for (int i = 0; i < loopCount; i++)
{
try
{
XMLUtil.parse(SOME_XML); // ignore returned doc
}
catch (Throwable t)
{
t.printStackTrace();
errors.put(arg, t);
break;
}
// random delay ...
if (randomNextInt != 0)
{
int msecs = random.nextInt(randomNextInt);
try {Thread.sleep(msecs);} catch (Exception exception){};
}
}
}
}
}

View File

@@ -1,679 +0,0 @@
/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.web.forms.xforms;
import java.io.*;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.Vector;
import java.util.ResourceBundle;
import junit.framework.AssertionFailedError;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.util.BaseTest;
import org.alfresco.util.XMLUtil;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.Pointer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.chiba.xml.ns.NamespaceConstants;
import org.chiba.xml.events.XFormsEventNames;
import org.chiba.xml.events.XMLEvent;
import org.chiba.xml.xforms.ChibaBean;
import org.chiba.xml.xforms.exception.XFormsException;
import org.chiba.xml.xforms.XFormsElement;
import org.chiba.xml.events.DOMEventNames;
import org.w3c.dom.*;
import org.w3c.dom.events.*;
import org.xml.sax.*;
/**
* JUnit tests to exercise the the schema to xforms converter
*
* @author ariel backenroth
*/
public class Schema2XFormsTest
extends BaseTest
{
private final static Log LOGGER = LogFactory.getLog(Schema2XFormsTest.class);
public void testOneStringTestWithEmptyInstanceDocument()
throws Exception
{
final Document schemaDocument = this.loadTestResourceDocument("xforms/unit-tests/one-string-test.xsd");
final Document xformsDocument = Schema2XFormsTest.buildXForm(null, schemaDocument, "one-string-test");
this.runXForm(xformsDocument);
final JXPathContext xpathContext = JXPathContext.newContext(xformsDocument);
Pointer pointer = xpathContext.getPointer("//*[@id='input_0']");
assertNotNull(pointer);
String s = ((Element)pointer.getNode()).getAttributeNS(NamespaceConstants.XFORMS_NS, "bind");
assertNotNull(s);
pointer = xpathContext.getPointer("//*[@id='" + s + "']");
assertNotNull(pointer);
assertEquals("true()", ((Element)pointer.getNode()).getAttributeNS(NamespaceConstants.XFORMS_NS, "required"));
pointer = xpathContext.getPointer("//" + NamespaceConstants.XFORMS_PREFIX + ":instance[@id='instance_0']/one-string-test/string");
assertNotNull(pointer);
assertEquals("default-value", ((Element)pointer.getNode()).getTextContent());
}
public void testOneStringTestWithInstanceDocument()
throws Exception
{
final Document instanceDocument = XMLUtil.parse("<one-string-test><string>test</string></one-string-test>");
final Document schemaDocument = this.loadTestResourceDocument("xforms/unit-tests/one-string-test.xsd");
final Document xformsDocument = Schema2XFormsTest.buildXForm(instanceDocument, schemaDocument, "one-string-test");
this.runXForm(xformsDocument);
final JXPathContext xpathContext = JXPathContext.newContext(xformsDocument);
Pointer pointer = xpathContext.getPointer("//*[@id='input_0']");
assertNotNull(pointer);
String s = ((Element)pointer.getNode()).getAttributeNS(NamespaceConstants.XFORMS_NS, "bind");
pointer = xpathContext.getPointer("//*[@id='" + s + "']");
assertNotNull(pointer);
assertEquals("true()", ((Element)pointer.getNode()).getAttributeNS(NamespaceConstants.XFORMS_NS, "required"));
pointer = xpathContext.getPointer("//" + NamespaceConstants.XFORMS_PREFIX + ":instance[@id='instance_0']/one-string-test/string");
assertNotNull(pointer);
assertEquals("test", ((Element)pointer.getNode()).getTextContent());
}
public void testNumbers()
throws Exception
{
final Document schemaDocument = this.loadTestResourceDocument("xforms/unit-tests/number-test.xsd");
final Document xformsDocument = Schema2XFormsTest.buildXForm(null, schemaDocument, "number-test");
System.err.println("generated xform " + XMLUtil.toString(xformsDocument));
final Element[] repeatedNumbers = Schema2XFormsTest.resolveXFormsControl(xformsDocument, "/number-test/repeated_numbers");
final ChibaBean chibaBean = this.runXForm(xformsDocument);
try
{
chibaBean.dispatch(repeatedNumbers[0].getAttribute("id") + "-insert_before", DOMEventNames.ACTIVATE);
fail("expected to reproduce WCM-778");
}
catch (XFormsException bindingIssue)
{
// tracked as WCM-778
}
}
public void testRepeatConstraintsTest()
throws Exception
{
final Document schemaDocument = this.loadTestResourceDocument("xforms/unit-tests/repeat-constraints-test.xsd");
final Document xformsDocument = Schema2XFormsTest.buildXForm(null, schemaDocument, "repeat-constraints-test");
Schema2XFormsTest.assertRepeatProperties(xformsDocument,
"/repeat-constraints-test/one-to-inf",
new SchemaUtil.Occurrence(1, SchemaUtil.Occurrence.UNBOUNDED));
Schema2XFormsTest.assertRepeatProperties(xformsDocument,
"/repeat-constraints-test/zero-to-inf",
new SchemaUtil.Occurrence(0, SchemaUtil.Occurrence.UNBOUNDED));
Schema2XFormsTest.assertRepeatProperties(xformsDocument,
"/repeat-constraints-test/one-to-five",
new SchemaUtil.Occurrence(1, 5));
Schema2XFormsTest.assertRepeatProperties(xformsDocument,
"/repeat-constraints-test/three-to-five",
new SchemaUtil.Occurrence(3, 5));
Schema2XFormsTest.assertRepeatProperties(xformsDocument,
"/repeat-constraints-test/zero-to-five",
new SchemaUtil.Occurrence(0, 5));
Schema2XFormsTest.assertRepeatProperties(xformsDocument,
"/repeat-constraints-test/referenced-string",
new SchemaUtil.Occurrence(1, SchemaUtil.Occurrence.UNBOUNDED));
Schema2XFormsTest.assertRepeatProperties(xformsDocument,
"/repeat-constraints-test/nested-outer-zero-to-inf",
new SchemaUtil.Occurrence(0, SchemaUtil.Occurrence.UNBOUNDED));
Schema2XFormsTest.assertRepeatProperties(xformsDocument,
"/repeat-constraints-test/nested-outer-zero-to-inf/nested-zero-to-inf-inner-zero-to-inf",
new SchemaUtil.Occurrence(0, SchemaUtil.Occurrence.UNBOUNDED));
Schema2XFormsTest.assertRepeatProperties(xformsDocument,
"/repeat-constraints-test/nested-outer-zero-to-inf/nested-zero-to-inf-inner-one-to-inf",
new SchemaUtil.Occurrence(1, SchemaUtil.Occurrence.UNBOUNDED));
Schema2XFormsTest.assertRepeatProperties(xformsDocument,
"/repeat-constraints-test/nested-outer-one-to-inf",
new SchemaUtil.Occurrence(1, SchemaUtil.Occurrence.UNBOUNDED));
Schema2XFormsTest.assertRepeatProperties(xformsDocument,
"/repeat-constraints-test/nested-outer-one-to-inf/nested-one-to-inf-inner-zero-to-inf",
new SchemaUtil.Occurrence(0, SchemaUtil.Occurrence.UNBOUNDED));
Schema2XFormsTest.assertRepeatProperties(xformsDocument,
"/repeat-constraints-test/nested-outer-one-to-inf/nested-one-to-inf-inner-one-to-inf",
new SchemaUtil.Occurrence(1, SchemaUtil.Occurrence.UNBOUNDED));
Schema2XFormsTest.assertRepeatProperties(xformsDocument,
"/repeat-constraints-test/nested-outer-three-to-five",
new SchemaUtil.Occurrence(3, 5));
Schema2XFormsTest.assertRepeatProperties(xformsDocument,
"/repeat-constraints-test/nested-outer-three-to-five/nested-three-to-five-inner-zero-to-inf",
new SchemaUtil.Occurrence(0, SchemaUtil.Occurrence.UNBOUNDED));
Schema2XFormsTest.assertRepeatProperties(xformsDocument,
"/repeat-constraints-test/nested-outer-three-to-five/nested-three-to-five-inner-one-to-inf",
new SchemaUtil.Occurrence(1, SchemaUtil.Occurrence.UNBOUNDED));
Schema2XFormsTest.assertRepeatProperties(xformsDocument,
"/repeat-constraints-test/nested-outer-outer-three-to-inf",
new SchemaUtil.Occurrence(3, SchemaUtil.Occurrence.UNBOUNDED));
Schema2XFormsTest.assertRepeatProperties(xformsDocument,
"/repeat-constraints-test/nested-outer-outer-three-to-inf/nested-outer-inner-five-to-inf",
new SchemaUtil.Occurrence(5, SchemaUtil.Occurrence.UNBOUNDED));
Schema2XFormsTest.assertRepeatProperties(xformsDocument,
"/repeat-constraints-test/nested-outer-outer-three-to-inf/nested-outer-inner-five-to-inf/nested-inner-inner-seven-to-inf",
new SchemaUtil.Occurrence(7, SchemaUtil.Occurrence.UNBOUNDED));
this.runXForm(xformsDocument);
}
public void testRootElementWithExtension()
throws Exception
{
final Document schemaDocument = this.loadTestResourceDocument("xforms/unit-tests/root-element-with-extension-test.xsd");
Document xformsDocument = Schema2XFormsTest.buildXForm(null, schemaDocument, "without-extension-test");
this.runXForm(xformsDocument);
assertEquals(3, xformsDocument.getElementsByTagNameNS(NamespaceConstants.XFORMS_NS, "input").getLength());
try
{
xformsDocument = Schema2XFormsTest.buildXForm(null, schemaDocument, "with-extension-test");
fail("expected failure creating xform with root element with-extension-test in schema " + XMLUtil.toString(schemaDocument));
}
catch (FormBuilderException fbe)
{
LOGGER.debug("got expected exception " + fbe.getMessage());
}
}
public void testSwitch()
throws Exception
{
final Document schemaDocument = this.loadTestResourceDocument("xforms/unit-tests/switch-test.xsd");
final Document xformsDocument = Schema2XFormsTest.buildXForm(null, schemaDocument, "switch-test");
this.runXForm(xformsDocument);
// assertEquals(3, xformsDocument.getElementsByTagNameNS(NamespaceConstants.XFORMS_NS, "input").getLength());
//
// try
// {
// xformsDocument = Schema2XFormsTest.buildXForm(null, schemaDocument, "with-extension-test");
// fail("expected failure creating xform with root element with-extension-test in schema " + XMLUtil.toString(schemaDocument));
// }
// catch (FormBuilderException fbe)
// {
// }
}
public void testDerivedType()
throws Exception
{
final Document schemaDocument = this.loadTestResourceDocument("xforms/unit-tests/derived-type-test.xsd");
final Document xformsDocument = Schema2XFormsTest.buildXForm(null, schemaDocument, "derived-type-test");
this.runXForm(xformsDocument);
LOGGER.debug("generated xforms " + XMLUtil.toString(xformsDocument));
assertBindProperties(xformsDocument,
"/derived-type-test/raw-normalized-string",
"normalizedString",
"normalizedString");
assertControlProperties(xformsDocument,
"/derived-type-test/raw-normalized-string",
NamespaceConstants.XFORMS_PREFIX + ":input");
assertBindProperties(xformsDocument,
"/derived-type-test/non-empty-normalized-string",
"non-empty-normalized-string-type",
"normalizedString");
assertControlProperties(xformsDocument,
"/derived-type-test/non-empty-normalized-string",
NamespaceConstants.XFORMS_PREFIX + ":input");
assertBindProperties(xformsDocument,
"/derived-type-test/raw-string",
"string",
"string");
assertControlProperties(xformsDocument,
"/derived-type-test/raw-string",
NamespaceConstants.XFORMS_PREFIX + ":textarea");
assertBindProperties(xformsDocument,
"/derived-type-test/non-empty-string",
"non-empty-string-type",
"string");
assertControlProperties(xformsDocument,
"/derived-type-test/non-empty-string",
NamespaceConstants.XFORMS_PREFIX + ":textarea");
assertBindProperties(xformsDocument,
"/derived-type-test/raw-any-uri",
"anyURI",
"anyURI");
assertControlProperties(xformsDocument,
"/derived-type-test/raw-any-uri",
NamespaceConstants.XFORMS_PREFIX + ":upload");
assertBindProperties(xformsDocument,
"/derived-type-test/non-empty-any-uri",
"non-empty-any-uri-type",
"anyURI");
assertControlProperties(xformsDocument,
"/derived-type-test/non-empty-any-uri",
NamespaceConstants.XFORMS_PREFIX + ":upload");
assertBindProperties(xformsDocument,
"/derived-type-test/raw-decimal",
"decimal",
"decimal");
assertControlProperties(xformsDocument,
"/derived-type-test/raw-decimal",
NamespaceConstants.XFORMS_PREFIX + ":input");
try
{
assertBindProperties(xformsDocument,
"/derived-type-test/non-zero-decimal",
"non-zero-decimal-type",
"decimal");
fail("expected union type non-zero-decimal to fail");
}
catch (AssertionFailedError ignore)
{
}
assertControlProperties(xformsDocument,
"/derived-type-test/non-zero-decimal",
NamespaceConstants.XFORMS_PREFIX + ":input");
assertBindProperties(xformsDocument,
"/derived-type-test/raw-positive-integer",
"positiveInteger",
"positiveInteger");
Element control = assertControlProperties(xformsDocument,
"/derived-type-test/raw-positive-integer",
NamespaceConstants.XFORMS_PREFIX + ":input");
assertEquals(0, Integer.parseInt(control.getAttributeNS(NamespaceService.ALFRESCO_URI, "fractionDigits")));
assertBindProperties(xformsDocument,
"/derived-type-test/one-to-ten-positive-integer",
"one-to-ten-positive-integer-type",
"positiveInteger");
control = assertControlProperties(xformsDocument,
"/derived-type-test/one-to-ten-positive-integer",
NamespaceConstants.XFORMS_PREFIX + ":range");
assertEquals(1, Integer.parseInt(control.getAttributeNS(NamespaceConstants.XFORMS_NS, "start")));
assertEquals(10, Integer.parseInt(control.getAttributeNS(NamespaceConstants.XFORMS_NS, "end")));
assertEquals(0, Integer.parseInt(control.getAttributeNS(NamespaceService.ALFRESCO_URI, "fractionDigits")));
assertBindProperties(xformsDocument,
"/derived-type-test/raw-boolean",
"boolean",
"boolean");
assertControlProperties(xformsDocument,
"/derived-type-test/raw-boolean",
NamespaceConstants.XFORMS_PREFIX + ":select1");
assertBindProperties(xformsDocument,
"/derived-type-test/always-true-boolean",
"always-true-boolean-type",
"boolean");
assertControlProperties(xformsDocument,
"/derived-type-test/always-true-boolean",
NamespaceConstants.XFORMS_PREFIX + ":select1");
try
{
assertBindProperties(xformsDocument,
"/derived-type-test/raw-any-type",
"anyType",
"anyType");
fail("expected unexpected behavior for anyType");
}
catch (AssertionFailedError ignore)
{
}
assertControlProperties(xformsDocument,
"/derived-type-test/raw-any-type",
NamespaceConstants.XFORMS_PREFIX + ":textarea");
}
public void testRecursive()
throws Exception
{
final Document schemaDocument = this.loadTestResourceDocument("xforms/unit-tests/recursive-test.xsd");
Document xformsDocument = Schema2XFormsTest.buildXForm(null, schemaDocument, "non-recursive-test");
this.runXForm(xformsDocument);
try
{
xformsDocument = Schema2XFormsTest.buildXForm(null, schemaDocument, "recursive-test");
fail("expected failure creating xform with recursive element definition root element recursive-test in schema " + XMLUtil.toString(schemaDocument));
}
catch (FormBuilderException fbe)
{
LOGGER.debug("got expected exception " + fbe.getMessage());
}
try
{
xformsDocument = Schema2XFormsTest.buildXForm(null, schemaDocument, "nested-recursive-test");
fail("expected failure creating xform with recursive element definition root element nested-recursive-test in schema " + XMLUtil.toString(schemaDocument));
}
catch (FormBuilderException fbe)
{
LOGGER.debug("got expected exception " + fbe.getMessage());
}
}
public void testAnnotation()
throws Exception
{
final Document schemaDocument = this.loadTestResourceDocument("xforms/unit-tests/annotation-test.xsd");
Document xformsDocument = Schema2XFormsTest.buildXForm(null, schemaDocument, "annotation-test");
this.runXForm(xformsDocument);
System.err.println("generated xform " + XMLUtil.toString(xformsDocument));
Element control = assertControlProperties(xformsDocument,
"/annotation-test/upload_in_root",
NamespaceConstants.XFORMS_PREFIX + ":upload");
assertEquals("upload_in_root", control.getAttributeNS(NamespaceConstants.XFORMS_NS, "appearance"));
control = assertControlProperties(xformsDocument,
"/annotation-test/string_in_root",
NamespaceConstants.XFORMS_PREFIX + ":textarea");
assertEquals("string_in_root", control.getAttributeNS(NamespaceConstants.XFORMS_NS, "appearance"));
control = assertControlProperties(xformsDocument,
"/annotation-test/struct_1/upload_in_base",
NamespaceConstants.XFORMS_PREFIX + ":upload");
assertEquals("upload_in_base", control.getAttributeNS(NamespaceConstants.XFORMS_NS, "appearance"));
control = assertControlProperties(xformsDocument,
"/annotation-test/struct_1/string_in_base",
NamespaceConstants.XFORMS_PREFIX + ":textarea");
assertEquals("string_in_base", control.getAttributeNS(NamespaceConstants.XFORMS_NS, "appearance"));
control = assertControlProperties(xformsDocument,
"/annotation-test/struct_1/upload_in_struct",
NamespaceConstants.XFORMS_PREFIX + ":upload");
assertEquals("upload_in_struct", control.getAttributeNS(NamespaceConstants.XFORMS_NS, "appearance"));
control = assertControlProperties(xformsDocument,
"/annotation-test/struct_1/string_in_struct",
NamespaceConstants.XFORMS_PREFIX + ":textarea");
assertEquals("string_in_struct", control.getAttributeNS(NamespaceConstants.XFORMS_NS, "appearance"));
}
public void testConstraint()
throws Exception
{
final Document schemaDocument = this.loadTestResourceDocument("xforms/unit-tests/constraint-test.xsd");
Document xformsDocument = Schema2XFormsTest.buildXForm(null, schemaDocument, "constraint-test");
final ChibaBean chibaBean = this.runXForm(xformsDocument);
final LinkedList<XMLEvent> events = new LinkedList<XMLEvent>();
final EventListener el = new EventListener()
{
public void handleEvent(final Event e)
{
events.add((XMLEvent)e);
}
};
((EventTarget)chibaBean.getXMLContainer().getDocumentElement()).addEventListener(XFormsEventNames.VALID, el, true);
((EventTarget)chibaBean.getXMLContainer().getDocumentElement()).addEventListener(XFormsEventNames.INVALID, el, true);
((EventTarget)chibaBean.getXMLContainer().getDocumentElement()).addEventListener(XFormsEventNames.SUBMIT_DONE, el, true);
((EventTarget)chibaBean.getXMLContainer().getDocumentElement()).addEventListener(XFormsEventNames.SUBMIT_ERROR, el, true);
Element e = Schema2XFormsTest.resolveXFormsControl(xformsDocument, "/constraint-test/zip-pattern")[0];
chibaBean.updateControlValue(e.getAttribute("id"), "not a zip");
assertEquals(1, events.size());
assertEquals(XFormsEventNames.INVALID, events.get(0).getType());
events.clear();
chibaBean.updateControlValue(e.getAttribute("id"), "94110");
assertEquals(1, events.size());
assertEquals(XFormsEventNames.VALID, events.get(0).getType());
events.clear();
e = Schema2XFormsTest.resolveXFormsControl(xformsDocument, "/constraint-test/email-pattern")[0];
chibaBean.updateControlValue(e.getAttribute("id"), "iamnotanemailaddress");
assertEquals(1, events.size());
assertEquals(XFormsEventNames.INVALID, events.get(0).getType());
events.clear();
chibaBean.updateControlValue(e.getAttribute("id"), "ariel.backenroth@alfresco.org");
assertEquals(1, events.size());
assertEquals(XFormsEventNames.VALID, events.get(0).getType());
events.clear();
Element[] controls = Schema2XFormsTest.resolveXFormsControl(xformsDocument, "/constraint-test/repeated-zip-pattern/.");
assertEquals(3 /* 2 actual + prototype */, controls.length);
Element[] repeat = Schema2XFormsTest.resolveXFormsControl(xformsDocument, "/constraint-test/repeated-zip-pattern");
assertEquals(4 /* 1 repeat + 3 triggers */, repeat.length);
final Element[] bindForRepeat = Schema2XFormsTest.resolveBind(xformsDocument, "/constraint-test/repeated-zip-pattern");
assertEquals(bindForRepeat[bindForRepeat.length - 1].getAttribute("id"), repeat[0].getAttributeNS(NamespaceConstants.XFORMS_NS, "bind"));
for (int i = 1; i <= Integer.parseInt(bindForRepeat[bindForRepeat.length - 1].getAttributeNS(NamespaceConstants.XFORMS_NS, "minOccurs")); i++)
{
chibaBean.updateRepeatIndex(repeat[0].getAttribute("id"), i);
chibaBean.updateControlValue(controls[controls.length - 1].getAttribute("id"), "notavalidzip");
}
// assertEquals("unexpected events " + events, controls.length, events.size());
for (final Event event : events)
{
assertEquals(XFormsEventNames.INVALID, event.getType());
}
events.clear();
chibaBean.dispatch("submit", DOMEventNames.ACTIVATE);
assertEquals(1, events.size());
assertEquals(XFormsEventNames.SUBMIT_ERROR, events.get(0).getType());
events.clear();
for (final Element c : controls)
{
chibaBean.updateControlValue(c.getAttribute("id"), "07666");
}
// assertEquals("unexpected events " + events, controls.length, events.size());
for (final Event event : events)
{
assertEquals(XFormsEventNames.VALID, event.getType());
}
events.clear();
chibaBean.dispatch("submit", DOMEventNames.ACTIVATE);
assertEquals(1, events.size());
assertEquals(XFormsEventNames.SUBMIT_DONE, events.get(0).getType());
}
private static void assertRepeatProperties(final Document xformsDocument,
final String nodeset,
final SchemaUtil.Occurrence o)
{
final Element[] bindElements = Schema2XFormsTest.resolveBind(xformsDocument, nodeset);
assertNotNull("unable to resolve bind for nodeset " + nodeset, bindElements);
assertFalse("unable to resolve bind for nodeset " + nodeset, 0 == bindElements.length);
final Element nodesetBindElement = bindElements[bindElements.length - 1];
assertEquals("unexpected minimum value for nodeset " + nodeset,
o.minimum,
Integer.parseInt(nodesetBindElement.getAttributeNS(NamespaceConstants.XFORMS_NS, "minOccurs")));
if (o.isUnbounded())
{
assertEquals("unexpected maximum value for nodeset " + nodeset,
"unbounded",
nodesetBindElement.getAttributeNS(NamespaceConstants.XFORMS_NS, "maxOccurs"));
}
else
{
assertEquals("unexpected maximum value for nodeset " + nodeset,
o.maximum,
Integer.parseInt(nodesetBindElement.getAttributeNS(NamespaceConstants.XFORMS_NS, "maxOccurs")));
}
assertEquals("unexpected required value for nodeset " + nodeset,
(o.minimum != 0 && nodesetBindElement.hasAttributeNS(NamespaceConstants.XFORMS_NS, "type")) + "()",
nodesetBindElement.getAttributeNS(NamespaceConstants.XFORMS_NS, "required"));
JXPathContext xpathContext = JXPathContext.newContext(xformsDocument);
String xpath = "//*[@" + NamespaceConstants.XFORMS_PREFIX + ":bind='" + nodesetBindElement.getAttribute("id") + "']";
assertEquals(4, xpathContext.selectNodes(xpath).size());
xpath = ("//" + NamespaceConstants.XFORMS_PREFIX +
":repeat[@" + NamespaceConstants.XFORMS_PREFIX +
":bind='" + nodesetBindElement.getAttribute("id") + "']");
assertEquals(1, xpathContext.selectNodes(xpath).size());
xpath = ("//" + NamespaceConstants.XFORMS_PREFIX +
":trigger[@" + NamespaceConstants.XFORMS_PREFIX +
":bind='" + nodesetBindElement.getAttribute("id") + "']");
assertEquals(3, xpathContext.selectNodes(xpath).size());
int nestingFactor = 1;
for (int i = 0; i < bindElements.length - 1; i++)
{
final SchemaUtil.Occurrence parentO = Schema2XFormsTest.occuranceFromBind(bindElements[i]);
if (parentO.isRepeated())
{
nestingFactor = nestingFactor * (1 + parentO.minimum);
}
}
final Pointer instance0 = xpathContext.getPointer("//" + NamespaceConstants.XFORMS_PREFIX + ":instance[@id='instance_0']");
assertNotNull(instance0);
assertNotNull(instance0.getNode());
xpathContext = xpathContext.getRelativeContext(instance0);
xpath = nodeset.substring(1);
assertEquals("unexpected result for instance nodeset " + xpath + " in " + instance0.getNode(),
nestingFactor * (o.minimum + 1),
xpathContext.selectNodes(xpath).size());
xpath = nodeset.substring(1) + "[@" + NamespaceService.ALFRESCO_PREFIX + ":prototype='true']";
assertEquals("unexpected result for instance prototype nodeset " + nodeset + " in " + instance0.getNode(),
nestingFactor,
xpathContext.selectNodes(xpath).size());
}
private static Element assertBindProperties(final Document xformsDocument,
final String nodeset,
final String schemaType,
final String builtInType)
{
final Element[] binds = Schema2XFormsTest.resolveBind(xformsDocument, nodeset);
assertEquals("unexpected type for nodeset " + nodeset,
schemaType,
binds[binds.length - 1].getAttributeNS(NamespaceConstants.XFORMS_NS, "type"));
assertEquals("unexpected built in type for nodeset " + nodeset,
builtInType,
binds[binds.length - 1].getAttributeNS(NamespaceService.ALFRESCO_URI, "builtInType"));
return binds[binds.length - 1];
}
private static Element assertControlProperties(final Document xformsDocument,
final String nodeset,
final String controlType)
{
final Element[] controls = Schema2XFormsTest.resolveXFormsControl(xformsDocument, nodeset);
assertEquals("unexpected xforms control for " + nodeset,
controlType,
controls[controls.length - 1].getNodeName());
return controls[controls.length - 1];
}
/**
* Returns the resolved bind and all parents binds for the nodeset.
*/
private static Element[] resolveBind(final Document xformsDocument, final String nodeset)
{
JXPathContext xpathContext = JXPathContext.newContext(xformsDocument);
assertNotNull(nodeset);
assertEquals('/', nodeset.charAt(0));
final String rootNodePath = nodeset.replaceFirst("(\\/[^\\/]+).*", "$1");
assertNotNull(rootNodePath);
String xpath = ("//" + NamespaceConstants.XFORMS_PREFIX +
":bind[@" + NamespaceConstants.XFORMS_PREFIX +
":nodeset='" + rootNodePath + "']");
Pointer pointer = xpathContext.getPointer(xpath);
assertNotNull("unable to resolve xpath for root node " + xpath, pointer);
assertNotNull("unable to resolve xpath for root node " + xpath, pointer.getNode());
if (nodeset.equals(rootNodePath))
{
return new Element[] { (Element)pointer.getNode() };
}
xpathContext = xpathContext.getRelativeContext(pointer);
// substring the path to the next slash and split it
final LinkedList<Element> result = new LinkedList<Element>();
result.add((Element)pointer.getNode());
for (String p : nodeset.substring(rootNodePath.length() + 1).split("/"))
{
xpath = NamespaceConstants.XFORMS_PREFIX + ":bind[starts-with(@" + NamespaceConstants.XFORMS_PREFIX + ":nodeset, '" + p + "')]";
pointer = xpathContext.getPointer(xpath);
assertNotNull("unable to resolve path " + xpath +
" on bind with nodeset " + result.getLast().getAttributeNS(NamespaceConstants.XFORMS_NS, "nodeset"),
pointer);
assertNotNull("unable to resolve path " + xpath +
" on bind with nodeset " + result.getLast().getAttributeNS(NamespaceConstants.XFORMS_NS, "nodeset"),
pointer.getNode());
xpathContext = xpathContext.getRelativeContext(pointer);
result.add((Element)pointer.getNode());
}
return (Element[])result.toArray(new Element[result.size()]);
}
private static Element[] resolveXFormsControl(final Document xformsDocument,
final String nodeset)
{
final Element[] binds = Schema2XFormsTest.resolveBind(xformsDocument, nodeset);
assertNotNull(binds);
assertFalse(binds.length == 0);
final String bindId = binds[binds.length - 1].getAttribute("id");
final JXPathContext xpathContext = JXPathContext.newContext(xformsDocument);
String xpath = "//*[@" + NamespaceConstants.XFORMS_PREFIX + ":bind='" + bindId + "']";
return (Element[])xpathContext.selectNodes(xpath).toArray(new Element[0]);
}
private Document loadTestResourceDocument(final String path)
throws IOException, SAXException
{
File f = new File(this.getResourcesDir());
for (final String p : path.split("/"))
{
f = new File(f, p);
}
return XMLUtil.parse(f);
}
private ChibaBean runXForm(final Document xformsDocument)
throws Exception
{
final ChibaBean chibaBean = new ChibaBean();
String webResourceDir = System.getProperty("alfresco.web.resources.dir",
this.getResourcesDir() + File.separator + ".." + File.separator + "web");
chibaBean.setConfig(webResourceDir + File.separator +
"WEB-INF" + File.separator + "chiba.xml");
chibaBean.setXMLContainer(xformsDocument);
chibaBean.init();
return chibaBean;
}
private static Document buildXForm(final Document instanceDocument,
final Document schemaDocument,
final String rootElementName)
throws FormBuilderException
{
final Schema2XForms s2xf = new Schema2XForms("/test_action",
Schema2XForms.SubmitMethod.POST,
"echo://fake.base.url", true);
return s2xf.buildXForm(instanceDocument,
schemaDocument,
rootElementName,
new ResourceBundle()
{
public Object handleGetObject(final String key)
{
if (key == null)
{
throw new NullPointerException();
}
return null;
}
public Enumeration<String> getKeys()
{
return new Vector<String>().elements();
}
}).getFirst();
}
private static SchemaUtil.Occurrence occuranceFromBind(final Element bindElement)
{
return new SchemaUtil.Occurrence(bindElement.hasAttributeNS(NamespaceConstants.XFORMS_NS, "minOccurs")
? Integer.parseInt(bindElement.getAttributeNS(NamespaceConstants.XFORMS_NS, "minOccurs"))
: 1,
bindElement.hasAttributeNS(NamespaceConstants.XFORMS_NS, "maxOccurs")
? ("unbounded".equals(bindElement.getAttributeNS(NamespaceConstants.XFORMS_NS, "maxOccurs"))
? SchemaUtil.Occurrence.UNBOUNDED
: Integer.parseInt(bindElement.getAttributeNS(NamespaceConstants.XFORMS_NS, "maxOccurs")))
: 1);
}
}

View File

@@ -426,8 +426,12 @@ public class XFormsProcessor implements FormProcessor
{
return result;
}
throw new RuntimeException("widget definitions " + this +
" and " + other + " collide");
if (LOGGER.isInfoEnabled())
{
LOGGER.info("widget definitions " + this + " and " + other + " may collide");
}
return 0;
}
public String toString()