/*
 * Copyright 2012 MarkLogic Corporation
 *
 * 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.
 */
package com.marklogic.client.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.marklogic.client.EditableNamespaceContext;
import com.marklogic.client.NamespacesManager;

public class NamespacesManagerTest {
	@BeforeClass
	public static void beforeClass() {
		Common.connectAdmin();
	}
	@AfterClass
	public static void afterClass() {
		Common.release();
	}

	@Test
	public void testWriteReadPrefix() {
		NamespacesManager nsMgr = Common.client.newNamespacesManager();
		
		nsMgr.addPrefix("dc", "http://purl.org/dc/terms/");

		String nsUri = nsMgr.readPrefix("dc");
		assertEquals("Could not read namespace", nsUri, "http://purl.org/dc/terms/");

		nsMgr.addPrefix("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
		nsMgr.addPrefix("skos", "http://www.w3.org/2004/02/skos/core#");

		EditableNamespaceContext context = nsMgr.readAll();
		assertEquals("Failed to retrieve three namespaces", 3, context.size());
		assertEquals("Did not retrieve RDF namespace", 
				"http://purl.org/dc/terms/",
				context.get("dc"));
		assertEquals("Did not retrieve RDF namespace", 
				"http://www.w3.org/1999/02/22-rdf-syntax-ns#",
				context.get("rdf"));
		assertEquals("Did not retrieve SKOS namespace", 
				"http://www.w3.org/2004/02/skos/core#",
				context.get("skos"));

		nsMgr.updatePrefix("dc", "http://diverted/category/");

		nsUri = nsMgr.readPrefix("dc");
		assertEquals("Could not read namespace", nsUri, "http://diverted/category/");

		nsMgr.deletePrefix("dc");
		context = nsMgr.readAll();
		assertEquals("Failed to delete namespace", 2, context.size());

		nsMgr.deleteAll();
		context = nsMgr.readAll();
		assertTrue("Failed to delete all namespaces",
				context == null || context.size() == 0);
	}
}

@@ -1570,32 +1570,69 @@
 nsMgr.readAll();
+%0A%0A%09%09int initialSize = context.size();
 %0A%09%09assertEquals(
@@ -1616,38 +1616,36 @@
 size();%0A%09%09assert
-Equals
+True
 (%22Failed to retr
@@ -1672,25 +1672,24 @@
 s%22, 
-3, context.size()
+initialSize %3E= 3
 );%0A%09
@@ -2264,32 +2264,67 @@
 sMgr.readAll();%0A
+%09%09// assumes no concurrent deletes%0A
 %09%09assertEquals(%22
@@ -2352,17 +2352,31 @@
 space%22, 
-2
+initialSize - 1
 , contex

allow for pre-existing namespaces