Skip to content

Commit

Permalink
tests for Java 8 default methods
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenzoBettini committed Feb 8, 2024
1 parent d76bf84 commit fb28bcf
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,52 @@ public void testWildcardSuperType() throws Exception {
"does not override it");
}

@Test public void testConflictingDefaultMethods() throws Exception {
var source = "class C implements "
+ "test.InterfaceWithDefaultMethod1,"
+ "test.InterfaceWithDefaultMethod2 { }";
var model = parse(source);
validationHelper.assertError(model, MY_CLASS, CONFLICTING_DEFAULT_METHODS,
source.indexOf("C"), 1,
"The type C inherits multiple implementations of the method foo()"
+ " from InterfaceWithDefaultMethod1 and InterfaceWithDefaultMethod2.");
}

@Test public void testConflictingDefaultMethods00() throws Exception {
var source = "interface C extends "
+ "test.InterfaceWithDefaultMethod1,"
+ "test.InterfaceWithDefaultMethod2 { }";
var model = parse(source);
validationHelper.assertError(model, MY_INTERFACE, CONFLICTING_DEFAULT_METHODS,
source.indexOf("C"), 1,
"The type C inherits multiple implementations of the method foo()"
+ " from InterfaceWithDefaultMethod1 and InterfaceWithDefaultMethod2.");
}

@Test public void testConflictingDefaultMethods01() throws Exception {
var source = "class C implements "
+ "test.InterfaceWithDefaultMethod1,"
+ "test.InterfaceWithAbstractMethod { }";
var model = parse(source);
validationHelper.assertError(model, MY_CLASS, CONFLICTING_DEFAULT_METHODS,
source.indexOf("C"), 1,
"The non-abstract method foo() inherited from "
+ "InterfaceWithDefaultMethod1 conflicts with the method foo()"
+ " inherited from InterfaceWithAbstractMethod.");
}

@Test public void testConflictingDefaultMethods02() throws Exception {
var source = "class C implements "
+ "test.InterfaceWithAbstractMethod,"
+ "test.InterfaceWithDefaultMethod1 { }";
var model = parse(source);
validationHelper.assertError(model, MY_CLASS, CONFLICTING_DEFAULT_METHODS,
source.indexOf("C"), 1,
"The non-abstract method foo() inherited from "
+ "InterfaceWithDefaultMethod1 conflicts with the method foo()"
+ " inherited from InterfaceWithAbstractMethod.");
}

@Test public void testDuplicateParameter() throws Exception {
var source = "class Foo { def void foo(int x, int x) {} }";
var model = parse(source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,35 @@
*******************************************************************************/
package org.eclipse.xtext.xbase.tests.validation;

import org.eclipse.xtext.util.JavaVersion;
import org.eclipse.xtext.xbase.testing.JavaVersionModule;
import org.eclipse.xtext.xbase.testlanguages.JvmGenericTypeValidatorTestLangRuntimeModule;
import org.eclipse.xtext.xbase.testlanguages.JvmGenericTypeValidatorTestLangStandaloneSetup;
import org.eclipse.xtext.xbase.testlanguages.tests.JvmGenericTypeValidatorTestLangInjectorProvider;

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

/**
* To let the validator/scoping/linking find the Java classes of "testdata" referred in some tests also in an OSGI and Maven/Tycho
* environment. This is required because the original {@link JvmGenericTypeValidatorTestLangInjectorProvider} is not in this bundle.
* Moreover, we configure Java 8.
*
* @author Lorenzo Bettini - Initial contribution and API
*/
public class JvmGenericTypeValidatorTestLangInjectorProviderCustom extends JvmGenericTypeValidatorTestLangInjectorProvider {

@Override
protected Injector internalCreateInjector() {
return new JvmGenericTypeValidatorTestLangStandaloneSetup() {
@Override
public Injector createInjector() {
return Guice.createInjector(createRuntimeModule(),
new JavaVersionModule(JavaVersion.JAVA8));
}
}.createInjectorAndDoEMFRegistration();
}

@Override
protected JvmGenericTypeValidatorTestLangRuntimeModule createRuntimeModule() {
// make it work also with Maven/Tycho and OSGI
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*******************************************************************************
* Copyright (c) 2024 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 test;

/**
* @author Lorenzo Bettini - Initial contribution and API
*/
public interface InterfaceWithAbstractMethod {
void foo();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*******************************************************************************
* Copyright (c) 2024 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 test;

/**
* @author Lorenzo Bettini - Initial contribution and API
*/
public interface InterfaceWithDefaultMethod1 {
default void foo() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*******************************************************************************
* Copyright (c) 2024 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 test;

/**
* @author Lorenzo Bettini - Initial contribution and API
*/
public interface InterfaceWithDefaultMethod2 {
default void foo() {
}
}

0 comments on commit fb28bcf

Please sign in to comment.