Package org.apache.xindice.integration.client.services

Source Code of org.apache.xindice.integration.client.services.MetaTest

/*
* 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: MetaTest.java,v 1.4 2004/02/08 03:57:02 vgritsenko Exp $
*/

package org.apache.xindice.integration.client.services;

import org.apache.xindice.client.xmldb.services.MetaService;
import org.apache.xindice.core.meta.MetaData;
import org.apache.xindice.integration.client.AbstractXmlDbClientTest;

import org.xmldb.api.base.Collection;

/**
* Test MetaService
*
* @version CVS $Revision: 1.4 $, $Date: 2004/02/08 03:57:02 $
* @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
*/
public class MetaTest extends AbstractXmlDbClientTest {

    static final String DOCUMENT_ID1 = "doc1";
    static final String DOCUMENT_ID2 = "doc2";

    static final String DOCUMENT1 =
            "<?xml version=\"1.0\"?>" +
            "<person>" +
            "  <first>John</first>" +
            "  <last>Smith</last>" +
            "  <age>30</age>" +
            "  <phone type=\"work\">555-345-6789</phone>" +
            "</person>";

    static final String DOCUMENT2 =
            "<?xml version=\"1.0\"?>" +
            "<person>" +
            "  <first>John</first>" +
            "  <last>Smith</last>" +
            "  <age>31</age>" +
            "  <phone type=\"work\">555-345-6789</phone>" +
            "</person>";

    static final String ATTRIBUTE_ID = "MetaDataAttribute";

    public void setUp() throws Exception {
        super.setUp();

        this.client.insertDocument(TEST_COLLECTION_PATH, DOCUMENT_ID1, DOCUMENT1);
    }

    public void tearDown() throws Exception {
        this.client.removeDocument(TEST_COLLECTION_PATH, DOCUMENT_ID1);

        super.tearDown();
    }

    public void testMetaServicePresent() throws Exception {
        // Get meta service
        Collection col = this.client.getCollection(TEST_COLLECTION_PATH);
        MetaService service = (MetaService) col.getService("MetaService", "1.0");
        assertNotNull("MetaService is not found!", service);
    }

    public void testGetCollectionMetaData() throws Exception {
        // Get meta service
        Collection col = this.client.getCollection(TEST_COLLECTION_PATH);
        MetaService service = (MetaService) col.getService("MetaService", "1.0");

        // Get meta data
        MetaData data = service.getMetaData();
        assertNotNull("MetaData is not found for the collection!", data);
        assertTrue("Creation time is 0!", data.getCreatedTime() != 0);
        assertTrue("Modification time is 0!", data.getLastModifiedTime() != 0);
        long created0 = data.getCreatedTime();
        long modified0 = data.getLastModifiedTime();

        // Sleep and get it again
        Thread.sleep(150);
        data = service.getMetaData();
        long created1 = data.getCreatedTime();
        long modified1 = data.getLastModifiedTime();
        assertEquals("Creation time changed on 2nd get (diff: "+ (created1 - created0) +")!", created0, created1);
        assertEquals("Modification time changed on 2nd get (diff: "+ (modified1 - modified0) +")!", modified0, modified1);

        // Sleep, add new document, and get metadata again
        Thread.sleep(150);
        this.client.insertDocument(TEST_COLLECTION_PATH, DOCUMENT_ID2, DOCUMENT2);

        data = service.getMetaData();
        assertNotNull("MetaData is not found for the collection after update!", data);
        long created2 = data.getCreatedTime();
        long modified2 = data.getLastModifiedTime();
        assertEquals("Creation time changed on update (diff: "+ (created2 - created1) +")!", created1, created2);
        assertTrue("Modification time has not changed on update!", modified1 != modified2);
    }

    public void testGetDocumentMetaData() throws Exception {
        // Get meta service
        Collection col = this.client.getCollection(TEST_COLLECTION_PATH);
        MetaService service = (MetaService) col.getService("MetaService", "1.0");

        // Get meta data
        MetaData data = service.getMetaData(DOCUMENT_ID1);
        assertNotNull("MetaData is not found for document " + DOCUMENT_ID1 + "!", data);
        assertTrue("Creation time is 0!", data.getCreatedTime() != 0);
        assertTrue("Modification time is 0!", data.getLastModifiedTime() != 0);
        long created0 = data.getCreatedTime();
        long modified0 = data.getLastModifiedTime();

        // Sleep and get it again
        Thread.sleep(150);
        data = service.getMetaData(DOCUMENT_ID1);
        long created1 = data.getCreatedTime();
        long modified1 = data.getLastModifiedTime();
        assertEquals("Creation time changed on 2nd get (diff: "+ (created1 - created0) +")!", created0, created1);
        assertEquals("Modification time changed on 2nd get (diff: "+ (modified1 - modified0) +")!", modified0, modified1);

        // Sleep and update the document and get data again
        Thread.sleep(150);
        this.client.updateDocument(TEST_COLLECTION_PATH, DOCUMENT_ID1, DOCUMENT2);

        data = service.getMetaData(DOCUMENT_ID1);
        assertNotNull("MetaData is not found for document " + DOCUMENT_ID1 + " after update!", data);
        long created2 = data.getCreatedTime();
        long modified2 = data.getLastModifiedTime();
        assertEquals("Creation time changed on update (diff: "+ (created2 - created1) +")!", created1, created2);
        assertTrue("Modification time has not changed on update!", modified1 != modified2);
    }

    public void testSetCollectionMetaData() throws Exception {
        // Get meta service
        Collection col = this.client.getCollection(TEST_COLLECTION_PATH);
        MetaService service = (MetaService) col.getService("MetaService", "1.0");

        // Get meta data
        MetaData data = service.getMetaData();
        long created0 = data.getCreatedTime();
        long modified0 = data.getLastModifiedTime();

        // Sleep and set metadata
        Thread.sleep(150);
        data.setContext(created0 - 500, modified0 - 500);
        service.setMetaData(data);

        // Get metadata again
        data = service.getMetaData();
        long created1 = data.getCreatedTime();
        long modified1 = data.getLastModifiedTime();
        assertEquals("Creation time changed on set (diff: "+ (created1 - created0) +")!", created0, created1);
        assertEquals("Modification time changed on set (diff: "+ (modified1 - modified0) +")!", modified0, modified1);
    }

    public void testSetDocumentMetaData() throws Exception {
        // Get meta service
        Collection col = this.client.getCollection(TEST_COLLECTION_PATH);
        MetaService service = (MetaService) col.getService("MetaService", "1.0");

        // Get meta data
        MetaData data = service.getMetaData(DOCUMENT_ID1);
        long created0 = data.getCreatedTime();
        long modified0 = data.getLastModifiedTime();

        // Sleep and set metadata
        Thread.sleep(150);
        data.setContext(created0 - 500, modified0 - 500);
        service.setMetaData(DOCUMENT_ID1, data);

        // Get metadata again
        data = service.getMetaData(DOCUMENT_ID1);
        long created1 = data.getCreatedTime();
        long modified1 = data.getLastModifiedTime();
        assertEquals("Creation time changed on set (diff: "+ (created1 - created0) +")!", created0, created1);
        assertEquals("Modification time changed on set (diff: "+ (modified1 - modified0) +")!", modified0, modified1);
    }

    public void testSetCollectionMetaDataProperty() throws Exception {
        // Get meta service
        Collection col = this.client.getCollection(TEST_COLLECTION_PATH);
        MetaService service = (MetaService) col.getService("MetaService", "1.0");

        // Get meta data
        String value0 = "MetaAttributeValue" + System.currentTimeMillis();
        MetaData data = service.getMetaData();
        data.setAttribute(ATTRIBUTE_ID, value0);
        service.setMetaData(data);

        // Get metadata again
        data = service.getMetaData();
        String value1 = (String)data.getAttribute(ATTRIBUTE_ID);
        assertEquals("Attribute value does not match!", value0, value1);
    }

    public void testSetDocumentMetaDataProperty() throws Exception {
        // Get meta service
        Collection col = this.client.getCollection(TEST_COLLECTION_PATH);
        MetaService service = (MetaService) col.getService("MetaService", "1.0");

        // Get meta data
        String value0 = "MetaAttributeValue" + System.currentTimeMillis();
        MetaData data = service.getMetaData(DOCUMENT_ID1);
        data.setAttribute(ATTRIBUTE_ID, value0);
        service.setMetaData(DOCUMENT_ID1, data);

        // Get metadata again
        data = service.getMetaData(DOCUMENT_ID1);
        String value1 = (String)data.getAttribute(ATTRIBUTE_ID);
        assertEquals("Attribute value does not match!", value0, value1);
    }
}
TOP

Related Classes of org.apache.xindice.integration.client.services.MetaTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.