Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.eclipse.emf.ecore.impl.EObjectImpl;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.impl.ResourceImpl;
import org.eclipse.xtext.util.OnChangeEvictingCache.CacheAdapter;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -33,6 +34,7 @@
import com.avaloq.tools.ddk.typesystem.typemodel.IType;
import com.avaloq.tools.ddk.typesystem.typemodel.TypeModelFactory;
import com.avaloq.tools.ddk.typesystem.typemodel.TypeModelPackage;
import com.avaloq.tools.ddk.xtext.resource.ResourceOnlyCacheAdapter;


@SuppressWarnings("nls")
Expand Down Expand Up @@ -172,6 +174,29 @@ protected IType type(final IExpression expression) {
assertEquals(1, typeComputations[0], "type of expression1 not computed exactly once");
}

@Test
void testTypeCacheWithResourceOnlyCacheAdapter() {
Resource resource = new ResourceImpl();
resource.getContents().add(expression1Container);
final CacheAdapter cache = ResourceOnlyCacheAdapter.install(resource);
int[] typeComputations = new int[1];
ITypeProvider provider = new PlainTypeProvider() {
@Override
protected IType type(final IExpression expression) {
typeComputations[0]++;
return super.type(expression);
}
};
assertEquals(type1, provider.getType(expression1), "type with resource-only cache adapter not type1");
expression1Container.eUnset(expressionContainerReference);
expression1Container.eSet(expressionContainerReference, expression1);
assertEquals(type1, provider.getType(expression1), "type after object change not type1");
assertEquals(1, typeComputations[0], "object change evicted the cached type");
cache.clearValues();
assertEquals(type1, provider.getType(expression1), "type after clear not type1");
assertEquals(2, typeComputations[0], "explicit clear did not evict the cached type");
}

@Test
void testDoNothingTypeProvider() {
// we are mainly testing that the AbstractTypeProvider does not crash when the subclass does nothing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.avaloq.tools.ddk.xtext.resource.PatternAwareEObjectDescriptionLookUpTest;
import com.avaloq.tools.ddk.xtext.resource.ReferenceUriCachingTest;
import com.avaloq.tools.ddk.xtext.resource.ResourceDescriptionDeltaTest;
import com.avaloq.tools.ddk.xtext.resource.ResourceOnlyCacheAdapterTest;
import com.avaloq.tools.ddk.xtext.resource.SelectorFragmentIndexTest;
import com.avaloq.tools.ddk.xtext.scoping.AbstractScopeNameProviderTest;
import com.avaloq.tools.ddk.xtext.scoping.ContainerQueryBuilderTest;
Expand Down Expand Up @@ -76,7 +77,8 @@
SelectorFragmentIndexTest.class,
PatternAwareEObjectDescriptionLookUpConcurrencyTest.class,
IssueAwareRuleTest.class,
AbstractScopeNameProviderTest.class})
AbstractScopeNameProviderTest.class,
ResourceOnlyCacheAdapterTest.class})
// @Format-On
public class XtextTestSuite {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
/*******************************************************************************
* Copyright (c) 2026 Avaloq Group AG and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Avaloq Group AG - initial API and implementation
*******************************************************************************/
package com.avaloq.tools.ddk.xtext.resource;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceImpl;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.xtext.resource.DerivedStateAwareResource;
import org.eclipse.xtext.resource.IDerivedStateComputer;
import org.eclipse.xtext.util.OnChangeEvictingCache;
import org.eclipse.xtext.util.OnChangeEvictingCache.CacheAdapter;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import com.avaloq.tools.ddk.xtext.build.BuildPhases;


@SuppressWarnings("nls")
public class ResourceOnlyCacheAdapterTest {

private static final String KEY = "key";
private static final String VALUE = "value";
private static final EcoreFactory FACTORY = EcoreFactory.eINSTANCE;

private EClass root;
private EAttribute attribute;
private CountingResource resource;

/** Resource counting the calls to {@link #getContents()}. */
private static class CountingResource extends ResourceImpl {
private int getContentsCalls;

CountingResource() {
super(URI.createURI("test:/r.xmi"));
}

@Override
public EList<EObject> getContents() {
getContentsCalls++;
return super.getContents();
}
}

/** Derived state aware resource which is loaded without parsing anything. */
private static class LoadedDerivedStateResource extends DerivedStateAwareResource {
LoadedDerivedStateResource() {
isLoaded = true;
}
}

@BeforeEach
void setUp() {
root = FACTORY.createEClass();
attribute = FACTORY.createEAttribute();
root.getEStructuralFeatures().add(attribute);
resource = new CountingResource();
resource.getContents().add(root);
resource.getContentsCalls = 0;
}

private CacheAdapter installWithValue() {
CacheAdapter adapter = ResourceOnlyCacheAdapter.install(resource);
adapter.set(KEY, VALUE);
return adapter;
}

@Test
void adaptsOnlyTheResource() {
CacheAdapter adapter = ResourceOnlyCacheAdapter.install(resource);
assertInstanceOf(ResourceOnlyCacheAdapter.class, adapter);
assertSame(adapter, new OnChangeEvictingCache().getOrCreate(resource));
assertFalse(root.eAdapters().contains(adapter));
assertFalse(attribute.eAdapters().contains(adapter));
assertEquals(0, resource.getContentsCalls, "installing must not access the contents");

EClass newRoot = FACTORY.createEClass();
resource.getContents().add(newRoot);
assertFalse(newRoot.eAdapters().contains(adapter));
}

@Test
void keepsExistingCacheAdapter() {
CacheAdapter existing = new OnChangeEvictingCache().getOrCreate(resource);
assertSame(existing, ResourceOnlyCacheAdapter.install(resource));
assertEquals(1, resource.eAdapters().size());
}

@Test
void deepChangeKeepsValues() {
CacheAdapter adapter = installWithValue();
attribute.setName("changedAttribute");
root.setName("changedClass");
assertEquals(VALUE, adapter.get(KEY));
}

@Test
void addingRootClearsValues() {
CacheAdapter adapter = installWithValue();
resource.getContents().add(FACTORY.createEClass());
assertNull(adapter.get(KEY));
}

@Test
void removingRootClearsValues() {
CacheAdapter adapter = installWithValue();
resource.getContents().remove(root);
assertNull(adapter.get(KEY));
}

@Test
void unloadClearsValues() {
CacheAdapter adapter = installWithValue();
resource.unload();
assertNull(adapter.get(KEY));
}

@Test
void explicitClearClearsValues() {
CacheAdapter adapter = installWithValue();
new OnChangeEvictingCache().clear(resource);
assertNull(adapter.get(KEY));
}

@Test
void removingAdapterDoesNotAccessContents() {
CacheAdapter adapter = ResourceOnlyCacheAdapter.install(resource);
resource.eAdapters().remove(adapter);
assertEquals(0, resource.getContentsCalls);
}

@Test
void derivedStateInstallAndDiscardClearValues() {
int[] installs = new int[1];
DerivedStateAwareResource derivedResource = new LoadedDerivedStateResource();
derivedResource.setCache(new OnChangeEvictingCache());
derivedResource.setDerivedStateComputer(new IDerivedStateComputer() {
@Override
public void installDerivedState(final DerivedStateAwareResource res, final boolean preIndexingPhase) {
installs[0]++;
}

@Override
public void discardDerivedState(final DerivedStateAwareResource res) {
// nothing to discard
}
});

CacheAdapter adapter = ResourceOnlyCacheAdapter.install(derivedResource);
assertEquals(0, installs[0], "installing must not install the derived state");

adapter.set(KEY, VALUE);
derivedResource.installDerivedState(false);
assertEquals(1, installs[0]);
assertNull(adapter.get(KEY));

adapter.set(KEY, VALUE);
derivedResource.discardDerivedState();
assertNull(adapter.get(KEY));
}

@Test
void installerOnlyInstallsWhileIndexing() {
ResourceSet resourceSet = new ResourceSetImpl();
ResourceOnlyCacheAdapter.IndexingInstaller.addTo(resourceSet);
ResourceOnlyCacheAdapter.IndexingInstaller.addTo(resourceSet);
assertEquals(1, resourceSet.eAdapters().size());

Resource notIndexed = resourceSet.createResource(URI.createURI("test:/a.xmi"));
assertFalse(new OnChangeEvictingCache().getOrCreate(notIndexed) instanceof ResourceOnlyCacheAdapter);

BuildPhases.setIndexing(resourceSet, true);
Resource indexed = resourceSet.createResource(URI.createURI("test:/b.xmi"));
assertInstanceOf(ResourceOnlyCacheAdapter.class, new OnChangeEvictingCache().getOrCreate(indexed));
}

@Test
void resourceCacheStatisticsReadAdapterFields() {
ResourceOnlyCacheAdapter.install(resource);
ResourceCache<String, String> cache = new ResourceCache<>(resource, false);
cache.set(KEY, VALUE);
assertEquals(1, cache.getStatistics().getEntries());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ public void set(final K key, final V value) {
@SuppressWarnings("unchecked")
public CacheStatistics getStatistics() {
try {
Field hitsField = backend.getClass().getDeclaredField("hits"); //$NON-NLS-1$
Field hitsField = CacheAdapter.class.getDeclaredField("hits"); //$NON-NLS-1$
hitsField.setAccessible(true);
int hits = hitsField.getInt(backend);

Field missesField = backend.getClass().getDeclaredField("misses"); //$NON-NLS-1$
Field missesField = CacheAdapter.class.getDeclaredField("misses"); //$NON-NLS-1$
missesField.setAccessible(true);
int misses = missesField.getInt(backend);

Field valuesField = backend.getClass().getDeclaredField("values"); //$NON-NLS-1$
Field valuesField = CacheAdapter.class.getDeclaredField("values"); //$NON-NLS-1$
valuesField.setAccessible(true);
Map<Object, Object> values = (Map<Object, Object>) valuesField.get(backend);

Expand Down
Loading