Package test.xmldb.levelzero

Source Code of test.xmldb.levelzero.StringTest

// You can redistribute this software and/or modify it under the terms of
// the Ozone Library License version 1 published by ozone-db.org.
//
// The original code and portions created by SMB are
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
//
package test.xmldb.levelzero;

import junit.framework.*;
import test.xmldb.*;

import java.io.BufferedReader;
import java.io.FileReader;

import org.xml.sax.InputSource;
import org.apache.xerces.parsers.DOMParser;

import org.xmldb.api.modules.XMLResource;
/**
* @author  Per Nyfelt
*/
public class StringTest extends XMLDBTestCase implements LevelZeroTestConstants {

    /** Creates new StringTest */
    public StringTest(String name) {
        super(name);
    }

    public static Test suite() {
        return new TestSuite(StringTest.class);
    }

   /**
     * test all scenarios for using XML as text (String)
     */
    public void testString() {
        try {
            System.out.println("\nLevelZeroTest.testString() - started\n");

            // read in the text file so we have something to compare with
            BufferedReader in = new BufferedReader(new FileReader(xmlFileName));
            InputSource source = new InputSource(in);
            DOMParser parser = new DOMParser();
            parser.parse(source);
            String xmlString = toString(parser.getDocument()).trim();
            XMLResource res = insertStringDocument(id, super.toString(document));

            String result = retrieveTextDocument(id).trim();

            super.assertNotNull("LevelZeroTest.testString() - result", result);
            super.assertEquals("LevelZeroTest.testString() - length", xmlString.length(), result.length());

            // this fails right now since there is no clear method cleaning out previous
            // content and append is the standard behavious for XMLContainer
            System.out.println("StringTest.testString() - updateStringDocument uncommented");
            // updateStringDocument(id);

        } catch (Exception e) {
            e.printStackTrace();
            fail( e.getMessage( ) );
        }
   }

   private XMLResource insertStringDocument(String id, String document) throws Exception {
        XMLResource res = (XMLResource) col.createResource(id, XMLResource.RESOURCE_TYPE);
        super.assertEquals("LevelZeroTest.testString() - id",res.getId(), id);
        super.assertSame(res.getParentCollection(),col);
        res.setContent(document);
        col.storeResource(res);
        return res;
   }

   private String retrieveTextDocument(String id) throws Exception {
        XMLResource resource = (XMLResource) col.getResource(id);
        return (String)resource.getContent();
    }

   private void updateStringDocument(String id) throws Exception {
        XMLResource resource = (XMLResource) col.getResource(id);
        String document = (String)resource.getContent();

        //change the XML content
        document = document.toLowerCase();

        resource.setContent(document);
        col.storeResource(resource);
   }
}
TOP

Related Classes of test.xmldb.levelzero.StringTest

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.