mirror of
https://github.com/sigmasternchen/xtext-core
synced 2025-03-15 08:18:55 +00:00
[eclipse/xtext#1777] ported xtend code to java
Signed-off-by: Christian Dietrich <christian.dietrich@itemis.de>
This commit is contained in:
parent
321fc7f595
commit
7297fd4786
26 changed files with 1884 additions and 3770 deletions
|
@ -0,0 +1,411 @@
|
|||
/**
|
||||
* Copyright (c) 2016, 2020 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide.tests.server.concurrent;
|
||||
|
||||
import java.util.concurrent.CancellationException;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import org.apache.log4j.Level;
|
||||
import org.eclipse.xtext.ide.server.ServerModule;
|
||||
import org.eclipse.xtext.ide.server.concurrent.AbstractRequest;
|
||||
import org.eclipse.xtext.ide.server.concurrent.ReadRequest;
|
||||
import org.eclipse.xtext.ide.server.concurrent.RequestManager;
|
||||
import org.eclipse.xtext.ide.server.concurrent.WriteRequest;
|
||||
import org.eclipse.xtext.service.OperationCanceledManager;
|
||||
import org.eclipse.xtext.testing.RepeatedTest;
|
||||
import org.eclipse.xtext.testing.logging.LoggingTester;
|
||||
import org.eclipse.xtext.util.CancelIndicator;
|
||||
import org.eclipse.xtext.xbase.lib.Exceptions;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
import com.google.common.util.concurrent.Uninterruptibles;
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
|
||||
/**
|
||||
* @author kosyakov - Initial contribution and API
|
||||
*/
|
||||
public class RequestManagerTest {
|
||||
@Rule
|
||||
public RepeatedTest.Rule rule = new RepeatedTest.Rule(false);
|
||||
|
||||
@Inject
|
||||
private RequestManager requestManager;
|
||||
|
||||
@Inject
|
||||
private Provider<ExecutorService> executorServiceProvider;
|
||||
|
||||
@Inject
|
||||
private Provider<OperationCanceledManager> cancelManagerProvider;
|
||||
|
||||
private AtomicInteger sharedState;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
sharedState = new AtomicInteger();
|
||||
Guice.createInjector(new ServerModule()).injectMembers(this);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
requestManager.shutdown();
|
||||
sharedState = null;
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
public void testRunWriteLogExceptionNonCancellable() throws Exception {
|
||||
LoggingTester.LogCapture logResult = LoggingTester.captureLogging(Level.ALL, WriteRequest.class, () -> {
|
||||
CompletableFuture<Object> future = requestManager.runWrite(() -> null, (CancelIndicator $0, Object $1) -> {
|
||||
throw new RuntimeException();
|
||||
});
|
||||
try {
|
||||
future.join();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
});
|
||||
logResult.assertLogEntry("Error during request:");
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
public void testRunWriteLogExceptionCancellable() throws Exception {
|
||||
LoggingTester.LogCapture logResult = LoggingTester.captureLogging(Level.ALL, WriteRequest.class, () -> {
|
||||
CompletableFuture<Object> future = requestManager.runWrite(() -> {
|
||||
throw new RuntimeException();
|
||||
}, (ci, o) -> null);
|
||||
try {
|
||||
future.join();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
});
|
||||
logResult.assertLogEntry("Error during request:");
|
||||
}
|
||||
|
||||
@Test(timeout = 1000, expected = ExecutionException.class)
|
||||
public void testRunWriteCatchException() throws Exception {
|
||||
LoggingTester.captureLogging(Level.ALL, WriteRequest.class, () -> {
|
||||
try {
|
||||
CompletableFuture<Object> future = requestManager.runWrite(() -> {
|
||||
throw new RuntimeException();
|
||||
}, (ci, o) -> null);
|
||||
Assert.assertEquals("Foo", future.get());
|
||||
} catch (Throwable t) {
|
||||
throw Exceptions.sneakyThrow(t);
|
||||
}
|
||||
});
|
||||
Assert.fail("unreachable");
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
public void testRunReadLogException() throws Exception {
|
||||
LoggingTester.LogCapture logResult = LoggingTester.captureLogging(Level.ALL, ReadRequest.class, () -> {
|
||||
CompletableFuture<Object> future = requestManager.runRead((CancelIndicator it) -> {
|
||||
throw new RuntimeException();
|
||||
});
|
||||
try {
|
||||
future.join();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
});
|
||||
logResult.assertLogEntry("Error during request:");
|
||||
}
|
||||
|
||||
@Test(timeout = 1000, expected = ExecutionException.class)
|
||||
public void testRunReadCatchException() throws Exception {
|
||||
LoggingTester.captureLogging(Level.ALL, ReadRequest.class, () -> {
|
||||
try {
|
||||
CompletableFuture<Object> future = requestManager.runRead((CancelIndicator it) -> {
|
||||
throw new RuntimeException();
|
||||
});
|
||||
Assert.assertEquals("Foo", future.get());
|
||||
} catch (Throwable e) {
|
||||
throw Exceptions.sneakyThrow(e);
|
||||
}
|
||||
});
|
||||
Assert.fail();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteWaitsForReadToFinish_01() throws Exception {
|
||||
AtomicBoolean marked = new AtomicBoolean(false);
|
||||
CountDownLatch countDownInRead = new CountDownLatch(1);
|
||||
CountDownLatch countDownInWrite = new CountDownLatch(1);
|
||||
CountDownLatch proceedWithWrite = new CountDownLatch(1);
|
||||
CompletableFuture<Object> reader = requestManager.runRead((CancelIndicator cancelIndicator) -> {
|
||||
countDownInRead.countDown();
|
||||
Uninterruptibles.awaitUninterruptibly(countDownInWrite);
|
||||
marked.set(true);
|
||||
proceedWithWrite.countDown();
|
||||
return null;
|
||||
});
|
||||
Uninterruptibles.awaitUninterruptibly(countDownInRead);
|
||||
CompletableFuture<Object> writer = requestManager.runWrite(() -> null,
|
||||
(CancelIndicator cancelIndicator, Object ignored) -> {
|
||||
countDownInWrite.countDown();
|
||||
Uninterruptibles.awaitUninterruptibly(proceedWithWrite);
|
||||
Assert.assertTrue(marked.get());
|
||||
return null;
|
||||
});
|
||||
try {
|
||||
Uninterruptibles.getUninterruptibly(writer, 100, TimeUnit.MILLISECONDS);
|
||||
Assert.fail("Expected timeout");
|
||||
} catch (TimeoutException e) {
|
||||
Assert.assertFalse(marked.get());
|
||||
}
|
||||
countDownInWrite.countDown();
|
||||
Uninterruptibles.getUninterruptibly(writer, 100, TimeUnit.MILLISECONDS);
|
||||
Assert.assertTrue(reader.isDone());
|
||||
Assert.assertFalse(reader.isCancelled());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteWaitsForReadToFinish_02() throws Exception {
|
||||
AtomicBoolean marked = new AtomicBoolean(false);
|
||||
CountDownLatch countDownInRead = new CountDownLatch(1);
|
||||
CountDownLatch countDownInWrite = new CountDownLatch(1);
|
||||
CountDownLatch proceedWithWrite = new CountDownLatch(1);
|
||||
CompletableFuture<Object> reader = requestManager.runRead((CancelIndicator cancelIndicator) -> {
|
||||
countDownInRead.countDown();
|
||||
Uninterruptibles.awaitUninterruptibly(countDownInWrite);
|
||||
marked.set(true);
|
||||
proceedWithWrite.countDown();
|
||||
throw new CancellationException();
|
||||
});
|
||||
Uninterruptibles.awaitUninterruptibly(countDownInRead);
|
||||
CompletableFuture<Object> writer = requestManager.runWrite(() -> null,
|
||||
(CancelIndicator cancelIndicator, Object ignored) -> {
|
||||
countDownInWrite.countDown();
|
||||
Uninterruptibles.awaitUninterruptibly(proceedWithWrite);
|
||||
Assert.assertTrue(marked.get());
|
||||
return null;
|
||||
});
|
||||
try {
|
||||
Uninterruptibles.<Object>getUninterruptibly(writer, 100, TimeUnit.MILLISECONDS);
|
||||
Assert.fail("Expected timeout");
|
||||
} catch (TimeoutException e) {
|
||||
Assert.assertFalse(marked.get());
|
||||
}
|
||||
countDownInWrite.countDown();
|
||||
Uninterruptibles.getUninterruptibly(writer, 100, TimeUnit.MILLISECONDS);
|
||||
Assert.assertTrue(reader.isDone());
|
||||
Assert.assertTrue(reader.isCancelled());
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
public void testRunRead() throws Exception {
|
||||
CompletableFuture<String> future = requestManager.runRead((CancelIndicator it) -> {
|
||||
return "Foo";
|
||||
});
|
||||
Assert.assertEquals("Foo", future.get());
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
public void testRunReadConcurrent() throws Exception {
|
||||
CompletableFuture<Integer> future = requestManager.runRead((CancelIndicator it) -> {
|
||||
try {
|
||||
while (sharedState.get() == 0) {
|
||||
Thread.sleep(10);
|
||||
}
|
||||
return sharedState.incrementAndGet();
|
||||
} catch (InterruptedException e) {
|
||||
throw Exceptions.sneakyThrow(e);
|
||||
}
|
||||
});
|
||||
requestManager.runRead((CancelIndicator it) -> {
|
||||
return Integer.valueOf(sharedState.incrementAndGet());
|
||||
});
|
||||
future.join();
|
||||
Assert.assertEquals(2, sharedState.get());
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
public void testRunReadAfterWrite() throws Exception {
|
||||
requestManager.runWrite(() -> null, (CancelIndicator $0, Object $1) -> {
|
||||
return Integer.valueOf(sharedState.incrementAndGet());
|
||||
});
|
||||
CompletableFuture<Integer> future = requestManager.runRead((CancelIndicator it) -> {
|
||||
return Integer.valueOf(sharedState.get());
|
||||
});
|
||||
Assert.assertEquals(1, (future.get()).intValue());
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
public void testRunWrite() throws Exception {
|
||||
requestManager.runWrite(() -> null, (CancelIndicator $0, Object $1) -> {
|
||||
return Integer.valueOf(sharedState.incrementAndGet());
|
||||
}).join();
|
||||
Assert.assertEquals(1, sharedState.get());
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
public void testRunWriteAfterWrite() throws Exception {
|
||||
requestManager.runWrite(() -> null, (CancelIndicator $0, Object $1) -> {
|
||||
return Integer.valueOf(sharedState.incrementAndGet());
|
||||
}).join();
|
||||
requestManager.runWrite(() -> null, (CancelIndicator $0, Object $1) -> {
|
||||
if (sharedState.get() != 0) {
|
||||
return sharedState.incrementAndGet();
|
||||
}
|
||||
return null;
|
||||
}).join();
|
||||
Assert.assertEquals(2, sharedState.get());
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
@RepeatedTest(times = 50)
|
||||
public void testRunWriteAfterReadStarted() throws Exception {
|
||||
CountDownLatch readStarted = new CountDownLatch(1);
|
||||
requestManager.runRead((CancelIndicator it) -> {
|
||||
readStarted.countDown();
|
||||
return sharedState.incrementAndGet();
|
||||
});
|
||||
Uninterruptibles.awaitUninterruptibly(readStarted);
|
||||
requestManager.runWrite(() -> null, (CancelIndicator $0, Object $1) -> {
|
||||
Assert.assertEquals(1, sharedState.get());
|
||||
return sharedState.incrementAndGet();
|
||||
}).join();
|
||||
Assert.assertEquals(2, sharedState.get());
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
@RepeatedTest(times = 50)
|
||||
public void testRunWriteBeforeReadStarted() throws Exception {
|
||||
CountDownLatch writeSubmitted = new CountDownLatch(1);
|
||||
AtomicBoolean firstWriteDone = new AtomicBoolean();
|
||||
requestManager.runWrite(() -> {
|
||||
Uninterruptibles.awaitUninterruptibly(writeSubmitted);
|
||||
firstWriteDone.set(true);
|
||||
return null;
|
||||
}, (CancelIndicator $0, Object $1) -> {
|
||||
return Integer.valueOf(sharedState.incrementAndGet());
|
||||
});
|
||||
requestManager.runRead((CancelIndicator it) -> {
|
||||
return Integer.valueOf(sharedState.incrementAndGet());
|
||||
});
|
||||
CompletableFuture<Integer> joinMe = requestManager.runWrite(() -> null, (CancelIndicator $0, Object $1) -> {
|
||||
Assert.assertEquals(0, sharedState.get());
|
||||
Assert.assertTrue(firstWriteDone.get());
|
||||
return sharedState.incrementAndGet();
|
||||
});
|
||||
writeSubmitted.countDown();
|
||||
joinMe.join();
|
||||
Assert.assertTrue(firstWriteDone.get());
|
||||
Assert.assertEquals(1, sharedState.get());
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
public void testCancelRead() throws Exception {
|
||||
AtomicBoolean isCanceled = new AtomicBoolean(false);
|
||||
CompletableFuture<Object> future = requestManager.runRead((CancelIndicator cancelIndicator) -> {
|
||||
try {
|
||||
try {
|
||||
sharedState.incrementAndGet();
|
||||
while ((!cancelIndicator.isCanceled())) {
|
||||
Thread.sleep(10);
|
||||
}
|
||||
} finally {
|
||||
isCanceled.set(true);
|
||||
}
|
||||
return null;
|
||||
} catch (Throwable t) {
|
||||
throw Exceptions.sneakyThrow(t);
|
||||
}
|
||||
});
|
||||
while (sharedState.get() == 0) {
|
||||
Thread.sleep(10);
|
||||
}
|
||||
future.cancel(true);
|
||||
while (!isCanceled.get()) {
|
||||
Thread.sleep(10);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The tests assumes an implementation of a Command that has access to the
|
||||
* request manager
|
||||
*/
|
||||
@Test(timeout = 5000)
|
||||
@RepeatedTest(times = 50)
|
||||
public void testReadCommandSubmitsWriteCommand() throws Exception {
|
||||
Thread mainThread = Thread.currentThread();
|
||||
CountDownLatch submittedFromMain = new CountDownLatch(1);
|
||||
CountDownLatch addedFromReader = new CountDownLatch(1);
|
||||
AtomicReference<Thread> readerThreadRef = new AtomicReference<Thread>();
|
||||
RequestManager myRequestManager = new RequestManager(executorServiceProvider.get(),
|
||||
cancelManagerProvider.get()) {
|
||||
@Override
|
||||
protected void addRequest(AbstractRequest<?> request) {
|
||||
if (((request instanceof WriteRequest)
|
||||
&& Objects.equal(Thread.currentThread(), readerThreadRef.get()))) {
|
||||
super.addRequest(request);
|
||||
addedFromReader.countDown();
|
||||
Uninterruptibles.awaitUninterruptibly(submittedFromMain, 100, TimeUnit.MILLISECONDS);
|
||||
} else {
|
||||
super.addRequest(request);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void submitRequest(AbstractRequest<?> request) {
|
||||
if (((request instanceof WriteRequest) && Objects.equal(Thread.currentThread(), mainThread))) {
|
||||
super.submitRequest(request);
|
||||
submittedFromMain.countDown();
|
||||
} else {
|
||||
super.submitRequest(request);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CompletableFuture<Void> cancel() {
|
||||
if (Objects.equal(Thread.currentThread(), mainThread)) {
|
||||
Uninterruptibles.awaitUninterruptibly(addedFromReader, 100, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
return super.cancel();
|
||||
}
|
||||
};
|
||||
CountDownLatch threadSet = new CountDownLatch(1);
|
||||
CompletableFuture<CompletableFuture<Object>> readResult = myRequestManager.runRead((CancelIndicator it) -> {
|
||||
readerThreadRef.set(Thread.currentThread());
|
||||
threadSet.countDown();
|
||||
return myRequestManager.runWrite(() -> null, (ci, o) -> null);
|
||||
});
|
||||
Uninterruptibles.awaitUninterruptibly(threadSet);
|
||||
Assert.assertNotNull(readerThreadRef.get());
|
||||
CompletableFuture<Object> writeResult = myRequestManager.runWrite(() -> null, (ci, o) -> null);
|
||||
CompletableFuture<Object> writeFromReader = readResult.get();
|
||||
try {
|
||||
writeFromReader.get();
|
||||
try {
|
||||
writeResult.get();
|
||||
} catch (CancellationException e) {
|
||||
Assert.assertTrue(writeFromReader.isDone());
|
||||
Assert.assertTrue(writeResult.isDone());
|
||||
Assert.assertTrue(writeFromReader.isCancelled() != writeResult.isCancelled());
|
||||
}
|
||||
} catch (CancellationException e) {
|
||||
writeResult.get();
|
||||
Assert.assertTrue(writeFromReader.isDone());
|
||||
Assert.assertTrue(writeResult.isDone());
|
||||
Assert.assertTrue(writeFromReader.isCancelled() != writeResult.isCancelled());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,398 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2016, 2017, 2018 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.ide.tests.server.concurrent
|
||||
|
||||
import com.google.common.util.concurrent.Uninterruptibles
|
||||
import com.google.inject.Guice
|
||||
import com.google.inject.Inject
|
||||
import com.google.inject.Provider
|
||||
import java.util.concurrent.CancellationException
|
||||
import java.util.concurrent.CountDownLatch
|
||||
import java.util.concurrent.ExecutionException
|
||||
import java.util.concurrent.ExecutorService
|
||||
import java.util.concurrent.TimeUnit
|
||||
import java.util.concurrent.TimeoutException
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
import java.util.concurrent.atomic.AtomicReference
|
||||
import org.apache.log4j.Level
|
||||
import org.eclipse.xtext.ide.server.ServerModule
|
||||
import org.eclipse.xtext.ide.server.concurrent.AbstractRequest
|
||||
import org.eclipse.xtext.ide.server.concurrent.ReadRequest
|
||||
import org.eclipse.xtext.ide.server.concurrent.RequestManager
|
||||
import org.eclipse.xtext.ide.server.concurrent.WriteRequest
|
||||
import org.eclipse.xtext.service.OperationCanceledManager
|
||||
import org.eclipse.xtext.testing.RepeatedTest
|
||||
import org.eclipse.xtext.testing.logging.LoggingTester
|
||||
import org.junit.After
|
||||
import org.junit.Assert
|
||||
import org.junit.Before
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
|
||||
import static org.junit.Assert.*
|
||||
|
||||
/**
|
||||
* @author kosyakov - Initial contribution and API
|
||||
*/
|
||||
class RequestManagerTest {
|
||||
|
||||
@Rule public RepeatedTest.Rule rule = new RepeatedTest.Rule(false)
|
||||
|
||||
@Inject
|
||||
RequestManager requestManager
|
||||
|
||||
@Inject
|
||||
Provider<ExecutorService> executorServiceProvider
|
||||
|
||||
@Inject
|
||||
Provider<OperationCanceledManager> cancelManagerProvider
|
||||
|
||||
AtomicInteger sharedState
|
||||
|
||||
@Before
|
||||
def void setUp() {
|
||||
sharedState = new AtomicInteger
|
||||
Guice.createInjector(new ServerModule).injectMembers(this)
|
||||
}
|
||||
|
||||
@After
|
||||
def void tearDown() {
|
||||
requestManager.shutdown
|
||||
sharedState = null
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
def void testRunWriteLogExceptionNonCancellable() {
|
||||
val logResult = LoggingTester.captureLogging(Level.ALL, WriteRequest, [
|
||||
val future = requestManager.runWrite([], [
|
||||
throw new RuntimeException();
|
||||
])
|
||||
|
||||
// join future to assert log later
|
||||
try {
|
||||
future.join
|
||||
} catch (Exception e) {}
|
||||
])
|
||||
|
||||
logResult.assertLogEntry("Error during request:")
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
def void testRunWriteLogExceptionCancellable() {
|
||||
val logResult = LoggingTester.captureLogging(Level.ALL, WriteRequest, [
|
||||
val future = requestManager.runWrite([
|
||||
throw new RuntimeException();
|
||||
], [])
|
||||
|
||||
// join future to assert log later
|
||||
try {
|
||||
future.join
|
||||
} catch (Exception e) {}
|
||||
])
|
||||
|
||||
logResult.assertLogEntry("Error during request:")
|
||||
}
|
||||
|
||||
@Test(timeout = 1000, expected = ExecutionException)
|
||||
def void testRunWriteCatchException() {
|
||||
LoggingTester.captureLogging(Level.ALL, WriteRequest, [
|
||||
val future = requestManager.runWrite([
|
||||
throw new RuntimeException()
|
||||
], [])
|
||||
|
||||
assertEquals('Foo', future.get)
|
||||
])
|
||||
|
||||
Assert.fail("unreachable")
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
def void testRunReadLogException() {
|
||||
val logResult = LoggingTester.captureLogging(Level.ALL, ReadRequest, [
|
||||
val future = requestManager.runRead([
|
||||
throw new RuntimeException();
|
||||
])
|
||||
|
||||
// join future to assert log later
|
||||
try {
|
||||
future.join
|
||||
} catch (Exception e) {}
|
||||
])
|
||||
|
||||
logResult.assertLogEntry("Error during request:")
|
||||
}
|
||||
|
||||
@Test(timeout = 1000, expected = ExecutionException)
|
||||
def void testRunReadCatchException() {
|
||||
LoggingTester.captureLogging(Level.ALL, ReadRequest, [
|
||||
val future = requestManager.runRead([
|
||||
throw new RuntimeException()
|
||||
])
|
||||
|
||||
assertEquals('Foo', future.get)
|
||||
])
|
||||
|
||||
Assert.fail
|
||||
}
|
||||
|
||||
@Test
|
||||
def void testWriteWaitsForReadToFinish_01() throws Exception {
|
||||
val marked = new AtomicBoolean(false)
|
||||
val countDownInRead = new CountDownLatch(1)
|
||||
val countDownInWrite = new CountDownLatch(1)
|
||||
val proceedWithWrite = new CountDownLatch(1)
|
||||
val reader = requestManager.runRead [ cancelIndicator |
|
||||
countDownInRead.countDown
|
||||
Uninterruptibles.awaitUninterruptibly(countDownInWrite)
|
||||
marked.set(true)
|
||||
proceedWithWrite.countDown
|
||||
return null
|
||||
]
|
||||
Uninterruptibles.awaitUninterruptibly(countDownInRead)
|
||||
val writer = requestManager.runWrite([], [ cancelIndicator, ignored |
|
||||
countDownInWrite.countDown
|
||||
Uninterruptibles.awaitUninterruptibly(proceedWithWrite)
|
||||
assertTrue(marked.get)
|
||||
null
|
||||
])
|
||||
try {
|
||||
Uninterruptibles.getUninterruptibly(writer, 100, TimeUnit.MILLISECONDS)
|
||||
fail("Expected timeout")
|
||||
} catch(TimeoutException e) {
|
||||
assertFalse(marked.get()) // this should not be the case
|
||||
}
|
||||
countDownInWrite.countDown;
|
||||
Uninterruptibles.getUninterruptibly(writer, 100, TimeUnit.MILLISECONDS)
|
||||
assertTrue(reader.isDone)
|
||||
assertFalse(reader.isCancelled)
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
def void testWriteWaitsForReadToFinish_02() throws Exception {
|
||||
val marked = new AtomicBoolean(false)
|
||||
val countDownInRead = new CountDownLatch(1)
|
||||
val countDownInWrite = new CountDownLatch(1)
|
||||
val proceedWithWrite = new CountDownLatch(1)
|
||||
val reader = requestManager.runRead [ cancelIndicator |
|
||||
countDownInRead.countDown
|
||||
Uninterruptibles.awaitUninterruptibly(countDownInWrite)
|
||||
marked.set(true)
|
||||
proceedWithWrite.countDown
|
||||
throw new CancellationException
|
||||
]
|
||||
Uninterruptibles.awaitUninterruptibly(countDownInRead)
|
||||
val writer = requestManager.runWrite([], [ cancelIndicator, ignored |
|
||||
countDownInWrite.countDown
|
||||
Uninterruptibles.awaitUninterruptibly(proceedWithWrite)
|
||||
assertTrue(marked.get)
|
||||
null
|
||||
])
|
||||
try {
|
||||
Uninterruptibles.getUninterruptibly(writer, 100, TimeUnit.MILLISECONDS)
|
||||
fail("Expected timeout")
|
||||
} catch(TimeoutException e) {
|
||||
assertFalse(marked.get()) // this should not be the case
|
||||
}
|
||||
countDownInWrite.countDown;
|
||||
Uninterruptibles.getUninterruptibly(writer, 100, TimeUnit.MILLISECONDS)
|
||||
assertTrue(reader.isDone)
|
||||
assertTrue(reader.isCancelled)
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
def void testRunRead() {
|
||||
val future = requestManager.runRead [
|
||||
'Foo'
|
||||
]
|
||||
assertEquals('Foo', future.get)
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
def void testRunReadConcurrent() {
|
||||
val future = requestManager.runRead [
|
||||
while (sharedState.get == 0) {
|
||||
Thread.sleep(10)
|
||||
}
|
||||
sharedState.incrementAndGet
|
||||
]
|
||||
requestManager.runRead [
|
||||
sharedState.incrementAndGet
|
||||
]
|
||||
future.join
|
||||
assertEquals(2, sharedState.get)
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
def void testRunReadAfterWrite() {
|
||||
requestManager.runWrite([], [
|
||||
sharedState.incrementAndGet
|
||||
])
|
||||
val future = requestManager.runRead [
|
||||
sharedState.get
|
||||
]
|
||||
assertEquals(1, future.get)
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
def void testRunWrite() {
|
||||
requestManager.runWrite([], [
|
||||
sharedState.incrementAndGet
|
||||
]).join
|
||||
assertEquals(1, sharedState.get)
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
def void testRunWriteAfterWrite() {
|
||||
requestManager.runWrite([], [
|
||||
sharedState.incrementAndGet
|
||||
]).join
|
||||
requestManager.runWrite([], [
|
||||
if (sharedState.get != 0)
|
||||
sharedState.incrementAndGet as Integer
|
||||
]).join
|
||||
assertEquals(2, sharedState.get)
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
@RepeatedTest(times=50)
|
||||
def void testRunWriteAfterReadStarted() {
|
||||
val readStarted = new CountDownLatch(1)
|
||||
requestManager.runRead [
|
||||
readStarted.countDown
|
||||
sharedState.incrementAndGet
|
||||
]
|
||||
Uninterruptibles.awaitUninterruptibly(readStarted)
|
||||
requestManager.runWrite([], [
|
||||
assertEquals (1, sharedState.get)
|
||||
sharedState.incrementAndGet
|
||||
]).join
|
||||
assertEquals(2, sharedState.get)
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
@RepeatedTest(times=50)
|
||||
def void testRunWriteBeforeReadStarted() {
|
||||
val writeSubmitted = new CountDownLatch(1)
|
||||
val firstWriteDone = new AtomicBoolean
|
||||
requestManager.runWrite([
|
||||
Uninterruptibles.awaitUninterruptibly(writeSubmitted)
|
||||
firstWriteDone.set(true)
|
||||
null
|
||||
], [
|
||||
sharedState.incrementAndGet
|
||||
])
|
||||
requestManager.runRead [
|
||||
sharedState.incrementAndGet
|
||||
]
|
||||
val joinMe = requestManager.runWrite([], [
|
||||
assertEquals (0, sharedState.get)
|
||||
assertTrue(firstWriteDone.get)
|
||||
sharedState.incrementAndGet
|
||||
])
|
||||
writeSubmitted.countDown
|
||||
joinMe.join
|
||||
assertTrue(firstWriteDone.get)
|
||||
assertEquals(1, sharedState.get)
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
def void testCancelRead() {
|
||||
val isCanceled = new AtomicBoolean(false)
|
||||
val future = requestManager.runRead [ cancelIndicator |
|
||||
try {
|
||||
sharedState.incrementAndGet
|
||||
while (!cancelIndicator.isCanceled) {
|
||||
Thread.sleep(10)
|
||||
}
|
||||
} finally {
|
||||
isCanceled.set(true)
|
||||
}
|
||||
return null
|
||||
]
|
||||
while (sharedState.get === 0) {
|
||||
Thread.sleep(10)
|
||||
}
|
||||
future.cancel(true)
|
||||
while (!isCanceled.get) {
|
||||
Thread.sleep(10)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* The tests assumes an implementation of a Command that has access to the request manager
|
||||
*
|
||||
*/
|
||||
@Test(timeout = 5000)
|
||||
@RepeatedTest(times=50)
|
||||
def void testReadCommandSubmitsWriteCommand() {
|
||||
val mainThread = Thread.currentThread
|
||||
val submittedFromMain = new CountDownLatch(1)
|
||||
val addedFromReader = new CountDownLatch(1)
|
||||
val AtomicReference<Thread> readerThreadRef = new AtomicReference
|
||||
val myRequestManager = new RequestManager(executorServiceProvider.get, cancelManagerProvider.get) {
|
||||
|
||||
override protected addRequest(AbstractRequest<?> request) {
|
||||
if (request instanceof WriteRequest && Thread.currentThread == readerThreadRef.get) {
|
||||
super.addRequest(request)
|
||||
addedFromReader.countDown
|
||||
Uninterruptibles.awaitUninterruptibly(submittedFromMain, 100, TimeUnit.MILLISECONDS)
|
||||
} else {
|
||||
super.addRequest(request)
|
||||
}
|
||||
}
|
||||
|
||||
override protected submitRequest(AbstractRequest<?> request) {
|
||||
if (request instanceof WriteRequest && Thread.currentThread == mainThread) {
|
||||
super.submitRequest(request)
|
||||
submittedFromMain.countDown
|
||||
} else {
|
||||
super.submitRequest(request)
|
||||
}
|
||||
}
|
||||
|
||||
override protected cancel() {
|
||||
if (Thread.currentThread == mainThread) {
|
||||
Uninterruptibles.awaitUninterruptibly(addedFromReader, 100, TimeUnit.MILLISECONDS)
|
||||
}
|
||||
super.cancel()
|
||||
}
|
||||
|
||||
}
|
||||
val threadSet = new CountDownLatch(1)
|
||||
val readResult = myRequestManager.runRead [
|
||||
readerThreadRef.set(Thread.currentThread)
|
||||
threadSet.countDown
|
||||
return myRequestManager.runWrite([], [])
|
||||
]
|
||||
Uninterruptibles.awaitUninterruptibly(threadSet)
|
||||
assertNotNull(readerThreadRef.get)
|
||||
val writeResult = myRequestManager.runWrite([], [])
|
||||
|
||||
val writeFromReader = readResult.get
|
||||
try {
|
||||
writeFromReader.get
|
||||
try {
|
||||
writeResult.get
|
||||
} catch(CancellationException ce) {
|
||||
// one of both will be cancelled
|
||||
assertTrue(writeFromReader.isDone)
|
||||
assertTrue(writeResult.isDone)
|
||||
assertTrue(writeFromReader.isCancelled != writeResult.isCancelled)
|
||||
}
|
||||
} catch(CancellationException ce) {
|
||||
writeResult.get
|
||||
// one of both will be cancelled
|
||||
assertTrue(writeFromReader.isDone)
|
||||
assertTrue(writeResult.isDone)
|
||||
assertTrue(writeFromReader.isCancelled != writeResult.isCancelled)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,575 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2016, 2017, 2018 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.ide.tests.server.concurrent;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
import com.google.common.util.concurrent.Uninterruptibles;
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
import java.util.concurrent.CancellationException;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import org.apache.log4j.Level;
|
||||
import org.eclipse.xtext.ide.server.ServerModule;
|
||||
import org.eclipse.xtext.ide.server.concurrent.AbstractRequest;
|
||||
import org.eclipse.xtext.ide.server.concurrent.ReadRequest;
|
||||
import org.eclipse.xtext.ide.server.concurrent.RequestManager;
|
||||
import org.eclipse.xtext.ide.server.concurrent.WriteRequest;
|
||||
import org.eclipse.xtext.service.OperationCanceledManager;
|
||||
import org.eclipse.xtext.testing.RepeatedTest;
|
||||
import org.eclipse.xtext.testing.logging.LoggingTester;
|
||||
import org.eclipse.xtext.util.CancelIndicator;
|
||||
import org.eclipse.xtext.xbase.lib.Exceptions;
|
||||
import org.eclipse.xtext.xbase.lib.Functions.Function0;
|
||||
import org.eclipse.xtext.xbase.lib.Functions.Function1;
|
||||
import org.eclipse.xtext.xbase.lib.Functions.Function2;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author kosyakov - Initial contribution and API
|
||||
*/
|
||||
@SuppressWarnings("all")
|
||||
public class RequestManagerTest {
|
||||
@Rule
|
||||
public RepeatedTest.Rule rule = new RepeatedTest.Rule(false);
|
||||
|
||||
@Inject
|
||||
private RequestManager requestManager;
|
||||
|
||||
@Inject
|
||||
private Provider<ExecutorService> executorServiceProvider;
|
||||
|
||||
@Inject
|
||||
private Provider<OperationCanceledManager> cancelManagerProvider;
|
||||
|
||||
private AtomicInteger sharedState;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
AtomicInteger _atomicInteger = new AtomicInteger();
|
||||
this.sharedState = _atomicInteger;
|
||||
ServerModule _serverModule = new ServerModule();
|
||||
Guice.createInjector(_serverModule).injectMembers(this);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
this.requestManager.shutdown();
|
||||
this.sharedState = null;
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
public void testRunWriteLogExceptionNonCancellable() {
|
||||
final Runnable _function = () -> {
|
||||
final Function0<Object> _function_1 = () -> {
|
||||
return null;
|
||||
};
|
||||
final Function2<CancelIndicator, Object, Object> _function_2 = (CancelIndicator $0, Object $1) -> {
|
||||
throw new RuntimeException();
|
||||
};
|
||||
final CompletableFuture<Object> future = this.requestManager.<Object, Object>runWrite(_function_1, _function_2);
|
||||
try {
|
||||
future.join();
|
||||
} catch (final Throwable _t) {
|
||||
if (_t instanceof Exception) {
|
||||
} else {
|
||||
throw Exceptions.sneakyThrow(_t);
|
||||
}
|
||||
}
|
||||
};
|
||||
final LoggingTester.LogCapture logResult = LoggingTester.captureLogging(Level.ALL, WriteRequest.class, _function);
|
||||
logResult.assertLogEntry("Error during request:");
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
public void testRunWriteLogExceptionCancellable() {
|
||||
final Runnable _function = () -> {
|
||||
final Function0<Object> _function_1 = () -> {
|
||||
throw new RuntimeException();
|
||||
};
|
||||
final Function2<CancelIndicator, Object, Object> _function_2 = (CancelIndicator $0, Object $1) -> {
|
||||
return null;
|
||||
};
|
||||
final CompletableFuture<Object> future = this.requestManager.<Object, Object>runWrite(_function_1, _function_2);
|
||||
try {
|
||||
future.join();
|
||||
} catch (final Throwable _t) {
|
||||
if (_t instanceof Exception) {
|
||||
} else {
|
||||
throw Exceptions.sneakyThrow(_t);
|
||||
}
|
||||
}
|
||||
};
|
||||
final LoggingTester.LogCapture logResult = LoggingTester.captureLogging(Level.ALL, WriteRequest.class, _function);
|
||||
logResult.assertLogEntry("Error during request:");
|
||||
}
|
||||
|
||||
@Test(timeout = 1000, expected = ExecutionException.class)
|
||||
public void testRunWriteCatchException() {
|
||||
final Runnable _function = () -> {
|
||||
try {
|
||||
final Function0<Object> _function_1 = () -> {
|
||||
throw new RuntimeException();
|
||||
};
|
||||
final Function2<CancelIndicator, Object, Object> _function_2 = (CancelIndicator $0, Object $1) -> {
|
||||
return null;
|
||||
};
|
||||
final CompletableFuture<Object> future = this.requestManager.<Object, Object>runWrite(_function_1, _function_2);
|
||||
Assert.assertEquals("Foo", future.get());
|
||||
} catch (Throwable _e) {
|
||||
throw Exceptions.sneakyThrow(_e);
|
||||
}
|
||||
};
|
||||
LoggingTester.captureLogging(Level.ALL, WriteRequest.class, _function);
|
||||
Assert.fail("unreachable");
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
public void testRunReadLogException() {
|
||||
final Runnable _function = () -> {
|
||||
final Function1<CancelIndicator, Object> _function_1 = (CancelIndicator it) -> {
|
||||
throw new RuntimeException();
|
||||
};
|
||||
final CompletableFuture<Object> future = this.requestManager.<Object>runRead(_function_1);
|
||||
try {
|
||||
future.join();
|
||||
} catch (final Throwable _t) {
|
||||
if (_t instanceof Exception) {
|
||||
} else {
|
||||
throw Exceptions.sneakyThrow(_t);
|
||||
}
|
||||
}
|
||||
};
|
||||
final LoggingTester.LogCapture logResult = LoggingTester.captureLogging(Level.ALL, ReadRequest.class, _function);
|
||||
logResult.assertLogEntry("Error during request:");
|
||||
}
|
||||
|
||||
@Test(timeout = 1000, expected = ExecutionException.class)
|
||||
public void testRunReadCatchException() {
|
||||
final Runnable _function = () -> {
|
||||
try {
|
||||
final Function1<CancelIndicator, Object> _function_1 = (CancelIndicator it) -> {
|
||||
throw new RuntimeException();
|
||||
};
|
||||
final CompletableFuture<Object> future = this.requestManager.<Object>runRead(_function_1);
|
||||
Assert.assertEquals("Foo", future.get());
|
||||
} catch (Throwable _e) {
|
||||
throw Exceptions.sneakyThrow(_e);
|
||||
}
|
||||
};
|
||||
LoggingTester.captureLogging(Level.ALL, ReadRequest.class, _function);
|
||||
Assert.fail();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteWaitsForReadToFinish_01() throws Exception {
|
||||
final AtomicBoolean marked = new AtomicBoolean(false);
|
||||
final CountDownLatch countDownInRead = new CountDownLatch(1);
|
||||
final CountDownLatch countDownInWrite = new CountDownLatch(1);
|
||||
final CountDownLatch proceedWithWrite = new CountDownLatch(1);
|
||||
final Function1<CancelIndicator, Object> _function = (CancelIndicator cancelIndicator) -> {
|
||||
countDownInRead.countDown();
|
||||
Uninterruptibles.awaitUninterruptibly(countDownInWrite);
|
||||
marked.set(true);
|
||||
proceedWithWrite.countDown();
|
||||
return null;
|
||||
};
|
||||
final CompletableFuture<Object> reader = this.requestManager.<Object>runRead(_function);
|
||||
Uninterruptibles.awaitUninterruptibly(countDownInRead);
|
||||
final Function0<Object> _function_1 = () -> {
|
||||
return null;
|
||||
};
|
||||
final Function2<CancelIndicator, Object, Object> _function_2 = (CancelIndicator cancelIndicator, Object ignored) -> {
|
||||
Object _xblockexpression = null;
|
||||
{
|
||||
countDownInWrite.countDown();
|
||||
Uninterruptibles.awaitUninterruptibly(proceedWithWrite);
|
||||
Assert.assertTrue(marked.get());
|
||||
_xblockexpression = null;
|
||||
}
|
||||
return _xblockexpression;
|
||||
};
|
||||
final CompletableFuture<Object> writer = this.requestManager.<Object, Object>runWrite(_function_1, _function_2);
|
||||
try {
|
||||
Uninterruptibles.<Object>getUninterruptibly(writer, 100, TimeUnit.MILLISECONDS);
|
||||
Assert.fail("Expected timeout");
|
||||
} catch (final Throwable _t) {
|
||||
if (_t instanceof TimeoutException) {
|
||||
Assert.assertFalse(marked.get());
|
||||
} else {
|
||||
throw Exceptions.sneakyThrow(_t);
|
||||
}
|
||||
}
|
||||
countDownInWrite.countDown();
|
||||
Uninterruptibles.<Object>getUninterruptibly(writer, 100, TimeUnit.MILLISECONDS);
|
||||
Assert.assertTrue(reader.isDone());
|
||||
Assert.assertFalse(reader.isCancelled());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteWaitsForReadToFinish_02() throws Exception {
|
||||
final AtomicBoolean marked = new AtomicBoolean(false);
|
||||
final CountDownLatch countDownInRead = new CountDownLatch(1);
|
||||
final CountDownLatch countDownInWrite = new CountDownLatch(1);
|
||||
final CountDownLatch proceedWithWrite = new CountDownLatch(1);
|
||||
final Function1<CancelIndicator, Object> _function = (CancelIndicator cancelIndicator) -> {
|
||||
countDownInRead.countDown();
|
||||
Uninterruptibles.awaitUninterruptibly(countDownInWrite);
|
||||
marked.set(true);
|
||||
proceedWithWrite.countDown();
|
||||
throw new CancellationException();
|
||||
};
|
||||
final CompletableFuture<Object> reader = this.requestManager.<Object>runRead(_function);
|
||||
Uninterruptibles.awaitUninterruptibly(countDownInRead);
|
||||
final Function0<Object> _function_1 = () -> {
|
||||
return null;
|
||||
};
|
||||
final Function2<CancelIndicator, Object, Object> _function_2 = (CancelIndicator cancelIndicator, Object ignored) -> {
|
||||
Object _xblockexpression = null;
|
||||
{
|
||||
countDownInWrite.countDown();
|
||||
Uninterruptibles.awaitUninterruptibly(proceedWithWrite);
|
||||
Assert.assertTrue(marked.get());
|
||||
_xblockexpression = null;
|
||||
}
|
||||
return _xblockexpression;
|
||||
};
|
||||
final CompletableFuture<Object> writer = this.requestManager.<Object, Object>runWrite(_function_1, _function_2);
|
||||
try {
|
||||
Uninterruptibles.<Object>getUninterruptibly(writer, 100, TimeUnit.MILLISECONDS);
|
||||
Assert.fail("Expected timeout");
|
||||
} catch (final Throwable _t) {
|
||||
if (_t instanceof TimeoutException) {
|
||||
Assert.assertFalse(marked.get());
|
||||
} else {
|
||||
throw Exceptions.sneakyThrow(_t);
|
||||
}
|
||||
}
|
||||
countDownInWrite.countDown();
|
||||
Uninterruptibles.<Object>getUninterruptibly(writer, 100, TimeUnit.MILLISECONDS);
|
||||
Assert.assertTrue(reader.isDone());
|
||||
Assert.assertTrue(reader.isCancelled());
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
public void testRunRead() {
|
||||
try {
|
||||
final Function1<CancelIndicator, String> _function = (CancelIndicator it) -> {
|
||||
return "Foo";
|
||||
};
|
||||
final CompletableFuture<String> future = this.requestManager.<String>runRead(_function);
|
||||
Assert.assertEquals("Foo", future.get());
|
||||
} catch (Throwable _e) {
|
||||
throw Exceptions.sneakyThrow(_e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
public void testRunReadConcurrent() {
|
||||
final Function1<CancelIndicator, Integer> _function = (CancelIndicator it) -> {
|
||||
try {
|
||||
int _xblockexpression = (int) 0;
|
||||
{
|
||||
while ((this.sharedState.get() == 0)) {
|
||||
Thread.sleep(10);
|
||||
}
|
||||
_xblockexpression = this.sharedState.incrementAndGet();
|
||||
}
|
||||
return Integer.valueOf(_xblockexpression);
|
||||
} catch (Throwable _e) {
|
||||
throw Exceptions.sneakyThrow(_e);
|
||||
}
|
||||
};
|
||||
final CompletableFuture<Integer> future = this.requestManager.<Integer>runRead(_function);
|
||||
final Function1<CancelIndicator, Integer> _function_1 = (CancelIndicator it) -> {
|
||||
return Integer.valueOf(this.sharedState.incrementAndGet());
|
||||
};
|
||||
this.requestManager.<Integer>runRead(_function_1);
|
||||
future.join();
|
||||
Assert.assertEquals(2, this.sharedState.get());
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
public void testRunReadAfterWrite() {
|
||||
try {
|
||||
final Function0<Object> _function = () -> {
|
||||
return null;
|
||||
};
|
||||
final Function2<CancelIndicator, Object, Integer> _function_1 = (CancelIndicator $0, Object $1) -> {
|
||||
return Integer.valueOf(this.sharedState.incrementAndGet());
|
||||
};
|
||||
this.requestManager.<Object, Integer>runWrite(_function, _function_1);
|
||||
final Function1<CancelIndicator, Integer> _function_2 = (CancelIndicator it) -> {
|
||||
return Integer.valueOf(this.sharedState.get());
|
||||
};
|
||||
final CompletableFuture<Integer> future = this.requestManager.<Integer>runRead(_function_2);
|
||||
Assert.assertEquals(1, (future.get()).intValue());
|
||||
} catch (Throwable _e) {
|
||||
throw Exceptions.sneakyThrow(_e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
public void testRunWrite() {
|
||||
final Function0<Object> _function = () -> {
|
||||
return null;
|
||||
};
|
||||
final Function2<CancelIndicator, Object, Integer> _function_1 = (CancelIndicator $0, Object $1) -> {
|
||||
return Integer.valueOf(this.sharedState.incrementAndGet());
|
||||
};
|
||||
this.requestManager.<Object, Integer>runWrite(_function, _function_1).join();
|
||||
Assert.assertEquals(1, this.sharedState.get());
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
public void testRunWriteAfterWrite() {
|
||||
final Function0<Object> _function = () -> {
|
||||
return null;
|
||||
};
|
||||
final Function2<CancelIndicator, Object, Integer> _function_1 = (CancelIndicator $0, Object $1) -> {
|
||||
return Integer.valueOf(this.sharedState.incrementAndGet());
|
||||
};
|
||||
this.requestManager.<Object, Integer>runWrite(_function, _function_1).join();
|
||||
final Function0<Object> _function_2 = () -> {
|
||||
return null;
|
||||
};
|
||||
final Function2<CancelIndicator, Object, Integer> _function_3 = (CancelIndicator $0, Object $1) -> {
|
||||
Integer _xifexpression = null;
|
||||
int _get = this.sharedState.get();
|
||||
boolean _notEquals = (_get != 0);
|
||||
if (_notEquals) {
|
||||
int _incrementAndGet = this.sharedState.incrementAndGet();
|
||||
_xifexpression = ((Integer) Integer.valueOf(_incrementAndGet));
|
||||
}
|
||||
return _xifexpression;
|
||||
};
|
||||
this.requestManager.<Object, Integer>runWrite(_function_2, _function_3).join();
|
||||
Assert.assertEquals(2, this.sharedState.get());
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
@RepeatedTest(times = 50)
|
||||
public void testRunWriteAfterReadStarted() {
|
||||
final CountDownLatch readStarted = new CountDownLatch(1);
|
||||
final Function1<CancelIndicator, Integer> _function = (CancelIndicator it) -> {
|
||||
int _xblockexpression = (int) 0;
|
||||
{
|
||||
readStarted.countDown();
|
||||
_xblockexpression = this.sharedState.incrementAndGet();
|
||||
}
|
||||
return Integer.valueOf(_xblockexpression);
|
||||
};
|
||||
this.requestManager.<Integer>runRead(_function);
|
||||
Uninterruptibles.awaitUninterruptibly(readStarted);
|
||||
final Function0<Object> _function_1 = () -> {
|
||||
return null;
|
||||
};
|
||||
final Function2<CancelIndicator, Object, Integer> _function_2 = (CancelIndicator $0, Object $1) -> {
|
||||
int _xblockexpression = (int) 0;
|
||||
{
|
||||
Assert.assertEquals(1, this.sharedState.get());
|
||||
_xblockexpression = this.sharedState.incrementAndGet();
|
||||
}
|
||||
return Integer.valueOf(_xblockexpression);
|
||||
};
|
||||
this.requestManager.<Object, Integer>runWrite(_function_1, _function_2).join();
|
||||
Assert.assertEquals(2, this.sharedState.get());
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
@RepeatedTest(times = 50)
|
||||
public void testRunWriteBeforeReadStarted() {
|
||||
final CountDownLatch writeSubmitted = new CountDownLatch(1);
|
||||
final AtomicBoolean firstWriteDone = new AtomicBoolean();
|
||||
final Function0<Object> _function = () -> {
|
||||
Object _xblockexpression = null;
|
||||
{
|
||||
Uninterruptibles.awaitUninterruptibly(writeSubmitted);
|
||||
firstWriteDone.set(true);
|
||||
_xblockexpression = null;
|
||||
}
|
||||
return _xblockexpression;
|
||||
};
|
||||
final Function2<CancelIndicator, Object, Integer> _function_1 = (CancelIndicator $0, Object $1) -> {
|
||||
return Integer.valueOf(this.sharedState.incrementAndGet());
|
||||
};
|
||||
this.requestManager.<Object, Integer>runWrite(_function, _function_1);
|
||||
final Function1<CancelIndicator, Integer> _function_2 = (CancelIndicator it) -> {
|
||||
return Integer.valueOf(this.sharedState.incrementAndGet());
|
||||
};
|
||||
this.requestManager.<Integer>runRead(_function_2);
|
||||
final Function0<Object> _function_3 = () -> {
|
||||
return null;
|
||||
};
|
||||
final Function2<CancelIndicator, Object, Integer> _function_4 = (CancelIndicator $0, Object $1) -> {
|
||||
int _xblockexpression = (int) 0;
|
||||
{
|
||||
Assert.assertEquals(0, this.sharedState.get());
|
||||
Assert.assertTrue(firstWriteDone.get());
|
||||
_xblockexpression = this.sharedState.incrementAndGet();
|
||||
}
|
||||
return Integer.valueOf(_xblockexpression);
|
||||
};
|
||||
final CompletableFuture<Integer> joinMe = this.requestManager.<Object, Integer>runWrite(_function_3, _function_4);
|
||||
writeSubmitted.countDown();
|
||||
joinMe.join();
|
||||
Assert.assertTrue(firstWriteDone.get());
|
||||
Assert.assertEquals(1, this.sharedState.get());
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
public void testCancelRead() {
|
||||
try {
|
||||
final AtomicBoolean isCanceled = new AtomicBoolean(false);
|
||||
final Function1<CancelIndicator, Object> _function = (CancelIndicator cancelIndicator) -> {
|
||||
try {
|
||||
try {
|
||||
this.sharedState.incrementAndGet();
|
||||
while ((!cancelIndicator.isCanceled())) {
|
||||
Thread.sleep(10);
|
||||
}
|
||||
} finally {
|
||||
isCanceled.set(true);
|
||||
}
|
||||
return null;
|
||||
} catch (Throwable _e) {
|
||||
throw Exceptions.sneakyThrow(_e);
|
||||
}
|
||||
};
|
||||
final CompletableFuture<Object> future = this.requestManager.<Object>runRead(_function);
|
||||
while ((this.sharedState.get() == 0)) {
|
||||
Thread.sleep(10);
|
||||
}
|
||||
future.cancel(true);
|
||||
while ((!isCanceled.get())) {
|
||||
Thread.sleep(10);
|
||||
}
|
||||
} catch (Throwable _e) {
|
||||
throw Exceptions.sneakyThrow(_e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The tests assumes an implementation of a Command that has access to the request manager
|
||||
*/
|
||||
@Test(timeout = 5000)
|
||||
@RepeatedTest(times = 50)
|
||||
public void testReadCommandSubmitsWriteCommand() {
|
||||
try {
|
||||
final Thread mainThread = Thread.currentThread();
|
||||
final CountDownLatch submittedFromMain = new CountDownLatch(1);
|
||||
final CountDownLatch addedFromReader = new CountDownLatch(1);
|
||||
final AtomicReference<Thread> readerThreadRef = new AtomicReference<Thread>();
|
||||
ExecutorService _get = this.executorServiceProvider.get();
|
||||
OperationCanceledManager _get_1 = this.cancelManagerProvider.get();
|
||||
final RequestManager myRequestManager = new RequestManager(_get, _get_1) {
|
||||
@Override
|
||||
protected void addRequest(final AbstractRequest<?> request) {
|
||||
if (((request instanceof WriteRequest) && Objects.equal(Thread.currentThread(), readerThreadRef.get()))) {
|
||||
super.addRequest(request);
|
||||
addedFromReader.countDown();
|
||||
Uninterruptibles.awaitUninterruptibly(submittedFromMain, 100, TimeUnit.MILLISECONDS);
|
||||
} else {
|
||||
super.addRequest(request);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void submitRequest(final AbstractRequest<?> request) {
|
||||
if (((request instanceof WriteRequest) && Objects.equal(Thread.currentThread(), mainThread))) {
|
||||
super.submitRequest(request);
|
||||
submittedFromMain.countDown();
|
||||
} else {
|
||||
super.submitRequest(request);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CompletableFuture<Void> cancel() {
|
||||
CompletableFuture<Void> _xblockexpression = null;
|
||||
{
|
||||
Thread _currentThread = Thread.currentThread();
|
||||
boolean _equals = Objects.equal(_currentThread, mainThread);
|
||||
if (_equals) {
|
||||
Uninterruptibles.awaitUninterruptibly(addedFromReader, 100, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
_xblockexpression = super.cancel();
|
||||
}
|
||||
return _xblockexpression;
|
||||
}
|
||||
};
|
||||
final CountDownLatch threadSet = new CountDownLatch(1);
|
||||
final Function1<CancelIndicator, CompletableFuture<Object>> _function = (CancelIndicator it) -> {
|
||||
readerThreadRef.set(Thread.currentThread());
|
||||
threadSet.countDown();
|
||||
final Function0<Object> _function_1 = () -> {
|
||||
return null;
|
||||
};
|
||||
final Function2<CancelIndicator, Object, Object> _function_2 = (CancelIndicator $0, Object $1) -> {
|
||||
return null;
|
||||
};
|
||||
return myRequestManager.<Object, Object>runWrite(_function_1, _function_2);
|
||||
};
|
||||
final CompletableFuture<CompletableFuture<Object>> readResult = myRequestManager.<CompletableFuture<Object>>runRead(_function);
|
||||
Uninterruptibles.awaitUninterruptibly(threadSet);
|
||||
Assert.assertNotNull(readerThreadRef.get());
|
||||
final Function0<Object> _function_1 = () -> {
|
||||
return null;
|
||||
};
|
||||
final Function2<CancelIndicator, Object, Object> _function_2 = (CancelIndicator $0, Object $1) -> {
|
||||
return null;
|
||||
};
|
||||
final CompletableFuture<Object> writeResult = myRequestManager.<Object, Object>runWrite(_function_1, _function_2);
|
||||
final CompletableFuture<Object> writeFromReader = readResult.get();
|
||||
try {
|
||||
writeFromReader.get();
|
||||
try {
|
||||
writeResult.get();
|
||||
} catch (final Throwable _t) {
|
||||
if (_t instanceof CancellationException) {
|
||||
Assert.assertTrue(writeFromReader.isDone());
|
||||
Assert.assertTrue(writeResult.isDone());
|
||||
boolean _isCancelled = writeFromReader.isCancelled();
|
||||
boolean _isCancelled_1 = writeResult.isCancelled();
|
||||
boolean _notEquals = (_isCancelled != _isCancelled_1);
|
||||
Assert.assertTrue(_notEquals);
|
||||
} else {
|
||||
throw Exceptions.sneakyThrow(_t);
|
||||
}
|
||||
}
|
||||
} catch (final Throwable _t) {
|
||||
if (_t instanceof CancellationException) {
|
||||
writeResult.get();
|
||||
Assert.assertTrue(writeFromReader.isDone());
|
||||
Assert.assertTrue(writeResult.isDone());
|
||||
boolean _isCancelled = writeFromReader.isCancelled();
|
||||
boolean _isCancelled_1 = writeResult.isCancelled();
|
||||
boolean _notEquals = (_isCancelled != _isCancelled_1);
|
||||
Assert.assertTrue(_notEquals);
|
||||
} else {
|
||||
throw Exceptions.sneakyThrow(_t);
|
||||
}
|
||||
}
|
||||
} catch (Throwable _e) {
|
||||
throw Exceptions.sneakyThrow(_e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,261 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2012, 2020 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.resource;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
import org.junit.Test;
|
||||
|
||||
public abstract class AbstractXtextResourceSetTest extends AbstractResourceSetTest {
|
||||
|
||||
@Override
|
||||
protected abstract XtextResourceSet createEmptyResourceSet();
|
||||
|
||||
@Test
|
||||
public void testResourcesAreInMap() {
|
||||
XtextResourceSet rs = createEmptyResourceSet();
|
||||
|
||||
assertEquals(0, rs.getURIResourceMap().size());
|
||||
|
||||
XtextResource resource = new XtextResource();
|
||||
resource.setURI(URI.createFileURI(new File("foo").getAbsolutePath()));
|
||||
|
||||
rs.getResources().add(resource);
|
||||
|
||||
assertEquals(1, rs.getURIResourceMap().size());
|
||||
|
||||
rs.getResources().remove(resource);
|
||||
|
||||
assertTrue(resource.eAdapters().isEmpty());
|
||||
assertEquals(0, rs.getURIResourceMap().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResourcesAreInMap_02() {
|
||||
XtextResourceSet rs = createEmptyResourceSet();
|
||||
|
||||
assertEquals(0, rs.getURIResourceMap().size());
|
||||
|
||||
XtextResource resource = new XtextResource();
|
||||
resource.setURI(URI.createFileURI(new File("foo").getAbsolutePath()));
|
||||
|
||||
rs.getResources().add(resource);
|
||||
|
||||
assertEquals(1, rs.getURIResourceMap().size());
|
||||
|
||||
rs.getResources().remove(resource);
|
||||
|
||||
assertTrue(resource.eAdapters().isEmpty());
|
||||
assertEquals(0, rs.getURIResourceMap().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResourcesAreInMap_03() {
|
||||
XtextResourceSet rs = createEmptyResourceSet();
|
||||
|
||||
assertEquals(0, rs.getURIResourceMap().size());
|
||||
|
||||
XtextResource resource = new XtextResource();
|
||||
|
||||
rs.getResources().add(resource);
|
||||
assertEquals(1, rs.getURIResourceMap().size());
|
||||
assertEquals(resource, rs.getURIResourceMap().get(null));
|
||||
|
||||
// set the URI
|
||||
resource.setURI(URI.createFileURI(new File("foo").getAbsolutePath()));
|
||||
assertEquals(1, rs.getURIResourceMap().size());
|
||||
assertFalse(rs.getURIResourceMap().containsKey(null));
|
||||
assertEquals(resource, rs.getURIResourceMap().get(resource.getURI()));
|
||||
assertEquals(resource, rs.getURIResourceMap().get(rs.getURIConverter().normalize(resource.getURI())));
|
||||
|
||||
// set the URI
|
||||
resource.setURI(URI.createFileURI(new File("bar").getAbsolutePath()));
|
||||
assertEquals(1, rs.getURIResourceMap().size());
|
||||
assertFalse(rs.getURIResourceMap().containsKey(null));
|
||||
assertEquals(resource, rs.getURIResourceMap().get(resource.getURI()));
|
||||
assertEquals(resource, rs.getURIResourceMap().get(rs.getURIConverter().normalize(resource.getURI())));
|
||||
|
||||
// set the URI back to null
|
||||
resource.setURI(null);
|
||||
assertEquals(1, rs.getURIResourceMap().size());
|
||||
assertEquals(resource, rs.getURIResourceMap().get(null));
|
||||
|
||||
// remove the resource
|
||||
rs.getResources().remove(resource);
|
||||
|
||||
assertTrue(resource.eAdapters().isEmpty());
|
||||
assertEquals(0, rs.getURIResourceMap().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResourcesAreCleared_01() {
|
||||
XtextResourceSet rs = createEmptyResourceSet();
|
||||
|
||||
assertEquals(0, rs.getURIResourceMap().size());
|
||||
|
||||
XtextResource resource = new XtextResource();
|
||||
resource.setURI(URI.createFileURI(new File("foo").getAbsolutePath()));
|
||||
|
||||
rs.getResources().add(resource);
|
||||
assertEquals(1, rs.getURIResourceMap().size());
|
||||
|
||||
rs.getResources().clear();
|
||||
|
||||
assertTrue(resource.eAdapters().isEmpty());
|
||||
assertEquals(0, rs.getURIResourceMap().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResourcesAreClearedWithDeliverFalse_01() {
|
||||
XtextResourceSet rs = createEmptyResourceSet();
|
||||
|
||||
assertEquals(0, rs.getURIResourceMap().size());
|
||||
|
||||
XtextResource resource = new XtextResource();
|
||||
resource.setURI(URI.createFileURI(new File("foo").getAbsolutePath()));
|
||||
|
||||
rs.getResources().add(resource);
|
||||
|
||||
assertEquals(1, rs.getURIResourceMap().size());
|
||||
rs.eSetDeliver(false);
|
||||
rs.getResources().clear();
|
||||
|
||||
assertTrue(resource.eAdapters().isEmpty());
|
||||
assertEquals(0, rs.getURIResourceMap().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResourcesAreInMapWithNormalizedURI_01() {
|
||||
XtextResourceSet rs = createEmptyResourceSet();
|
||||
|
||||
assertEquals(0, rs.getURIResourceMap().size());
|
||||
|
||||
XtextResource resource = new XtextResource();
|
||||
resource.setURI(URI.createURI("/a/../foo"));
|
||||
|
||||
rs.getResources().add(resource);
|
||||
|
||||
assertEquals(2, rs.getURIResourceMap().size());
|
||||
|
||||
rs.getResources().remove(resource);
|
||||
|
||||
assertTrue(resource.eAdapters().isEmpty());
|
||||
assertEquals(0, rs.getURIResourceMap().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResourcesAreInMapWithNormalizedURI_02() {
|
||||
XtextResourceSet rs = createEmptyResourceSet();
|
||||
|
||||
assertEquals(0, rs.getURIResourceMap().size());
|
||||
|
||||
XtextResource resource = new XtextResource();
|
||||
resource.setURI(URI.createURI("/a/../foo"));
|
||||
|
||||
rs.getResources().add(resource);
|
||||
|
||||
assertEquals(2, rs.getURIResourceMap().size());
|
||||
|
||||
rs.getResources().remove(resource);
|
||||
|
||||
assertTrue(resource.eAdapters().isEmpty());
|
||||
assertEquals(0, rs.getURIResourceMap().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResourcesAreInMapWithNormalizedURI_03() {
|
||||
XtextResourceSet rs = createEmptyResourceSet();
|
||||
|
||||
assertEquals(0, rs.getURIResourceMap().size());
|
||||
|
||||
XtextResource resource = new XtextResource();
|
||||
|
||||
rs.getResources().add(resource);
|
||||
assertEquals(1, rs.getURIResourceMap().size());
|
||||
assertEquals(resource, rs.getURIResourceMap().get(null));
|
||||
|
||||
assertEquals(0, rs.getNormalizationMap().size());
|
||||
|
||||
// set the URI
|
||||
resource.setURI(URI.createURI("/a/../foo"));
|
||||
assertEquals(2, rs.getURIResourceMap().size());
|
||||
assertFalse(rs.getURIResourceMap().containsKey(null));
|
||||
assertEquals(resource, rs.getURIResourceMap().get(resource.getURI()));
|
||||
assertEquals(resource, rs.getURIResourceMap().get(rs.getURIConverter().normalize(resource.getURI())));
|
||||
|
||||
assertEquals(1, rs.getNormalizationMap().size());
|
||||
assertEquals(rs.getURIConverter().normalize(resource.getURI()), rs.getNormalizationMap().get(resource.getURI()));
|
||||
|
||||
// set the URI
|
||||
resource.setURI(URI.createURI("/a/../bar"));
|
||||
assertEquals(2, rs.getURIResourceMap().size());
|
||||
assertFalse(rs.getURIResourceMap().containsKey(null));
|
||||
assertEquals(resource, rs.getURIResourceMap().get(resource.getURI()));
|
||||
assertEquals(resource, rs.getURIResourceMap().get(rs.getURIConverter().normalize(resource.getURI())));
|
||||
|
||||
assertEquals(1, rs.getNormalizationMap().size());
|
||||
assertEquals(rs.getURIConverter().normalize(resource.getURI()), rs.getNormalizationMap().get(resource.getURI()));
|
||||
|
||||
// set the URI back to null
|
||||
resource.setURI(null);
|
||||
assertEquals(1, rs.getURIResourceMap().size());
|
||||
assertEquals(resource, rs.getURIResourceMap().get(null));
|
||||
|
||||
assertEquals(0, rs.getNormalizationMap().size());
|
||||
|
||||
// remove the resource
|
||||
rs.getResources().remove(resource);
|
||||
|
||||
assertTrue(resource.eAdapters().isEmpty());
|
||||
assertEquals(0, rs.getURIResourceMap().size());
|
||||
|
||||
assertEquals(0, rs.getNormalizationMap().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResourcesAreClearedWithNormalizedURI_01() {
|
||||
XtextResourceSet rs = createEmptyResourceSet();
|
||||
|
||||
assertEquals(0, rs.getURIResourceMap().size());
|
||||
|
||||
XtextResource resource = new XtextResource();
|
||||
resource.setURI(URI.createURI("/a/../foo"));
|
||||
|
||||
rs.getResources().add(resource);
|
||||
|
||||
assertEquals(2, rs.getURIResourceMap().size());
|
||||
|
||||
rs.getResources().clear();
|
||||
|
||||
assertTrue(resource.eAdapters().isEmpty());
|
||||
assertEquals(0, rs.getURIResourceMap().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResourcesAreClearedWithDeliverFalseWithNormalizedURI_01() {
|
||||
XtextResourceSet rs = createEmptyResourceSet();
|
||||
|
||||
assertEquals(0, rs.getURIResourceMap().size());
|
||||
|
||||
XtextResource resource = new XtextResource();
|
||||
resource.setURI(URI.createURI("//a/../foo"));
|
||||
|
||||
rs.getResources().add(resource);
|
||||
|
||||
assertEquals(2, rs.getURIResourceMap().size());
|
||||
rs.eSetDeliver(false);
|
||||
rs.getResources().clear();
|
||||
|
||||
assertTrue(resource.eAdapters().isEmpty());
|
||||
assertEquals(0, rs.getURIResourceMap().size());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
/**
|
||||
* Copyright (c) 2012, 2020 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.resource;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
import org.eclipse.emf.ecore.resource.Resource;
|
||||
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
|
||||
import org.eclipse.xtext.xbase.lib.IntegerRange;
|
||||
import org.eclipse.xtext.xbase.lib.IterableExtensions;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Sets;
|
||||
|
||||
public class SynchronizedXtextResourceSetTest extends AbstractXtextResourceSetTest {
|
||||
@Override
|
||||
protected XtextResourceSet createEmptyResourceSet() {
|
||||
return new SynchronizedXtextResourceSet();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSynchronization() throws Exception {
|
||||
SynchronizedXtextResourceSet resourceSet = (SynchronizedXtextResourceSet) createEmptyResourceSet();
|
||||
Resource.Factory nullFactory = (URI uri) -> {
|
||||
NullResource result = new NullResource();
|
||||
result.setURI(uri);
|
||||
return result;
|
||||
};
|
||||
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xmi", nullFactory);
|
||||
List<Thread> threads = new ArrayList<>();
|
||||
new IntegerRange(1, 10).forEach((Integer i) -> {
|
||||
threads.add(new Thread(() -> {
|
||||
List<Resource> resources = CollectionLiterals.<Resource>newArrayList();
|
||||
for (int j = 0; j < 5000; j++) {
|
||||
Resource resource = resourceSet.createResource(URI.createURI(i + " " + j + ".xmi"));
|
||||
Assert.assertNotNull(resource);
|
||||
resources.add(resource);
|
||||
resource.setURI(URI.createURI(resource.getURI() + "b"));
|
||||
}
|
||||
}));
|
||||
});
|
||||
for (Thread thread : threads) {
|
||||
thread.start();
|
||||
}
|
||||
for (Thread thread : threads) {
|
||||
thread.join();
|
||||
}
|
||||
Assert.assertEquals(50000, resourceSet.getResources().size());
|
||||
Assert.assertEquals(Sets.newLinkedHashSet(resourceSet.getResources()).size(),
|
||||
Sets.newLinkedHashSet(resourceSet.getURIResourceMap().values()).size());
|
||||
Assert.assertEquals(
|
||||
Sets.newLinkedHashSet(Iterables.concat(Lists.transform(resourceSet.getResources(), (Resource it) -> {
|
||||
return Lists.<URI>newArrayList(it.getURI(), resourceSet.getURIConverter().normalize(it.getURI()));
|
||||
}))), resourceSet.getURIResourceMap().keySet());
|
||||
Assert.assertEquals(
|
||||
Joiner.on("\n")
|
||||
.join(IterableExtensions.sort(IterableExtensions.toList(Lists.<Resource, String>transform(
|
||||
resourceSet.getResources(), it -> it.getURI().toString())))),
|
||||
Joiner.on("\n").join(IterableExtensions.sort(IterableExtensions
|
||||
.toList(Iterables.transform(resourceSet.getNormalizationMap().keySet(), URI::toString)))));
|
||||
}
|
||||
}
|
|
@ -1,302 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2012, 2020 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.resource
|
||||
|
||||
import java.io.File
|
||||
import org.eclipse.emf.common.util.URI
|
||||
import org.eclipse.emf.ecore.resource.Resource
|
||||
import org.junit.Test
|
||||
|
||||
import static org.junit.Assert.*
|
||||
|
||||
abstract class AbstractXtextResourceSetTest extends AbstractResourceSetTest {
|
||||
|
||||
override protected XtextResourceSet createEmptyResourceSet()
|
||||
|
||||
@Test
|
||||
def void testResourcesAreInMap() {
|
||||
val rs = createEmptyResourceSet
|
||||
|
||||
assertEquals(0, rs.URIResourceMap.size)
|
||||
|
||||
val resource = new XtextResource
|
||||
resource.URI = URI::createFileURI(new File('foo').absolutePath)
|
||||
|
||||
rs.resources += resource
|
||||
|
||||
assertEquals(1, rs.URIResourceMap.size)
|
||||
|
||||
rs.resources.remove(resource)
|
||||
|
||||
assertTrue(resource.eAdapters.empty)
|
||||
assertEquals(0, rs.URIResourceMap.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
def void testResourcesAreInMap_02() {
|
||||
val rs = createEmptyResourceSet
|
||||
|
||||
assertEquals(0, rs.URIResourceMap.size)
|
||||
|
||||
val resource = new XtextResource
|
||||
resource.URI = URI::createFileURI(new File('foo').absolutePath)
|
||||
|
||||
rs.resources += newArrayList(resource)
|
||||
|
||||
assertEquals(1, rs.URIResourceMap.size)
|
||||
|
||||
rs.resources.remove(resource)
|
||||
|
||||
assertTrue(resource.eAdapters.empty)
|
||||
assertEquals(0, rs.URIResourceMap.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
def void testResourcesAreInMap_03() {
|
||||
val rs = createEmptyResourceSet
|
||||
|
||||
assertEquals(0, rs.URIResourceMap.size)
|
||||
|
||||
val resource = new XtextResource
|
||||
|
||||
rs.resources += resource
|
||||
assertEquals(1, rs.URIResourceMap.size)
|
||||
assertEquals(resource, rs.URIResourceMap.get(null))
|
||||
|
||||
// set the URI
|
||||
resource.URI = URI::createFileURI(new File('foo').absolutePath)
|
||||
assertEquals(1, rs.URIResourceMap.size)
|
||||
assertFalse(rs.URIResourceMap.containsKey(null))
|
||||
assertEquals(resource, rs.URIResourceMap.get(resource.URI))
|
||||
assertEquals(resource, rs.URIResourceMap.get(rs.URIConverter.normalize(resource.URI)))
|
||||
|
||||
// set the URI
|
||||
resource.URI = URI::createFileURI(new File('bar').absolutePath)
|
||||
assertEquals(1, rs.URIResourceMap.size)
|
||||
assertFalse(rs.URIResourceMap.containsKey(null))
|
||||
assertEquals(resource, rs.URIResourceMap.get(resource.URI))
|
||||
assertEquals(resource, rs.URIResourceMap.get(rs.URIConverter.normalize(resource.URI)))
|
||||
|
||||
// set the URI back to null
|
||||
resource.URI = null
|
||||
assertEquals(1, rs.URIResourceMap.size)
|
||||
assertEquals(resource, rs.URIResourceMap.get(null))
|
||||
|
||||
// remove the resource
|
||||
rs.resources.remove(resource)
|
||||
|
||||
assertTrue(resource.eAdapters.empty)
|
||||
assertEquals(0, rs.URIResourceMap.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
def void testResourcesAreCleared_01() {
|
||||
val rs = createEmptyResourceSet
|
||||
|
||||
assertEquals(0, rs.URIResourceMap.size)
|
||||
|
||||
val resource = new XtextResource
|
||||
resource.URI = URI::createFileURI(new File('foo').absolutePath)
|
||||
|
||||
rs.resources += newArrayList(resource)
|
||||
assertEquals(1, rs.URIResourceMap.size)
|
||||
|
||||
rs.resources.clear
|
||||
|
||||
assertTrue(resource.eAdapters.empty)
|
||||
assertEquals(0, rs.URIResourceMap.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
def void testResourcesAreClearedWithDeliverFalse_01() {
|
||||
val rs = createEmptyResourceSet
|
||||
|
||||
assertEquals(0, rs.URIResourceMap.size)
|
||||
|
||||
val resource = new XtextResource
|
||||
resource.URI = URI::createFileURI(new File('foo').absolutePath)
|
||||
|
||||
rs.resources += newArrayList(resource)
|
||||
|
||||
assertEquals(1, rs.URIResourceMap.size)
|
||||
rs.eSetDeliver(false)
|
||||
rs.resources.clear
|
||||
|
||||
assertTrue(resource.eAdapters.empty)
|
||||
assertEquals(0, rs.URIResourceMap.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
def void testResourcesAreInMapWithNormalizedURI_01() {
|
||||
val rs = createEmptyResourceSet
|
||||
|
||||
assertEquals(0, rs.URIResourceMap.size)
|
||||
|
||||
val resource = new XtextResource
|
||||
resource.URI = URI::createURI('/a/../foo')
|
||||
|
||||
rs.resources += resource
|
||||
|
||||
assertEquals(2, rs.URIResourceMap.size)
|
||||
|
||||
rs.resources.remove(resource)
|
||||
|
||||
assertTrue(resource.eAdapters.empty)
|
||||
assertEquals(0, rs.URIResourceMap.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
def void testResourcesAreInMapWithNormalizedURI_02() {
|
||||
val rs = createEmptyResourceSet
|
||||
|
||||
assertEquals(0, rs.URIResourceMap.size)
|
||||
|
||||
val resource = new XtextResource
|
||||
resource.URI = URI::createURI('/a/../foo')
|
||||
|
||||
rs.resources += newArrayList(resource)
|
||||
|
||||
assertEquals(2, rs.URIResourceMap.size)
|
||||
|
||||
rs.resources.remove(resource)
|
||||
|
||||
assertTrue(resource.eAdapters.empty)
|
||||
assertEquals(0, rs.URIResourceMap.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
def void testResourcesAreInMapWithNormalizedURI_03() {
|
||||
val rs = createEmptyResourceSet
|
||||
|
||||
assertEquals(0, rs.URIResourceMap.size)
|
||||
|
||||
val resource = new XtextResource
|
||||
|
||||
rs.resources += resource
|
||||
assertEquals(1, rs.URIResourceMap.size)
|
||||
assertEquals(resource, rs.URIResourceMap.get(null))
|
||||
|
||||
assertEquals(0, rs.getNormalizationMap.size)
|
||||
|
||||
// set the URI
|
||||
resource.URI = URI::createURI('/a/../foo')
|
||||
assertEquals(2, rs.URIResourceMap.size)
|
||||
assertFalse(rs.URIResourceMap.containsKey(null))
|
||||
assertEquals(resource, rs.URIResourceMap.get(resource.URI))
|
||||
assertEquals(resource, rs.URIResourceMap.get(rs.URIConverter.normalize(resource.URI)))
|
||||
|
||||
assertEquals(1, rs.getNormalizationMap.size)
|
||||
assertEquals(rs.URIConverter.normalize(resource.URI), rs.getNormalizationMap.get(resource.URI))
|
||||
|
||||
// set the URI
|
||||
resource.URI = URI::createURI('/a/../bar')
|
||||
assertEquals(2, rs.URIResourceMap.size)
|
||||
assertFalse(rs.URIResourceMap.containsKey(null))
|
||||
assertEquals(resource, rs.URIResourceMap.get(resource.URI))
|
||||
assertEquals(resource, rs.URIResourceMap.get(rs.URIConverter.normalize(resource.URI)))
|
||||
|
||||
assertEquals(1, rs.getNormalizationMap.size)
|
||||
assertEquals(rs.URIConverter.normalize(resource.URI), rs.getNormalizationMap.get(resource.URI))
|
||||
|
||||
// set the URI back to null
|
||||
resource.URI = null
|
||||
assertEquals(1, rs.URIResourceMap.size)
|
||||
assertEquals(resource, rs.URIResourceMap.get(null))
|
||||
|
||||
assertEquals(0, rs.getNormalizationMap.size)
|
||||
|
||||
// remove the resource
|
||||
rs.resources.remove(resource)
|
||||
|
||||
assertTrue(resource.eAdapters.empty)
|
||||
assertEquals(0, rs.URIResourceMap.size)
|
||||
|
||||
assertEquals(0, rs.getNormalizationMap.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
def void testResourcesAreClearedWithNormalizedURI_01() {
|
||||
val rs = createEmptyResourceSet
|
||||
|
||||
assertEquals(0, rs.URIResourceMap.size)
|
||||
|
||||
val resource = new XtextResource
|
||||
resource.URI = URI::createURI('/a/../foo')
|
||||
|
||||
rs.resources += newArrayList(resource)
|
||||
|
||||
assertEquals(2, rs.URIResourceMap.size)
|
||||
|
||||
rs.resources.clear
|
||||
|
||||
assertTrue(resource.eAdapters.empty)
|
||||
assertEquals(0, rs.URIResourceMap.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
def void testResourcesAreClearedWithDeliverFalseWithNormalizedURI_01() {
|
||||
val rs = createEmptyResourceSet
|
||||
|
||||
assertEquals(0, rs.URIResourceMap.size)
|
||||
|
||||
val resource = new XtextResource
|
||||
resource.URI = URI::createURI('//a/../foo')
|
||||
|
||||
rs.resources += newArrayList(resource)
|
||||
|
||||
assertEquals(2, rs.URIResourceMap.size)
|
||||
rs.eSetDeliver(false)
|
||||
rs.resources.clear
|
||||
|
||||
assertTrue(resource.eAdapters.empty)
|
||||
assertEquals(0, rs.URIResourceMap.size)
|
||||
}
|
||||
}
|
||||
|
||||
class SynchronizedXtextResourceSetTest extends AbstractXtextResourceSetTest {
|
||||
|
||||
override protected createEmptyResourceSet() {
|
||||
new SynchronizedXtextResourceSet
|
||||
}
|
||||
|
||||
@Test
|
||||
def void testSynchronization() {
|
||||
val resourceSet = createEmptyResourceSet as SynchronizedXtextResourceSet
|
||||
val Resource.Factory nullFactory = [ uri |
|
||||
val result = new NullResource
|
||||
result.URI = uri
|
||||
result
|
||||
]
|
||||
resourceSet.resourceFactoryRegistry.extensionToFactoryMap.put('xmi', nullFactory)
|
||||
val threads = newArrayList()
|
||||
(1..10).forEach [ i |
|
||||
threads.add(new Thread [
|
||||
val resources = newArrayList
|
||||
for(var int j = 0; j < 5000; j++) {
|
||||
val resource = resourceSet.createResource(URI.createURI(i + " " + j + ".xmi"))
|
||||
assertNotNull(resource)
|
||||
resources += resource
|
||||
resource.URI = URI.createURI(resource.getURI + 'b')
|
||||
}
|
||||
])
|
||||
]
|
||||
for (Thread thread : threads) {
|
||||
thread.start();
|
||||
}
|
||||
|
||||
for (Thread thread : threads) {
|
||||
thread.join();
|
||||
}
|
||||
assertEquals(50000, resourceSet.resources.size)
|
||||
assertEquals(resourceSet.resources.toSet.size, resourceSet.URIResourceMap.values.toSet.size)
|
||||
assertEquals(resourceSet.resources.map[ #[getURI, resourceSet.URIConverter.normalize(URI) ] ].flatten.toSet, resourceSet.URIResourceMap.keySet)
|
||||
assertEquals(resourceSet.resources.map[ getURI.toString ].toList.sort.join('\n'), resourceSet.getNormalizationMap.keySet.map[toString].toList.sort.join('\n'))
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,265 @@
|
|||
/**
|
||||
* Copyright (c) 2017, 2020 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.resource.containers;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
import org.eclipse.emf.ecore.resource.Resource;
|
||||
import org.eclipse.xtext.naming.QualifiedName;
|
||||
import org.eclipse.xtext.resource.IEObjectDescription;
|
||||
import org.eclipse.xtext.resource.IResourceDescription;
|
||||
import org.eclipse.xtext.resource.LiveContainerTestLanguageInjectorProvider;
|
||||
import org.eclipse.xtext.resource.XtextResourceSet;
|
||||
import org.eclipse.xtext.resource.impl.ChunkedResourceDescriptions;
|
||||
import org.eclipse.xtext.resource.impl.LiveShadowedChunkedResourceDescriptions;
|
||||
import org.eclipse.xtext.resource.impl.ResourceDescriptionsData;
|
||||
import org.eclipse.xtext.resource.liveContainerTestLanguage.LiveContainerTestLanguagePackage;
|
||||
import org.eclipse.xtext.resource.liveContainerTestLanguage.Model;
|
||||
import org.eclipse.xtext.testing.InjectWith;
|
||||
import org.eclipse.xtext.testing.XtextRunner;
|
||||
import org.eclipse.xtext.testing.util.ParseHelper;
|
||||
import org.eclipse.xtext.workspace.ProjectConfigAdapter;
|
||||
import org.eclipse.xtext.workspace.WorkspaceConfig;
|
||||
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
|
||||
import org.eclipse.xtext.xbase.lib.IterableExtensions;
|
||||
import org.eclipse.xtext.xbase.lib.ListExtensions;
|
||||
import org.eclipse.xtext.xbase.lib.Pair;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
|
||||
/**
|
||||
* @author koehnlein - Initial contribution and API
|
||||
*/
|
||||
@RunWith(XtextRunner.class)
|
||||
@InjectWith(LiveContainerTestLanguageInjectorProvider.class)
|
||||
public class LiveShadowedChunkedContainerTest {
|
||||
@Inject
|
||||
private ParseHelper<Model> parseHelper;
|
||||
|
||||
@Inject
|
||||
private Provider<XtextResourceSet> resourceSetProvider;
|
||||
|
||||
@Inject
|
||||
private IResourceDescription.Manager resourceDescriptionManager;
|
||||
|
||||
@Inject
|
||||
private Provider<LiveShadowedChunkedResourceDescriptions> provider;
|
||||
|
||||
private WorkspaceConfig workspaceConfig;
|
||||
|
||||
private ProjectConfig fooProject;
|
||||
|
||||
private ProjectConfig barProject;
|
||||
|
||||
private URI fooURI;
|
||||
|
||||
private URI barURI;
|
||||
|
||||
private XtextResourceSet rs1;
|
||||
|
||||
private LiveShadowedChunkedContainer fooContainer;
|
||||
|
||||
private LiveShadowedChunkedContainer barContainer;
|
||||
|
||||
private LiveShadowedChunkedResourceDescriptions liveShadowedChunkedResourceDescriptions;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
workspaceConfig = new WorkspaceConfig();
|
||||
fooProject = new ProjectConfig("foo", workspaceConfig);
|
||||
barProject = new ProjectConfig("bar", workspaceConfig);
|
||||
XtextResourceSet rs0 = resourceSetProvider.get();
|
||||
fooURI = IterableExtensions.head(fooProject.getSourceFolders()).getPath().trimSegments(1)
|
||||
.appendSegment("foo.livecontainertestlanguage");
|
||||
barURI = IterableExtensions.head(barProject.getSourceFolders()).getPath().trimSegments(1)
|
||||
.appendSegment("bar.livecontainertestlanguage");
|
||||
Map<String, ResourceDescriptionsData> chunks = Collections.unmodifiableMap(CollectionLiterals.newHashMap(
|
||||
Pair.of("foo",
|
||||
createResourceDescriptionData(
|
||||
Collections.singletonList(parseHelper.parse("foo", fooURI, rs0).eResource()))),
|
||||
Pair.of("bar", createResourceDescriptionData(
|
||||
Collections.singletonList(parseHelper.parse("bar", barURI, rs0).eResource())))));
|
||||
rs1 = resourceSetProvider.get();
|
||||
new ChunkedResourceDescriptions(chunks, rs1);
|
||||
ProjectConfigAdapter.install(rs1, fooProject);
|
||||
liveShadowedChunkedResourceDescriptions = provider.get();
|
||||
liveShadowedChunkedResourceDescriptions.setContext(rs1);
|
||||
fooContainer = new LiveShadowedChunkedContainer(liveShadowedChunkedResourceDescriptions, "foo");
|
||||
barContainer = new LiveShadowedChunkedContainer(liveShadowedChunkedResourceDescriptions, "bar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRenameElement() throws Exception {
|
||||
parseHelper.parse("fooChange", fooURI, rs1);
|
||||
Assert.assertEquals("fooChange", IterableExtensions.join(
|
||||
IterableExtensions.map(fooContainer.getExportedObjects(), it -> it.getQualifiedName().toString()),
|
||||
","));
|
||||
Assert.assertEquals("fooChange",
|
||||
IterableExtensions
|
||||
.join(IterableExtensions.map(
|
||||
fooContainer.getExportedObjects(LiveContainerTestLanguagePackage.Literals.MODEL,
|
||||
QualifiedName.create("fooChange"), false),
|
||||
it -> it.getQualifiedName().toString()), ","));
|
||||
Assert.assertEquals("fooChange",
|
||||
IterableExtensions.join(IterableExtensions.map(
|
||||
fooContainer.getExportedObjectsByType(LiveContainerTestLanguagePackage.Literals.MODEL),
|
||||
it -> it.getQualifiedName().toString()), ","));
|
||||
Assert.assertEquals(1, IterableExtensions.size(fooContainer.getResourceDescriptions()));
|
||||
Assert.assertEquals(1, fooContainer.getResourceDescriptionCount());
|
||||
Assert.assertTrue(fooContainer.hasResourceDescription(fooURI));
|
||||
Assert.assertFalse(fooContainer.hasResourceDescription(barURI));
|
||||
Assert.assertEquals(fooURI, fooContainer.getResourceDescription(fooURI).getURI());
|
||||
Assert.assertEquals("bar", IterableExtensions.join(
|
||||
IterableExtensions.map(barContainer.getExportedObjects(), it -> it.getQualifiedName().toString()),
|
||||
","));
|
||||
Assert.assertEquals("bar",
|
||||
IterableExtensions.join(IterableExtensions
|
||||
.map(barContainer.getExportedObjects(LiveContainerTestLanguagePackage.Literals.MODEL,
|
||||
QualifiedName.create("bar"), false), it -> it.getQualifiedName().toString()),
|
||||
","));
|
||||
Assert.assertEquals("bar",
|
||||
exportedObjects(barContainer));
|
||||
Assert.assertEquals(1, IterableExtensions.size(barContainer.getResourceDescriptions()));
|
||||
Assert.assertEquals(1, barContainer.getResourceDescriptionCount());
|
||||
Assert.assertTrue(barContainer.hasResourceDescription(barURI));
|
||||
Assert.assertFalse(barContainer.hasResourceDescription(fooURI));
|
||||
Assert.assertEquals(barURI, barContainer.getResourceDescription(barURI).getURI());
|
||||
assertGlobalDescriptionsAreUnaffected();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddRemoveResource() throws Exception {
|
||||
Resource bazResource = parseHelper
|
||||
.parse("baz", IterableExtensions.head(fooProject.getSourceFolders()).getPath()
|
||||
.trimSegments(1).appendSegment("baz.livecontainertestlanguage"), rs1)
|
||||
.eResource();
|
||||
Assert.assertEquals(2, IterableExtensions.size(fooContainer.getResourceDescriptions()));
|
||||
Assert.assertEquals(2, fooContainer.getResourceDescriptionCount());
|
||||
Assert.assertEquals("baz,foo", exportedObjects(fooContainer));
|
||||
Assert.assertEquals(1, IterableExtensions.size(barContainer.getResourceDescriptions()));
|
||||
Assert.assertEquals(1, barContainer.getResourceDescriptionCount());
|
||||
assertGlobalDescriptionsAreUnaffected();
|
||||
rs1.getResources().remove(bazResource);
|
||||
Assert.assertEquals(1, IterableExtensions.size(fooContainer.getResourceDescriptions()));
|
||||
Assert.assertEquals(1, fooContainer.getResourceDescriptionCount());
|
||||
Assert.assertEquals("foo", exportedObjects(fooContainer));
|
||||
Assert.assertEquals(1, IterableExtensions.size(barContainer.getResourceDescriptions()));
|
||||
Assert.assertEquals(1, barContainer.getResourceDescriptionCount());
|
||||
assertGlobalDescriptionsAreUnaffected();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMoveResourceBetweenContainers() throws Exception {
|
||||
URI oldURI = IterableExtensions.head(fooProject.getSourceFolders()).getPath().trimSegments(1)
|
||||
.appendSegment("baz.livecontainertestlanguage");
|
||||
Resource bazResource = parseHelper.parse("baz", oldURI, rs1).eResource();
|
||||
Assert.assertEquals(2, IterableExtensions.size(fooContainer.getResourceDescriptions()));
|
||||
Assert.assertEquals(2, fooContainer.getResourceDescriptionCount());
|
||||
Assert.assertEquals("baz,foo", exportedObjects(fooContainer));
|
||||
Assert.assertEquals(oldURI,
|
||||
IterableExtensions
|
||||
.head(fooContainer.getExportedObjects(
|
||||
LiveContainerTestLanguagePackage.Literals.MODEL, QualifiedName.create("baz"), false))
|
||||
.getEObjectURI().trimFragment());
|
||||
Assert.assertEquals(1, IterableExtensions.size(barContainer.getResourceDescriptions()));
|
||||
Assert.assertEquals(1, barContainer.getResourceDescriptionCount());
|
||||
assertGlobalDescriptionsAreUnaffected();
|
||||
URI newURI = URI.createURI(bazResource.getURI().toString().replace("/foo/", "/bar/"));
|
||||
bazResource.setURI(newURI);
|
||||
Assert.assertEquals(1, IterableExtensions.size(fooContainer.getResourceDescriptions()));
|
||||
Assert.assertEquals(1, fooContainer.getResourceDescriptionCount());
|
||||
Assert.assertEquals("foo", exportedObjects(fooContainer));
|
||||
Assert.assertEquals(1, IterableExtensions.size(fooContainer.getResourceDescriptions()));
|
||||
Assert.assertEquals(1, fooContainer.getResourceDescriptionCount());
|
||||
Assert.assertEquals("bar,baz", IterableExtensions.join(IterableExtensions.sort(
|
||||
IterableExtensions.<IEObjectDescription, String>map(barContainer.getExportedObjects(), it -> it.getQualifiedName().toString())),
|
||||
","));
|
||||
Assert.assertEquals(2, IterableExtensions.size(barContainer.getResourceDescriptions()));
|
||||
Assert.assertEquals(2, barContainer.getResourceDescriptionCount());
|
||||
Assert.assertEquals(newURI,
|
||||
IterableExtensions
|
||||
.head(barContainer.getExportedObjects(
|
||||
LiveContainerTestLanguagePackage.Literals.MODEL, QualifiedName.create("baz"), false))
|
||||
.getEObjectURI().trimFragment());
|
||||
assertGlobalDescriptionsAreUnaffected();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddToNewContainer() throws Exception {
|
||||
ProjectConfig bazProject = new ProjectConfig("baz", workspaceConfig);
|
||||
URI newURI = IterableExtensions.head(bazProject.getSourceFolders()).getPath().trimSegments(1)
|
||||
.appendSegment("baz.livecontainertestlanguage");
|
||||
parseHelper.parse("baz", newURI, rs1);
|
||||
LiveShadowedChunkedContainer bazContainer = new LiveShadowedChunkedContainer(
|
||||
liveShadowedChunkedResourceDescriptions, "baz");
|
||||
Assert.assertEquals(1, IterableExtensions.size(fooContainer.getResourceDescriptions()));
|
||||
Assert.assertEquals(1, fooContainer.getResourceDescriptionCount());
|
||||
Assert.assertEquals("foo", exportedObjects(fooContainer));
|
||||
Assert.assertEquals(1, IterableExtensions.size(barContainer.getResourceDescriptions()));
|
||||
Assert.assertEquals(1, barContainer.getResourceDescriptionCount());
|
||||
Assert.assertEquals("bar", exportedObjects(barContainer));
|
||||
Assert.assertEquals(1, IterableExtensions.size(bazContainer.getResourceDescriptions()));
|
||||
Assert.assertEquals(1, bazContainer.getResourceDescriptionCount());
|
||||
Assert.assertEquals("baz", exportedObjects(bazContainer));
|
||||
Assert.assertEquals(newURI,
|
||||
IterableExtensions
|
||||
.head(bazContainer.getExportedObjects(
|
||||
LiveContainerTestLanguagePackage.Literals.MODEL, QualifiedName.create("baz"), false))
|
||||
.getEObjectURI().trimFragment());
|
||||
assertGlobalDescriptionsAreUnaffected();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteElement() throws Exception {
|
||||
parseHelper.parse("foo", fooURI, rs1).eResource().getContents().clear();
|
||||
Assert.assertEquals("", IterableExtensions.join(
|
||||
IterableExtensions.map(fooContainer.getExportedObjects(), it -> it.getQualifiedName().toString()),
|
||||
","));
|
||||
Assert.assertEquals(1, IterableExtensions.size(fooContainer.getResourceDescriptions()));
|
||||
Assert.assertEquals(1, fooContainer.getResourceDescriptionCount());
|
||||
Assert.assertEquals(0, IterableExtensions.size(fooContainer.getExportedObjects()));
|
||||
Assert.assertEquals(1, IterableExtensions.size(barContainer.getResourceDescriptions()));
|
||||
Assert.assertEquals(1, barContainer.getResourceDescriptionCount());
|
||||
assertGlobalDescriptionsAreUnaffected();
|
||||
}
|
||||
|
||||
private String exportedObjects(LiveShadowedChunkedContainer container) {
|
||||
return IterableExtensions.join(IterableExtensions.sort(
|
||||
IterableExtensions.<IEObjectDescription, String>map(container.getExportedObjects(), it -> it.getQualifiedName().toString())),
|
||||
",");
|
||||
}
|
||||
|
||||
private void assertGlobalDescriptionsAreUnaffected() {
|
||||
Assert.assertEquals("foo", IterableExtensions.join(
|
||||
IterableExtensions.map(
|
||||
((ChunkedResourceDescriptions) liveShadowedChunkedResourceDescriptions.getGlobalDescriptions())
|
||||
.getContainer("foo").getExportedObjects(),
|
||||
it -> it.getQualifiedName().toString()),
|
||||
","));
|
||||
Assert.assertEquals("bar", IterableExtensions.join(
|
||||
IterableExtensions.map(
|
||||
((ChunkedResourceDescriptions) liveShadowedChunkedResourceDescriptions.getGlobalDescriptions())
|
||||
.getContainer("bar").getExportedObjects(),
|
||||
it -> it.getQualifiedName().toString()),
|
||||
","));
|
||||
}
|
||||
|
||||
private ResourceDescriptionsData createResourceDescriptionData(List<Resource> resources) {
|
||||
return new ResourceDescriptionsData(
|
||||
ListExtensions.map(resources, it -> resourceDescriptionManager.getResourceDescription(it)));
|
||||
}
|
||||
}
|
|
@ -1,191 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2017, 2020 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.resource.containers
|
||||
|
||||
import com.google.inject.Inject
|
||||
import com.google.inject.Provider
|
||||
import org.eclipse.emf.common.util.URI
|
||||
import org.eclipse.emf.ecore.resource.Resource
|
||||
import org.eclipse.xtext.naming.QualifiedName
|
||||
import org.eclipse.xtext.resource.IResourceDescription
|
||||
import org.eclipse.xtext.resource.LiveContainerTestLanguageInjectorProvider
|
||||
import org.eclipse.xtext.resource.XtextResourceSet
|
||||
import org.eclipse.xtext.resource.impl.ChunkedResourceDescriptions
|
||||
import org.eclipse.xtext.resource.impl.LiveShadowedChunkedResourceDescriptions
|
||||
import org.eclipse.xtext.resource.impl.ResourceDescriptionsData
|
||||
import org.eclipse.xtext.resource.liveContainerTestLanguage.Model
|
||||
import org.eclipse.xtext.testing.InjectWith
|
||||
import org.eclipse.xtext.testing.XtextRunner
|
||||
import org.eclipse.xtext.testing.util.ParseHelper
|
||||
import org.eclipse.xtext.workspace.ProjectConfigAdapter
|
||||
import org.eclipse.xtext.workspace.WorkspaceConfig
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
|
||||
import static org.eclipse.xtext.resource.liveContainerTestLanguage.LiveContainerTestLanguagePackage.Literals.*
|
||||
import static org.junit.Assert.*
|
||||
|
||||
/**
|
||||
* @author koehnlein - Initial contribution and API
|
||||
*/
|
||||
@RunWith(XtextRunner)
|
||||
@InjectWith(LiveContainerTestLanguageInjectorProvider)
|
||||
class LiveShadowedChunkedContainerTest {
|
||||
|
||||
@Inject extension ParseHelper<Model>
|
||||
@Inject Provider<XtextResourceSet> resourceSetProvider
|
||||
@Inject IResourceDescription.Manager resourceDescriptionManager
|
||||
@Inject Provider<LiveShadowedChunkedResourceDescriptions> provider
|
||||
|
||||
WorkspaceConfig workspaceConfig
|
||||
ProjectConfig fooProject
|
||||
ProjectConfig barProject
|
||||
|
||||
URI fooURI
|
||||
URI barURI
|
||||
|
||||
XtextResourceSet rs1
|
||||
|
||||
LiveShadowedChunkedContainer fooContainer
|
||||
LiveShadowedChunkedContainer barContainer
|
||||
|
||||
LiveShadowedChunkedResourceDescriptions liveShadowedChunkedResourceDescriptions
|
||||
|
||||
@Before
|
||||
def void setUp() {
|
||||
workspaceConfig = new WorkspaceConfig()
|
||||
fooProject = new ProjectConfig('foo', workspaceConfig)
|
||||
barProject = new ProjectConfig('bar', workspaceConfig)
|
||||
val rs0 = resourceSetProvider.get
|
||||
fooURI = fooProject.sourceFolders.head.path.trimSegments(1).appendSegment('foo.livecontainertestlanguage')
|
||||
barURI = barProject.sourceFolders.head.path.trimSegments(1).appendSegment('bar.livecontainertestlanguage')
|
||||
val chunks = #{
|
||||
'foo' -> createResourceDescriptionData('foo'.parse(fooURI, rs0).eResource),
|
||||
'bar' -> createResourceDescriptionData('bar'.parse(barURI, rs0).eResource)
|
||||
}
|
||||
rs1 = resourceSetProvider.get
|
||||
new ChunkedResourceDescriptions(chunks, rs1)
|
||||
ProjectConfigAdapter.install(rs1, fooProject)
|
||||
liveShadowedChunkedResourceDescriptions = provider.get
|
||||
liveShadowedChunkedResourceDescriptions.context = rs1
|
||||
fooContainer = new LiveShadowedChunkedContainer(liveShadowedChunkedResourceDescriptions, 'foo')
|
||||
barContainer = new LiveShadowedChunkedContainer(liveShadowedChunkedResourceDescriptions, 'bar')
|
||||
}
|
||||
|
||||
@Test
|
||||
def testRenameElement() {
|
||||
'fooChange'.parse(fooURI, rs1)
|
||||
assertEquals('fooChange', fooContainer.exportedObjects.map[qualifiedName.toString].join(','))
|
||||
assertEquals('fooChange', fooContainer.getExportedObjects(MODEL, QualifiedName.create('fooChange'), false).map[qualifiedName.toString].join(','))
|
||||
assertEquals('fooChange', fooContainer.getExportedObjectsByType(MODEL).map[qualifiedName.toString].join(','))
|
||||
assertEquals(1, fooContainer.resourceDescriptions.size)
|
||||
assertEquals(1, fooContainer.resourceDescriptionCount)
|
||||
assertTrue(fooContainer.hasResourceDescription(fooURI))
|
||||
assertFalse(fooContainer.hasResourceDescription(barURI))
|
||||
assertEquals(fooURI, fooContainer.getResourceDescription(fooURI).URI)
|
||||
|
||||
assertEquals('bar', barContainer.exportedObjects.map[qualifiedName.toString].join(','))
|
||||
assertEquals('bar', barContainer.getExportedObjects(MODEL, QualifiedName.create('bar'), false).map[qualifiedName.toString].join(','))
|
||||
assertEquals('bar', barContainer.getExportedObjectsByType(MODEL).map[qualifiedName.toString].join(','))
|
||||
assertEquals(1, barContainer.resourceDescriptions.size)
|
||||
assertEquals(1, barContainer.resourceDescriptionCount)
|
||||
assertTrue(barContainer.hasResourceDescription(barURI))
|
||||
assertFalse(barContainer.hasResourceDescription(fooURI))
|
||||
assertEquals(barURI, barContainer.getResourceDescription(barURI).URI)
|
||||
assertGlobalDescriptionsAreUnaffected()
|
||||
}
|
||||
|
||||
@Test
|
||||
def testAddRemoveResource() {
|
||||
val bazResource = 'baz'.parse(fooProject.sourceFolders.head.path.trimSegments(1).appendSegment('baz.livecontainertestlanguage'), rs1).eResource
|
||||
assertEquals(2, fooContainer.resourceDescriptions.size)
|
||||
assertEquals(2, fooContainer.resourceDescriptionCount)
|
||||
assertEquals('baz,foo', fooContainer.exportedObjects.map[qualifiedName.toString].sort.join(','))
|
||||
assertEquals(1, barContainer.resourceDescriptions.size)
|
||||
assertEquals(1, barContainer.resourceDescriptionCount)
|
||||
assertGlobalDescriptionsAreUnaffected()
|
||||
rs1.resources.remove(bazResource)
|
||||
assertEquals(1, fooContainer.resourceDescriptions.size)
|
||||
assertEquals(1, fooContainer.resourceDescriptionCount)
|
||||
assertEquals('foo', fooContainer.exportedObjects.map[qualifiedName.toString].sort.join(','))
|
||||
assertEquals(1, barContainer.resourceDescriptions.size)
|
||||
assertEquals(1, barContainer.resourceDescriptionCount)
|
||||
assertGlobalDescriptionsAreUnaffected()
|
||||
}
|
||||
|
||||
@Test
|
||||
def testMoveResourceBetweenContainers() {
|
||||
val oldURI = fooProject.sourceFolders.head.path.trimSegments(1).appendSegment('baz.livecontainertestlanguage')
|
||||
val bazResource = 'baz'.parse(oldURI, rs1).eResource
|
||||
assertEquals(2, fooContainer.resourceDescriptions.size)
|
||||
assertEquals(2, fooContainer.resourceDescriptionCount)
|
||||
assertEquals('baz,foo', fooContainer.exportedObjects.map[qualifiedName.toString].sort.join(','))
|
||||
assertEquals(oldURI, fooContainer.getExportedObjects(MODEL, QualifiedName.create('baz'), false).head.EObjectURI.trimFragment)
|
||||
assertEquals(1, barContainer.resourceDescriptions.size)
|
||||
assertEquals(1, barContainer.resourceDescriptionCount)
|
||||
assertGlobalDescriptionsAreUnaffected()
|
||||
val newURI = URI.createURI(bazResource.URI.toString().replace('/foo/', '/bar/'))
|
||||
bazResource.URI = newURI
|
||||
assertEquals(1, fooContainer.resourceDescriptions.size)
|
||||
assertEquals(1, fooContainer.resourceDescriptionCount)
|
||||
assertEquals('foo', fooContainer.exportedObjects.map[qualifiedName.toString].sort.join(','))
|
||||
assertEquals(1, fooContainer.resourceDescriptions.size)
|
||||
assertEquals(1, fooContainer.resourceDescriptionCount)
|
||||
assertEquals('bar,baz', barContainer.exportedObjects.map[qualifiedName.toString].sort.join(','))
|
||||
assertEquals(2, barContainer.resourceDescriptions.size)
|
||||
assertEquals(2, barContainer.resourceDescriptionCount)
|
||||
assertEquals(newURI, barContainer.getExportedObjects(MODEL, QualifiedName.create('baz'), false).head.EObjectURI.trimFragment)
|
||||
assertGlobalDescriptionsAreUnaffected()
|
||||
}
|
||||
|
||||
@Test
|
||||
def testAddToNewContainer() {
|
||||
val bazProject = new ProjectConfig('baz', workspaceConfig)
|
||||
val newURI = bazProject.sourceFolders.head.path.trimSegments(1).appendSegment('baz.livecontainertestlanguage')
|
||||
'baz'.parse(newURI, rs1)
|
||||
val bazContainer = new LiveShadowedChunkedContainer(liveShadowedChunkedResourceDescriptions, 'baz')
|
||||
assertEquals(1, fooContainer.resourceDescriptions.size)
|
||||
assertEquals(1, fooContainer.resourceDescriptionCount)
|
||||
assertEquals('foo', fooContainer.exportedObjects.map[qualifiedName.toString].sort.join(','))
|
||||
assertEquals(1, barContainer.resourceDescriptions.size)
|
||||
assertEquals(1, barContainer.resourceDescriptionCount)
|
||||
assertEquals('bar', barContainer.exportedObjects.map[qualifiedName.toString].sort.join(','))
|
||||
assertEquals(1, bazContainer.resourceDescriptions.size)
|
||||
assertEquals(1, bazContainer.resourceDescriptionCount)
|
||||
assertEquals('baz', bazContainer.exportedObjects.map[qualifiedName.toString].sort.join(','))
|
||||
assertEquals(newURI, bazContainer.getExportedObjects(MODEL, QualifiedName.create('baz'), false).head.EObjectURI.trimFragment)
|
||||
assertGlobalDescriptionsAreUnaffected()
|
||||
}
|
||||
|
||||
@Test
|
||||
def testDeleteElement() {
|
||||
'foo'.parse(fooURI, rs1).eResource.contents.clear()
|
||||
assertEquals('', fooContainer.exportedObjects.map[qualifiedName.toString].join(','))
|
||||
assertEquals(1, fooContainer.resourceDescriptions.size)
|
||||
assertEquals(1, fooContainer.resourceDescriptionCount)
|
||||
assertEquals(0, fooContainer.exportedObjects.size)
|
||||
assertEquals(1, barContainer.resourceDescriptions.size)
|
||||
assertEquals(1, barContainer.resourceDescriptionCount)
|
||||
assertGlobalDescriptionsAreUnaffected()
|
||||
}
|
||||
|
||||
private def void assertGlobalDescriptionsAreUnaffected() {
|
||||
assertEquals('foo', (liveShadowedChunkedResourceDescriptions.globalDescriptions as ChunkedResourceDescriptions).getContainer('foo').getExportedObjects.map[qualifiedName.toString].join(','))
|
||||
assertEquals('bar', (liveShadowedChunkedResourceDescriptions.globalDescriptions as ChunkedResourceDescriptions).getContainer('bar').getExportedObjects.map[qualifiedName.toString].join(','))
|
||||
}
|
||||
|
||||
|
||||
private def createResourceDescriptionData(Resource... resources) {
|
||||
new ResourceDescriptionsData(resources.map[
|
||||
resourceDescriptionManager.getResourceDescription(it)
|
||||
])
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,106 @@
|
|||
/**
|
||||
* Copyright (c) 2014, 2020 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.resource.persistence;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.emf.ecore.EcorePackage;
|
||||
import org.eclipse.emf.ecore.util.EcoreUtil;
|
||||
import org.eclipse.xtext.linking.LangATestLanguageStandaloneSetup;
|
||||
import org.eclipse.xtext.linking.langATestLanguage.Main;
|
||||
import org.eclipse.xtext.linking.langATestLanguage.Type;
|
||||
import org.eclipse.xtext.resource.IReferenceDescription;
|
||||
import org.eclipse.xtext.resource.XtextResourceSet;
|
||||
import org.eclipse.xtext.tests.AbstractXtextTests;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.collect.Iterables;
|
||||
|
||||
/**
|
||||
* @author Sven Efftinge - Initial contribution and API
|
||||
*/
|
||||
public class PortableURIsTest extends AbstractXtextTests {
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
with(new LangATestLanguageStandaloneSetup());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPortableUris() throws Exception {
|
||||
XtextResourceSet resourceSet = get(XtextResourceSet.class);
|
||||
StorageAwareResource resourceA = (StorageAwareResource) resourceSet
|
||||
.createResource(URI.createURI("hubba:/bubba.langatestlanguage"));
|
||||
StorageAwareResource resourceB = (StorageAwareResource) resourceSet
|
||||
.createResource(URI.createURI("hubba:/bubba2.langatestlanguage"));
|
||||
resourceB.load(getAsStream("type B"), null);
|
||||
resourceA.load(getAsStream("import 'hubba:/bubba2.langatestlanguage' type A extends B"), null);
|
||||
Type extended = Iterables
|
||||
.getFirst(Iterables.getFirst(Iterables.filter(resourceA.getContents(), Main.class), null).getTypes(),
|
||||
null)
|
||||
.getExtends();
|
||||
URI uri = EcoreUtil.getURI(extended);
|
||||
URI portableURI = resourceA.getPortableURIs().toPortableURI(resourceA, uri);
|
||||
Assert.assertEquals(resourceA.getURI(), portableURI.trimFragment());
|
||||
Assert.assertTrue(resourceA.getPortableURIs().isPortableURIFragment(portableURI.fragment()));
|
||||
Assert.assertSame(extended, resourceA.getEObject(portableURI.fragment()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPortableReferenceDescriptions() throws Exception {
|
||||
XtextResourceSet resourceSet = get(XtextResourceSet.class);
|
||||
StorageAwareResource resourceA = (StorageAwareResource) resourceSet
|
||||
.createResource(URI.createURI("hubba:/bubba.langatestlanguage"));
|
||||
StorageAwareResource resourceB = (StorageAwareResource) resourceSet
|
||||
.createResource(URI.createURI("hubba:/bubba2.langatestlanguage"));
|
||||
resourceB.load(getAsStream("type B"), null);
|
||||
resourceA.load(getAsStream("import 'hubba:/bubba2.langatestlanguage' type A extends B"), null);
|
||||
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
||||
ResourceStorageWritable writable = resourceA.getResourceStorageFacade().createResourceStorageWritable(bout);
|
||||
writable.writeResource(resourceA);
|
||||
IResourceStorageFacade _resourceStorageFacade = resourceA.getResourceStorageFacade();
|
||||
ResourceStorageLoadable loadable = _resourceStorageFacade
|
||||
.createResourceStorageLoadable(new ByteArrayInputStream(bout.toByteArray()));
|
||||
StorageAwareResource resourceC = (StorageAwareResource) resourceSet
|
||||
.createResource(URI.createURI("hubba:/bubba3.langatestlanguage"));
|
||||
resourceC.loadFromStorage(loadable);
|
||||
IReferenceDescription refDesc = Iterables
|
||||
.getFirst(resourceC.getResourceDescription().getReferenceDescriptions(), null);
|
||||
Assert.assertSame(
|
||||
Iterables.getFirst(((Main) Iterables.getFirst(resourceB.getContents(), null)).getTypes(), null),
|
||||
resourceSet.getEObject(refDesc.getTargetEObjectUri(), false));
|
||||
Assert.assertSame(
|
||||
Iterables.getFirst(((Main) Iterables.getFirst(resourceC.getContents(), null)).getTypes(), null),
|
||||
resourceSet.getEObject(refDesc.getSourceEObjectUri(), false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEObjectRelativeFragments() throws Exception {
|
||||
checkFragmentBothDirections(EcorePackage.eINSTANCE, EcorePackage.eINSTANCE.getEAnnotation_Details());
|
||||
checkFragmentBothDirections(EcorePackage.eINSTANCE.getEAttribute_EAttributeType(),
|
||||
EcorePackage.eINSTANCE.getEAttribute_EAttributeType());
|
||||
try {
|
||||
checkFragmentBothDirections(EcorePackage.eINSTANCE.getEAnnotation_EModelElement(),
|
||||
EcorePackage.eINSTANCE.getEAttribute_EAttributeType());
|
||||
Assert.fail();
|
||||
} catch (IllegalArgumentException _t) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void checkFragmentBothDirections(EObject container, EObject child) {
|
||||
PortableURIs portableURIs = new PortableURIs();
|
||||
String fragment = portableURIs.getFragment(container, child);
|
||||
Assert.assertSame(child, portableURIs.getEObject(container, fragment));
|
||||
}
|
||||
}
|
|
@ -1,97 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2014, 2020 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.resource.persistence
|
||||
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.io.ByteArrayOutputStream
|
||||
import org.eclipse.emf.common.util.URI
|
||||
import org.eclipse.emf.ecore.EObject
|
||||
import org.eclipse.emf.ecore.EcorePackage
|
||||
import org.eclipse.emf.ecore.util.EcoreUtil
|
||||
import org.eclipse.xtext.linking.LangATestLanguageStandaloneSetup
|
||||
import org.eclipse.xtext.linking.langATestLanguage.Main
|
||||
import org.eclipse.xtext.resource.XtextResourceSet
|
||||
import org.junit.Assert
|
||||
import org.junit.Test
|
||||
import org.eclipse.xtext.tests.AbstractXtextTests
|
||||
|
||||
/**
|
||||
* @author Sven Efftinge - Initial contribution and API
|
||||
*/
|
||||
class PortableURIsTest extends AbstractXtextTests {
|
||||
|
||||
override setUp() throws Exception {
|
||||
super.setUp();
|
||||
with(new LangATestLanguageStandaloneSetup());
|
||||
}
|
||||
|
||||
@Test def void testPortableUris() {
|
||||
val resourceSet = get(XtextResourceSet)
|
||||
val resourceA = resourceSet.createResource(URI.createURI("hubba:/bubba.langatestlanguage")) as StorageAwareResource
|
||||
val resourceB = resourceSet.createResource(URI.createURI("hubba:/bubba2.langatestlanguage")) as StorageAwareResource
|
||||
resourceB.load(getAsStream('''
|
||||
type B
|
||||
'''), null)
|
||||
resourceA.load(getAsStream('''
|
||||
import 'hubba:/bubba2.langatestlanguage'
|
||||
|
||||
type A extends B
|
||||
'''), null)
|
||||
val extended = resourceA.contents.filter(Main).head.types.head.extends
|
||||
val uri = EcoreUtil.getURI(extended)
|
||||
val portableURI = resourceA.portableURIs.toPortableURI(resourceA, uri)
|
||||
assertEquals(resourceA.URI, portableURI.trimFragment)
|
||||
assertTrue(resourceA.portableURIs.isPortableURIFragment(portableURI.fragment))
|
||||
assertSame(extended, resourceA.getEObject(portableURI.fragment))
|
||||
}
|
||||
|
||||
@Test def void testPortableReferenceDescriptions() {
|
||||
val resourceSet = get(XtextResourceSet)
|
||||
val resourceA = resourceSet.createResource(URI.createURI("hubba:/bubba.langatestlanguage")) as StorageAwareResource
|
||||
val resourceB = resourceSet.createResource(URI.createURI("hubba:/bubba2.langatestlanguage")) as StorageAwareResource
|
||||
resourceB.load(getAsStream('''
|
||||
type B
|
||||
'''), null)
|
||||
resourceA.load(getAsStream('''
|
||||
import 'hubba:/bubba2.langatestlanguage'
|
||||
|
||||
type A extends B
|
||||
'''), null)
|
||||
val bout = new ByteArrayOutputStream
|
||||
val writable = resourceA.resourceStorageFacade.createResourceStorageWritable(bout)
|
||||
writable.writeResource(resourceA)
|
||||
|
||||
val loadable = resourceA.resourceStorageFacade.createResourceStorageLoadable(new ByteArrayInputStream(bout.toByteArray))
|
||||
|
||||
val resourceC = resourceSet.createResource(URI.createURI("hubba:/bubba3.langatestlanguage")) as StorageAwareResource
|
||||
resourceC.loadFromStorage(loadable)
|
||||
|
||||
val refDesc = resourceC.resourceDescription.referenceDescriptions.head
|
||||
assertSame((resourceB.contents.head as Main).types.head, resourceSet.getEObject(refDesc.targetEObjectUri, false))
|
||||
assertSame((resourceC.contents.head as Main).types.head, resourceSet.getEObject(refDesc.sourceEObjectUri, false))
|
||||
}
|
||||
|
||||
|
||||
@Test def void testEObjectRelativeFragments() {
|
||||
checkFragmentBothDirections(EcorePackage.eINSTANCE, EcorePackage.eINSTANCE.EAnnotation_Details)
|
||||
checkFragmentBothDirections(EcorePackage.eINSTANCE.EAttribute_EAttributeType, EcorePackage.eINSTANCE.EAttribute_EAttributeType)
|
||||
try {
|
||||
checkFragmentBothDirections(EcorePackage.eINSTANCE.EAnnotation_EModelElement, EcorePackage.eINSTANCE.EAttribute_EAttributeType)
|
||||
Assert.fail();
|
||||
} catch (IllegalArgumentException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
def checkFragmentBothDirections(EObject container, EObject child) {
|
||||
val portableURIs = new PortableURIs()
|
||||
val fragment = portableURIs.getFragment(container, child)
|
||||
Assert.assertSame(child, portableURIs.getEObject(container, fragment))
|
||||
}
|
||||
}
|
|
@ -1,209 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2012, 2020 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.resource;
|
||||
|
||||
import com.google.common.collect.Iterables;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import org.eclipse.emf.common.util.EList;
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
import org.eclipse.emf.ecore.resource.Resource;
|
||||
import org.eclipse.xtext.resource.AbstractResourceSetTest;
|
||||
import org.eclipse.xtext.resource.XtextResource;
|
||||
import org.eclipse.xtext.resource.XtextResourceSet;
|
||||
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
@SuppressWarnings("all")
|
||||
public abstract class AbstractXtextResourceSetTest extends AbstractResourceSetTest {
|
||||
@Override
|
||||
protected abstract XtextResourceSet createEmptyResourceSet();
|
||||
|
||||
@Test
|
||||
public void testResourcesAreInMap() {
|
||||
final XtextResourceSet rs = this.createEmptyResourceSet();
|
||||
Assert.assertEquals(0, rs.getURIResourceMap().size());
|
||||
final XtextResource resource = new XtextResource();
|
||||
resource.setURI(URI.createFileURI(new File("foo").getAbsolutePath()));
|
||||
EList<Resource> _resources = rs.getResources();
|
||||
_resources.add(resource);
|
||||
Assert.assertEquals(1, rs.getURIResourceMap().size());
|
||||
rs.getResources().remove(resource);
|
||||
Assert.assertTrue(resource.eAdapters().isEmpty());
|
||||
Assert.assertEquals(0, rs.getURIResourceMap().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResourcesAreInMap_02() {
|
||||
final XtextResourceSet rs = this.createEmptyResourceSet();
|
||||
Assert.assertEquals(0, rs.getURIResourceMap().size());
|
||||
final XtextResource resource = new XtextResource();
|
||||
resource.setURI(URI.createFileURI(new File("foo").getAbsolutePath()));
|
||||
EList<Resource> _resources = rs.getResources();
|
||||
ArrayList<Resource> _newArrayList = CollectionLiterals.<Resource>newArrayList(resource);
|
||||
Iterables.<Resource>addAll(_resources, _newArrayList);
|
||||
Assert.assertEquals(1, rs.getURIResourceMap().size());
|
||||
rs.getResources().remove(resource);
|
||||
Assert.assertTrue(resource.eAdapters().isEmpty());
|
||||
Assert.assertEquals(0, rs.getURIResourceMap().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResourcesAreInMap_03() {
|
||||
final XtextResourceSet rs = this.createEmptyResourceSet();
|
||||
Assert.assertEquals(0, rs.getURIResourceMap().size());
|
||||
final XtextResource resource = new XtextResource();
|
||||
EList<Resource> _resources = rs.getResources();
|
||||
_resources.add(resource);
|
||||
Assert.assertEquals(1, rs.getURIResourceMap().size());
|
||||
Assert.assertEquals(resource, rs.getURIResourceMap().get(null));
|
||||
resource.setURI(URI.createFileURI(new File("foo").getAbsolutePath()));
|
||||
Assert.assertEquals(1, rs.getURIResourceMap().size());
|
||||
Assert.assertFalse(rs.getURIResourceMap().containsKey(null));
|
||||
Assert.assertEquals(resource, rs.getURIResourceMap().get(resource.getURI()));
|
||||
Assert.assertEquals(resource, rs.getURIResourceMap().get(rs.getURIConverter().normalize(resource.getURI())));
|
||||
resource.setURI(URI.createFileURI(new File("bar").getAbsolutePath()));
|
||||
Assert.assertEquals(1, rs.getURIResourceMap().size());
|
||||
Assert.assertFalse(rs.getURIResourceMap().containsKey(null));
|
||||
Assert.assertEquals(resource, rs.getURIResourceMap().get(resource.getURI()));
|
||||
Assert.assertEquals(resource, rs.getURIResourceMap().get(rs.getURIConverter().normalize(resource.getURI())));
|
||||
resource.setURI(null);
|
||||
Assert.assertEquals(1, rs.getURIResourceMap().size());
|
||||
Assert.assertEquals(resource, rs.getURIResourceMap().get(null));
|
||||
rs.getResources().remove(resource);
|
||||
Assert.assertTrue(resource.eAdapters().isEmpty());
|
||||
Assert.assertEquals(0, rs.getURIResourceMap().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResourcesAreCleared_01() {
|
||||
final XtextResourceSet rs = this.createEmptyResourceSet();
|
||||
Assert.assertEquals(0, rs.getURIResourceMap().size());
|
||||
final XtextResource resource = new XtextResource();
|
||||
resource.setURI(URI.createFileURI(new File("foo").getAbsolutePath()));
|
||||
EList<Resource> _resources = rs.getResources();
|
||||
ArrayList<Resource> _newArrayList = CollectionLiterals.<Resource>newArrayList(resource);
|
||||
Iterables.<Resource>addAll(_resources, _newArrayList);
|
||||
Assert.assertEquals(1, rs.getURIResourceMap().size());
|
||||
rs.getResources().clear();
|
||||
Assert.assertTrue(resource.eAdapters().isEmpty());
|
||||
Assert.assertEquals(0, rs.getURIResourceMap().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResourcesAreClearedWithDeliverFalse_01() {
|
||||
final XtextResourceSet rs = this.createEmptyResourceSet();
|
||||
Assert.assertEquals(0, rs.getURIResourceMap().size());
|
||||
final XtextResource resource = new XtextResource();
|
||||
resource.setURI(URI.createFileURI(new File("foo").getAbsolutePath()));
|
||||
EList<Resource> _resources = rs.getResources();
|
||||
ArrayList<Resource> _newArrayList = CollectionLiterals.<Resource>newArrayList(resource);
|
||||
Iterables.<Resource>addAll(_resources, _newArrayList);
|
||||
Assert.assertEquals(1, rs.getURIResourceMap().size());
|
||||
rs.eSetDeliver(false);
|
||||
rs.getResources().clear();
|
||||
Assert.assertTrue(resource.eAdapters().isEmpty());
|
||||
Assert.assertEquals(0, rs.getURIResourceMap().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResourcesAreInMapWithNormalizedURI_01() {
|
||||
final XtextResourceSet rs = this.createEmptyResourceSet();
|
||||
Assert.assertEquals(0, rs.getURIResourceMap().size());
|
||||
final XtextResource resource = new XtextResource();
|
||||
resource.setURI(URI.createURI("/a/../foo"));
|
||||
EList<Resource> _resources = rs.getResources();
|
||||
_resources.add(resource);
|
||||
Assert.assertEquals(2, rs.getURIResourceMap().size());
|
||||
rs.getResources().remove(resource);
|
||||
Assert.assertTrue(resource.eAdapters().isEmpty());
|
||||
Assert.assertEquals(0, rs.getURIResourceMap().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResourcesAreInMapWithNormalizedURI_02() {
|
||||
final XtextResourceSet rs = this.createEmptyResourceSet();
|
||||
Assert.assertEquals(0, rs.getURIResourceMap().size());
|
||||
final XtextResource resource = new XtextResource();
|
||||
resource.setURI(URI.createURI("/a/../foo"));
|
||||
EList<Resource> _resources = rs.getResources();
|
||||
ArrayList<Resource> _newArrayList = CollectionLiterals.<Resource>newArrayList(resource);
|
||||
Iterables.<Resource>addAll(_resources, _newArrayList);
|
||||
Assert.assertEquals(2, rs.getURIResourceMap().size());
|
||||
rs.getResources().remove(resource);
|
||||
Assert.assertTrue(resource.eAdapters().isEmpty());
|
||||
Assert.assertEquals(0, rs.getURIResourceMap().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResourcesAreInMapWithNormalizedURI_03() {
|
||||
final XtextResourceSet rs = this.createEmptyResourceSet();
|
||||
Assert.assertEquals(0, rs.getURIResourceMap().size());
|
||||
final XtextResource resource = new XtextResource();
|
||||
EList<Resource> _resources = rs.getResources();
|
||||
_resources.add(resource);
|
||||
Assert.assertEquals(1, rs.getURIResourceMap().size());
|
||||
Assert.assertEquals(resource, rs.getURIResourceMap().get(null));
|
||||
Assert.assertEquals(0, rs.getNormalizationMap().size());
|
||||
resource.setURI(URI.createURI("/a/../foo"));
|
||||
Assert.assertEquals(2, rs.getURIResourceMap().size());
|
||||
Assert.assertFalse(rs.getURIResourceMap().containsKey(null));
|
||||
Assert.assertEquals(resource, rs.getURIResourceMap().get(resource.getURI()));
|
||||
Assert.assertEquals(resource, rs.getURIResourceMap().get(rs.getURIConverter().normalize(resource.getURI())));
|
||||
Assert.assertEquals(1, rs.getNormalizationMap().size());
|
||||
Assert.assertEquals(rs.getURIConverter().normalize(resource.getURI()), rs.getNormalizationMap().get(resource.getURI()));
|
||||
resource.setURI(URI.createURI("/a/../bar"));
|
||||
Assert.assertEquals(2, rs.getURIResourceMap().size());
|
||||
Assert.assertFalse(rs.getURIResourceMap().containsKey(null));
|
||||
Assert.assertEquals(resource, rs.getURIResourceMap().get(resource.getURI()));
|
||||
Assert.assertEquals(resource, rs.getURIResourceMap().get(rs.getURIConverter().normalize(resource.getURI())));
|
||||
Assert.assertEquals(1, rs.getNormalizationMap().size());
|
||||
Assert.assertEquals(rs.getURIConverter().normalize(resource.getURI()), rs.getNormalizationMap().get(resource.getURI()));
|
||||
resource.setURI(null);
|
||||
Assert.assertEquals(1, rs.getURIResourceMap().size());
|
||||
Assert.assertEquals(resource, rs.getURIResourceMap().get(null));
|
||||
Assert.assertEquals(0, rs.getNormalizationMap().size());
|
||||
rs.getResources().remove(resource);
|
||||
Assert.assertTrue(resource.eAdapters().isEmpty());
|
||||
Assert.assertEquals(0, rs.getURIResourceMap().size());
|
||||
Assert.assertEquals(0, rs.getNormalizationMap().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResourcesAreClearedWithNormalizedURI_01() {
|
||||
final XtextResourceSet rs = this.createEmptyResourceSet();
|
||||
Assert.assertEquals(0, rs.getURIResourceMap().size());
|
||||
final XtextResource resource = new XtextResource();
|
||||
resource.setURI(URI.createURI("/a/../foo"));
|
||||
EList<Resource> _resources = rs.getResources();
|
||||
ArrayList<Resource> _newArrayList = CollectionLiterals.<Resource>newArrayList(resource);
|
||||
Iterables.<Resource>addAll(_resources, _newArrayList);
|
||||
Assert.assertEquals(2, rs.getURIResourceMap().size());
|
||||
rs.getResources().clear();
|
||||
Assert.assertTrue(resource.eAdapters().isEmpty());
|
||||
Assert.assertEquals(0, rs.getURIResourceMap().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResourcesAreClearedWithDeliverFalseWithNormalizedURI_01() {
|
||||
final XtextResourceSet rs = this.createEmptyResourceSet();
|
||||
Assert.assertEquals(0, rs.getURIResourceMap().size());
|
||||
final XtextResource resource = new XtextResource();
|
||||
resource.setURI(URI.createURI("//a/../foo"));
|
||||
EList<Resource> _resources = rs.getResources();
|
||||
ArrayList<Resource> _newArrayList = CollectionLiterals.<Resource>newArrayList(resource);
|
||||
Iterables.<Resource>addAll(_resources, _newArrayList);
|
||||
Assert.assertEquals(2, rs.getURIResourceMap().size());
|
||||
rs.eSetDeliver(false);
|
||||
rs.getResources().clear();
|
||||
Assert.assertTrue(resource.eAdapters().isEmpty());
|
||||
Assert.assertEquals(0, rs.getURIResourceMap().size());
|
||||
}
|
||||
}
|
|
@ -1,101 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2012, 2020 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.resource;
|
||||
|
||||
import com.google.common.collect.Iterables;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
import org.eclipse.emf.ecore.resource.Resource;
|
||||
import org.eclipse.xtext.resource.AbstractXtextResourceSetTest;
|
||||
import org.eclipse.xtext.resource.NullResource;
|
||||
import org.eclipse.xtext.resource.SynchronizedXtextResourceSet;
|
||||
import org.eclipse.xtext.resource.XtextResourceSet;
|
||||
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
|
||||
import org.eclipse.xtext.xbase.lib.Exceptions;
|
||||
import org.eclipse.xtext.xbase.lib.Functions.Function1;
|
||||
import org.eclipse.xtext.xbase.lib.IntegerRange;
|
||||
import org.eclipse.xtext.xbase.lib.IterableExtensions;
|
||||
import org.eclipse.xtext.xbase.lib.ListExtensions;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
@SuppressWarnings("all")
|
||||
public class SynchronizedXtextResourceSetTest extends AbstractXtextResourceSetTest {
|
||||
@Override
|
||||
protected XtextResourceSet createEmptyResourceSet() {
|
||||
return new SynchronizedXtextResourceSet();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSynchronization() {
|
||||
try {
|
||||
XtextResourceSet _createEmptyResourceSet = this.createEmptyResourceSet();
|
||||
final SynchronizedXtextResourceSet resourceSet = ((SynchronizedXtextResourceSet) _createEmptyResourceSet);
|
||||
final Resource.Factory _function = (URI uri) -> {
|
||||
NullResource _xblockexpression = null;
|
||||
{
|
||||
final NullResource result = new NullResource();
|
||||
result.setURI(uri);
|
||||
_xblockexpression = result;
|
||||
}
|
||||
return _xblockexpression;
|
||||
};
|
||||
final Resource.Factory nullFactory = _function;
|
||||
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xmi", nullFactory);
|
||||
final ArrayList<Thread> threads = CollectionLiterals.<Thread>newArrayList();
|
||||
final Consumer<Integer> _function_1 = (Integer i) -> {
|
||||
final Runnable _function_2 = () -> {
|
||||
final ArrayList<Resource> resources = CollectionLiterals.<Resource>newArrayList();
|
||||
for (int j = 0; (j < 5000); j++) {
|
||||
{
|
||||
String _plus = (i + " ");
|
||||
String _plus_1 = (_plus + Integer.valueOf(j));
|
||||
String _plus_2 = (_plus_1 + ".xmi");
|
||||
final Resource resource = resourceSet.createResource(URI.createURI(_plus_2));
|
||||
Assert.assertNotNull(resource);
|
||||
resources.add(resource);
|
||||
URI _uRI = resource.getURI();
|
||||
String _plus_3 = (_uRI + "b");
|
||||
resource.setURI(URI.createURI(_plus_3));
|
||||
}
|
||||
}
|
||||
};
|
||||
Thread _thread = new Thread(_function_2);
|
||||
threads.add(_thread);
|
||||
};
|
||||
new IntegerRange(1, 10).forEach(_function_1);
|
||||
for (final Thread thread : threads) {
|
||||
thread.start();
|
||||
}
|
||||
for (final Thread thread_1 : threads) {
|
||||
thread_1.join();
|
||||
}
|
||||
Assert.assertEquals(50000, resourceSet.getResources().size());
|
||||
Assert.assertEquals(IterableExtensions.<Resource>toSet(resourceSet.getResources()).size(), IterableExtensions.<Resource>toSet(resourceSet.getURIResourceMap().values()).size());
|
||||
final Function1<Resource, List<URI>> _function_2 = (Resource it) -> {
|
||||
URI _uRI = it.getURI();
|
||||
URI _normalize = resourceSet.getURIConverter().normalize(it.getURI());
|
||||
return Collections.<URI>unmodifiableList(CollectionLiterals.<URI>newArrayList(_uRI, _normalize));
|
||||
};
|
||||
Assert.assertEquals(IterableExtensions.<URI>toSet(Iterables.<URI>concat(ListExtensions.<Resource, List<URI>>map(resourceSet.getResources(), _function_2))), resourceSet.getURIResourceMap().keySet());
|
||||
final Function1<Resource, String> _function_3 = (Resource it) -> {
|
||||
return it.getURI().toString();
|
||||
};
|
||||
final Function1<URI, String> _function_4 = (URI it) -> {
|
||||
return it.toString();
|
||||
};
|
||||
Assert.assertEquals(IterableExtensions.join(IterableExtensions.<String>sort(IterableExtensions.<String>toList(ListExtensions.<Resource, String>map(resourceSet.getResources(), _function_3))), "\n"), IterableExtensions.join(IterableExtensions.<String>sort(IterableExtensions.<String>toList(IterableExtensions.<URI, String>map(resourceSet.getNormalizationMap().keySet(), _function_4))), "\n"));
|
||||
} catch (Throwable _e) {
|
||||
throw Exceptions.sneakyThrow(_e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,300 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2017, 2020 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.resource.containers;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
import org.eclipse.emf.ecore.resource.Resource;
|
||||
import org.eclipse.xtext.naming.QualifiedName;
|
||||
import org.eclipse.xtext.resource.IEObjectDescription;
|
||||
import org.eclipse.xtext.resource.IResourceDescription;
|
||||
import org.eclipse.xtext.resource.IResourceDescriptions;
|
||||
import org.eclipse.xtext.resource.LiveContainerTestLanguageInjectorProvider;
|
||||
import org.eclipse.xtext.resource.XtextResourceSet;
|
||||
import org.eclipse.xtext.resource.containers.LiveShadowedChunkedContainer;
|
||||
import org.eclipse.xtext.resource.containers.ProjectConfig;
|
||||
import org.eclipse.xtext.resource.containers.SourceFolder;
|
||||
import org.eclipse.xtext.resource.impl.ChunkedResourceDescriptions;
|
||||
import org.eclipse.xtext.resource.impl.LiveShadowedChunkedResourceDescriptions;
|
||||
import org.eclipse.xtext.resource.impl.ResourceDescriptionsData;
|
||||
import org.eclipse.xtext.resource.liveContainerTestLanguage.LiveContainerTestLanguagePackage;
|
||||
import org.eclipse.xtext.resource.liveContainerTestLanguage.Model;
|
||||
import org.eclipse.xtext.testing.InjectWith;
|
||||
import org.eclipse.xtext.testing.XtextRunner;
|
||||
import org.eclipse.xtext.testing.util.ParseHelper;
|
||||
import org.eclipse.xtext.workspace.ProjectConfigAdapter;
|
||||
import org.eclipse.xtext.workspace.WorkspaceConfig;
|
||||
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
|
||||
import org.eclipse.xtext.xbase.lib.Conversions;
|
||||
import org.eclipse.xtext.xbase.lib.Exceptions;
|
||||
import org.eclipse.xtext.xbase.lib.Extension;
|
||||
import org.eclipse.xtext.xbase.lib.Functions.Function1;
|
||||
import org.eclipse.xtext.xbase.lib.IterableExtensions;
|
||||
import org.eclipse.xtext.xbase.lib.ListExtensions;
|
||||
import org.eclipse.xtext.xbase.lib.Pair;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
/**
|
||||
* @author koehnlein - Initial contribution and API
|
||||
*/
|
||||
@RunWith(XtextRunner.class)
|
||||
@InjectWith(LiveContainerTestLanguageInjectorProvider.class)
|
||||
@SuppressWarnings("all")
|
||||
public class LiveShadowedChunkedContainerTest {
|
||||
@Inject
|
||||
@Extension
|
||||
private ParseHelper<Model> _parseHelper;
|
||||
|
||||
@Inject
|
||||
private Provider<XtextResourceSet> resourceSetProvider;
|
||||
|
||||
@Inject
|
||||
private IResourceDescription.Manager resourceDescriptionManager;
|
||||
|
||||
@Inject
|
||||
private Provider<LiveShadowedChunkedResourceDescriptions> provider;
|
||||
|
||||
private WorkspaceConfig workspaceConfig;
|
||||
|
||||
private ProjectConfig fooProject;
|
||||
|
||||
private ProjectConfig barProject;
|
||||
|
||||
private URI fooURI;
|
||||
|
||||
private URI barURI;
|
||||
|
||||
private XtextResourceSet rs1;
|
||||
|
||||
private LiveShadowedChunkedContainer fooContainer;
|
||||
|
||||
private LiveShadowedChunkedContainer barContainer;
|
||||
|
||||
private LiveShadowedChunkedResourceDescriptions liveShadowedChunkedResourceDescriptions;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
try {
|
||||
WorkspaceConfig _workspaceConfig = new WorkspaceConfig();
|
||||
this.workspaceConfig = _workspaceConfig;
|
||||
ProjectConfig _projectConfig = new ProjectConfig("foo", this.workspaceConfig);
|
||||
this.fooProject = _projectConfig;
|
||||
ProjectConfig _projectConfig_1 = new ProjectConfig("bar", this.workspaceConfig);
|
||||
this.barProject = _projectConfig_1;
|
||||
final XtextResourceSet rs0 = this.resourceSetProvider.get();
|
||||
this.fooURI = IterableExtensions.<SourceFolder>head(this.fooProject.getSourceFolders()).getPath().trimSegments(1).appendSegment("foo.livecontainertestlanguage");
|
||||
this.barURI = IterableExtensions.<SourceFolder>head(this.barProject.getSourceFolders()).getPath().trimSegments(1).appendSegment("bar.livecontainertestlanguage");
|
||||
ResourceDescriptionsData _createResourceDescriptionData = this.createResourceDescriptionData(this._parseHelper.parse("foo", this.fooURI, rs0).eResource());
|
||||
Pair<String, ResourceDescriptionsData> _mappedTo = Pair.<String, ResourceDescriptionsData>of("foo", _createResourceDescriptionData);
|
||||
ResourceDescriptionsData _createResourceDescriptionData_1 = this.createResourceDescriptionData(this._parseHelper.parse("bar", this.barURI, rs0).eResource());
|
||||
Pair<String, ResourceDescriptionsData> _mappedTo_1 = Pair.<String, ResourceDescriptionsData>of("bar", _createResourceDescriptionData_1);
|
||||
final Map<String, ResourceDescriptionsData> chunks = Collections.<String, ResourceDescriptionsData>unmodifiableMap(CollectionLiterals.<String, ResourceDescriptionsData>newHashMap(_mappedTo, _mappedTo_1));
|
||||
this.rs1 = this.resourceSetProvider.get();
|
||||
new ChunkedResourceDescriptions(chunks, this.rs1);
|
||||
ProjectConfigAdapter.install(this.rs1, this.fooProject);
|
||||
this.liveShadowedChunkedResourceDescriptions = this.provider.get();
|
||||
this.liveShadowedChunkedResourceDescriptions.setContext(this.rs1);
|
||||
LiveShadowedChunkedContainer _liveShadowedChunkedContainer = new LiveShadowedChunkedContainer(this.liveShadowedChunkedResourceDescriptions, "foo");
|
||||
this.fooContainer = _liveShadowedChunkedContainer;
|
||||
LiveShadowedChunkedContainer _liveShadowedChunkedContainer_1 = new LiveShadowedChunkedContainer(this.liveShadowedChunkedResourceDescriptions, "bar");
|
||||
this.barContainer = _liveShadowedChunkedContainer_1;
|
||||
} catch (Throwable _e) {
|
||||
throw Exceptions.sneakyThrow(_e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRenameElement() {
|
||||
try {
|
||||
this._parseHelper.parse("fooChange", this.fooURI, this.rs1);
|
||||
final Function1<IEObjectDescription, String> _function = (IEObjectDescription it) -> {
|
||||
return it.getQualifiedName().toString();
|
||||
};
|
||||
Assert.assertEquals("fooChange", IterableExtensions.join(IterableExtensions.<IEObjectDescription, String>map(this.fooContainer.getExportedObjects(), _function), ","));
|
||||
final Function1<IEObjectDescription, String> _function_1 = (IEObjectDescription it) -> {
|
||||
return it.getQualifiedName().toString();
|
||||
};
|
||||
Assert.assertEquals("fooChange", IterableExtensions.join(IterableExtensions.<IEObjectDescription, String>map(this.fooContainer.getExportedObjects(LiveContainerTestLanguagePackage.Literals.MODEL, QualifiedName.create("fooChange"), false), _function_1), ","));
|
||||
final Function1<IEObjectDescription, String> _function_2 = (IEObjectDescription it) -> {
|
||||
return it.getQualifiedName().toString();
|
||||
};
|
||||
Assert.assertEquals("fooChange", IterableExtensions.join(IterableExtensions.<IEObjectDescription, String>map(this.fooContainer.getExportedObjectsByType(LiveContainerTestLanguagePackage.Literals.MODEL), _function_2), ","));
|
||||
Assert.assertEquals(1, IterableExtensions.size(this.fooContainer.getResourceDescriptions()));
|
||||
Assert.assertEquals(1, this.fooContainer.getResourceDescriptionCount());
|
||||
Assert.assertTrue(this.fooContainer.hasResourceDescription(this.fooURI));
|
||||
Assert.assertFalse(this.fooContainer.hasResourceDescription(this.barURI));
|
||||
Assert.assertEquals(this.fooURI, this.fooContainer.getResourceDescription(this.fooURI).getURI());
|
||||
final Function1<IEObjectDescription, String> _function_3 = (IEObjectDescription it) -> {
|
||||
return it.getQualifiedName().toString();
|
||||
};
|
||||
Assert.assertEquals("bar", IterableExtensions.join(IterableExtensions.<IEObjectDescription, String>map(this.barContainer.getExportedObjects(), _function_3), ","));
|
||||
final Function1<IEObjectDescription, String> _function_4 = (IEObjectDescription it) -> {
|
||||
return it.getQualifiedName().toString();
|
||||
};
|
||||
Assert.assertEquals("bar", IterableExtensions.join(IterableExtensions.<IEObjectDescription, String>map(this.barContainer.getExportedObjects(LiveContainerTestLanguagePackage.Literals.MODEL, QualifiedName.create("bar"), false), _function_4), ","));
|
||||
final Function1<IEObjectDescription, String> _function_5 = (IEObjectDescription it) -> {
|
||||
return it.getQualifiedName().toString();
|
||||
};
|
||||
Assert.assertEquals("bar", IterableExtensions.join(IterableExtensions.<IEObjectDescription, String>map(this.barContainer.getExportedObjectsByType(LiveContainerTestLanguagePackage.Literals.MODEL), _function_5), ","));
|
||||
Assert.assertEquals(1, IterableExtensions.size(this.barContainer.getResourceDescriptions()));
|
||||
Assert.assertEquals(1, this.barContainer.getResourceDescriptionCount());
|
||||
Assert.assertTrue(this.barContainer.hasResourceDescription(this.barURI));
|
||||
Assert.assertFalse(this.barContainer.hasResourceDescription(this.fooURI));
|
||||
Assert.assertEquals(this.barURI, this.barContainer.getResourceDescription(this.barURI).getURI());
|
||||
this.assertGlobalDescriptionsAreUnaffected();
|
||||
} catch (Throwable _e) {
|
||||
throw Exceptions.sneakyThrow(_e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddRemoveResource() {
|
||||
try {
|
||||
final Resource bazResource = this._parseHelper.parse("baz", IterableExtensions.<SourceFolder>head(this.fooProject.getSourceFolders()).getPath().trimSegments(1).appendSegment("baz.livecontainertestlanguage"), this.rs1).eResource();
|
||||
Assert.assertEquals(2, IterableExtensions.size(this.fooContainer.getResourceDescriptions()));
|
||||
Assert.assertEquals(2, this.fooContainer.getResourceDescriptionCount());
|
||||
final Function1<IEObjectDescription, String> _function = (IEObjectDescription it) -> {
|
||||
return it.getQualifiedName().toString();
|
||||
};
|
||||
Assert.assertEquals("baz,foo", IterableExtensions.join(IterableExtensions.<String>sort(IterableExtensions.<IEObjectDescription, String>map(this.fooContainer.getExportedObjects(), _function)), ","));
|
||||
Assert.assertEquals(1, IterableExtensions.size(this.barContainer.getResourceDescriptions()));
|
||||
Assert.assertEquals(1, this.barContainer.getResourceDescriptionCount());
|
||||
this.assertGlobalDescriptionsAreUnaffected();
|
||||
this.rs1.getResources().remove(bazResource);
|
||||
Assert.assertEquals(1, IterableExtensions.size(this.fooContainer.getResourceDescriptions()));
|
||||
Assert.assertEquals(1, this.fooContainer.getResourceDescriptionCount());
|
||||
final Function1<IEObjectDescription, String> _function_1 = (IEObjectDescription it) -> {
|
||||
return it.getQualifiedName().toString();
|
||||
};
|
||||
Assert.assertEquals("foo", IterableExtensions.join(IterableExtensions.<String>sort(IterableExtensions.<IEObjectDescription, String>map(this.fooContainer.getExportedObjects(), _function_1)), ","));
|
||||
Assert.assertEquals(1, IterableExtensions.size(this.barContainer.getResourceDescriptions()));
|
||||
Assert.assertEquals(1, this.barContainer.getResourceDescriptionCount());
|
||||
this.assertGlobalDescriptionsAreUnaffected();
|
||||
} catch (Throwable _e) {
|
||||
throw Exceptions.sneakyThrow(_e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMoveResourceBetweenContainers() {
|
||||
try {
|
||||
final URI oldURI = IterableExtensions.<SourceFolder>head(this.fooProject.getSourceFolders()).getPath().trimSegments(1).appendSegment("baz.livecontainertestlanguage");
|
||||
final Resource bazResource = this._parseHelper.parse("baz", oldURI, this.rs1).eResource();
|
||||
Assert.assertEquals(2, IterableExtensions.size(this.fooContainer.getResourceDescriptions()));
|
||||
Assert.assertEquals(2, this.fooContainer.getResourceDescriptionCount());
|
||||
final Function1<IEObjectDescription, String> _function = (IEObjectDescription it) -> {
|
||||
return it.getQualifiedName().toString();
|
||||
};
|
||||
Assert.assertEquals("baz,foo", IterableExtensions.join(IterableExtensions.<String>sort(IterableExtensions.<IEObjectDescription, String>map(this.fooContainer.getExportedObjects(), _function)), ","));
|
||||
Assert.assertEquals(oldURI, IterableExtensions.<IEObjectDescription>head(this.fooContainer.getExportedObjects(LiveContainerTestLanguagePackage.Literals.MODEL, QualifiedName.create("baz"), false)).getEObjectURI().trimFragment());
|
||||
Assert.assertEquals(1, IterableExtensions.size(this.barContainer.getResourceDescriptions()));
|
||||
Assert.assertEquals(1, this.barContainer.getResourceDescriptionCount());
|
||||
this.assertGlobalDescriptionsAreUnaffected();
|
||||
final URI newURI = URI.createURI(bazResource.getURI().toString().replace("/foo/", "/bar/"));
|
||||
bazResource.setURI(newURI);
|
||||
Assert.assertEquals(1, IterableExtensions.size(this.fooContainer.getResourceDescriptions()));
|
||||
Assert.assertEquals(1, this.fooContainer.getResourceDescriptionCount());
|
||||
final Function1<IEObjectDescription, String> _function_1 = (IEObjectDescription it) -> {
|
||||
return it.getQualifiedName().toString();
|
||||
};
|
||||
Assert.assertEquals("foo", IterableExtensions.join(IterableExtensions.<String>sort(IterableExtensions.<IEObjectDescription, String>map(this.fooContainer.getExportedObjects(), _function_1)), ","));
|
||||
Assert.assertEquals(1, IterableExtensions.size(this.fooContainer.getResourceDescriptions()));
|
||||
Assert.assertEquals(1, this.fooContainer.getResourceDescriptionCount());
|
||||
final Function1<IEObjectDescription, String> _function_2 = (IEObjectDescription it) -> {
|
||||
return it.getQualifiedName().toString();
|
||||
};
|
||||
Assert.assertEquals("bar,baz", IterableExtensions.join(IterableExtensions.<String>sort(IterableExtensions.<IEObjectDescription, String>map(this.barContainer.getExportedObjects(), _function_2)), ","));
|
||||
Assert.assertEquals(2, IterableExtensions.size(this.barContainer.getResourceDescriptions()));
|
||||
Assert.assertEquals(2, this.barContainer.getResourceDescriptionCount());
|
||||
Assert.assertEquals(newURI, IterableExtensions.<IEObjectDescription>head(this.barContainer.getExportedObjects(LiveContainerTestLanguagePackage.Literals.MODEL, QualifiedName.create("baz"), false)).getEObjectURI().trimFragment());
|
||||
this.assertGlobalDescriptionsAreUnaffected();
|
||||
} catch (Throwable _e) {
|
||||
throw Exceptions.sneakyThrow(_e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddToNewContainer() {
|
||||
try {
|
||||
final ProjectConfig bazProject = new ProjectConfig("baz", this.workspaceConfig);
|
||||
final URI newURI = IterableExtensions.<SourceFolder>head(bazProject.getSourceFolders()).getPath().trimSegments(1).appendSegment("baz.livecontainertestlanguage");
|
||||
this._parseHelper.parse("baz", newURI, this.rs1);
|
||||
final LiveShadowedChunkedContainer bazContainer = new LiveShadowedChunkedContainer(this.liveShadowedChunkedResourceDescriptions, "baz");
|
||||
Assert.assertEquals(1, IterableExtensions.size(this.fooContainer.getResourceDescriptions()));
|
||||
Assert.assertEquals(1, this.fooContainer.getResourceDescriptionCount());
|
||||
final Function1<IEObjectDescription, String> _function = (IEObjectDescription it) -> {
|
||||
return it.getQualifiedName().toString();
|
||||
};
|
||||
Assert.assertEquals("foo", IterableExtensions.join(IterableExtensions.<String>sort(IterableExtensions.<IEObjectDescription, String>map(this.fooContainer.getExportedObjects(), _function)), ","));
|
||||
Assert.assertEquals(1, IterableExtensions.size(this.barContainer.getResourceDescriptions()));
|
||||
Assert.assertEquals(1, this.barContainer.getResourceDescriptionCount());
|
||||
final Function1<IEObjectDescription, String> _function_1 = (IEObjectDescription it) -> {
|
||||
return it.getQualifiedName().toString();
|
||||
};
|
||||
Assert.assertEquals("bar", IterableExtensions.join(IterableExtensions.<String>sort(IterableExtensions.<IEObjectDescription, String>map(this.barContainer.getExportedObjects(), _function_1)), ","));
|
||||
Assert.assertEquals(1, IterableExtensions.size(bazContainer.getResourceDescriptions()));
|
||||
Assert.assertEquals(1, bazContainer.getResourceDescriptionCount());
|
||||
final Function1<IEObjectDescription, String> _function_2 = (IEObjectDescription it) -> {
|
||||
return it.getQualifiedName().toString();
|
||||
};
|
||||
Assert.assertEquals("baz", IterableExtensions.join(IterableExtensions.<String>sort(IterableExtensions.<IEObjectDescription, String>map(bazContainer.getExportedObjects(), _function_2)), ","));
|
||||
Assert.assertEquals(newURI, IterableExtensions.<IEObjectDescription>head(bazContainer.getExportedObjects(LiveContainerTestLanguagePackage.Literals.MODEL, QualifiedName.create("baz"), false)).getEObjectURI().trimFragment());
|
||||
this.assertGlobalDescriptionsAreUnaffected();
|
||||
} catch (Throwable _e) {
|
||||
throw Exceptions.sneakyThrow(_e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteElement() {
|
||||
try {
|
||||
this._parseHelper.parse("foo", this.fooURI, this.rs1).eResource().getContents().clear();
|
||||
final Function1<IEObjectDescription, String> _function = (IEObjectDescription it) -> {
|
||||
return it.getQualifiedName().toString();
|
||||
};
|
||||
Assert.assertEquals("", IterableExtensions.join(IterableExtensions.<IEObjectDescription, String>map(this.fooContainer.getExportedObjects(), _function), ","));
|
||||
Assert.assertEquals(1, IterableExtensions.size(this.fooContainer.getResourceDescriptions()));
|
||||
Assert.assertEquals(1, this.fooContainer.getResourceDescriptionCount());
|
||||
Assert.assertEquals(0, IterableExtensions.size(this.fooContainer.getExportedObjects()));
|
||||
Assert.assertEquals(1, IterableExtensions.size(this.barContainer.getResourceDescriptions()));
|
||||
Assert.assertEquals(1, this.barContainer.getResourceDescriptionCount());
|
||||
this.assertGlobalDescriptionsAreUnaffected();
|
||||
} catch (Throwable _e) {
|
||||
throw Exceptions.sneakyThrow(_e);
|
||||
}
|
||||
}
|
||||
|
||||
private void assertGlobalDescriptionsAreUnaffected() {
|
||||
IResourceDescriptions _globalDescriptions = this.liveShadowedChunkedResourceDescriptions.getGlobalDescriptions();
|
||||
final Function1<IEObjectDescription, String> _function = (IEObjectDescription it) -> {
|
||||
return it.getQualifiedName().toString();
|
||||
};
|
||||
Assert.assertEquals("foo", IterableExtensions.join(IterableExtensions.<IEObjectDescription, String>map(((ChunkedResourceDescriptions) _globalDescriptions).getContainer("foo").getExportedObjects(), _function), ","));
|
||||
IResourceDescriptions _globalDescriptions_1 = this.liveShadowedChunkedResourceDescriptions.getGlobalDescriptions();
|
||||
final Function1<IEObjectDescription, String> _function_1 = (IEObjectDescription it) -> {
|
||||
return it.getQualifiedName().toString();
|
||||
};
|
||||
Assert.assertEquals("bar", IterableExtensions.join(IterableExtensions.<IEObjectDescription, String>map(((ChunkedResourceDescriptions) _globalDescriptions_1).getContainer("bar").getExportedObjects(), _function_1), ","));
|
||||
}
|
||||
|
||||
private ResourceDescriptionsData createResourceDescriptionData(final Resource... resources) {
|
||||
final Function1<Resource, IResourceDescription> _function = (Resource it) -> {
|
||||
return this.resourceDescriptionManager.getResourceDescription(it);
|
||||
};
|
||||
List<IResourceDescription> _map = ListExtensions.<Resource, IResourceDescription>map(((List<Resource>)Conversions.doWrapArray(resources)), _function);
|
||||
return new ResourceDescriptionsData(_map);
|
||||
}
|
||||
}
|
|
@ -1,137 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2014, 2020 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.resource.persistence;
|
||||
|
||||
import com.google.common.collect.Iterables;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
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.util.EcoreUtil;
|
||||
import org.eclipse.xtend2.lib.StringConcatenation;
|
||||
import org.eclipse.xtext.linking.LangATestLanguageStandaloneSetup;
|
||||
import org.eclipse.xtext.linking.langATestLanguage.Main;
|
||||
import org.eclipse.xtext.linking.langATestLanguage.Type;
|
||||
import org.eclipse.xtext.resource.IReferenceDescription;
|
||||
import org.eclipse.xtext.resource.XtextResourceSet;
|
||||
import org.eclipse.xtext.resource.persistence.IResourceStorageFacade;
|
||||
import org.eclipse.xtext.resource.persistence.PortableURIs;
|
||||
import org.eclipse.xtext.resource.persistence.ResourceStorageLoadable;
|
||||
import org.eclipse.xtext.resource.persistence.ResourceStorageWritable;
|
||||
import org.eclipse.xtext.resource.persistence.StorageAwareResource;
|
||||
import org.eclipse.xtext.tests.AbstractXtextTests;
|
||||
import org.eclipse.xtext.xbase.lib.Exceptions;
|
||||
import org.eclipse.xtext.xbase.lib.IterableExtensions;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Sven Efftinge - Initial contribution and API
|
||||
*/
|
||||
@SuppressWarnings("all")
|
||||
public class PortableURIsTest extends AbstractXtextTests {
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
LangATestLanguageStandaloneSetup _langATestLanguageStandaloneSetup = new LangATestLanguageStandaloneSetup();
|
||||
this.with(_langATestLanguageStandaloneSetup);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPortableUris() {
|
||||
try {
|
||||
final XtextResourceSet resourceSet = this.<XtextResourceSet>get(XtextResourceSet.class);
|
||||
Resource _createResource = resourceSet.createResource(URI.createURI("hubba:/bubba.langatestlanguage"));
|
||||
final StorageAwareResource resourceA = ((StorageAwareResource) _createResource);
|
||||
Resource _createResource_1 = resourceSet.createResource(URI.createURI("hubba:/bubba2.langatestlanguage"));
|
||||
final StorageAwareResource resourceB = ((StorageAwareResource) _createResource_1);
|
||||
StringConcatenation _builder = new StringConcatenation();
|
||||
_builder.append("type B");
|
||||
_builder.newLine();
|
||||
resourceB.load(this.getAsStream(_builder.toString()), null);
|
||||
StringConcatenation _builder_1 = new StringConcatenation();
|
||||
_builder_1.append("import \'hubba:/bubba2.langatestlanguage\'");
|
||||
_builder_1.newLine();
|
||||
_builder_1.newLine();
|
||||
_builder_1.append("type A extends B");
|
||||
_builder_1.newLine();
|
||||
resourceA.load(this.getAsStream(_builder_1.toString()), null);
|
||||
final Type extended = IterableExtensions.<Type>head(IterableExtensions.<Main>head(Iterables.<Main>filter(resourceA.getContents(), Main.class)).getTypes()).getExtends();
|
||||
final URI uri = EcoreUtil.getURI(extended);
|
||||
final URI portableURI = resourceA.getPortableURIs().toPortableURI(resourceA, uri);
|
||||
Assert.assertEquals(resourceA.getURI(), portableURI.trimFragment());
|
||||
Assert.assertTrue(resourceA.getPortableURIs().isPortableURIFragment(portableURI.fragment()));
|
||||
Assert.assertSame(extended, resourceA.getEObject(portableURI.fragment()));
|
||||
} catch (Throwable _e) {
|
||||
throw Exceptions.sneakyThrow(_e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPortableReferenceDescriptions() {
|
||||
try {
|
||||
final XtextResourceSet resourceSet = this.<XtextResourceSet>get(XtextResourceSet.class);
|
||||
Resource _createResource = resourceSet.createResource(URI.createURI("hubba:/bubba.langatestlanguage"));
|
||||
final StorageAwareResource resourceA = ((StorageAwareResource) _createResource);
|
||||
Resource _createResource_1 = resourceSet.createResource(URI.createURI("hubba:/bubba2.langatestlanguage"));
|
||||
final StorageAwareResource resourceB = ((StorageAwareResource) _createResource_1);
|
||||
StringConcatenation _builder = new StringConcatenation();
|
||||
_builder.append("type B");
|
||||
_builder.newLine();
|
||||
resourceB.load(this.getAsStream(_builder.toString()), null);
|
||||
StringConcatenation _builder_1 = new StringConcatenation();
|
||||
_builder_1.append("import \'hubba:/bubba2.langatestlanguage\'");
|
||||
_builder_1.newLine();
|
||||
_builder_1.newLine();
|
||||
_builder_1.append("type A extends B");
|
||||
_builder_1.newLine();
|
||||
resourceA.load(this.getAsStream(_builder_1.toString()), null);
|
||||
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
||||
final ResourceStorageWritable writable = resourceA.getResourceStorageFacade().createResourceStorageWritable(bout);
|
||||
writable.writeResource(resourceA);
|
||||
IResourceStorageFacade _resourceStorageFacade = resourceA.getResourceStorageFacade();
|
||||
byte[] _byteArray = bout.toByteArray();
|
||||
ByteArrayInputStream _byteArrayInputStream = new ByteArrayInputStream(_byteArray);
|
||||
final ResourceStorageLoadable loadable = _resourceStorageFacade.createResourceStorageLoadable(_byteArrayInputStream);
|
||||
Resource _createResource_2 = resourceSet.createResource(URI.createURI("hubba:/bubba3.langatestlanguage"));
|
||||
final StorageAwareResource resourceC = ((StorageAwareResource) _createResource_2);
|
||||
resourceC.loadFromStorage(loadable);
|
||||
final IReferenceDescription refDesc = IterableExtensions.<IReferenceDescription>head(resourceC.getResourceDescription().getReferenceDescriptions());
|
||||
EObject _head = IterableExtensions.<EObject>head(resourceB.getContents());
|
||||
Assert.assertSame(IterableExtensions.<Type>head(((Main) _head).getTypes()), resourceSet.getEObject(refDesc.getTargetEObjectUri(), false));
|
||||
EObject _head_1 = IterableExtensions.<EObject>head(resourceC.getContents());
|
||||
Assert.assertSame(IterableExtensions.<Type>head(((Main) _head_1).getTypes()), resourceSet.getEObject(refDesc.getSourceEObjectUri(), false));
|
||||
} catch (Throwable _e) {
|
||||
throw Exceptions.sneakyThrow(_e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEObjectRelativeFragments() {
|
||||
this.checkFragmentBothDirections(EcorePackage.eINSTANCE, EcorePackage.eINSTANCE.getEAnnotation_Details());
|
||||
this.checkFragmentBothDirections(EcorePackage.eINSTANCE.getEAttribute_EAttributeType(), EcorePackage.eINSTANCE.getEAttribute_EAttributeType());
|
||||
try {
|
||||
this.checkFragmentBothDirections(EcorePackage.eINSTANCE.getEAnnotation_EModelElement(), EcorePackage.eINSTANCE.getEAttribute_EAttributeType());
|
||||
Assert.fail();
|
||||
} catch (final Throwable _t) {
|
||||
if (_t instanceof IllegalArgumentException) {
|
||||
} else {
|
||||
throw Exceptions.sneakyThrow(_t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void checkFragmentBothDirections(final EObject container, final EObject child) {
|
||||
final PortableURIs portableURIs = new PortableURIs();
|
||||
final String fragment = portableURIs.getFragment(container, child);
|
||||
Assert.assertSame(child, portableURIs.getEObject(container, fragment));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,91 @@
|
|||
/**
|
||||
* Copyright (c) 2015, 2020 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.xtext.generator;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.emf.mwe.utils.DirectoryCleaner;
|
||||
import org.eclipse.xtext.xbase.lib.Exceptions;
|
||||
import org.eclipse.xtext.xtext.generator.model.IXtextGeneratorFileSystemAccess;
|
||||
import org.eclipse.xtext.xtext.generator.model.project.ISubProjectConfig;
|
||||
import org.eclipse.xtext.xtext.generator.model.project.IXtextProjectConfig;
|
||||
|
||||
import com.google.common.base.Predicates;
|
||||
import com.google.common.collect.FluentIterable;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Injector;
|
||||
|
||||
/**
|
||||
* This component cleans all directories for generated code according to the
|
||||
* project configuration (usually 'src-gen' for plain layout and
|
||||
* 'src/main/xtext-gen' for Maven/Gradle layout).
|
||||
*
|
||||
* @noextend This class should not be extended by clients.
|
||||
*/
|
||||
public class XtextDirectoryCleaner implements IGuiceAwareGeneratorComponent {
|
||||
@Inject
|
||||
private IXtextProjectConfig config;
|
||||
|
||||
private boolean enabled = true;
|
||||
|
||||
private boolean useDefaultExcludes = true;
|
||||
|
||||
private List<String> excludes = new ArrayList<>();
|
||||
|
||||
private List<String> extraDirectories = new ArrayList<>();
|
||||
|
||||
public void addExtraDirectory(String directory) {
|
||||
extraDirectories.add(directory);
|
||||
}
|
||||
|
||||
public void addExclude(String exclude) {
|
||||
excludes.add(exclude);
|
||||
}
|
||||
|
||||
public void clean() {
|
||||
if (!enabled) {
|
||||
return;
|
||||
}
|
||||
ArrayList<String> directories = new ArrayList<>();
|
||||
Iterable<IXtextGeneratorFileSystemAccess> fsas = Iterables.concat(
|
||||
Lists.transform(config.getEnabledProjects(), ISubProjectConfig::getSrcGen),
|
||||
Lists.newArrayList(config.getRuntime().getEcoreModel()));
|
||||
Iterables.addAll(directories, FluentIterable.from(fsas).filter(Predicates.notNull())
|
||||
.transform(IXtextGeneratorFileSystemAccess::getPath).filter(it -> new File(it).isDirectory()));
|
||||
Iterables.addAll(directories, extraDirectories);
|
||||
DirectoryCleaner delegate = new DirectoryCleaner();
|
||||
delegate.setUseDefaultExcludes(useDefaultExcludes);
|
||||
excludes.forEach(it -> delegate.addExclude(it));
|
||||
for (String it : directories) {
|
||||
try {
|
||||
delegate.cleanFolder(it);
|
||||
} catch (FileNotFoundException e) {
|
||||
throw Exceptions.sneakyThrow(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize(Injector injector) {
|
||||
injector.injectMembers(this);
|
||||
}
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public void setUseDefaultExcludes(boolean useDefaultExcludes) {
|
||||
this.useDefaultExcludes = useDefaultExcludes;
|
||||
}
|
||||
}
|
|
@ -1,65 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2015, 2017 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.xtext.generator
|
||||
|
||||
import com.google.inject.Inject
|
||||
import com.google.inject.Injector
|
||||
import java.io.File
|
||||
import java.util.List
|
||||
import org.eclipse.emf.mwe.utils.DirectoryCleaner
|
||||
import org.eclipse.xtend.lib.annotations.Accessors
|
||||
import org.eclipse.xtext.xtext.generator.model.project.IXtextProjectConfig
|
||||
|
||||
/**
|
||||
* This component cleans all directories for generated code according to the project configuration
|
||||
* (usually 'src-gen' for plain layout and 'src/main/xtext-gen' for Maven/Gradle layout).
|
||||
*
|
||||
* @noextend This class should not be extended by clients.
|
||||
*/
|
||||
class XtextDirectoryCleaner implements IGuiceAwareGeneratorComponent {
|
||||
|
||||
@Inject IXtextProjectConfig config
|
||||
|
||||
@Accessors(PUBLIC_SETTER)
|
||||
boolean enabled = true
|
||||
|
||||
@Accessors(PUBLIC_SETTER)
|
||||
boolean useDefaultExcludes = true
|
||||
|
||||
List<String> excludes = newArrayList
|
||||
List<String> extraDirectories = newArrayList
|
||||
|
||||
def void addExtraDirectory(String directory) {
|
||||
extraDirectories += directory
|
||||
}
|
||||
|
||||
def void addExclude(String exclude) {
|
||||
excludes += exclude
|
||||
}
|
||||
|
||||
def void clean() {
|
||||
if (!enabled)
|
||||
return;
|
||||
|
||||
val directories = newArrayList
|
||||
directories += (config.enabledProjects.map[srcGen] + #[config.runtime.ecoreModel]).filterNull.map[path].filter[new File(it).isDirectory]
|
||||
directories += extraDirectories
|
||||
|
||||
val delegate = new DirectoryCleaner
|
||||
delegate.useDefaultExcludes = useDefaultExcludes
|
||||
excludes.forEach[delegate.addExclude(it)]
|
||||
|
||||
directories.forEach[delegate.cleanFolder(it)]
|
||||
}
|
||||
|
||||
override initialize(Injector injector) {
|
||||
injector.injectMembers(this)
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,152 @@
|
|||
/**
|
||||
* Copyright (c) 2015, 2020 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.xtext.generator;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.eclipse.emf.codegen.ecore.genmodel.GenModel;
|
||||
import org.eclipse.emf.codegen.ecore.genmodel.GenModelPackage;
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
import org.eclipse.emf.common.util.WrappedException;
|
||||
import org.eclipse.emf.ecore.EPackage;
|
||||
import org.eclipse.emf.ecore.EcorePackage;
|
||||
import org.eclipse.emf.ecore.resource.Resource;
|
||||
import org.eclipse.emf.ecore.resource.ResourceSet;
|
||||
import org.eclipse.emf.mwe.utils.GenModelHelper;
|
||||
import org.eclipse.emf.mwe.utils.StandaloneSetup;
|
||||
import org.eclipse.xtext.resource.IResourceServiceProvider;
|
||||
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
|
||||
|
||||
import com.google.common.collect.Iterables;
|
||||
|
||||
/**
|
||||
* Initializes a resource set in order to load the grammar of a language. The
|
||||
* resources to include are specified via
|
||||
* {@link XtextGeneratorLanguage#addReferencedResource(String)}.
|
||||
*/
|
||||
public class XtextGeneratorResourceSetInitializer {
|
||||
private static final Logger LOG = Logger.getLogger(XtextGeneratorResourceSetInitializer.class);
|
||||
|
||||
public void initialize(ResourceSet resourceSet, List<String> referencedResources) {
|
||||
StandaloneSetup delegate = new StandaloneSetup();
|
||||
delegate.setResourceSet(resourceSet);
|
||||
resourceSet.getPackageRegistry().put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE);
|
||||
referencedResources.forEach((String it) -> {
|
||||
loadResource(it, resourceSet);
|
||||
});
|
||||
registerGenModels(resourceSet);
|
||||
registerEPackages(resourceSet);
|
||||
}
|
||||
|
||||
private void loadResource(String loadedResource, ResourceSet resourceSet) {
|
||||
URI loadedResourceUri = URI.createURI(loadedResource);
|
||||
ensureResourceCanBeLoaded(loadedResourceUri, resourceSet);
|
||||
resourceSet.getResource(loadedResourceUri, true);
|
||||
}
|
||||
|
||||
private void ensureResourceCanBeLoaded(URI loadedResource, ResourceSet resourceSet) {
|
||||
String fileExtension = loadedResource.fileExtension();
|
||||
if (fileExtension != null) {
|
||||
switch (fileExtension) {
|
||||
case "genmodel":
|
||||
GenModelPackage.eINSTANCE.getEFactoryInstance();
|
||||
IResourceServiceProvider resourceServiceProvider = IResourceServiceProvider.Registry.INSTANCE
|
||||
.getResourceServiceProvider(loadedResource);
|
||||
if (resourceServiceProvider == null) {
|
||||
try {
|
||||
Class<?> genModelSupport = Class.forName("org.eclipse.emf.codegen.ecore.xtext.GenModelSupport");
|
||||
Object instance = genModelSupport.getDeclaredConstructor().newInstance();
|
||||
genModelSupport.getDeclaredMethod("createInjectorAndDoEMFRegistration").invoke(instance);
|
||||
} catch (ClassNotFoundException e) {
|
||||
LOG.debug(
|
||||
"org.eclipse.emf.codegen.ecore.xtext.GenModelSupport not found, GenModels will not be indexed");
|
||||
} catch (Exception e) {
|
||||
LOG.error("Couldn't initialize GenModel support.", e);
|
||||
}
|
||||
}
|
||||
return;
|
||||
case "ecore":
|
||||
IResourceServiceProvider resourceServiceProvider2 = IResourceServiceProvider.Registry.INSTANCE
|
||||
.getResourceServiceProvider(loadedResource);
|
||||
if (resourceServiceProvider2 == null) {
|
||||
try {
|
||||
Class<?> ecore = Class.forName("org.eclipse.xtext.ecore.EcoreSupportStandaloneSetup");
|
||||
ecore.getDeclaredMethod("setup", new Class[] {}).invoke(null);
|
||||
} catch (ClassNotFoundException e) {
|
||||
LOG.error("Couldn't initialize Ecore support. Is 'org.eclipse.xtext.ecore' on the classpath?");
|
||||
LOG.debug(e.getMessage(), e);
|
||||
} catch (Exception e) {
|
||||
LOG.error("Couldn't initialize Ecore support.", e);
|
||||
}
|
||||
}
|
||||
return;
|
||||
case "xcore":
|
||||
IResourceServiceProvider resourceServiceProvider3 = IResourceServiceProvider.Registry.INSTANCE
|
||||
.getResourceServiceProvider(loadedResource);
|
||||
if (resourceServiceProvider3 == null) {
|
||||
try {
|
||||
Class<?> xcore = Class.forName("org.eclipse.emf.ecore.xcore.XcoreStandaloneSetup");
|
||||
xcore.getDeclaredMethod("doSetup", new Class[] {}).invoke(null);
|
||||
} catch (ClassNotFoundException e) {
|
||||
LOG.error("Couldn't initialize Xcore support. Is it on the classpath?");
|
||||
LOG.debug(e.getMessage(), e);
|
||||
} catch (Exception e) {
|
||||
LOG.error("Couldn't initialize Xcore support.", e);
|
||||
}
|
||||
}
|
||||
URI xcoreLangURI = URI
|
||||
.createPlatformResourceURI("/org.eclipse.emf.ecore.xcore.lib/model/XcoreLang.xcore", true);
|
||||
try {
|
||||
resourceSet.getResource(xcoreLangURI, true);
|
||||
} catch (WrappedException e) {
|
||||
LOG.error("Could not load XcoreLang.xcore.", e);
|
||||
Resource brokenResource = resourceSet.getResource(xcoreLangURI, false);
|
||||
resourceSet.getResources().remove(brokenResource);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void registerEPackages(ResourceSet resourceSet) {
|
||||
each(resourceSet, EPackage.class, (EPackage it) -> {
|
||||
register(it);
|
||||
});
|
||||
}
|
||||
|
||||
private void register(EPackage ePackage) {
|
||||
EPackage.Registry registry = ePackage.eResource().getResourceSet().getPackageRegistry();
|
||||
Object existing = registry.get(ePackage.getNsURI());
|
||||
if (existing == null) {
|
||||
registry.put(ePackage.getNsURI(), ePackage);
|
||||
}
|
||||
}
|
||||
|
||||
private void registerGenModels(ResourceSet resourceSet) {
|
||||
each(resourceSet, GenModel.class, (GenModel it) -> {
|
||||
register(it);
|
||||
});
|
||||
}
|
||||
|
||||
private void register(GenModel genModel) {
|
||||
new GenModelHelper().registerGenModel(genModel);
|
||||
}
|
||||
|
||||
private <Type extends Object> void each(ResourceSet resourceSet, Class<Type> type,
|
||||
Procedure1<? super Type> strategy) {
|
||||
for (int i = 0; i < resourceSet.getResources().size(); i++) {
|
||||
Resource resource = resourceSet.getResources().get(i);
|
||||
Iterables.filter(resource.getContents(), type).forEach((Type it) -> {
|
||||
strategy.apply(it);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,131 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2015, 2020 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.xtext.generator
|
||||
|
||||
import java.util.List
|
||||
import org.apache.log4j.Logger
|
||||
import org.eclipse.emf.codegen.ecore.genmodel.GenModel
|
||||
import org.eclipse.emf.codegen.ecore.genmodel.GenModelPackage
|
||||
import org.eclipse.emf.common.util.URI
|
||||
import org.eclipse.emf.common.util.WrappedException
|
||||
import org.eclipse.emf.ecore.EPackage
|
||||
import org.eclipse.emf.ecore.EcorePackage
|
||||
import org.eclipse.emf.ecore.resource.ResourceSet
|
||||
import org.eclipse.emf.mwe.utils.GenModelHelper
|
||||
import org.eclipse.emf.mwe.utils.StandaloneSetup
|
||||
import org.eclipse.xtext.resource.IResourceServiceProvider
|
||||
|
||||
/**
|
||||
* Initializes a resource set in order to load the grammar of a language. The resources to include are specified
|
||||
* via {@link XtextGeneratorLanguage#addReferencedResource(String)}.
|
||||
*/
|
||||
class XtextGeneratorResourceSetInitializer {
|
||||
|
||||
static val Logger LOG = Logger.getLogger(XtextGeneratorResourceSetInitializer)
|
||||
|
||||
def void initialize(ResourceSet resourceSet, List<String> referencedResources) {
|
||||
val delegate = new StandaloneSetup
|
||||
delegate.resourceSet = resourceSet
|
||||
resourceSet.packageRegistry.put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE)
|
||||
referencedResources.forEach[
|
||||
loadResource(resourceSet)
|
||||
]
|
||||
registerGenModels(resourceSet)
|
||||
registerEPackages(resourceSet)
|
||||
}
|
||||
|
||||
private def void loadResource(String loadedResource, ResourceSet resourceSet) {
|
||||
val loadedResourceUri = URI.createURI(loadedResource)
|
||||
ensureResourceCanBeLoaded(loadedResourceUri, resourceSet)
|
||||
resourceSet.getResource(loadedResourceUri, true)
|
||||
}
|
||||
|
||||
private def void ensureResourceCanBeLoaded(URI loadedResource, ResourceSet resourceSet) {
|
||||
switch (loadedResource.fileExtension) {
|
||||
case 'genmodel': {
|
||||
GenModelPackage.eINSTANCE.getEFactoryInstance
|
||||
val resourceServiceProvider = IResourceServiceProvider.Registry.INSTANCE.getResourceServiceProvider(loadedResource)
|
||||
if (resourceServiceProvider === null) {
|
||||
try {
|
||||
val genModelSupport = Class.forName('org.eclipse.emf.codegen.ecore.xtext.GenModelSupport')
|
||||
val instance = genModelSupport.getDeclaredConstructor().newInstance()
|
||||
genModelSupport.getDeclaredMethod('createInjectorAndDoEMFRegistration').invoke(instance)
|
||||
} catch (ClassNotFoundException e) {
|
||||
LOG.debug("org.eclipse.emf.codegen.ecore.xtext.GenModelSupport not found, GenModels will not be indexed")
|
||||
} catch (Exception e) {
|
||||
LOG.error("Couldn't initialize GenModel support.", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
case 'ecore': {
|
||||
val resourceServiceProvider = IResourceServiceProvider.Registry.INSTANCE.getResourceServiceProvider(loadedResource)
|
||||
if (resourceServiceProvider === null) {
|
||||
try {
|
||||
val ecore = Class.forName('org.eclipse.xtext.ecore.EcoreSupportStandaloneSetup')
|
||||
ecore.getDeclaredMethod('setup', #[]).invoke(null)
|
||||
} catch (ClassNotFoundException e) {
|
||||
LOG.error("Couldn't initialize Ecore support. Is 'org.eclipse.xtext.ecore' on the classpath?")
|
||||
LOG.debug(e.getMessage(), e)
|
||||
} catch (Exception e) {
|
||||
LOG.error("Couldn't initialize Ecore support.", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
case 'xcore': {
|
||||
val resourceServiceProvider = IResourceServiceProvider.Registry.INSTANCE.getResourceServiceProvider(loadedResource)
|
||||
if (resourceServiceProvider === null) {
|
||||
try {
|
||||
val xcore = Class.forName('org.eclipse.emf.ecore.xcore.XcoreStandaloneSetup')
|
||||
xcore.getDeclaredMethod('doSetup', #[]).invoke(null)
|
||||
} catch (ClassNotFoundException e) {
|
||||
LOG.error("Couldn't initialize Xcore support. Is it on the classpath?")
|
||||
LOG.debug(e.getMessage(), e)
|
||||
} catch (Exception e) {
|
||||
LOG.error("Couldn't initialize Xcore support.", e)
|
||||
}
|
||||
}
|
||||
val xcoreLangURI = URI.createPlatformResourceURI('/org.eclipse.emf.ecore.xcore.lib/model/XcoreLang.xcore', true)
|
||||
try {
|
||||
resourceSet.getResource(xcoreLangURI, true)
|
||||
} catch (WrappedException e) {
|
||||
LOG.error("Could not load XcoreLang.xcore.", e)
|
||||
val brokenResource = resourceSet.getResource(xcoreLangURI, false)
|
||||
resourceSet.resources.remove(brokenResource)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private def void registerEPackages(ResourceSet resourceSet) {
|
||||
resourceSet.each(EPackage) [ register ]
|
||||
}
|
||||
|
||||
private def void register(EPackage ePackage) {
|
||||
val registry = ePackage.eResource.resourceSet.packageRegistry
|
||||
if (registry.get(ePackage.nsURI) === null) {
|
||||
registry.put(ePackage.nsURI, ePackage)
|
||||
}
|
||||
}
|
||||
|
||||
private def void registerGenModels(ResourceSet resourceSet) {
|
||||
resourceSet.each(GenModel) [ register ]
|
||||
}
|
||||
|
||||
private def void register(GenModel genModel) {
|
||||
new GenModelHelper().registerGenModel(genModel)
|
||||
}
|
||||
|
||||
private def <Type> void each(ResourceSet resourceSet, Class<Type> type, (Type)=>void strategy) {
|
||||
// don't use forEach loop since the given strategy may trigger additional resource loading
|
||||
for(var i = 0; i < resourceSet.resources.size; i++) {
|
||||
val resource = resourceSet.resources.get(i)
|
||||
resource.contents.filter(type).forEach[ strategy.apply(it) ]
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,296 @@
|
|||
/**
|
||||
* Copyright (c) 2015, 2020 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.xtext.generator.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.emf.codegen.ecore.genmodel.GenClass;
|
||||
import org.eclipse.emf.codegen.ecore.genmodel.GenPackage;
|
||||
import org.eclipse.emf.ecore.EClass;
|
||||
import org.eclipse.emf.ecore.EPackage;
|
||||
import org.eclipse.emf.ecore.resource.ResourceSet;
|
||||
import org.eclipse.xtext.xbase.lib.IterableExtensions;
|
||||
import org.eclipse.xtext.xtext.generator.IXtextGeneratorLanguage;
|
||||
import org.eclipse.xtext.xtext.generator.util.GenModelUtil2;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.base.Splitter;
|
||||
import com.google.common.collect.Iterables;
|
||||
|
||||
/**
|
||||
* Reference to a Java type. Use this for automatic import of types in
|
||||
* {@link JavaFileAccess} and {@link XtendFileAccess}.
|
||||
*/
|
||||
public class TypeReference {
|
||||
public static class QualifiedClassName {
|
||||
private final String packageName;
|
||||
|
||||
private final String className;
|
||||
|
||||
public String getPackageName() {
|
||||
return packageName;
|
||||
}
|
||||
|
||||
public String getClassName() {
|
||||
return className;
|
||||
}
|
||||
|
||||
public QualifiedClassName(String packageName, String className) {
|
||||
this.packageName = packageName;
|
||||
this.className = className;
|
||||
}
|
||||
}
|
||||
|
||||
public static TypeReference typeRef(String name, TypeReference... arguments) {
|
||||
return new TypeReference(name, arguments == null ? null : Arrays.asList(arguments));
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated this method is available for backwards compatibility reasons
|
||||
*/
|
||||
@Deprecated
|
||||
public static TypeReference guessTypeRef(String name, TypeReference... arguments) {
|
||||
return new TypeReference(name, arguments == null ? null : Arrays.asList(arguments), false);
|
||||
}
|
||||
|
||||
public static TypeReference typeRef(Class<?> clazz, TypeReference... arguments) {
|
||||
return new TypeReference(clazz, arguments == null ? null : Arrays.asList(arguments));
|
||||
}
|
||||
|
||||
public static TypeReference typeRef(EClass clazz, IXtextGeneratorLanguage language) {
|
||||
ResourceSet _resourceSet = language.getResourceSet();
|
||||
return new TypeReference(clazz, _resourceSet);
|
||||
}
|
||||
|
||||
private final String packageName;
|
||||
|
||||
private final List<String> simpleNames;
|
||||
|
||||
private final List<TypeReference> typeArguments;
|
||||
|
||||
public TypeReference(String qualifiedName) {
|
||||
this(qualifiedName, ((List<TypeReference>) null));
|
||||
}
|
||||
|
||||
public TypeReference(String qualifiedName, List<TypeReference> arguments) {
|
||||
this(qualifiedName, arguments, true);
|
||||
}
|
||||
|
||||
private TypeReference(String qualifiedName, List<TypeReference> arguments, boolean strict) {
|
||||
this(TypeReference.getPackageName(qualifiedName, strict), TypeReference.getClassName(qualifiedName, strict),
|
||||
arguments);
|
||||
}
|
||||
|
||||
public TypeReference(String packageName, String className) {
|
||||
this(packageName, className, null);
|
||||
}
|
||||
|
||||
public TypeReference(String packageName, String className, List<TypeReference> arguments) {
|
||||
if (packageName == null) {
|
||||
throw new IllegalArgumentException("Invalid package name: " + packageName);
|
||||
}
|
||||
if (className == null) {
|
||||
throw new IllegalArgumentException("Invalid class name: " + className);
|
||||
}
|
||||
this.packageName = packageName;
|
||||
this.simpleNames = Arrays.asList(className.split("\\."));
|
||||
if (arguments == null) {
|
||||
arguments = Collections.emptyList();
|
||||
}
|
||||
this.typeArguments = arguments;
|
||||
}
|
||||
|
||||
public TypeReference(Class<?> clazz) {
|
||||
this(clazz, null);
|
||||
}
|
||||
|
||||
public TypeReference(Class<?> clazz, List<TypeReference> arguments) {
|
||||
if (clazz.isPrimitive()) {
|
||||
throw new IllegalArgumentException("Type is primitive: " + clazz.getName());
|
||||
}
|
||||
if (clazz.isAnonymousClass()) {
|
||||
throw new IllegalArgumentException("Class is anonymous: " + clazz.getName());
|
||||
}
|
||||
if (clazz.isLocalClass()) {
|
||||
throw new IllegalArgumentException("Class is local: " + clazz.getName());
|
||||
}
|
||||
this.packageName = clazz.getPackage().getName();
|
||||
this.simpleNames = new ArrayList<>();
|
||||
if (arguments == null) {
|
||||
arguments = Collections.emptyList();
|
||||
}
|
||||
this.typeArguments = arguments;
|
||||
Class<?> c = clazz;
|
||||
do {
|
||||
this.simpleNames.add(0, c.getSimpleName());
|
||||
c = c.getDeclaringClass();
|
||||
} while (c != null);
|
||||
}
|
||||
|
||||
public TypeReference(EClass clazz, ResourceSet resourceSet) {
|
||||
this(TypeReference.getQualifiedName(clazz, resourceSet));
|
||||
}
|
||||
|
||||
public TypeReference(QualifiedClassName qualifiedClazzName) {
|
||||
this(qualifiedClazzName.packageName, qualifiedClazzName.className, null);
|
||||
}
|
||||
|
||||
public TypeReference(EPackage epackage, ResourceSet resourceSet) {
|
||||
this(TypeReference.getQualifiedName(epackage, resourceSet));
|
||||
}
|
||||
|
||||
private static String getPackageName(String qualifiedName, boolean strict) {
|
||||
List<String> segments = IterableExtensions.toList(Splitter.on(".").split(qualifiedName));
|
||||
if (segments.size() == 1) {
|
||||
return "";
|
||||
}
|
||||
if (strict) {
|
||||
List<String> packageSegments = segments.subList(0, segments.size() - 1);
|
||||
if (!Iterables.isEmpty(Iterables.filter(packageSegments, it -> Character.isUpperCase(it.charAt(0))))) {
|
||||
throw new IllegalArgumentException("Cannot determine the package name of '" + qualifiedName
|
||||
+ "'. Please use the TypeReference(packageName, className) constructor");
|
||||
}
|
||||
return IterableExtensions.join(packageSegments, ".");
|
||||
} else {
|
||||
List<String> packageSegments = segments.subList(0, segments.size() - 1);
|
||||
while (!packageSegments.isEmpty()) {
|
||||
if (Character.isUpperCase(IterableExtensions.last(packageSegments).charAt(0))) {
|
||||
packageSegments = packageSegments.subList(0, packageSegments.size() - 1);
|
||||
} else {
|
||||
return Joiner.on(".").join(packageSegments);
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
private static String getClassName(String qualifiedName, boolean strict) {
|
||||
String packageName = TypeReference.getPackageName(qualifiedName, strict);
|
||||
if (packageName.isEmpty()) {
|
||||
return qualifiedName;
|
||||
} else {
|
||||
return qualifiedName.substring(packageName.length() + 1, qualifiedName.length());
|
||||
}
|
||||
}
|
||||
|
||||
private static QualifiedClassName getQualifiedName(EClass clazz, ResourceSet resourceSet) {
|
||||
if ("http://www.eclipse.org/2008/Xtext".equals(clazz.getEPackage().getNsURI())) {
|
||||
return new QualifiedClassName("org.eclipse.xtext", clazz.getName());
|
||||
} else {
|
||||
if ("http://www.eclipse.org/emf/2002/Ecore".equals(clazz.getEPackage().getNsURI())) {
|
||||
if (clazz.getInstanceTypeName() != null) {
|
||||
String itn = clazz.getInstanceTypeName();
|
||||
return new QualifiedClassName(itn.substring(0, itn.lastIndexOf(".")),
|
||||
itn.substring(itn.lastIndexOf(".") + 1).replace("$", "."));
|
||||
} else {
|
||||
return new QualifiedClassName("org.eclipse.emf.ecore", clazz.getName());
|
||||
}
|
||||
} else {
|
||||
if (clazz.getInstanceTypeName() != null) {
|
||||
String itn = clazz.getInstanceTypeName();
|
||||
return new QualifiedClassName(itn.substring(0, itn.lastIndexOf(".")),
|
||||
itn.substring(itn.lastIndexOf(".") + 1).replace("$", "."));
|
||||
} else {
|
||||
GenClass genClass = GenModelUtil2.getGenClass(clazz, resourceSet);
|
||||
String packageName = genClass.getGenPackage().getInterfacePackageName();
|
||||
return new QualifiedClassName(packageName, genClass.getInterfaceName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static QualifiedClassName getQualifiedName(EPackage epackage, ResourceSet resourceSet) {
|
||||
GenPackage genPackage = GenModelUtil2.getGenPackage(epackage, resourceSet);
|
||||
String packageName = null;
|
||||
if (genPackage.getGenModel().isSuppressEMFMetaData()) {
|
||||
packageName = genPackage.getQualifiedPackageClassName();
|
||||
} else {
|
||||
packageName = genPackage.getReflectionPackageName();
|
||||
}
|
||||
return new QualifiedClassName(packageName, genPackage.getPackageInterfaceName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getName() + IterableExtensions.join(typeArguments, "<", ", ", ">", TypeReference::toString);
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return packageName + "." + Joiner.on(".").join(simpleNames);
|
||||
}
|
||||
|
||||
public String getSimpleName() {
|
||||
return IterableExtensions.last(simpleNames);
|
||||
}
|
||||
|
||||
public String getPath() {
|
||||
return packageName.replace(".", "/") + "/" + Iterables.getFirst(simpleNames, null);
|
||||
}
|
||||
|
||||
public String getJavaPath() {
|
||||
return getPath() + ".java";
|
||||
}
|
||||
|
||||
public String getXtendPath() {
|
||||
return getPath() + ".xtend";
|
||||
}
|
||||
|
||||
public String getPackageName() {
|
||||
return packageName;
|
||||
}
|
||||
|
||||
public List<String> getSimpleNames() {
|
||||
return simpleNames;
|
||||
}
|
||||
|
||||
public List<TypeReference> getTypeArguments() {
|
||||
return typeArguments;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((packageName == null) ? 0 : packageName.hashCode());
|
||||
result = prime * result + ((simpleNames == null) ? 0 : simpleNames.hashCode());
|
||||
result = prime * result + ((typeArguments == null) ? 0 : typeArguments.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
TypeReference other = (TypeReference) obj;
|
||||
if (packageName == null) {
|
||||
if (other.packageName != null)
|
||||
return false;
|
||||
} else if (!packageName.equals(other.packageName))
|
||||
return false;
|
||||
if (simpleNames == null) {
|
||||
if (other.simpleNames != null)
|
||||
return false;
|
||||
} else if (!simpleNames.equals(other.simpleNames))
|
||||
return false;
|
||||
if (typeArguments == null) {
|
||||
if (other.typeArguments != null)
|
||||
return false;
|
||||
} else if (!typeArguments.equals(other.typeArguments))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,212 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2015, 2017 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.xtext.generator.model
|
||||
|
||||
import com.google.common.base.Splitter
|
||||
import java.util.Collections
|
||||
import java.util.List
|
||||
import org.eclipse.emf.ecore.EClass
|
||||
import org.eclipse.emf.ecore.EPackage
|
||||
import org.eclipse.emf.ecore.resource.ResourceSet
|
||||
import org.eclipse.xtend.lib.annotations.Accessors
|
||||
import org.eclipse.xtend.lib.annotations.EqualsHashCode
|
||||
import org.eclipse.xtext.xtext.generator.IXtextGeneratorLanguage
|
||||
import org.eclipse.xtext.xtext.generator.util.GenModelUtil2
|
||||
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor
|
||||
|
||||
/**
|
||||
* Reference to a Java type. Use this for automatic import of types in {@link JavaFileAccess}
|
||||
* and {@link XtendFileAccess}.
|
||||
*/
|
||||
@Accessors
|
||||
@EqualsHashCode
|
||||
class TypeReference {
|
||||
|
||||
static def TypeReference typeRef(String name, TypeReference... arguments) {
|
||||
new TypeReference(name, arguments)
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated this method is available for backwards compatibility reasons
|
||||
*/
|
||||
@Deprecated
|
||||
static def TypeReference guessTypeRef(String name, TypeReference... arguments) {
|
||||
new TypeReference(name, arguments, false)
|
||||
}
|
||||
|
||||
static def TypeReference typeRef(Class<?> clazz, TypeReference... arguments) {
|
||||
new TypeReference(clazz, arguments)
|
||||
}
|
||||
|
||||
static def TypeReference typeRef(EClass clazz, IXtextGeneratorLanguage language) {
|
||||
new TypeReference(clazz, language.resourceSet)
|
||||
}
|
||||
|
||||
val String packageName
|
||||
|
||||
val List<String> simpleNames
|
||||
|
||||
val List<TypeReference> typeArguments
|
||||
|
||||
new(String qualifiedName) {
|
||||
this(qualifiedName, null as List<TypeReference>)
|
||||
}
|
||||
|
||||
new(String qualifiedName, List<TypeReference> arguments) {
|
||||
this(qualifiedName, arguments, true)
|
||||
}
|
||||
|
||||
private new(String qualifiedName, List<TypeReference> arguments, boolean strict) {
|
||||
this(getPackageName(qualifiedName, strict), getClassName(qualifiedName, strict), arguments)
|
||||
}
|
||||
|
||||
new(String packageName, String className) {
|
||||
this(packageName, className, null)
|
||||
}
|
||||
|
||||
new(String packageName, String className, List<TypeReference> arguments) {
|
||||
if (packageName === null)
|
||||
throw new IllegalArgumentException('Invalid package name: ' + packageName)
|
||||
if (className === null)
|
||||
throw new IllegalArgumentException('Invalid class name: ' + className)
|
||||
this.packageName = packageName
|
||||
this.simpleNames = className.split('\\.')
|
||||
this.typeArguments = arguments ?: Collections.emptyList
|
||||
}
|
||||
|
||||
new(Class<?> clazz) {
|
||||
this(clazz, null)
|
||||
}
|
||||
|
||||
new(Class<?> clazz, List<TypeReference> arguments) {
|
||||
if (clazz.primitive)
|
||||
throw new IllegalArgumentException('Type is primitive: ' + clazz.name)
|
||||
if (clazz.anonymousClass)
|
||||
throw new IllegalArgumentException('Class is anonymous: ' + clazz.name)
|
||||
if (clazz.localClass)
|
||||
throw new IllegalArgumentException('Class is local: ' + clazz.name)
|
||||
this.packageName = clazz.package.name
|
||||
this.simpleNames = newArrayList
|
||||
this.typeArguments = arguments ?: Collections.emptyList
|
||||
var c = clazz
|
||||
do {
|
||||
simpleNames.add(0, c.simpleName)
|
||||
c = c.declaringClass
|
||||
} while (c !== null)
|
||||
}
|
||||
|
||||
new(EClass clazz, ResourceSet resourceSet) {
|
||||
// the qualified name might be a nested type, e.g. jav.util.Map.Entry
|
||||
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=483088
|
||||
this(getQualifiedName(clazz, resourceSet))
|
||||
}
|
||||
|
||||
new(QualifiedClassName qualifiedClazzName) {
|
||||
this(qualifiedClazzName.packageName, qualifiedClazzName.className, null)
|
||||
}
|
||||
|
||||
new(EPackage epackage, ResourceSet resourceSet) {
|
||||
this(getQualifiedName(epackage, resourceSet))
|
||||
}
|
||||
|
||||
private static def getPackageName(String qualifiedName, boolean strict) {
|
||||
val segments = Splitter.on('.').split(qualifiedName).toList
|
||||
if (segments.size == 1)
|
||||
return ""
|
||||
if (strict) {
|
||||
val packageSegments = segments.subList(0, segments.length -1)
|
||||
if (!packageSegments.filter[Character.isUpperCase(charAt(0))].isEmpty)
|
||||
throw new IllegalArgumentException("Cannot determine the package name of '" + qualifiedName + "'. Please use the TypeReference(packageName, className) constructor")
|
||||
return packageSegments.join(".")
|
||||
} else {
|
||||
var packageSegments = segments.subList(0, segments.length -1)
|
||||
while(!packageSegments.isEmpty) {
|
||||
if (Character.isUpperCase(packageSegments.last.charAt(0))) {
|
||||
packageSegments = packageSegments.subList(0, packageSegments.length -1)
|
||||
} else {
|
||||
return packageSegments.join(".")
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
private static def getClassName(String qualifiedName, boolean strict) {
|
||||
val packageName = qualifiedName.getPackageName(strict)
|
||||
if (packageName.isEmpty)
|
||||
qualifiedName
|
||||
else
|
||||
qualifiedName.substring(packageName.length + 1, qualifiedName.length)
|
||||
}
|
||||
|
||||
private static def QualifiedClassName getQualifiedName(EClass clazz, ResourceSet resourceSet) {
|
||||
if (clazz.EPackage.nsURI == 'http://www.eclipse.org/2008/Xtext') {
|
||||
new QualifiedClassName('org.eclipse.xtext', clazz.name)
|
||||
} else if (clazz.EPackage.nsURI == 'http://www.eclipse.org/emf/2002/Ecore') {
|
||||
if (clazz.instanceTypeName !== null) {
|
||||
val itn = clazz.instanceTypeName;
|
||||
new QualifiedClassName(itn.substring(0, itn.lastIndexOf('.')),
|
||||
itn.substring(itn.lastIndexOf(".") + 1).replace("$", "."))
|
||||
} else {
|
||||
new QualifiedClassName('org.eclipse.emf.ecore', clazz.name)
|
||||
}
|
||||
} else {
|
||||
if (clazz.instanceTypeName !== null) {
|
||||
val itn = clazz.instanceTypeName;
|
||||
new QualifiedClassName(itn.substring(0, itn.lastIndexOf('.')),
|
||||
itn.substring(itn.lastIndexOf(".") + 1).replace("$", "."))
|
||||
} else {
|
||||
val genClass = GenModelUtil2.getGenClass(clazz, resourceSet)
|
||||
val packageName = genClass.genPackage.getInterfacePackageName();
|
||||
new QualifiedClassName(packageName,
|
||||
genClass.interfaceName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static def QualifiedClassName getQualifiedName(EPackage epackage, ResourceSet resourceSet) {
|
||||
val genPackage = GenModelUtil2.getGenPackage(epackage, resourceSet)
|
||||
val packageName = if (genPackage.getGenModel().isSuppressEMFMetaData()) genPackage.getQualifiedPackageClassName() else genPackage.getReflectionPackageName()
|
||||
new QualifiedClassName(packageName,
|
||||
genPackage.packageInterfaceName)
|
||||
}
|
||||
|
||||
override toString() {
|
||||
name + typeArguments.join('<', ', ', '>', [toString])
|
||||
}
|
||||
|
||||
def String getName() {
|
||||
packageName + '.' + simpleNames.join('.')
|
||||
}
|
||||
|
||||
def String getSimpleName() {
|
||||
simpleNames.last
|
||||
}
|
||||
|
||||
def String getPath() {
|
||||
return packageName.replace('.', '/') + '/' + simpleNames.head
|
||||
}
|
||||
|
||||
def String getJavaPath() {
|
||||
path + ".java"
|
||||
}
|
||||
|
||||
def String getXtendPath() {
|
||||
path + ".xtend"
|
||||
}
|
||||
|
||||
@FinalFieldsConstructor
|
||||
static class QualifiedClassName {
|
||||
@Accessors(PUBLIC_GETTER)
|
||||
val String packageName
|
||||
@Accessors(PUBLIC_GETTER)
|
||||
val String className
|
||||
}
|
||||
|
||||
}
|
|
@ -1,108 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2015, 2017 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.xtext.generator;
|
||||
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Injector;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
import org.eclipse.emf.mwe.utils.DirectoryCleaner;
|
||||
import org.eclipse.xtend.lib.annotations.AccessorType;
|
||||
import org.eclipse.xtend.lib.annotations.Accessors;
|
||||
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
|
||||
import org.eclipse.xtext.xbase.lib.Exceptions;
|
||||
import org.eclipse.xtext.xbase.lib.Functions.Function1;
|
||||
import org.eclipse.xtext.xbase.lib.IterableExtensions;
|
||||
import org.eclipse.xtext.xbase.lib.ListExtensions;
|
||||
import org.eclipse.xtext.xtext.generator.IGuiceAwareGeneratorComponent;
|
||||
import org.eclipse.xtext.xtext.generator.model.IXtextGeneratorFileSystemAccess;
|
||||
import org.eclipse.xtext.xtext.generator.model.project.ISubProjectConfig;
|
||||
import org.eclipse.xtext.xtext.generator.model.project.IXtextProjectConfig;
|
||||
|
||||
/**
|
||||
* This component cleans all directories for generated code according to the project configuration
|
||||
* (usually 'src-gen' for plain layout and 'src/main/xtext-gen' for Maven/Gradle layout).
|
||||
*
|
||||
* @noextend This class should not be extended by clients.
|
||||
*/
|
||||
@SuppressWarnings("all")
|
||||
public class XtextDirectoryCleaner implements IGuiceAwareGeneratorComponent {
|
||||
@Inject
|
||||
private IXtextProjectConfig config;
|
||||
|
||||
@Accessors(AccessorType.PUBLIC_SETTER)
|
||||
private boolean enabled = true;
|
||||
|
||||
@Accessors(AccessorType.PUBLIC_SETTER)
|
||||
private boolean useDefaultExcludes = true;
|
||||
|
||||
private List<String> excludes = CollectionLiterals.<String>newArrayList();
|
||||
|
||||
private List<String> extraDirectories = CollectionLiterals.<String>newArrayList();
|
||||
|
||||
public void addExtraDirectory(final String directory) {
|
||||
this.extraDirectories.add(directory);
|
||||
}
|
||||
|
||||
public void addExclude(final String exclude) {
|
||||
this.excludes.add(exclude);
|
||||
}
|
||||
|
||||
public void clean() {
|
||||
if ((!this.enabled)) {
|
||||
return;
|
||||
}
|
||||
final ArrayList<String> directories = CollectionLiterals.<String>newArrayList();
|
||||
final Function1<ISubProjectConfig, IXtextGeneratorFileSystemAccess> _function = (ISubProjectConfig it) -> {
|
||||
return it.getSrcGen();
|
||||
};
|
||||
List<IXtextGeneratorFileSystemAccess> _map = ListExtensions.map(this.config.getEnabledProjects(), _function);
|
||||
IXtextGeneratorFileSystemAccess _ecoreModel = this.config.getRuntime().getEcoreModel();
|
||||
final Function1<IXtextGeneratorFileSystemAccess, String> _function_1 = (IXtextGeneratorFileSystemAccess it) -> {
|
||||
return it.getPath();
|
||||
};
|
||||
final Function1<String, Boolean> _function_2 = (String it) -> {
|
||||
return Boolean.valueOf(new File(it).isDirectory());
|
||||
};
|
||||
Iterable<String> _filter = IterableExtensions.<String>filter(IterableExtensions.<IXtextGeneratorFileSystemAccess, String>map(IterableExtensions.<IXtextGeneratorFileSystemAccess>filterNull(Iterables.<IXtextGeneratorFileSystemAccess>concat(_map, Collections.<IXtextGeneratorFileSystemAccess>unmodifiableList(CollectionLiterals.<IXtextGeneratorFileSystemAccess>newArrayList(_ecoreModel)))), _function_1), _function_2);
|
||||
Iterables.<String>addAll(directories, _filter);
|
||||
Iterables.<String>addAll(directories, this.extraDirectories);
|
||||
final DirectoryCleaner delegate = new DirectoryCleaner();
|
||||
delegate.setUseDefaultExcludes(this.useDefaultExcludes);
|
||||
final Consumer<String> _function_3 = (String it) -> {
|
||||
delegate.addExclude(it);
|
||||
};
|
||||
this.excludes.forEach(_function_3);
|
||||
final Consumer<String> _function_4 = (String it) -> {
|
||||
try {
|
||||
delegate.cleanFolder(it);
|
||||
} catch (Throwable _e) {
|
||||
throw Exceptions.sneakyThrow(_e);
|
||||
}
|
||||
};
|
||||
directories.forEach(_function_4);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize(final Injector injector) {
|
||||
injector.injectMembers(this);
|
||||
}
|
||||
|
||||
public void setEnabled(final boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public void setUseDefaultExcludes(final boolean useDefaultExcludes) {
|
||||
this.useDefaultExcludes = useDefaultExcludes;
|
||||
}
|
||||
}
|
|
@ -1,174 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2015, 2020 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.xtext.generator;
|
||||
|
||||
import com.google.common.collect.Iterables;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.eclipse.emf.codegen.ecore.genmodel.GenModel;
|
||||
import org.eclipse.emf.codegen.ecore.genmodel.GenModelPackage;
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
import org.eclipse.emf.common.util.WrappedException;
|
||||
import org.eclipse.emf.ecore.EPackage;
|
||||
import org.eclipse.emf.ecore.EcorePackage;
|
||||
import org.eclipse.emf.ecore.resource.Resource;
|
||||
import org.eclipse.emf.ecore.resource.ResourceSet;
|
||||
import org.eclipse.emf.mwe.utils.GenModelHelper;
|
||||
import org.eclipse.emf.mwe.utils.StandaloneSetup;
|
||||
import org.eclipse.xtext.resource.IResourceServiceProvider;
|
||||
import org.eclipse.xtext.xbase.lib.Exceptions;
|
||||
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
|
||||
|
||||
/**
|
||||
* Initializes a resource set in order to load the grammar of a language. The resources to include are specified
|
||||
* via {@link XtextGeneratorLanguage#addReferencedResource(String)}.
|
||||
*/
|
||||
@SuppressWarnings("all")
|
||||
public class XtextGeneratorResourceSetInitializer {
|
||||
private static final Logger LOG = Logger.getLogger(XtextGeneratorResourceSetInitializer.class);
|
||||
|
||||
public void initialize(final ResourceSet resourceSet, final List<String> referencedResources) {
|
||||
final StandaloneSetup delegate = new StandaloneSetup();
|
||||
delegate.setResourceSet(resourceSet);
|
||||
resourceSet.getPackageRegistry().put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE);
|
||||
final Consumer<String> _function = (String it) -> {
|
||||
this.loadResource(it, resourceSet);
|
||||
};
|
||||
referencedResources.forEach(_function);
|
||||
this.registerGenModels(resourceSet);
|
||||
this.registerEPackages(resourceSet);
|
||||
}
|
||||
|
||||
private void loadResource(final String loadedResource, final ResourceSet resourceSet) {
|
||||
final URI loadedResourceUri = URI.createURI(loadedResource);
|
||||
this.ensureResourceCanBeLoaded(loadedResourceUri, resourceSet);
|
||||
resourceSet.getResource(loadedResourceUri, true);
|
||||
}
|
||||
|
||||
private void ensureResourceCanBeLoaded(final URI loadedResource, final ResourceSet resourceSet) {
|
||||
String _fileExtension = loadedResource.fileExtension();
|
||||
if (_fileExtension != null) {
|
||||
switch (_fileExtension) {
|
||||
case "genmodel":
|
||||
GenModelPackage.eINSTANCE.getEFactoryInstance();
|
||||
final IResourceServiceProvider resourceServiceProvider = IResourceServiceProvider.Registry.INSTANCE.getResourceServiceProvider(loadedResource);
|
||||
if ((resourceServiceProvider == null)) {
|
||||
try {
|
||||
final Class<?> genModelSupport = Class.forName("org.eclipse.emf.codegen.ecore.xtext.GenModelSupport");
|
||||
final Object instance = genModelSupport.getDeclaredConstructor().newInstance();
|
||||
genModelSupport.getDeclaredMethod("createInjectorAndDoEMFRegistration").invoke(instance);
|
||||
} catch (final Throwable _t) {
|
||||
if (_t instanceof ClassNotFoundException) {
|
||||
XtextGeneratorResourceSetInitializer.LOG.debug("org.eclipse.emf.codegen.ecore.xtext.GenModelSupport not found, GenModels will not be indexed");
|
||||
} else if (_t instanceof Exception) {
|
||||
final Exception e_1 = (Exception)_t;
|
||||
XtextGeneratorResourceSetInitializer.LOG.error("Couldn\'t initialize GenModel support.", e_1);
|
||||
} else {
|
||||
throw Exceptions.sneakyThrow(_t);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case "ecore":
|
||||
final IResourceServiceProvider resourceServiceProvider_1 = IResourceServiceProvider.Registry.INSTANCE.getResourceServiceProvider(loadedResource);
|
||||
if ((resourceServiceProvider_1 == null)) {
|
||||
try {
|
||||
final Class<?> ecore = Class.forName("org.eclipse.xtext.ecore.EcoreSupportStandaloneSetup");
|
||||
ecore.getDeclaredMethod("setup", new Class[] {}).invoke(null);
|
||||
} catch (final Throwable _t) {
|
||||
if (_t instanceof ClassNotFoundException) {
|
||||
final ClassNotFoundException e = (ClassNotFoundException)_t;
|
||||
XtextGeneratorResourceSetInitializer.LOG.error("Couldn\'t initialize Ecore support. Is \'org.eclipse.xtext.ecore\' on the classpath?");
|
||||
XtextGeneratorResourceSetInitializer.LOG.debug(e.getMessage(), e);
|
||||
} else if (_t instanceof Exception) {
|
||||
final Exception e_1 = (Exception)_t;
|
||||
XtextGeneratorResourceSetInitializer.LOG.error("Couldn\'t initialize Ecore support.", e_1);
|
||||
} else {
|
||||
throw Exceptions.sneakyThrow(_t);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case "xcore":
|
||||
final IResourceServiceProvider resourceServiceProvider_2 = IResourceServiceProvider.Registry.INSTANCE.getResourceServiceProvider(loadedResource);
|
||||
if ((resourceServiceProvider_2 == null)) {
|
||||
try {
|
||||
final Class<?> xcore = Class.forName("org.eclipse.emf.ecore.xcore.XcoreStandaloneSetup");
|
||||
xcore.getDeclaredMethod("doSetup", new Class[] {}).invoke(null);
|
||||
} catch (final Throwable _t) {
|
||||
if (_t instanceof ClassNotFoundException) {
|
||||
final ClassNotFoundException e = (ClassNotFoundException)_t;
|
||||
XtextGeneratorResourceSetInitializer.LOG.error("Couldn\'t initialize Xcore support. Is it on the classpath?");
|
||||
XtextGeneratorResourceSetInitializer.LOG.debug(e.getMessage(), e);
|
||||
} else if (_t instanceof Exception) {
|
||||
final Exception e_1 = (Exception)_t;
|
||||
XtextGeneratorResourceSetInitializer.LOG.error("Couldn\'t initialize Xcore support.", e_1);
|
||||
} else {
|
||||
throw Exceptions.sneakyThrow(_t);
|
||||
}
|
||||
}
|
||||
}
|
||||
final URI xcoreLangURI = URI.createPlatformResourceURI("/org.eclipse.emf.ecore.xcore.lib/model/XcoreLang.xcore", true);
|
||||
try {
|
||||
resourceSet.getResource(xcoreLangURI, true);
|
||||
} catch (final Throwable _t) {
|
||||
if (_t instanceof WrappedException) {
|
||||
final WrappedException e = (WrappedException)_t;
|
||||
XtextGeneratorResourceSetInitializer.LOG.error("Could not load XcoreLang.xcore.", e);
|
||||
final Resource brokenResource = resourceSet.getResource(xcoreLangURI, false);
|
||||
resourceSet.getResources().remove(brokenResource);
|
||||
} else {
|
||||
throw Exceptions.sneakyThrow(_t);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void registerEPackages(final ResourceSet resourceSet) {
|
||||
final Procedure1<EPackage> _function = (EPackage it) -> {
|
||||
this.register(it);
|
||||
};
|
||||
this.<EPackage>each(resourceSet, EPackage.class, _function);
|
||||
}
|
||||
|
||||
private void register(final EPackage ePackage) {
|
||||
final EPackage.Registry registry = ePackage.eResource().getResourceSet().getPackageRegistry();
|
||||
Object _get = registry.get(ePackage.getNsURI());
|
||||
boolean _tripleEquals = (_get == null);
|
||||
if (_tripleEquals) {
|
||||
registry.put(ePackage.getNsURI(), ePackage);
|
||||
}
|
||||
}
|
||||
|
||||
private void registerGenModels(final ResourceSet resourceSet) {
|
||||
final Procedure1<GenModel> _function = (GenModel it) -> {
|
||||
this.register(it);
|
||||
};
|
||||
this.<GenModel>each(resourceSet, GenModel.class, _function);
|
||||
}
|
||||
|
||||
private void register(final GenModel genModel) {
|
||||
new GenModelHelper().registerGenModel(genModel);
|
||||
}
|
||||
|
||||
private <Type extends Object> void each(final ResourceSet resourceSet, final Class<Type> type, final Procedure1<? super Type> strategy) {
|
||||
for (int i = 0; (i < resourceSet.getResources().size()); i++) {
|
||||
{
|
||||
final Resource resource = resourceSet.getResources().get(i);
|
||||
final Consumer<Type> _function = (Type it) -> {
|
||||
strategy.apply(it);
|
||||
};
|
||||
Iterables.<Type>filter(resource.getContents(), type).forEach(_function);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,407 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2015, 2017 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.xtext.generator.model;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
import com.google.common.base.Splitter;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import org.eclipse.emf.codegen.ecore.genmodel.GenClass;
|
||||
import org.eclipse.emf.codegen.ecore.genmodel.GenPackage;
|
||||
import org.eclipse.emf.ecore.EClass;
|
||||
import org.eclipse.emf.ecore.EPackage;
|
||||
import org.eclipse.emf.ecore.resource.ResourceSet;
|
||||
import org.eclipse.xtend.lib.annotations.AccessorType;
|
||||
import org.eclipse.xtend.lib.annotations.Accessors;
|
||||
import org.eclipse.xtend.lib.annotations.EqualsHashCode;
|
||||
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor;
|
||||
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
|
||||
import org.eclipse.xtext.xbase.lib.Conversions;
|
||||
import org.eclipse.xtext.xbase.lib.Functions.Function1;
|
||||
import org.eclipse.xtext.xbase.lib.IterableExtensions;
|
||||
import org.eclipse.xtext.xbase.lib.Pure;
|
||||
import org.eclipse.xtext.xtext.generator.IXtextGeneratorLanguage;
|
||||
import org.eclipse.xtext.xtext.generator.util.GenModelUtil2;
|
||||
|
||||
/**
|
||||
* Reference to a Java type. Use this for automatic import of types in {@link JavaFileAccess}
|
||||
* and {@link XtendFileAccess}.
|
||||
*/
|
||||
@Accessors
|
||||
@EqualsHashCode
|
||||
@SuppressWarnings("all")
|
||||
public class TypeReference {
|
||||
@FinalFieldsConstructor
|
||||
public static class QualifiedClassName {
|
||||
@Accessors(AccessorType.PUBLIC_GETTER)
|
||||
private final String packageName;
|
||||
|
||||
@Accessors(AccessorType.PUBLIC_GETTER)
|
||||
private final String className;
|
||||
|
||||
@Pure
|
||||
public String getPackageName() {
|
||||
return this.packageName;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public String getClassName() {
|
||||
return this.className;
|
||||
}
|
||||
|
||||
public QualifiedClassName(final String packageName, final String className) {
|
||||
super();
|
||||
this.packageName = packageName;
|
||||
this.className = className;
|
||||
}
|
||||
}
|
||||
|
||||
public static TypeReference typeRef(final String name, final TypeReference... arguments) {
|
||||
return new TypeReference(name, (List<TypeReference>)Conversions.doWrapArray(arguments));
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated this method is available for backwards compatibility reasons
|
||||
*/
|
||||
@Deprecated
|
||||
public static TypeReference guessTypeRef(final String name, final TypeReference... arguments) {
|
||||
return new TypeReference(name, (List<TypeReference>)Conversions.doWrapArray(arguments), false);
|
||||
}
|
||||
|
||||
public static TypeReference typeRef(final Class<?> clazz, final TypeReference... arguments) {
|
||||
return new TypeReference(clazz, (List<TypeReference>)Conversions.doWrapArray(arguments));
|
||||
}
|
||||
|
||||
public static TypeReference typeRef(final EClass clazz, final IXtextGeneratorLanguage language) {
|
||||
ResourceSet _resourceSet = language.getResourceSet();
|
||||
return new TypeReference(clazz, _resourceSet);
|
||||
}
|
||||
|
||||
private final String packageName;
|
||||
|
||||
private final List<String> simpleNames;
|
||||
|
||||
private final List<TypeReference> typeArguments;
|
||||
|
||||
public TypeReference(final String qualifiedName) {
|
||||
this(qualifiedName, ((List<TypeReference>) null));
|
||||
}
|
||||
|
||||
public TypeReference(final String qualifiedName, final List<TypeReference> arguments) {
|
||||
this(qualifiedName, arguments, true);
|
||||
}
|
||||
|
||||
private TypeReference(final String qualifiedName, final List<TypeReference> arguments, final boolean strict) {
|
||||
this(TypeReference.getPackageName(qualifiedName, strict), TypeReference.getClassName(qualifiedName, strict), arguments);
|
||||
}
|
||||
|
||||
public TypeReference(final String packageName, final String className) {
|
||||
this(packageName, className, null);
|
||||
}
|
||||
|
||||
public TypeReference(final String packageName, final String className, final List<TypeReference> arguments) {
|
||||
if ((packageName == null)) {
|
||||
throw new IllegalArgumentException(("Invalid package name: " + packageName));
|
||||
}
|
||||
if ((className == null)) {
|
||||
throw new IllegalArgumentException(("Invalid class name: " + className));
|
||||
}
|
||||
this.packageName = packageName;
|
||||
this.simpleNames = ((List<String>)Conversions.doWrapArray(className.split("\\.")));
|
||||
List<TypeReference> _elvis = null;
|
||||
if (arguments != null) {
|
||||
_elvis = arguments;
|
||||
} else {
|
||||
List<TypeReference> _emptyList = Collections.<TypeReference>emptyList();
|
||||
_elvis = _emptyList;
|
||||
}
|
||||
this.typeArguments = _elvis;
|
||||
}
|
||||
|
||||
public TypeReference(final Class<?> clazz) {
|
||||
this(clazz, null);
|
||||
}
|
||||
|
||||
public TypeReference(final Class<?> clazz, final List<TypeReference> arguments) {
|
||||
boolean _isPrimitive = clazz.isPrimitive();
|
||||
if (_isPrimitive) {
|
||||
String _name = clazz.getName();
|
||||
String _plus = ("Type is primitive: " + _name);
|
||||
throw new IllegalArgumentException(_plus);
|
||||
}
|
||||
boolean _isAnonymousClass = clazz.isAnonymousClass();
|
||||
if (_isAnonymousClass) {
|
||||
String _name_1 = clazz.getName();
|
||||
String _plus_1 = ("Class is anonymous: " + _name_1);
|
||||
throw new IllegalArgumentException(_plus_1);
|
||||
}
|
||||
boolean _isLocalClass = clazz.isLocalClass();
|
||||
if (_isLocalClass) {
|
||||
String _name_2 = clazz.getName();
|
||||
String _plus_2 = ("Class is local: " + _name_2);
|
||||
throw new IllegalArgumentException(_plus_2);
|
||||
}
|
||||
this.packageName = clazz.getPackage().getName();
|
||||
this.simpleNames = CollectionLiterals.<String>newArrayList();
|
||||
List<TypeReference> _elvis = null;
|
||||
if (arguments != null) {
|
||||
_elvis = arguments;
|
||||
} else {
|
||||
List<TypeReference> _emptyList = Collections.<TypeReference>emptyList();
|
||||
_elvis = _emptyList;
|
||||
}
|
||||
this.typeArguments = _elvis;
|
||||
Class<?> c = clazz;
|
||||
do {
|
||||
{
|
||||
this.simpleNames.add(0, c.getSimpleName());
|
||||
c = c.getDeclaringClass();
|
||||
}
|
||||
} while((c != null));
|
||||
}
|
||||
|
||||
public TypeReference(final EClass clazz, final ResourceSet resourceSet) {
|
||||
this(TypeReference.getQualifiedName(clazz, resourceSet));
|
||||
}
|
||||
|
||||
public TypeReference(final TypeReference.QualifiedClassName qualifiedClazzName) {
|
||||
this(qualifiedClazzName.packageName, qualifiedClazzName.className, null);
|
||||
}
|
||||
|
||||
public TypeReference(final EPackage epackage, final ResourceSet resourceSet) {
|
||||
this(TypeReference.getQualifiedName(epackage, resourceSet));
|
||||
}
|
||||
|
||||
private static String getPackageName(final String qualifiedName, final boolean strict) {
|
||||
final List<String> segments = IterableExtensions.<String>toList(Splitter.on(".").split(qualifiedName));
|
||||
int _size = segments.size();
|
||||
boolean _equals = (_size == 1);
|
||||
if (_equals) {
|
||||
return "";
|
||||
}
|
||||
if (strict) {
|
||||
int _length = ((Object[])Conversions.unwrapArray(segments, Object.class)).length;
|
||||
int _minus = (_length - 1);
|
||||
final List<String> packageSegments = segments.subList(0, _minus);
|
||||
final Function1<String, Boolean> _function = (String it) -> {
|
||||
return Boolean.valueOf(Character.isUpperCase(it.charAt(0)));
|
||||
};
|
||||
boolean _isEmpty = IterableExtensions.isEmpty(IterableExtensions.<String>filter(packageSegments, _function));
|
||||
boolean _not = (!_isEmpty);
|
||||
if (_not) {
|
||||
throw new IllegalArgumentException((("Cannot determine the package name of \'" + qualifiedName) + "\'. Please use the TypeReference(packageName, className) constructor"));
|
||||
}
|
||||
return IterableExtensions.join(packageSegments, ".");
|
||||
} else {
|
||||
int _length_1 = ((Object[])Conversions.unwrapArray(segments, Object.class)).length;
|
||||
int _minus_1 = (_length_1 - 1);
|
||||
List<String> packageSegments_1 = segments.subList(0, _minus_1);
|
||||
while ((!packageSegments_1.isEmpty())) {
|
||||
boolean _isUpperCase = Character.isUpperCase(IterableExtensions.<String>last(packageSegments_1).charAt(0));
|
||||
if (_isUpperCase) {
|
||||
final List<String> _converted_packageSegments_1 = (List<String>)packageSegments_1;
|
||||
int _length_2 = ((Object[])Conversions.unwrapArray(_converted_packageSegments_1, Object.class)).length;
|
||||
int _minus_2 = (_length_2 - 1);
|
||||
packageSegments_1 = packageSegments_1.subList(0, _minus_2);
|
||||
} else {
|
||||
return IterableExtensions.join(packageSegments_1, ".");
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
private static String getClassName(final String qualifiedName, final boolean strict) {
|
||||
String _xblockexpression = null;
|
||||
{
|
||||
final String packageName = TypeReference.getPackageName(qualifiedName, strict);
|
||||
String _xifexpression = null;
|
||||
boolean _isEmpty = packageName.isEmpty();
|
||||
if (_isEmpty) {
|
||||
_xifexpression = qualifiedName;
|
||||
} else {
|
||||
int _length = packageName.length();
|
||||
int _plus = (_length + 1);
|
||||
_xifexpression = qualifiedName.substring(_plus, qualifiedName.length());
|
||||
}
|
||||
_xblockexpression = _xifexpression;
|
||||
}
|
||||
return _xblockexpression;
|
||||
}
|
||||
|
||||
private static TypeReference.QualifiedClassName getQualifiedName(final EClass clazz, final ResourceSet resourceSet) {
|
||||
TypeReference.QualifiedClassName _xifexpression = null;
|
||||
String _nsURI = clazz.getEPackage().getNsURI();
|
||||
boolean _equals = Objects.equal(_nsURI, "http://www.eclipse.org/2008/Xtext");
|
||||
if (_equals) {
|
||||
String _name = clazz.getName();
|
||||
_xifexpression = new TypeReference.QualifiedClassName("org.eclipse.xtext", _name);
|
||||
} else {
|
||||
TypeReference.QualifiedClassName _xifexpression_1 = null;
|
||||
String _nsURI_1 = clazz.getEPackage().getNsURI();
|
||||
boolean _equals_1 = Objects.equal(_nsURI_1, "http://www.eclipse.org/emf/2002/Ecore");
|
||||
if (_equals_1) {
|
||||
TypeReference.QualifiedClassName _xifexpression_2 = null;
|
||||
String _instanceTypeName = clazz.getInstanceTypeName();
|
||||
boolean _tripleNotEquals = (_instanceTypeName != null);
|
||||
if (_tripleNotEquals) {
|
||||
TypeReference.QualifiedClassName _xblockexpression = null;
|
||||
{
|
||||
final String itn = clazz.getInstanceTypeName();
|
||||
String _substring = itn.substring(0, itn.lastIndexOf("."));
|
||||
int _lastIndexOf = itn.lastIndexOf(".");
|
||||
int _plus = (_lastIndexOf + 1);
|
||||
String _replace = itn.substring(_plus).replace("$", ".");
|
||||
_xblockexpression = new TypeReference.QualifiedClassName(_substring, _replace);
|
||||
}
|
||||
_xifexpression_2 = _xblockexpression;
|
||||
} else {
|
||||
String _name_1 = clazz.getName();
|
||||
_xifexpression_2 = new TypeReference.QualifiedClassName("org.eclipse.emf.ecore", _name_1);
|
||||
}
|
||||
_xifexpression_1 = _xifexpression_2;
|
||||
} else {
|
||||
TypeReference.QualifiedClassName _xifexpression_3 = null;
|
||||
String _instanceTypeName_1 = clazz.getInstanceTypeName();
|
||||
boolean _tripleNotEquals_1 = (_instanceTypeName_1 != null);
|
||||
if (_tripleNotEquals_1) {
|
||||
TypeReference.QualifiedClassName _xblockexpression_1 = null;
|
||||
{
|
||||
final String itn = clazz.getInstanceTypeName();
|
||||
String _substring = itn.substring(0, itn.lastIndexOf("."));
|
||||
int _lastIndexOf = itn.lastIndexOf(".");
|
||||
int _plus = (_lastIndexOf + 1);
|
||||
String _replace = itn.substring(_plus).replace("$", ".");
|
||||
_xblockexpression_1 = new TypeReference.QualifiedClassName(_substring, _replace);
|
||||
}
|
||||
_xifexpression_3 = _xblockexpression_1;
|
||||
} else {
|
||||
TypeReference.QualifiedClassName _xblockexpression_2 = null;
|
||||
{
|
||||
final GenClass genClass = GenModelUtil2.getGenClass(clazz, resourceSet);
|
||||
final String packageName = genClass.getGenPackage().getInterfacePackageName();
|
||||
String _interfaceName = genClass.getInterfaceName();
|
||||
_xblockexpression_2 = new TypeReference.QualifiedClassName(packageName, _interfaceName);
|
||||
}
|
||||
_xifexpression_3 = _xblockexpression_2;
|
||||
}
|
||||
_xifexpression_1 = _xifexpression_3;
|
||||
}
|
||||
_xifexpression = _xifexpression_1;
|
||||
}
|
||||
return _xifexpression;
|
||||
}
|
||||
|
||||
private static TypeReference.QualifiedClassName getQualifiedName(final EPackage epackage, final ResourceSet resourceSet) {
|
||||
TypeReference.QualifiedClassName _xblockexpression = null;
|
||||
{
|
||||
final GenPackage genPackage = GenModelUtil2.getGenPackage(epackage, resourceSet);
|
||||
String _xifexpression = null;
|
||||
boolean _isSuppressEMFMetaData = genPackage.getGenModel().isSuppressEMFMetaData();
|
||||
if (_isSuppressEMFMetaData) {
|
||||
_xifexpression = genPackage.getQualifiedPackageClassName();
|
||||
} else {
|
||||
_xifexpression = genPackage.getReflectionPackageName();
|
||||
}
|
||||
final String packageName = _xifexpression;
|
||||
String _packageInterfaceName = genPackage.getPackageInterfaceName();
|
||||
_xblockexpression = new TypeReference.QualifiedClassName(packageName, _packageInterfaceName);
|
||||
}
|
||||
return _xblockexpression;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String _name = this.getName();
|
||||
final Function1<TypeReference, CharSequence> _function = (TypeReference it) -> {
|
||||
return it.toString();
|
||||
};
|
||||
String _join = IterableExtensions.<TypeReference>join(this.typeArguments, "<", ", ", ">", _function);
|
||||
return (_name + _join);
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
String _join = IterableExtensions.join(this.simpleNames, ".");
|
||||
return ((this.packageName + ".") + _join);
|
||||
}
|
||||
|
||||
public String getSimpleName() {
|
||||
return IterableExtensions.<String>last(this.simpleNames);
|
||||
}
|
||||
|
||||
public String getPath() {
|
||||
String _replace = this.packageName.replace(".", "/");
|
||||
String _plus = (_replace + "/");
|
||||
String _head = IterableExtensions.<String>head(this.simpleNames);
|
||||
return (_plus + _head);
|
||||
}
|
||||
|
||||
public String getJavaPath() {
|
||||
String _path = this.getPath();
|
||||
return (_path + ".java");
|
||||
}
|
||||
|
||||
public String getXtendPath() {
|
||||
String _path = this.getPath();
|
||||
return (_path + ".xtend");
|
||||
}
|
||||
|
||||
@Pure
|
||||
public String getPackageName() {
|
||||
return this.packageName;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public List<String> getSimpleNames() {
|
||||
return this.simpleNames;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public List<TypeReference> getTypeArguments() {
|
||||
return this.typeArguments;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Pure
|
||||
public boolean equals(final Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
TypeReference other = (TypeReference) obj;
|
||||
if (this.packageName == null) {
|
||||
if (other.packageName != null)
|
||||
return false;
|
||||
} else if (!this.packageName.equals(other.packageName))
|
||||
return false;
|
||||
if (this.simpleNames == null) {
|
||||
if (other.simpleNames != null)
|
||||
return false;
|
||||
} else if (!this.simpleNames.equals(other.simpleNames))
|
||||
return false;
|
||||
if (this.typeArguments == null) {
|
||||
if (other.typeArguments != null)
|
||||
return false;
|
||||
} else if (!this.typeArguments.equals(other.typeArguments))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Pure
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((this.packageName== null) ? 0 : this.packageName.hashCode());
|
||||
result = prime * result + ((this.simpleNames== null) ? 0 : this.simpleNames.hashCode());
|
||||
return prime * result + ((this.typeArguments== null) ? 0 : this.typeArguments.hashCode());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,228 @@
|
|||
/**
|
||||
* Copyright (c) 2015, 2020 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.xtext.wizard;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Set;
|
||||
|
||||
import org.eclipse.xtext.util.JUnitVersion;
|
||||
import org.eclipse.xtext.util.JavaVersion;
|
||||
import org.eclipse.xtext.util.Strings;
|
||||
import org.eclipse.xtext.util.XtextVersion;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
public class WizardConfiguration {
|
||||
private String rootLocation;
|
||||
|
||||
private String baseName;
|
||||
|
||||
private XtextVersion xtextVersion = XtextVersion.getCurrent();
|
||||
|
||||
private final Ecore2XtextConfiguration ecore2Xtext = new Ecore2XtextConfiguration();
|
||||
|
||||
private Charset encoding = Charset.defaultCharset();
|
||||
|
||||
private String lineDelimiter = Strings.newLine();
|
||||
|
||||
private BuildSystem preferredBuildSystem = BuildSystem.NONE;
|
||||
|
||||
private SourceLayout sourceLayout = SourceLayout.PLAIN;
|
||||
|
||||
private ProjectLayout projectLayout = ProjectLayout.FLAT;
|
||||
|
||||
private boolean needsGradleWrapper = true;
|
||||
|
||||
private JavaVersion javaVersion = JavaVersion.JAVA8;
|
||||
|
||||
private LanguageServer languageServer = LanguageServer.NONE;
|
||||
|
||||
private JUnitVersion junitVersion = JUnitVersion.DEFAULT;
|
||||
|
||||
private final LanguageDescriptor language = new LanguageDescriptor();
|
||||
|
||||
private final RuntimeProjectDescriptor runtimeProject = new RuntimeProjectDescriptor(this);
|
||||
|
||||
private final IdeProjectDescriptor ideProject = new IdeProjectDescriptor(this);
|
||||
|
||||
private final UiProjectDescriptor uiProject = new UiProjectDescriptor(this);
|
||||
|
||||
private final WebProjectDescriptor webProject = new WebProjectDescriptor(this);
|
||||
|
||||
private final ParentProjectDescriptor parentProject = new ParentProjectDescriptor(this);
|
||||
|
||||
private final TargetPlatformProject targetPlatformProject = new TargetPlatformProject(this);
|
||||
|
||||
private final SdkFeatureProject sdkProject = new SdkFeatureProject(this);
|
||||
|
||||
private final P2RepositoryProject p2Project = new P2RepositoryProject(this);
|
||||
|
||||
public Set<ProjectDescriptor> getEnabledProjects() {
|
||||
Iterable<ProjectDescriptor> productionProjects = Iterables.filter(
|
||||
Lists.newArrayList(parentProject, runtimeProject, ideProject,
|
||||
uiProject, webProject, targetPlatformProject, sdkProject, p2Project),
|
||||
ProjectDescriptor::isEnabled);
|
||||
Iterable<TestProjectDescriptor> testProjects = Iterables
|
||||
.filter(Iterables.transform(
|
||||
Iterables.filter(productionProjects, TestedProjectDescriptor.class),
|
||||
TestedProjectDescriptor::getTestProject), it -> it.isEnabled() && it.isSeparate());
|
||||
return ImmutableSet.copyOf(Iterables.concat(productionProjects, testProjects));
|
||||
}
|
||||
|
||||
public boolean needsMavenBuild() {
|
||||
return BuildSystem.MAVEN.equals(preferredBuildSystem)
|
||||
|| BuildSystem.GRADLE.equals(preferredBuildSystem) && uiProject.isEnabled();
|
||||
}
|
||||
|
||||
public boolean needsTychoBuild() {
|
||||
return needsMavenBuild() && runtimeProject.isEclipsePluginProject();
|
||||
}
|
||||
|
||||
public boolean needsGradleBuild() {
|
||||
return BuildSystem.GRADLE.equals(preferredBuildSystem);
|
||||
}
|
||||
|
||||
public boolean isNeedsGradleWrapper() {
|
||||
return needsGradleWrapper && needsGradleBuild();
|
||||
}
|
||||
|
||||
public String getRootLocation() {
|
||||
return rootLocation;
|
||||
}
|
||||
|
||||
public void setRootLocation(String rootLocation) {
|
||||
this.rootLocation = rootLocation;
|
||||
}
|
||||
|
||||
public String getBaseName() {
|
||||
return baseName;
|
||||
}
|
||||
|
||||
public void setBaseName(String baseName) {
|
||||
this.baseName = baseName;
|
||||
}
|
||||
|
||||
public XtextVersion getXtextVersion() {
|
||||
return xtextVersion;
|
||||
}
|
||||
|
||||
public void setXtextVersion(XtextVersion xtextVersion) {
|
||||
this.xtextVersion = xtextVersion;
|
||||
}
|
||||
|
||||
public Ecore2XtextConfiguration getEcore2Xtext() {
|
||||
return ecore2Xtext;
|
||||
}
|
||||
|
||||
public Charset getEncoding() {
|
||||
return encoding;
|
||||
}
|
||||
|
||||
public void setEncoding(Charset encoding) {
|
||||
this.encoding = encoding;
|
||||
}
|
||||
|
||||
public String getLineDelimiter() {
|
||||
return lineDelimiter;
|
||||
}
|
||||
|
||||
public void setLineDelimiter(String lineDelimiter) {
|
||||
this.lineDelimiter = lineDelimiter;
|
||||
}
|
||||
|
||||
public BuildSystem getPreferredBuildSystem() {
|
||||
return preferredBuildSystem;
|
||||
}
|
||||
|
||||
public void setPreferredBuildSystem(BuildSystem preferredBuildSystem) {
|
||||
this.preferredBuildSystem = preferredBuildSystem;
|
||||
}
|
||||
|
||||
public SourceLayout getSourceLayout() {
|
||||
return sourceLayout;
|
||||
}
|
||||
|
||||
public void setSourceLayout(SourceLayout sourceLayout) {
|
||||
this.sourceLayout = sourceLayout;
|
||||
}
|
||||
|
||||
public ProjectLayout getProjectLayout() {
|
||||
return projectLayout;
|
||||
}
|
||||
|
||||
public void setProjectLayout(ProjectLayout projectLayout) {
|
||||
this.projectLayout = projectLayout;
|
||||
}
|
||||
|
||||
public void setNeedsGradleWrapper(boolean needsGradleWrapper) {
|
||||
this.needsGradleWrapper = needsGradleWrapper;
|
||||
}
|
||||
|
||||
public JavaVersion getJavaVersion() {
|
||||
return javaVersion;
|
||||
}
|
||||
|
||||
public void setJavaVersion(JavaVersion javaVersion) {
|
||||
this.javaVersion = javaVersion;
|
||||
}
|
||||
|
||||
public LanguageServer getLanguageServer() {
|
||||
return languageServer;
|
||||
}
|
||||
|
||||
public void setLanguageServer(LanguageServer languageServer) {
|
||||
this.languageServer = languageServer;
|
||||
}
|
||||
|
||||
public JUnitVersion getJunitVersion() {
|
||||
return junitVersion;
|
||||
}
|
||||
|
||||
public void setJunitVersion(JUnitVersion junitVersion) {
|
||||
this.junitVersion = junitVersion;
|
||||
}
|
||||
|
||||
public LanguageDescriptor getLanguage() {
|
||||
return language;
|
||||
}
|
||||
|
||||
public RuntimeProjectDescriptor getRuntimeProject() {
|
||||
return runtimeProject;
|
||||
}
|
||||
|
||||
public IdeProjectDescriptor getIdeProject() {
|
||||
return ideProject;
|
||||
}
|
||||
|
||||
public UiProjectDescriptor getUiProject() {
|
||||
return uiProject;
|
||||
}
|
||||
|
||||
public WebProjectDescriptor getWebProject() {
|
||||
return webProject;
|
||||
}
|
||||
|
||||
public ParentProjectDescriptor getParentProject() {
|
||||
return parentProject;
|
||||
}
|
||||
|
||||
public TargetPlatformProject getTargetPlatformProject() {
|
||||
return targetPlatformProject;
|
||||
}
|
||||
|
||||
public SdkFeatureProject getSdkProject() {
|
||||
return sdkProject;
|
||||
}
|
||||
|
||||
public P2RepositoryProject getP2Project() {
|
||||
return p2Project;
|
||||
}
|
||||
}
|
|
@ -1,84 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2015, 2017 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.xtext.wizard
|
||||
|
||||
import com.google.common.collect.ImmutableSet
|
||||
import java.nio.charset.Charset
|
||||
import java.util.Set
|
||||
import org.eclipse.xtend.lib.annotations.Accessors
|
||||
import org.eclipse.xtext.util.XtextVersion
|
||||
import org.eclipse.xtext.util.JavaVersion
|
||||
import org.eclipse.xtext.util.Strings
|
||||
import org.eclipse.xtext.util.JUnitVersion
|
||||
|
||||
@Accessors
|
||||
class WizardConfiguration {
|
||||
String rootLocation
|
||||
String baseName
|
||||
XtextVersion xtextVersion = XtextVersion.current
|
||||
val Ecore2XtextConfiguration ecore2Xtext = new Ecore2XtextConfiguration
|
||||
|
||||
Charset encoding = Charset.defaultCharset
|
||||
String lineDelimiter = Strings.newLine
|
||||
BuildSystem preferredBuildSystem = BuildSystem.NONE
|
||||
|
||||
SourceLayout sourceLayout = SourceLayout.PLAIN
|
||||
ProjectLayout projectLayout = ProjectLayout.FLAT
|
||||
|
||||
boolean needsGradleWrapper = true
|
||||
JavaVersion javaVersion = JavaVersion.JAVA8
|
||||
LanguageServer languageServer = LanguageServer.NONE
|
||||
JUnitVersion junitVersion = JUnitVersion.DEFAULT;
|
||||
|
||||
val LanguageDescriptor language = new LanguageDescriptor
|
||||
|
||||
val runtimeProject = new RuntimeProjectDescriptor(this)
|
||||
val ideProject = new IdeProjectDescriptor(this)
|
||||
val uiProject = new UiProjectDescriptor(this)
|
||||
val webProject = new WebProjectDescriptor(this)
|
||||
val parentProject = new ParentProjectDescriptor(this)
|
||||
val targetPlatformProject = new TargetPlatformProject(this)
|
||||
val sdkProject = new SdkFeatureProject(this)
|
||||
val p2Project = new P2RepositoryProject(this)
|
||||
|
||||
def Set<ProjectDescriptor> getEnabledProjects() {
|
||||
val productionProjects = #[
|
||||
parentProject,
|
||||
runtimeProject,
|
||||
ideProject,
|
||||
uiProject,
|
||||
webProject,
|
||||
targetPlatformProject,
|
||||
sdkProject,
|
||||
p2Project
|
||||
].filter[enabled]
|
||||
|
||||
val testProjects = productionProjects
|
||||
.filter(TestedProjectDescriptor)
|
||||
.map[testProject]
|
||||
.filter[enabled && separate]
|
||||
ImmutableSet.copyOf(productionProjects + testProjects)
|
||||
}
|
||||
|
||||
def boolean needsMavenBuild() {
|
||||
preferredBuildSystem == BuildSystem.MAVEN || preferredBuildSystem == BuildSystem.GRADLE && uiProject.enabled
|
||||
}
|
||||
|
||||
def boolean needsTychoBuild() {
|
||||
needsMavenBuild && runtimeProject.isEclipsePluginProject
|
||||
}
|
||||
|
||||
def boolean needsGradleBuild() {
|
||||
preferredBuildSystem == BuildSystem.GRADLE
|
||||
}
|
||||
|
||||
def boolean isNeedsGradleWrapper() {
|
||||
return needsGradleWrapper && needsGradleBuild
|
||||
}
|
||||
}
|
|
@ -1,279 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2015, 2017 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.xtext.wizard;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.Iterables;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
import org.eclipse.xtend.lib.annotations.Accessors;
|
||||
import org.eclipse.xtext.util.JUnitVersion;
|
||||
import org.eclipse.xtext.util.JavaVersion;
|
||||
import org.eclipse.xtext.util.Strings;
|
||||
import org.eclipse.xtext.util.XtextVersion;
|
||||
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
|
||||
import org.eclipse.xtext.xbase.lib.Functions.Function1;
|
||||
import org.eclipse.xtext.xbase.lib.IterableExtensions;
|
||||
import org.eclipse.xtext.xbase.lib.Pure;
|
||||
import org.eclipse.xtext.xtext.wizard.BuildSystem;
|
||||
import org.eclipse.xtext.xtext.wizard.Ecore2XtextConfiguration;
|
||||
import org.eclipse.xtext.xtext.wizard.IdeProjectDescriptor;
|
||||
import org.eclipse.xtext.xtext.wizard.LanguageDescriptor;
|
||||
import org.eclipse.xtext.xtext.wizard.LanguageServer;
|
||||
import org.eclipse.xtext.xtext.wizard.P2RepositoryProject;
|
||||
import org.eclipse.xtext.xtext.wizard.ParentProjectDescriptor;
|
||||
import org.eclipse.xtext.xtext.wizard.ProjectDescriptor;
|
||||
import org.eclipse.xtext.xtext.wizard.ProjectLayout;
|
||||
import org.eclipse.xtext.xtext.wizard.RuntimeProjectDescriptor;
|
||||
import org.eclipse.xtext.xtext.wizard.SdkFeatureProject;
|
||||
import org.eclipse.xtext.xtext.wizard.SourceLayout;
|
||||
import org.eclipse.xtext.xtext.wizard.TargetPlatformProject;
|
||||
import org.eclipse.xtext.xtext.wizard.TestProjectDescriptor;
|
||||
import org.eclipse.xtext.xtext.wizard.TestedProjectDescriptor;
|
||||
import org.eclipse.xtext.xtext.wizard.UiProjectDescriptor;
|
||||
import org.eclipse.xtext.xtext.wizard.WebProjectDescriptor;
|
||||
|
||||
@Accessors
|
||||
@SuppressWarnings("all")
|
||||
public class WizardConfiguration {
|
||||
private String rootLocation;
|
||||
|
||||
private String baseName;
|
||||
|
||||
private XtextVersion xtextVersion = XtextVersion.getCurrent();
|
||||
|
||||
private final Ecore2XtextConfiguration ecore2Xtext = new Ecore2XtextConfiguration();
|
||||
|
||||
private Charset encoding = Charset.defaultCharset();
|
||||
|
||||
private String lineDelimiter = Strings.newLine();
|
||||
|
||||
private BuildSystem preferredBuildSystem = BuildSystem.NONE;
|
||||
|
||||
private SourceLayout sourceLayout = SourceLayout.PLAIN;
|
||||
|
||||
private ProjectLayout projectLayout = ProjectLayout.FLAT;
|
||||
|
||||
private boolean needsGradleWrapper = true;
|
||||
|
||||
private JavaVersion javaVersion = JavaVersion.JAVA8;
|
||||
|
||||
private LanguageServer languageServer = LanguageServer.NONE;
|
||||
|
||||
private JUnitVersion junitVersion = JUnitVersion.DEFAULT;
|
||||
|
||||
private final LanguageDescriptor language = new LanguageDescriptor();
|
||||
|
||||
private final RuntimeProjectDescriptor runtimeProject = new RuntimeProjectDescriptor(this);
|
||||
|
||||
private final IdeProjectDescriptor ideProject = new IdeProjectDescriptor(this);
|
||||
|
||||
private final UiProjectDescriptor uiProject = new UiProjectDescriptor(this);
|
||||
|
||||
private final WebProjectDescriptor webProject = new WebProjectDescriptor(this);
|
||||
|
||||
private final ParentProjectDescriptor parentProject = new ParentProjectDescriptor(this);
|
||||
|
||||
private final TargetPlatformProject targetPlatformProject = new TargetPlatformProject(this);
|
||||
|
||||
private final SdkFeatureProject sdkProject = new SdkFeatureProject(this);
|
||||
|
||||
private final P2RepositoryProject p2Project = new P2RepositoryProject(this);
|
||||
|
||||
public Set<ProjectDescriptor> getEnabledProjects() {
|
||||
ImmutableSet<ProjectDescriptor> _xblockexpression = null;
|
||||
{
|
||||
final Function1<ProjectDescriptor, Boolean> _function = (ProjectDescriptor it) -> {
|
||||
return Boolean.valueOf(it.isEnabled());
|
||||
};
|
||||
final Iterable<? extends ProjectDescriptor> productionProjects = IterableExtensions.filter(Collections.<ProjectDescriptor>unmodifiableList(CollectionLiterals.<ProjectDescriptor>newArrayList(this.parentProject, this.runtimeProject, this.ideProject, this.uiProject, this.webProject, this.targetPlatformProject, this.sdkProject, this.p2Project)), _function);
|
||||
final Function1<TestedProjectDescriptor, TestProjectDescriptor> _function_1 = (TestedProjectDescriptor it) -> {
|
||||
return it.getTestProject();
|
||||
};
|
||||
final Function1<TestProjectDescriptor, Boolean> _function_2 = (TestProjectDescriptor it) -> {
|
||||
return Boolean.valueOf((it.isEnabled() && it.isSeparate()));
|
||||
};
|
||||
final Iterable<TestProjectDescriptor> testProjects = IterableExtensions.<TestProjectDescriptor>filter(IterableExtensions.<TestedProjectDescriptor, TestProjectDescriptor>map(Iterables.<TestedProjectDescriptor>filter(productionProjects, TestedProjectDescriptor.class), _function_1), _function_2);
|
||||
Iterable<ProjectDescriptor> _plus = Iterables.<ProjectDescriptor>concat(productionProjects, testProjects);
|
||||
_xblockexpression = ImmutableSet.<ProjectDescriptor>copyOf(_plus);
|
||||
}
|
||||
return _xblockexpression;
|
||||
}
|
||||
|
||||
public boolean needsMavenBuild() {
|
||||
return (Objects.equal(this.preferredBuildSystem, BuildSystem.MAVEN) || (Objects.equal(this.preferredBuildSystem, BuildSystem.GRADLE) && this.uiProject.isEnabled()));
|
||||
}
|
||||
|
||||
public boolean needsTychoBuild() {
|
||||
return (this.needsMavenBuild() && this.runtimeProject.isEclipsePluginProject());
|
||||
}
|
||||
|
||||
public boolean needsGradleBuild() {
|
||||
return Objects.equal(this.preferredBuildSystem, BuildSystem.GRADLE);
|
||||
}
|
||||
|
||||
public boolean isNeedsGradleWrapper() {
|
||||
return (this.needsGradleWrapper && this.needsGradleBuild());
|
||||
}
|
||||
|
||||
@Pure
|
||||
public String getRootLocation() {
|
||||
return this.rootLocation;
|
||||
}
|
||||
|
||||
public void setRootLocation(final String rootLocation) {
|
||||
this.rootLocation = rootLocation;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public String getBaseName() {
|
||||
return this.baseName;
|
||||
}
|
||||
|
||||
public void setBaseName(final String baseName) {
|
||||
this.baseName = baseName;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public XtextVersion getXtextVersion() {
|
||||
return this.xtextVersion;
|
||||
}
|
||||
|
||||
public void setXtextVersion(final XtextVersion xtextVersion) {
|
||||
this.xtextVersion = xtextVersion;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public Ecore2XtextConfiguration getEcore2Xtext() {
|
||||
return this.ecore2Xtext;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public Charset getEncoding() {
|
||||
return this.encoding;
|
||||
}
|
||||
|
||||
public void setEncoding(final Charset encoding) {
|
||||
this.encoding = encoding;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public String getLineDelimiter() {
|
||||
return this.lineDelimiter;
|
||||
}
|
||||
|
||||
public void setLineDelimiter(final String lineDelimiter) {
|
||||
this.lineDelimiter = lineDelimiter;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public BuildSystem getPreferredBuildSystem() {
|
||||
return this.preferredBuildSystem;
|
||||
}
|
||||
|
||||
public void setPreferredBuildSystem(final BuildSystem preferredBuildSystem) {
|
||||
this.preferredBuildSystem = preferredBuildSystem;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public SourceLayout getSourceLayout() {
|
||||
return this.sourceLayout;
|
||||
}
|
||||
|
||||
public void setSourceLayout(final SourceLayout sourceLayout) {
|
||||
this.sourceLayout = sourceLayout;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public ProjectLayout getProjectLayout() {
|
||||
return this.projectLayout;
|
||||
}
|
||||
|
||||
public void setProjectLayout(final ProjectLayout projectLayout) {
|
||||
this.projectLayout = projectLayout;
|
||||
}
|
||||
|
||||
public void setNeedsGradleWrapper(final boolean needsGradleWrapper) {
|
||||
this.needsGradleWrapper = needsGradleWrapper;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public JavaVersion getJavaVersion() {
|
||||
return this.javaVersion;
|
||||
}
|
||||
|
||||
public void setJavaVersion(final JavaVersion javaVersion) {
|
||||
this.javaVersion = javaVersion;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public LanguageServer getLanguageServer() {
|
||||
return this.languageServer;
|
||||
}
|
||||
|
||||
public void setLanguageServer(final LanguageServer languageServer) {
|
||||
this.languageServer = languageServer;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public JUnitVersion getJunitVersion() {
|
||||
return this.junitVersion;
|
||||
}
|
||||
|
||||
public void setJunitVersion(final JUnitVersion junitVersion) {
|
||||
this.junitVersion = junitVersion;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public LanguageDescriptor getLanguage() {
|
||||
return this.language;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public RuntimeProjectDescriptor getRuntimeProject() {
|
||||
return this.runtimeProject;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public IdeProjectDescriptor getIdeProject() {
|
||||
return this.ideProject;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public UiProjectDescriptor getUiProject() {
|
||||
return this.uiProject;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public WebProjectDescriptor getWebProject() {
|
||||
return this.webProject;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public ParentProjectDescriptor getParentProject() {
|
||||
return this.parentProject;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public TargetPlatformProject getTargetPlatformProject() {
|
||||
return this.targetPlatformProject;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public SdkFeatureProject getSdkProject() {
|
||||
return this.sdkProject;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public P2RepositoryProject getP2Project() {
|
||||
return this.p2Project;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue