summaryrefslogtreecommitdiffstats
blob: 3f593ee931250b3bad4ccdcc15775aedfe80e534 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package org.eclipse.emf.ecore.xcore.tests.scoping;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.util.Iterator;

import org.eclipse.emf.codegen.ecore.genmodel.GenModelPackage;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.xcore.XcoreInjectorProvider;
import org.eclipse.emf.ecore.xcore.XcoreStandaloneSetup;
import org.eclipse.emf.ecore.xcore.resource.XcoreResource;
import org.eclipse.xtext.common.types.TypesPackage;
import org.eclipse.xtext.junit4.InjectWith;
import org.eclipse.xtext.junit4.XtextRunner;
import org.eclipse.xtext.resource.IEObjectDescription;
import org.eclipse.xtext.resource.IResourceDescription;
import org.eclipse.xtext.resource.IResourceDescription.Manager;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.resource.XtextResourceSet;
import org.eclipse.xtext.util.StringInputStream;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Provider;

@RunWith(XtextRunner.class)
@InjectWith(LazyGenmodelInferenceTest.MyXcoreInjectorProvider.class)
public class LazyGenmodelInferenceTest {

	@Inject
	private Provider<XtextResourceSet> resourceSetProvider;
	
	public static class MyXcoreInjectorProvider extends XcoreInjectorProvider {
		
		private Injector injector;
		
		@Override
		public Injector getInjector() {
			if (injector == null) {
				this.injector = new XcoreStandaloneSetup() {
					@Override
					public Injector createInjector() {
						return Guice.createInjector(new org.eclipse.emf.ecore.xcore.XcoreRuntimeModule() {
							public Class<? extends XtextResource> bindXtextResource() {
								return InspectableXcoreResource.class;
							}
						});
					}
				}.createInjectorAndDoEMFRegistration();
			}
			return injector;
		}
		
		@Override
		public void setupRegistry() {
			super.setupRegistry();
			if (injector != null) {
				new XcoreStandaloneSetup().register(injector);
			}
		}
	}
	
	public static class InspectableXcoreResource extends XcoreResource {
		
		public EList<EObject> getContentsUnsafe() {
			return contents;
		}
		
	}
	
	@Test
	public void testSetup() {
		XtextResourceSet resourceSet = resourceSetProvider.get();
		Resource resource = resourceSet.createResource(URI.createURI("foo.xcore"));
		assertTrue(resource.toString(), resource instanceof InspectableXcoreResource);
		assertNull(((InspectableXcoreResource)resource).getContentsUnsafe());
	}
	
	@Test
	public void testContentsWithoutDerived() throws IOException {
		XtextResourceSet resourceSet = resourceSetProvider.get();
		Resource resource = resourceSet.createResource(URI.createURI("foo.xcore"));
		resource.load(new StringInputStream("package foo.bar class Baz {}"), null);
		assertEquals(1, ((InspectableXcoreResource)resource).getContentsUnsafe().size());
	}
	
	@Test
	public void testContentsWithDerived() throws IOException {
		XtextResourceSet resourceSet = resourceSetProvider.get();
		Resource resource = resourceSet.createResource(URI.createURI("foo.xcore"));
		resource.load(new StringInputStream("package foo.bar class Baz {}"), null);
		assertTrue(1 < resource.getContents().size());
	}
	
	public void testResourceDescriptionManagerDoesNotResolve() throws IOException {
		XtextResourceSet resourceSet = resourceSetProvider.get();
		InspectableXcoreResource resource = (InspectableXcoreResource) resourceSet.createResource(URI.createURI("foo.xcore"));
		resource.load(new StringInputStream("package foo.bar class Baz {}"), null);
		Manager manager = resource.getResourceServiceProvider().getResourceDescriptionManager();
		IResourceDescription resourceDescription = manager.getResourceDescription(resource);
		
		Iterator<IEObjectDescription> eclass = resourceDescription.getExportedObjectsByType(EcorePackage.Literals.ECLASS)
		    .iterator();
		Iterator<IEObjectDescription> genclass = resourceDescription.getExportedObjectsByType(
		    GenModelPackage.Literals.GEN_CLASS).iterator();
		Iterator<IEObjectDescription> jvmTypes = resourceDescription.getExportedObjectsByType(
		    TypesPackage.Literals.JVM_GENERIC_TYPE).iterator();
		final String expected = "foo.bar.Baz";
		assertEquals(expected, eclass.next().getName().toString());
		assertFalse(eclass.hasNext());
		assertEquals(expected, genclass.next().getName().toString());
		assertFalse(genclass.hasNext());
		assertEquals(expected, jvmTypes.next().getName().toString());
		assertEquals(expected + "Impl", jvmTypes.next().getName().toString());
		assertFalse(genclass.hasNext());
		
		assertEquals(1, resource.getContentsUnsafe().size());
	}
	
}