Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support invocation, overriding and intercepting of default methods.
Fixed #25
- Loading branch information
Showing
4 changed files
with
152 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
...net.enilink.composition/src/test/java/net/enilink/composition/test/DefaultMethodTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 Fraunhofer IWU and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* Contributors: | ||
* Fraunhofer IWU - initial API and implementation | ||
*******************************************************************************/ | ||
package net.enilink.composition.test; | ||
|
||
import net.enilink.composition.annotations.Iri; | ||
import net.enilink.composition.annotations.ParameterTypes; | ||
import net.enilink.composition.mappers.RoleMapper; | ||
import org.aopalliance.intercept.MethodInvocation; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Tests for composition with default interface methods. | ||
*/ | ||
public class DefaultMethodTest extends CompositionTestCase { | ||
@Iri("urn:test:Value") | ||
public interface Value { | ||
} | ||
|
||
@Iri("urn:test:ConcreteType") | ||
public interface Concept { | ||
Value getValue(); | ||
|
||
void setValue(Value value); | ||
|
||
default boolean hasValue() { | ||
return getValue() != null; | ||
} | ||
|
||
default int testAbstractMethod() { | ||
return 1; | ||
} | ||
|
||
default int testInterceptMethod() { | ||
return 0; | ||
} | ||
|
||
default String testOnlyDefaultNoImpl() { | ||
return "a"; | ||
} | ||
} | ||
|
||
public static abstract class ConceptBehaviour implements Concept { | ||
Value value; | ||
|
||
@Override | ||
public Value getValue() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public void setValue(Value value) { | ||
this.value = value; | ||
} | ||
} | ||
|
||
public static abstract class ConceptBehaviour2 implements Concept { | ||
@Override | ||
public boolean hasValue() { | ||
return Concept.super.hasValue(); | ||
} | ||
|
||
@Override | ||
public int testInterceptMethod() { | ||
return 1; | ||
} | ||
} | ||
|
||
public static abstract class ConceptBehaviour3 implements Concept { | ||
@Override | ||
public abstract int testAbstractMethod(); | ||
} | ||
|
||
public static abstract class ConceptBehaviour4 implements Concept { | ||
@Override | ||
public abstract int testAbstractMethod(); | ||
|
||
@ParameterTypes({}) | ||
public int testInterceptMethod(MethodInvocation invocation) { | ||
return 3; | ||
} | ||
} | ||
|
||
@Override | ||
protected void initRoleMapper(RoleMapper<String> roleMapper) { | ||
super.initRoleMapper(roleMapper); | ||
|
||
roleMapper.addConcept(Value.class); | ||
roleMapper.addConcept(Concept.class); | ||
roleMapper.addBehaviour(ConceptBehaviour.class); | ||
roleMapper.addBehaviour(ConceptBehaviour2.class); | ||
roleMapper.addBehaviour(ConceptBehaviour3.class); | ||
roleMapper.addBehaviour(ConceptBehaviour4.class); | ||
} | ||
|
||
@Test | ||
public void testCreateAndSet() throws Exception { | ||
Concept object = objectFactory.createObject(Concept.class); | ||
Assert.assertEquals(object.getValue(), null); | ||
Assert.assertFalse(object.hasValue()); | ||
Value value = objectFactory.createObject(Value.class); | ||
object.setValue(value); | ||
Assert.assertEquals(object.getValue(), value); | ||
Assert.assertTrue(object.hasValue()); | ||
Assert.assertEquals(1, object.testAbstractMethod()); | ||
Assert.assertEquals(3, object.testInterceptMethod()); | ||
Assert.assertEquals("a", object.testOnlyDefaultNoImpl()); | ||
} | ||
} |