/*
* Copyright 1999-2004 The Apache Software Foundation.
*
* Licensed 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.
*
* CVS $Id: XUpdateQueryTest.java,v 1.6 2004/02/08 03:57:02 vgritsenko Exp $
*/
package org.apache.xindice.integration.client.services;
import org.apache.xindice.integration.client.AbstractXmlDbClientTest;
import org.xmldb.api.modules.XUpdateQueryService;
import org.xmldb.api.base.Collection;
/**
* @version CVS $Revision: 1.6 $, $Date: 2004/02/08 03:57:02 $
* @author Vladimir R. Bossicard <vladimir@apache.org>
*/
public class XUpdateQueryTest
extends AbstractXmlDbClientTest {
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\"?>\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\"?>\n" +
"<person status=\"divorced\">" +
"<first>Ben</first>" +
"<last>Smith</last>" +
"<phone type=\"work\">480-300-3003</phone>" +
"</person>";
String updatedDocument2 = "<?xml version=\"1.0\"?>\n" +
"<person status=\"divorced\">" +
"<first>Ben</first>" +
"<last>Benton</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);
assertEquals(6, count);
String doc = this.client.getDocument(TEST_COLLECTION_PATH, "doc1");
assertNotNull(doc);
assertEquals(updatedDocument1, doc);
doc = this.client.getDocument(TEST_COLLECTION_PATH, "doc2");
assertNotNull(doc);
assertEquals(updatedDocument2, doc);
}
}