Package org.exist.xmldb

Source Code of org.exist.xmldb.RemoteQueryTest

/*
* eXist Open Source Native XML Database
* Copyright (C) 2006-2009 The eXist Project
* http://exist-db.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*  $Id$
*/
package org.exist.xmldb;

import java.io.File;

import org.exist.test.TestConstants;
import org.exist.util.MimeType;
import org.exist.xmlrpc.XmlRpcTest;
import org.w3c.dom.Node;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.CompiledExpression;
import org.xmldb.api.base.Database;
import org.xmldb.api.base.Resource;
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.textui.TestRunner;

public class RemoteQueryTest extends RemoteDBTest {
    // jetty.port.standalone
  private static String uri = "xmldb:exist://localhost:" + System.getProperty("jetty.port") + "/xmlrpc" + XmldbURI.ROOT_COLLECTION;
 
  private Collection testCollection;
  private Collection xmlrpcCollection;
 
  public RemoteQueryTest(String name) {
    super(name);
  }

  public void testResourceSet() {
    try {
      String query = "//SPEECH[SPEAKER = 'HAMLET']";
      XQueryService service = (XQueryService) testCollection.getService("XQueryService", "1.0");
      service.setProperty("highlight-matches", "none");
      CompiledExpression compiled = service.compile(query);
      ResourceSet result = service.execute(compiled);
     
      assertEquals(result.getSize(), 359);
     
      for (int i = 0; i < result.getSize(); i++) {
        XMLResource r = (XMLResource) result.getResource(i);
        Node node = r.getContentAsDOM().getFirstChild();
        System.out.println(node.getNodeName());
        System.out.println(r.getContent());
      }
    } catch (Exception e) {
      e.printStackTrace();
      fail(e.getMessage());
    }
  }
 
  public void testExternalVar() {
    try {
      String query = XmlRpcTest.QUERY_MODULE_DATA;
      XQueryService service = (XQueryService) testCollection.getService("XQueryService", "1.0");
      service.setProperty("highlight-matches", "none");

      service.setNamespace("tm", "http://exist-db.org/test/module");
      service.setNamespace("tm-query", "http://exist-db.org/test/module/query");
           
      service.declareVariable("tm:imported-external-string", "imported-string-value");
      service.declareVariable("tm-query:local-external-string", "local-string-value");
     
      CompiledExpression compiled = service.compile(query);
      ResourceSet result = service.execute(compiled);
     
      assertEquals(result.getSize(), 2);
     
      for (int i = 0; i < result.getSize(); i++) {
        XMLResource r = (XMLResource) result.getResource(i);
        System.out.println(r.getContent());
      }
    } catch (Exception e) {
      e.printStackTrace();
      fail(e.getMessage());
    }
  }
  protected void setUp() {
    if (uri.startsWith("xmldb:exist://localhost"))
      initServer();
    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);
     
      Resource xr = testCollection.createResource("hamlet.xml", "XMLResource");
            String existHome = System.getProperty("exist.home");
            File existDir = existHome == null ? new File(".") : new File(existHome);
      File f = new File(existDir,"samples/shakespeare/hamlet.xml");
      xr.setContent(f);
      testCollection.storeResource(xr);
     
      xmlrpcCollection = service.createCollection("xmlrpc");
      assertNotNull(xmlrpcCollection);
     
      Resource br = xmlrpcCollection.createResource(TestConstants.TEST_MODULE_URI.toString(), "BinaryResource");
      ((EXistResource) br).setMimeType(MimeType.XQUERY_TYPE.getName());
            br.setContent(XmlRpcTest.MODULE_DATA);
      xmlrpcCollection.storeResource(br);
     
    } catch (ClassNotFoundException e) {
    } catch (InstantiationException e) {
    } catch (IllegalAccessException e) {
    } catch (XMLDBException e) {
      e.printStackTrace();
    }
  }
 
  protected void tearDown() throws Exception {
    try {
      if (!((CollectionImpl) testCollection).isRemoteCollection()) {
        DatabaseInstanceManager dim =
          (DatabaseInstanceManager) testCollection.getService(
              "DatabaseInstanceManager", "1.0");
        dim.shutdown();
      }
      testCollection = null;
      xmlrpcCollection = null;
     
      System.out.println("tearDown PASSED");
     
    } catch (XMLDBException e) {
      e.printStackTrace();
      fail(e.getMessage());
    }
  }
 
  public static void main(String[] args) {
    TestRunner.run(RemoteQueryTest.class);
  }
}
TOP

Related Classes of org.exist.xmldb.RemoteQueryTest

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.