package org.exist.xquery.test;

import org.exist.storage.DBBroker;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.Database;
import org.xmldb.api.base.ResourceSet;
import org.xmldb.api.base.XMLDBException;
import org.xmldb.api.modules.CollectionManagementService;
import org.xmldb.api.modules.XMLResource;
import org.xmldb.api.modules.XQueryService;

import junit.framework.TestCase;

public class NamespaceUpdateTest extends TestCase {

	private final static String URI = "xmldb:exist://" + DBBroker.ROOT_COLLECTION;
	
	private final static String namespaces =
		"<test xmlns='http://www.foo.com'>"
			+ "<section>"
			+ "<title>Test Document</title>"
			+ "<c:comment xmlns:c='http://www.other.com'>This is my comment</c:comment>"
			+ "</section>"
			+ "</test>";

	private Collection testCollection;
	
	public void testUpdateAttribute() {
		try {
			XQueryService service = (XQueryService) testCollection.getService("XQueryService", "1.0");
			String query =
				"declare namespace t='http://www.foo.com';\n" +
				"<test xmlns='http://www.foo.com'>\n" +
				"{\n" +
				"	update insert attribute { 'ID' } { 'myid' } into /t:test\n" +
				"}\n" +
				"</test>";
			service.query(query);
			
			query =
				"declare namespace t='http://www.foo.com';\n" +
				"/t:test/@ID";
			ResourceSet result = service.query(query);
			assertEquals(1, result.getSize());
			assertEquals("myid", result.getResource(0).getContent().toString());
		} catch (Exception e) {
			fail(e.getMessage());
		}
	}
	
	public void testUpdateElement() {
		try {
			XQueryService service = (XQueryService) testCollection.getService("XQueryService", "1.0");
			String query =
				"declare namespace t='http://www.foo.com';\n" +
				"<test xmlns='http://www.foo.com'>\n" +
				"{\n" +
				"	let $n := element { 'para' } { () } return $n\n" +
				"}\n" +
				"</test>";
			ResourceSet result = service.query(query);
			 assertEquals(1, result.getSize());
			System.out.println(result.getResource(0).getContent());
		} catch (Exception e) {
			fail(e.getMessage());
		}
	}
	
	protected void setUp() {
		try {
			// initialize driver
			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(
					URI,
					"admin",
					null);
			CollectionManagementService service =
				(CollectionManagementService) root.getService(
					"CollectionManagementService",
					"1.0");
			testCollection = service.createCollection("test");
			assertNotNull(testCollection);
			
			XMLResource doc =
				(XMLResource) root.createResource("namespace-updates.xml", "XMLResource");
			doc.setContent(namespaces);
			testCollection.storeResource(doc);
		} catch (ClassNotFoundException e) {
		} catch (InstantiationException e) {
		} catch (IllegalAccessException e) {
		} catch (XMLDBException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		junit.textui.TestRunner.run(NamespaceUpdateTest.class);
	}
}

@@ -1544,559 +1544,8 @@
 %7D%0A%09%0A
-%09public void testUpdateElement() %7B%0A%09%09try %7B%0A%09%09%09XQueryService service = (XQueryService) testCollection.getService(%22XQueryService%22, %221.0%22);%0A%09%09%09String query =%0A%09%09%09%09%22declare namespace t='http://www.foo.com';%5Cn%22 +%0A%09%09%09%09%22%3Ctest xmlns='http://www.foo.com'%3E%5Cn%22 +%0A%09%09%09%09%22%7B%5Cn%22 +%0A%09%09%09%09%22%09let $n := element %7B 'para' %7D %7B () %7D return $n%5Cn%22 +%0A%09%09%09%09%22%7D%5Cn%22 +%0A%09%09%09%09%22%3C/test%3E%22;%0A%09%09%09ResourceSet result = service.query(query);%0A%09%09%09 assertEquals(1, result.getSize());%0A%09%09%09System.out.println(result.getResource(0).getContent());%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09fail(e.getMessage());%0A%09%09%7D%0A%09%7D%0A%09%0A
 %09pro

Test cases.