-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
247 additions
and
4 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
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
80 changes: 80 additions & 0 deletions
80
core/src/test/java/org/jboss/jandex/test/PermittedSubclassesTest.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,80 @@ | ||
package org.jboss.jandex.test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import org.jboss.jandex.ClassInfo; | ||
import org.jboss.jandex.DotName; | ||
import org.jboss.jandex.Index; | ||
import org.jboss.jandex.Indexer; | ||
import org.jboss.jandex.test.util.IndexingUtil; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class PermittedSubclassesTest { | ||
@Test | ||
public void test() throws IOException { | ||
Indexer indexer = new Indexer(); | ||
indexer.index(PermittedSubclassesTest.class.getResourceAsStream("/test/expr/Add.class")); | ||
indexer.index(PermittedSubclassesTest.class.getResourceAsStream("/test/expr/Arith.class")); | ||
indexer.index(PermittedSubclassesTest.class.getResourceAsStream("/test/expr/BestValue.class")); | ||
indexer.index(PermittedSubclassesTest.class.getResourceAsStream("/test/expr/Expr.class")); | ||
indexer.index(PermittedSubclassesTest.class.getResourceAsStream("/test/expr/Mul.class")); | ||
indexer.index(PermittedSubclassesTest.class.getResourceAsStream("/test/expr/Value.class")); | ||
Index index = indexer.complete(); | ||
|
||
doTest(index); | ||
doTest(IndexingUtil.roundtrip(index)); | ||
} | ||
|
||
private void doTest(Index index) { | ||
ClassInfo expr = index.getClassByName("test.expr.Expr"); | ||
assertTrue(expr.isSealed()); | ||
assertFalse(expr.isFinal()); | ||
assertEquals(setOf("test.expr.Value", "test.expr.Arith"), expr.permittedSubclasses()); | ||
|
||
ClassInfo value = index.getClassByName("test.expr.Value"); | ||
assertFalse(value.isSealed()); | ||
assertFalse(value.isFinal()); | ||
assertTrue(value.interfaceNames().contains(DotName.createSimple("test.expr.Expr"))); | ||
assertEquals(Collections.emptySet(), value.permittedSubclasses()); | ||
|
||
ClassInfo bestValue = index.getClassByName("test.expr.BestValue"); | ||
assertFalse(bestValue.isSealed()); | ||
assertFalse(bestValue.isFinal()); | ||
assertEquals(bestValue.superName(), DotName.createSimple("test.expr.Value")); | ||
assertEquals(Collections.emptySet(), bestValue.permittedSubclasses()); | ||
|
||
ClassInfo arith = index.getClassByName("test.expr.Arith"); | ||
assertTrue(arith.isSealed()); | ||
assertFalse(arith.isFinal()); | ||
assertTrue(arith.isAbstract()); | ||
assertTrue(value.interfaceNames().contains(DotName.createSimple("test.expr.Expr"))); | ||
assertEquals(setOf("test.expr.Add", "test.expr.Mul"), arith.permittedSubclasses()); | ||
|
||
ClassInfo add = index.getClassByName("test.expr.Add"); | ||
assertFalse(add.isSealed()); | ||
assertTrue(add.isFinal()); | ||
assertEquals(DotName.createSimple("test.expr.Arith"), add.superName()); | ||
assertEquals(Collections.emptySet(), value.permittedSubclasses()); | ||
|
||
ClassInfo mul = index.getClassByName("test.expr.Mul"); | ||
assertFalse(mul.isSealed()); | ||
assertTrue(mul.isFinal()); | ||
assertEquals(DotName.createSimple("test.expr.Arith"), mul.superName()); | ||
assertEquals(Collections.emptySet(), value.permittedSubclasses()); | ||
} | ||
|
||
private static Set<DotName> setOf(String... strings) { | ||
Set<DotName> result = new HashSet<>(); | ||
for (String string : strings) { | ||
result.add(DotName.createSimple(string)); | ||
} | ||
return result; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package test.expr; | ||
|
||
public final class Add extends Arith { | ||
private final Expr left; | ||
private final Expr right; | ||
|
||
public Add(Expr left, Expr right) { | ||
this.left = left; | ||
this.right = right; | ||
} | ||
|
||
@Override | ||
public int eval() { | ||
return left.eval() + right.eval(); | ||
} | ||
} |
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,4 @@ | ||
package test.expr; | ||
|
||
public sealed abstract class Arith implements Expr permits Add, Mul { | ||
} |
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,7 @@ | ||
package test.expr; | ||
|
||
public class BestValue extends Value { | ||
public BestValue() { | ||
super(42); | ||
} | ||
} |
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,5 @@ | ||
package test.expr; | ||
|
||
public sealed interface Expr permits Value, Arith { | ||
int eval(); | ||
} |
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,16 @@ | ||
package test.expr; | ||
|
||
public final class Mul extends Arith { | ||
private final Expr left; | ||
private final Expr right; | ||
|
||
public Mul(Expr left, Expr right) { | ||
this.left = left; | ||
this.right = right; | ||
} | ||
|
||
@Override | ||
public int eval() { | ||
return left.eval() * right.eval(); | ||
} | ||
} |
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,14 @@ | ||
package test.expr; | ||
|
||
public non-sealed class Value implements Expr { | ||
private final int value; | ||
|
||
public Value(int value) { | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public int eval() { | ||
return value; | ||
} | ||
} |