Browse files

Tests for triggers

  • Loading branch information...
1 parent ea75c3c commit 0cb25ab242fcca66429e353d1d06bfb8e84b6030 @shabanovd shabanovd committed Jan 30, 2014
View
4 test/src/org/exist/collections/triggers/AllTriggerTests.java
@@ -1,6 +1,6 @@
/*
* eXist Open Source Native XML Database
- * Copyright (C) 2001-2011 The eXist Project
+ * Copyright (C) 2001-2014 The eXist Project
* http://exist-db.org
*
* This program is free software; you can redistribute it and/or
@@ -16,8 +16,6 @@
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * $Id$
*/
package org.exist.collections.triggers;
View
134 test/src/org/exist/collections/triggers/AnotherTrigger.java
@@ -0,0 +1,134 @@
+/*
+ * eXist Open Source Native XML Database
+ * Copyright (C) 2001-2014 The eXist Project
+ * http://exist-db.org
+ *
+ * This program 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 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program 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 this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.exist.collections.triggers;
+
+import org.exist.dom.DocumentImpl;
+import org.exist.storage.DBBroker;
+import org.exist.storage.txn.Txn;
+import org.exist.xmldb.XmldbURI;
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Test trigger to check if trigger configuration is working properly.
+ */
+public class AnotherTrigger extends SAXTrigger {
+
+ protected static StringBuilder sb = null;
+
+ protected static int count = 0;
+ protected static byte createDocumentEvents = 0;
+
+ public void configure(DBBroker broker, org.exist.collections.Collection parent, Map<String, List<?>> parameters) throws TriggerException {
+ super.configure(broker, parent, parameters);
+ }
+
+ @Override
+ public void beforeCreateDocument(DBBroker broker, Txn transaction, XmldbURI uri) throws TriggerException {
+ createDocumentEvents |= 1;
+ }
+
+ @Override
+ public void afterCreateDocument(DBBroker broker, Txn transaction, DocumentImpl document) {
+ createDocumentEvents |= 2;
+ }
+
+ @Override
+ public void beforeUpdateDocument(DBBroker broker, Txn transaction, DocumentImpl document) throws TriggerException {
+ }
+
+ @Override
+ public void afterUpdateDocument(DBBroker broker, Txn transaction, DocumentImpl document) {
+ }
+
+ @Override
+ public void beforeCopyDocument(DBBroker broker, Txn transaction, DocumentImpl document, XmldbURI newUri) throws TriggerException {
+ }
+
+ @Override
+ public void afterCopyDocument(DBBroker broker, Txn transaction, DocumentImpl document, XmldbURI newUri) {
+ }
+
+ @Override
+ public void beforeMoveDocument(DBBroker broker, Txn transaction, DocumentImpl document, XmldbURI newUri) throws TriggerException {
+ }
+
+ @Override
+ public void afterMoveDocument(DBBroker broker, Txn transaction, DocumentImpl document, XmldbURI newUri) {
+ }
+
+ @Override
+ public void beforeDeleteDocument(DBBroker broker, Txn transaction, DocumentImpl document) throws TriggerException {
+ }
+
+ @Override
+ public void afterDeleteDocument(DBBroker broker, Txn transaction, XmldbURI uri) throws TriggerException {
+ }
+
+ @Override
+ public void beforeUpdateDocumentMetadata(DBBroker broker, Txn txn, DocumentImpl document) throws TriggerException {
+ }
+
+ @Override
+ public void afterUpdateDocumentMetadata(DBBroker broker, Txn txn, DocumentImpl document) throws TriggerException {
+ }
+
+ @Override
+ public void startDocument() throws SAXException {
+ sb = new StringBuilder();
+
+ super.startDocument();
+ }
+
+ public void startElement(String namespaceURI, String localName, String qname, Attributes attributes) throws SAXException {
+ count++;
+
+ sb.append("<").append(qname);
+
+ for (int i = 0; i < attributes.getLength(); i++) {
+ sb.append(" ").append(attributes.getQName(i)).append("='").append(attributes.getValue(i)).append("'");
+ }
+
+ sb.append(">");
+
+ super.startElement(namespaceURI, localName, qname, attributes);
+ }
+
+ @Override
+ public void endElement(String namespaceURI, String localName, String qname) throws SAXException {
+
+ sb.append("</").append(qname).append(">");
+
+ super.endElement(namespaceURI, localName, qname);
+ }
+
+ @Override
+ public void characters(char[] ch, int start, int length) throws SAXException {
+
+ for (int i = 0; i < length; i++) {
+ sb.append(ch[start + i]);
+ }
+
+ super.characters(ch, start, length);
+ }
+}
View
232 test/src/org/exist/collections/triggers/SAXTriggerTest.java
@@ -0,0 +1,232 @@
+/*
+ * eXist Open Source Native XML Database
+ * Copyright (C) 2001-2014 The eXist Project
+ * http://exist-db.org
+ *
+ * This program 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 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program 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 this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.exist.collections.triggers;
+
+import static org.junit.Assert.*;
+
+import org.exist.EXistException;
+import org.exist.TestUtils;
+import org.exist.storage.BrokerPool;
+import org.exist.storage.DBBroker;
+import org.exist.xmldb.DatabaseInstanceManager;
+import org.exist.xmldb.IndexQueryService;
+import org.exist.xmldb.XmldbURI;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.xmldb.api.DatabaseManager;
+import org.xmldb.api.base.Collection;
+import org.xmldb.api.base.Database;
+import org.xmldb.api.base.Resource;
+import org.xmldb.api.base.XMLDBException;
+import org.xmldb.api.modules.CollectionManagementService;
+
+public class SAXTriggerTest {
+
+ private final static String DOCUMENT1_CONTENT =
+ "<test>"
+ + "<item id='1'><price>5.6</price><stock>22</stock></item>"
+ + "<item id='2'><price>7.4</price><stock>43</stock></item>"
+ + "<item id='3'><price>18.4</price><stock>5</stock></item>"
+ + "<item id='4'><price>65.54</price><stock>16</stock></item>"
+ + "</test>";
+
+ private final static String DOCUMENT2_CONTENT =
+ "<test>"
+ + "<item id='1'><price>5.6</price><stock>22</stock></item>"
+ + "</test>";
+
+ private final static String DOCUMENT3_CONTENT =
+ "<test test=\"valueTest\">\n" +
+ " <item id=\"1\" test=\"valueTest\">\n" +
+ " <price test=\"valueTest\">5.6</price>\n" +
+ " <stock test=\"valueTest\">22</stock>\n" +
+ " </item>\n" +
+ "</test>";
+
+ private final static String COLLECTION_CONFIG =
+ "<exist:collection xmlns:exist='http://exist-db.org/collection-config/1.0'>"
+ + " <exist:triggers>"
+ + " <exist:trigger class='org.exist.collections.triggers.StoreTrigger'/>"
+ + " </exist:triggers>"
+ + "</exist:collection>";
+
+
+ private final static String BASE_URI = "xmldb:exist://";
+
+ private final static String testCollection = "/db/triggers";
+
+ @Test
+ public void test() throws EXistException {
+
+ BrokerPool db = BrokerPool.getInstance();
+
+ db.registerDocumentTrigger(AnotherTrigger.class);
+
+ DBBroker broker = null;
+
+ try {
+ broker = db.get(db.getSecurityManager().getSystemSubject());
+
+ Collection root = DatabaseManager.getCollection(BASE_URI + testCollection, "admin", "");
+
+ Resource resource = root.createResource("data.xml", "XMLResource");
+ resource.setContent(DOCUMENT1_CONTENT);
+ root.storeResource(resource);
+
+ assertEquals(3, AnotherTrigger.createDocumentEvents);
+
+ assertEquals(26, AnotherTrigger.count);
+
+ assertEquals(DOCUMENT1_CONTENT, AnotherTrigger.sb.toString());
+
+ } catch (XMLDBException e) {
+ e.printStackTrace();
+ fail(e.getMessage());
+ } finally {
+ if (broker != null)
+ broker.release();
+ }
+ }
+
+ @Test
+ public void saxEventModifications() throws EXistException {
+
+ BrokerPool db = BrokerPool.getInstance();
+
+ db.registerDocumentTrigger(StoreTrigger.class);
+
+ DBBroker broker = null;
+
+ try {
+ broker = db.get(db.getSecurityManager().getSystemSubject());
+
+ Collection root = DatabaseManager.getCollection(BASE_URI + testCollection, "admin", "");
+
+ Resource resource = root.createResource("data.xml", "XMLResource");
+ resource.setContent(DOCUMENT2_CONTENT);
+ root.storeResource(resource);
+
+ resource = root.createResource("data.xml", "XMLResource");
+
+ assertEquals(DOCUMENT3_CONTENT, resource.getContent().toString());
+
+ } catch (XMLDBException e) {
+ e.printStackTrace();
+ fail(e.getMessage());
+ } finally {
+ if (broker != null)
+ broker.release();
+ }
+ }
+
+ @Test
+ public void saxEventModificationsAtXConf() throws EXistException {
+
+ BrokerPool db = BrokerPool.getInstance();
+
+ DBBroker broker = null;
+
+ try {
+ broker = db.get(db.getSecurityManager().getSystemSubject());
+
+ Collection root = DatabaseManager.getCollection(BASE_URI + testCollection, "admin", "");
+
+ IndexQueryService idxConf = (IndexQueryService) root.getService("IndexQueryService", "1.0");
+ idxConf.configureCollection(COLLECTION_CONFIG);
+
+ Resource resource = root.createResource("data.xml", "XMLResource");
+ resource.setContent(DOCUMENT2_CONTENT);
+ root.storeResource(resource);
+
+ resource = root.createResource("data.xml", "XMLResource");
+
+ assertEquals(DOCUMENT3_CONTENT, resource.getContent().toString());
+
+ } catch (XMLDBException e) {
+ e.printStackTrace();
+ fail(e.getMessage());
+ } finally {
+ if (broker != null)
+ broker.release();
+ }
+ }
+
+ @After
+ public void cleanDB() {
+ try {
+ Collection config = DatabaseManager.getCollection(BASE_URI + "/db/system/config" + testCollection, "admin", null);
+ if (config != null) {
+ CollectionManagementService mgmt = (CollectionManagementService) config.getService("CollectionManagementService", "1.0");
+ mgmt.removeCollection(".");
+ }
+ Collection root = DatabaseManager.getCollection(BASE_URI + testCollection, "admin", "");
+ Resource resource = root.getResource("messages.xml");
+ if (resource != null) {
+ root.removeResource(resource);
+ }
+ resource = root.getResource("data.xml");
+ if (resource != null) {
+ root.removeResource(resource);
+ }
+ } catch (XMLDBException e) {
+ e.printStackTrace();
+ fail(e.getMessage());
+ }
+ }
+
+ @BeforeClass
+ public static void initDB() {
+ // initialize XML:DB driver
+ try {
+ Class<?> cl = Class.forName("org.exist.xmldb.DatabaseImpl");
+ Database database = (Database) cl.newInstance();
+ database.setProperty("create-database", "true");
+ DatabaseManager.registerDatabase(database);
+
+ Collection root = DatabaseManager.getCollection(XmldbURI.LOCAL_DB, "admin", "");
+ CollectionManagementService mgmt = (CollectionManagementService) root.getService("CollectionManagementService", "1.0");
+ Collection testCol = mgmt.createCollection("triggers");
+
+ for (int i = 1; i <= 2; i++) {
+ mgmt = (CollectionManagementService) testCol.getService("CollectionManagementService", "1.0");
+ testCol = mgmt.createCollection("sub" + i);
+ }
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail(e.getMessage());
+ }
+ }
+
+ @AfterClass
+ public static void closeDB() {
+ TestUtils.cleanupDB();
+ try {
+ Collection root = DatabaseManager.getCollection(XmldbURI.LOCAL_DB, "admin", "");
+ DatabaseInstanceManager mgr = (DatabaseInstanceManager) root.getService("DatabaseInstanceManager", "1.0");
+ mgr.shutdown();
+ } catch (XMLDBException e) {
+ e.printStackTrace();
+ fail(e.getMessage());
+ }
+ }
+}
View
100 test/src/org/exist/collections/triggers/StoreTrigger.java
@@ -0,0 +1,100 @@
+/*
+ * eXist Open Source Native XML Database
+ * Copyright (C) 2001-2014 The eXist Project
+ * http://exist-db.org
+ *
+ * This program 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 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program 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 this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.exist.collections.triggers;
+
+import org.exist.dom.DocumentImpl;
+import org.exist.storage.DBBroker;
+import org.exist.storage.txn.Txn;
+import org.exist.xmldb.XmldbURI;
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.AttributesImpl;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Test trigger to check if trigger configuration is working properly.
+ */
+public class StoreTrigger extends SAXTrigger {
+
+ public void configure(DBBroker broker, org.exist.collections.Collection parent, Map<String, List<?>> parameters) throws TriggerException {
+ super.configure(broker, parent, parameters);
+ }
+
+ @Override
+ public void beforeCreateDocument(DBBroker broker, Txn transaction, XmldbURI uri) throws TriggerException {
+ }
+
+ @Override
+ public void afterCreateDocument(DBBroker broker, Txn transaction, DocumentImpl document) {
+ }
+
+ @Override
+ public void beforeUpdateDocument(DBBroker broker, Txn transaction, DocumentImpl document) throws TriggerException {
+ }
+
+ @Override
+ public void afterUpdateDocument(DBBroker broker, Txn transaction, DocumentImpl document) {
+ }
+
+ @Override
+ public void beforeCopyDocument(DBBroker broker, Txn transaction, DocumentImpl document, XmldbURI newUri) throws TriggerException {
+ }
+
+ @Override
+ public void afterCopyDocument(DBBroker broker, Txn transaction, DocumentImpl document, XmldbURI newUri) {
+ }
+
+ @Override
+ public void beforeMoveDocument(DBBroker broker, Txn transaction, DocumentImpl document, XmldbURI newUri) throws TriggerException {
+ }
+
+ @Override
+ public void afterMoveDocument(DBBroker broker, Txn transaction, DocumentImpl document, XmldbURI newUri) {
+ }
+
+ @Override
+ public void beforeDeleteDocument(DBBroker broker, Txn transaction, DocumentImpl document) throws TriggerException {
+ }
+
+ @Override
+ public void afterDeleteDocument(DBBroker broker, Txn transaction, XmldbURI uri) throws TriggerException {
+ }
+
+ @Override
+ public void beforeUpdateDocumentMetadata(DBBroker broker, Txn txn, DocumentImpl document) throws TriggerException {
+ }
+
+ @Override
+ public void afterUpdateDocumentMetadata(DBBroker broker, Txn txn, DocumentImpl document) throws TriggerException {
+ }
+
+ public void startElement(String namespaceURI, String localName, String qname, Attributes attributes) throws SAXException {
+
+ AttributesImpl attrs = new AttributesImpl();
+
+ attrs.setAttributes(attributes);
+
+ attrs.addAttribute("", "test", "test", "CDATA", "valueTest");
+
+ super.startElement(namespaceURI, localName, qname, attrs);
+ }
+}
View
7 test/src/org/exist/collections/triggers/TestTrigger.java
@@ -1,6 +1,6 @@
/*
* eXist Open Source Native XML Database
- * Copyright (C) 2001-2011 The eXist Project
+ * Copyright (C) 2001-2014 The eXist Project
* http://exist-db.org
*
* This program is free software; you can redistribute it and/or
@@ -16,12 +16,9 @@
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * $Id$
*/
package org.exist.collections.triggers;
-import org.exist.collections.Collection;
import org.exist.collections.IndexInfo;
import org.exist.dom.DefaultDocumentSet;
import org.exist.dom.DocumentImpl;
@@ -43,7 +40,7 @@
/**
* Test trigger to check if trigger configuration is working properly.
*/
-public class TestTrigger extends FilteringTrigger implements DocumentTrigger {
+public class TestTrigger extends SAXTrigger implements DocumentTrigger {
protected Logger LOG = Logger.getLogger(getClass());
View
4 test/src/org/exist/collections/triggers/TriggerConfigTest.java
@@ -1,6 +1,6 @@
/*
* eXist Open Source Native XML Database
- * Copyright (C) 2001-2011 The eXist Project
+ * Copyright (C) 2001-2014 The eXist Project
* http://exist-db.org
*
* This program is free software; you can redistribute it and/or
@@ -16,8 +16,6 @@
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * $Id$
*/
package org.exist.collections.triggers;
View
6 test/src/org/exist/collections/triggers/XQueryTriggerTest.java
@@ -1,6 +1,6 @@
/*
* eXist Open Source Native XML Database
- * Copyright (C) 2001-2011 The eXist Project
+ * Copyright (C) 2001-2014 The eXist Project
* http://exist-db.org
*
* This program is free software; you can redistribute it and/or
@@ -16,12 +16,10 @@
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * $Id$
*/
package org.exist.collections.triggers;
-import static junit.framework.Assert.assertEquals;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

0 comments on commit 0cb25ab

Please sign in to comment.