Package ca.ecliptical.emf.xpath.tests

Source Code of ca.ecliptical.emf.xpath.tests.EMFXPathTest

/*******************************************************************************
* Copyright (c) 2007 Ecliptical Software Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*     Ecliptical Software Inc. - initial API and implementation
*******************************************************************************/
package ca.ecliptical.emf.xpath.tests;

import java.util.HashSet;
import java.util.List;

import junit.framework.TestCase;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceFactoryImpl;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.util.ExtendedMetaData;
import org.eclipse.emf.ecore.util.FeatureMap;
import org.eclipse.emf.ecore.xml.type.AnyType;
import org.eclipse.emf.ecore.xml.type.SimpleAnyType;
import org.eclipse.emf.ecore.xml.type.XMLTypePackage;
import org.eclipse.example.library.Book;
import org.eclipse.example.library.BookCategory;
import org.eclipse.example.library.Library;
import org.eclipse.example.library.LibraryFactory;
import org.eclipse.example.library.LibraryPackage;
import org.eclipse.example.library.Writer;

import ca.ecliptical.emf.xpath.EMFXPath;

public class EMFXPathTest extends TestCase {

  protected Library fixture;

  public EMFXPathTest(String name) {
    super(name);
  }

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

    ResourceSet resourceSet = new ResourceSetImpl();
    resourceSet.getResourceFactoryRegistry()
        .getExtensionToFactoryMap()
        .put("library", new ResourceFactoryImpl());
    Resource resource = resourceSet.createResource(URI.createURI("test.library"));

    // create a library with three books and two writers
    fixture = LibraryFactory.eINSTANCE.createLibrary();
    fixture.setName("Test Library");
    resource.getContents().add(fixture);

    Book book1 = LibraryFactory.eINSTANCE.createBook();
    book1.setTitle("Book 1");
    book1.setPages(1000);
    book1.setCategory(BookCategory.MYSTERY_LITERAL);
    fixture.getBooks().add(book1);

    Book book2 = LibraryFactory.eINSTANCE.createBook();
    book2.setTitle("Book 2");
    book2.setPages(2000);
    book2.setCategory(BookCategory.SCIENCE_FICTION_LITERAL);
    fixture.getBooks().add(book2);

    Book book3 = LibraryFactory.eINSTANCE.createBook();
    book3.setTitle("Book 3");
    book3.setPages(3000);
    book3.setCategory(BookCategory.BIOGRAPHY_LITERAL);
    fixture.getBooks().add(book3);

    Writer writer1 = LibraryFactory.eINSTANCE.createWriter();
    writer1.setName("Writer 1");
    writer1.getBooks().add(book1);
    fixture.getWriters().add(writer1);

    Writer writer2 = LibraryFactory.eINSTANCE.createWriter();
    writer2.setName("Writer 2");
    writer2.getBooks().add(book2);
    writer2.getBooks().add(book3);
    fixture.getWriters().add(writer2);
   
    // add some anonymous content to the resource
    // e.g., an XML document consisting of
    // <test xmlns="urn:test">
    //   <subTest>subTest1</subTest>
    //   <subTest>2</subTest>
    // </test>
    EPackage testPackage = ExtendedMetaData.INSTANCE.demandPackage("urn:test");
    EClass docEClass = ExtendedMetaData.INSTANCE.getDocumentRoot(testPackage);
    EObject doc = EcoreUtil.create(docEClass);
    resource.getContents().add(doc);
   
    EStructuralFeature docMixedFeature = ExtendedMetaData.INSTANCE.getMixedFeature(docEClass);
    FeatureMap docMixed = (FeatureMap) doc.eGet(docMixedFeature);
    AnyType test = (AnyType) EcoreUtil.create(XMLTypePackage.Literals.ANY_TYPE);
    docMixed.add(ExtendedMetaData.INSTANCE.demandFeature("urn:test", "test", true), test);
    SimpleAnyType subTest1 = (SimpleAnyType) EcoreUtil.create(XMLTypePackage.Literals.SIMPLE_ANY_TYPE);
    test.getMixed().add(ExtendedMetaData.INSTANCE.demandFeature("urn:test", "subTest", true), subTest1);
    subTest1.setInstanceType(XMLTypePackage.Literals.STRING);
    subTest1.setValue("subTest1");
    SimpleAnyType subTest2 = (SimpleAnyType) EcoreUtil.create(XMLTypePackage.Literals.SIMPLE_ANY_TYPE);
    test.getMixed().add(ExtendedMetaData.INSTANCE.demandFeature("urn:test", "subTest", true), subTest2);
    subTest2.setInstanceType(XMLTypePackage.Literals.INT);
    subTest2.setValue(new Integer(2));
  }

  public final void testSelectChildWithPredicate() throws Exception {
    EMFXPath xpath = new EMFXPath("lib:books[lib:title='Book 1']");
    xpath.addNamespace("lib", LibraryPackage.eNS_URI);
    Book book = (Book) xpath.selectSingleNode(fixture);
    assertNotNull(book);
    assertEquals("Book 1", book.getTitle());
  }

  public final void testNonContainmentRef() throws Exception {
    EMFXPath xpath = new EMFXPath("lib:books[lib:author=current()/lib:writers[lib:name='Writer 1']]");
    xpath.addNamespace("lib", LibraryPackage.eNS_URI);
    Book book = (Book) xpath.selectSingleNode(fixture);
    assertNotNull(book);
    assertEquals("Book 1", book.getTitle());
  }
 
  public final void testStringValueOf() throws Exception {
    EMFXPath xpath = new EMFXPath("lib:writers[lib:books[.=//lib:books[lib:category='Mystery']]]/lib:name");
    xpath.addNamespace("lib", LibraryPackage.eNS_URI);
    String result = xpath.stringValueOf(fixture);
    assertEquals("Writer 1", result);
  }
 
  public final void testSum() throws Exception {
    EMFXPath xpath = new EMFXPath("sum(lib:books/lib:pages)");
    xpath.addNamespace("lib", LibraryPackage.eNS_URI);
    Number result = xpath.numberValueOf(fixture);
    assertNotNull(result);
    assertEquals(6000, result.intValue());
  }
 
  public final void testFeatureMap() throws Exception {
    EMFXPath xpath = new EMFXPath("/resources/contents/t:test/t:subTest[x:value='2']");
    xpath.addNamespace("t", "urn:test");
    xpath.addNamespace("x", XMLTypePackage.eNS_URI);
    SimpleAnyType result = (SimpleAnyType) xpath.selectSingleNode(fixture);
    assertNotNull(result);
    assertEquals(new Integer(2), result.getValue());
  }
 
  public final void testNamespaces() throws Exception {
    EMFXPath xpath = new EMFXPath("/resources/contents/t:test/t:subTest[1]/x:value/namespace::*");
    xpath.addNamespace("t", "urn:test");
    xpath.addNamespace("x", XMLTypePackage.eNS_URI);
    List list = xpath.selectNodes(fixture);
    assertNotNull(list);
    assertEquals(2, list.size());
    assertTrue(list.get(0) instanceof EPackage);
    assertTrue(list.get(1) instanceof EPackage);
    HashSet values = new HashSet(2);
    values.add(((EPackage) list.get(0)).getNsURI());
    values.add(((EPackage) list.get(1)).getNsURI());
    assertTrue(values.contains("urn:test"));
    assertTrue(values.contains(XMLTypePackage.eNS_URI));
  }

  public final void testContentsFunction() throws Exception {
    EMFXPath xpath = new EMFXPath("lib:books[contents(lib:author)/lib:name='Writer 1']");
    xpath.addNamespace("lib", LibraryPackage.eNS_URI);
    Book book = (Book) xpath.selectSingleNode(fixture);
    assertNotNull(book);
    assertEquals("Book 1", book.getTitle());
  }

  public final void testEClassFunction() throws Exception {
    EMFXPath xpath = new EMFXPath("eClass()/ecore:name");
    xpath.addNamespace("ecore", EcorePackage.eNS_URI);
    String eClassName = xpath.stringValueOf(fixture);
    assertEquals("Library", eClassName);
  }
 
  public static void main(String[] args) throws Exception {
    EMFXPathTest test = new EMFXPathTest(null);
    test.setUp();
    EMFXPath.dump(test.fixture.eResource().getResourceSet(), System.out);
  }
}
TOP

Related Classes of ca.ecliptical.emf.xpath.tests.EMFXPathTest

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.