/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $Id: XUpdateQueryTest.java 512591 2007-02-28 03:35:44Z vgritsenko $
*/
package org.apache.xindice.integration.client.services;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xindice.integration.client.AbstractXmlDbClientTest;
import org.custommonkey.xmlunit.XMLAssert;
import org.xmldb.api.base.Collection;
import org.xmldb.api.modules.XUpdateQueryService;
/**
* @version $Revision: 512591 $, $Date: 2007-02-27 22:35:44 -0500 (Tue, 27 Feb 2007) $
* @author Vladimir R. Bossicard <vladimir@apache.org>
*/
public class XUpdateQueryTest
extends AbstractXmlDbClientTest {
private static final Log log = LogFactory.getLog(XUpdateQueryTest.class);
public void setUp() throws Exception {
super.setUp();
String document1 = "<?xml version=\"1.0\"?>" +
"<person status=\"single\">" +
"<first>John</first>" +
"<last>Smith</last>" +
"<phone type=\"work\">555-345-6789</phone>" +
"</person>";
String document2 = "<?xml version=\"1.0\"?>" +
"<person status=\"married\">" +
"<first>Sally</first>" +
"<last>Benton</last>" +
"<phone type=\"work\">555-345-6789</phone>" +
"</person>";
this.client.insertDocument(TEST_COLLECTION_PATH, "doc1", document1);
this.client.insertDocument(TEST_COLLECTION_PATH, "doc2", document2);
}
public void tearDown() throws Exception {
this.client.removeDocument(TEST_COLLECTION_PATH, "doc1");
this.client.removeDocument(TEST_COLLECTION_PATH, "doc2");
super.tearDown();
}
public void testUpdateDocument() throws Exception {
String query =
"<xupdate:modifications version=\"1.0\" xmlns:xupdate=\"http://www.xmldb.org/xupdate\">" +
" <xupdate:update select=\"/person/@status\">married</xupdate:update>" +
" <xupdate:update select=\"/person/first\">Ben</xupdate:update>" +
" <xupdate:update select=\"/person/phone[@type = 'work']\">480-300-3003</xupdate:update>" +
"</xupdate:modifications>";
String updatedDocument = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<person status=\"married\">" +
"<first>Ben</first>" +
"<last>Smith</last>" +
"<phone type=\"work\">480-300-3003</phone>" +
"</person>";
Collection col = this.client.getCollection(TEST_COLLECTION_PATH);
XUpdateQueryService service = (XUpdateQueryService) col.getService("XUpdateQueryService", "1.0");
long count = service.updateResource("doc1", query);
assertEquals(1, count);
String doc = this.client.getDocument(TEST_COLLECTION_PATH, "doc1");
assertNotNull(doc);
assertEquals(updatedDocument, doc);
}
public void testUpdateCollection() throws Exception {
String query =
"<xupdate:modifications version=\"1.0\" xmlns:xupdate=\"http://www.xmldb.org/xupdate\">" +
" <xupdate:update select=\"/person/@status\">divorced</xupdate:update>" +
" <xupdate:update select=\"/person/first\">Ben</xupdate:update>" +
" <xupdate:update select=\"/person/phone[@type = 'work']\">480-300-3003</xupdate:update>" +
"</xupdate:modifications>";
String updatedDocument1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<person status=\"divorced\">" +
"<first>Ben</first>" +
"<last>Smith</last>" +
"<phone type=\"work\">480-300-3003</phone>" +
"</person>";
String updatedDocument2 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<person status=\"divorced\">" +
"<first>Ben</first>" +
"<last>Benton</last>" +
"<phone type=\"work\">480-300-3003</phone>" +
"</person>";
String query2 =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<xupdate:modifications version=\"1.0\" xmlns:xupdate=\"http://www.xmldb.org/xupdate\">" +
"<xupdate:append select=\"/person/first\">" +
"<xupdate:element name=\"test2\"/>" +
"</xupdate:append>" +
"<xupdate:append select=\"/person/last\">" +
"<xupdate:element name=\"test1\"/>" +
"</xupdate:append>" +
"</xupdate:modifications>";
String updatedDocument3 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<person status=\"divorced\">" +
"<first>Ben" +
"<test2 />" +
"</first>" +
"<last>Benton"+
"<test1 />" +
"</last>" +
"<phone type=\"work\">480-300-3003</phone>" +
"</person>";
Collection col = this.client.getCollection(TEST_COLLECTION_PATH);
XUpdateQueryService service = (XUpdateQueryService) col.getService("XUpdateQueryService", "1.0");
long count = service.update(query);
// Bug #30878 fixed: was 6 should be 2. The old code would cause multiple updates.
assertEquals(2, count);
String doc = this.client.getDocument(TEST_COLLECTION_PATH, "doc1");
assertNotNull(doc);
XMLAssert.assertXMLEqual(updatedDocument1, doc);
doc = this.client.getDocument(TEST_COLLECTION_PATH, "doc2");
assertNotNull(doc);
XMLAssert.assertXMLEqual(updatedDocument2, doc);
// test the second query should update 2 docs
count = service.update(query2);
assertEquals(2,count);
// check the second doc to make sure its correct
doc = this.client.getDocument(TEST_COLLECTION_PATH, "doc2");
log.debug("Doc2:=\n " + doc);
log.debug("UpdatedDoc3:\n " + updatedDocument3);
XMLAssert.assertXMLEqual(updatedDocument3,doc);
}
}