/*******************************************************************************
 * Copyright (c) 2008 itemis AG (http://www.itemis.eu) 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
 *
 *******************************************************************************/
package org.eclipse.xtext.index;

import java.util.Iterator;

import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.emfindex.EObjectDescriptor;
import org.eclipse.emf.emfindex.ResourceDescriptor;
import org.eclipse.emf.emfindex.query.QueryCommand;
import org.eclipse.emf.emfindex.query.QueryExecutor;
import org.eclipse.emf.emfindex.query.QueryResult;
import org.eclipse.emf.emfindex.query.ResourceDescriptorQuery;
import org.eclipse.xtext.index.indexTestLanguage.Namespace;
import org.eclipse.xtext.scoping.impl.AbstractIndexBasedTest;
import org.eclipse.xtext.util.StringInputStream;

/**
 * @author Sven Efftinge - Initial contribution and API
 * 
 */
public class DefaultDeclarativeResourceIndexerTest extends AbstractIndexBasedTest {

	@Override
	public void setUp() throws Exception {
		super.setUp();
		with(new IndexTestLanguageStandaloneSetup());
	}

	public void testSimple() throws Exception {
		final Resource r = getResource(new StringInputStream(
				  "foo.bar { " 
				+ "  entity Person {  " 
				+ "    String name "
				+ "  } " 
				+ "  datatype String " 
				+ "}"));

		indexResource(r);

		index.executeQueryCommand(new QueryCommand<Object>() {

			public Object execute(QueryExecutor queryExecutor) {
				ResourceDescriptorQuery query = new ResourceDescriptorQuery();
				query.setURI(normalizedURI(r));
				QueryResult<ResourceDescriptor> result = queryExecutor.execute(query);
				ResourceDescriptor resourceDescriptor = result.iterator().next();
				Iterator<EObjectDescriptor> descriptors = resourceDescriptor.getEObjectDescriptors().iterator();

				EObjectDescriptor namespace = descriptors.next();
				assertEquals("foo.bar", namespace.getName());

				descriptors = namespace.getEObjectDescriptors().iterator();

				EObjectDescriptor person = descriptors.next();
				assertEquals("foo.bar.Person", person.getName());
				assertEquals("foo.bar.Person.name", person.getEObjectDescriptors().get(0).getName());
				assertEquals("foo.bar.String", descriptors.next().getName());

				return null;
			}
		});

	}

	public void testSurpressIndexingForNamespaces() throws Exception {
		indexer = new DefaultDeclarativeResourceIndexer() {
			@SuppressWarnings("unused")
			protected boolean isIndex(Namespace ns) {
				return false;
			}
		};

		final Resource r = getResource(new StringInputStream(
				  "foo.bar { " 
				+ "  entity Person {  " 
				+ "    String name "
				+ "  } " 
				+ "  datatype String " 
				+ "}"));

		indexResource(r);

		index.executeQueryCommand(new QueryCommand<Object>() {

			public Object execute(QueryExecutor queryExecutor) {
				ResourceDescriptorQuery query = new ResourceDescriptorQuery();
				query.setURI(normalizedURI(r));
				QueryResult<ResourceDescriptor> result = queryExecutor.execute(query);
				ResourceDescriptor resourceDescriptor = result.iterator().next();
				Iterator<EObjectDescriptor> descriptors = resourceDescriptor.getEObjectDescriptors().iterator();

				//Indexing of Namespace is omitted by indexer (see above) 
//				EObjectDescriptor namespace = descriptors.next();
//				assertEquals("foo.bar", namespace.getName());
//
//				descriptors = namespace.getEObjectDescriptors().iterator();

				EObjectDescriptor person = descriptors.next();
				assertEquals("foo.bar.Person", person.getName());
				assertEquals("foo.bar.Person.name", person.getEObjectDescriptors().get(0).getName());
				assertEquals("foo.bar.String", descriptors.next().getName());

				return null;
			}
		});
	}
}

@@ -2166,24 +2166,26 @@
 etName());%0A%0A
+//
 %09%09%09%09descript
@@ -2382,34 +2382,17 @@
 .name%22, 
-person.getEObjectD
+d
 escripto
@@ -2389,32 +2389,30 @@
  descriptors
-().ge
+.nex
 t(
-0
 ).getName())
@@ -3801,26 +3801,9 @@
 e%22, 
-person.getEObjectD
+d
 escr
@@ -3812,16 +3812,14 @@
 tors
-().ge
+.nex
 t(
-0
 ).ge

Refactor test to use refactored structure of descriptors