/*******************************************************************************
* 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 junit.framework.TestCase;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.XMLResourceFactoryImpl;
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.Writer;
import ca.ecliptical.emf.xpath.EMFDOMXPath;
public class EMFDOMXPathTest extends TestCase {
protected Library fixture;
public EMFDOMXPathTest(String name) {
super(name);
}
protected void setUp() throws Exception {
super.setUp();
ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
.put("library", new XMLResourceFactoryImpl());
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);
}
public final void testSelectChildWithPredicate() throws Exception {
EMFDOMXPath xpath = new EMFDOMXPath("books[@title='Book 1']");
Book book = (Book) xpath.selectSingleNode(fixture);
assertNotNull(book);
assertEquals("Book 1", book.getTitle());
}
public final void testStringValueOf() throws Exception {
EMFDOMXPath xpath = new EMFDOMXPath("writers[last()]/@name");
String result = xpath.stringValueOf(fixture);
assertEquals("Writer 2", result);
}
public final void testSum() throws Exception {
EMFDOMXPath xpath = new EMFDOMXPath("sum(books/@pages)");
Number result = xpath.numberValueOf(fixture);
assertNotNull(result);
assertEquals(6000, result.intValue());
}
}