-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Tests to test java-17 new fatures Sealed Class and Sealed Interfa…
…ce (#486)
- Loading branch information
Showing
2 changed files
with
152 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package java17; | ||
|
||
import gov.nasa.jpf.util.test.TestJPF; | ||
import org.junit.Test; | ||
|
||
public class SealedClassTest extends TestJPF { | ||
|
||
// Shape is a "sealed" class, and it allows only "Circle", "Square" and "Rectangle" to extend. | ||
sealed class Shape permits Square, Rectangle { | ||
|
||
} | ||
|
||
|
||
// Here "Square" class is allowed to extend "Shape". Sealed class "Shape" allows "Square" to extends in "permits" clause. | ||
// But "Square" is "final class" so it cannot be extended by anyone more. | ||
final class Square extends Shape { | ||
|
||
} | ||
|
||
|
||
// Here "Rectangle" class is allowed to extend "Shape". Sealed class "Shape" allows "Rectangle" to extends in "permits" clause. | ||
// But "Rectangle" is "non-sealed class" so it can be extended by anyone more. | ||
non-sealed class Rectangle extends Shape { | ||
|
||
} | ||
|
||
|
||
// Here "CustomRectangle" class is allowed to extend "non-sealed class Rectangle". | ||
class CustomRectangle extends Rectangle { | ||
|
||
} | ||
|
||
@Test | ||
public void testSquareInstance() { | ||
if (verifyNoPropertyViolation()){ | ||
Shape shape = new Square(); | ||
assertTrue("", shape instanceof Square); | ||
} | ||
} | ||
|
||
@Test | ||
public void testRectangleInstance() { | ||
if (verifyNoPropertyViolation()){ | ||
Shape shape = new Rectangle(); | ||
assertTrue("", shape instanceof Rectangle); | ||
} | ||
} | ||
|
||
@Test | ||
public void testCustomRectangleInstance() { | ||
if (verifyNoPropertyViolation()){ | ||
Shape shape = new CustomRectangle(); | ||
assertTrue("", shape instanceof CustomRectangle); | ||
assertTrue("", shape instanceof Rectangle); | ||
} | ||
} | ||
} |
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,95 @@ | ||
package java17; | ||
|
||
import gov.nasa.jpf.util.test.TestJPF; | ||
import org.junit.Test; | ||
|
||
public class SealedInterfaceTest extends TestJPF { | ||
|
||
// "Shape" is a "sealed" interface, and it allows only "Circle", "Square" and "Rectangle" to implement. | ||
sealed interface Shape permits Square, Rectangle { | ||
double area(); | ||
} | ||
|
||
|
||
// Here "Square" class is allowed to implement "Shape". Sealed interface "Shape" allows "Square" to implement in "permits" clause. | ||
// But "Square" is "final class" so it cannot be extended by anyone more. | ||
final class Square implements Shape { | ||
private double length; | ||
|
||
public Square(double length){ | ||
this.length = length; | ||
} | ||
|
||
@Override | ||
public double area() { | ||
return (length * length); | ||
} | ||
} | ||
|
||
|
||
// Here "Rectangle" class is allowed to implement "Shape". Sealed interface "Shape" allows "Rectangle" to implement in "permits" clause. | ||
// But "Rectangle" is "non-sealed class" so it can be extended by anyone more. | ||
public non-sealed class Rectangle implements Shape { | ||
private double length; | ||
private double width; | ||
|
||
public Rectangle(double length, double width) { | ||
this.length = length; | ||
this.width = width; | ||
} | ||
|
||
@Override | ||
public double area() { | ||
return (length * width); | ||
} | ||
} | ||
|
||
|
||
// Here "CustomRectangle" class is allowed to extend "non-sealed class Rectangle". | ||
public class CustomRectangle extends Rectangle { | ||
public CustomRectangle(double length, double width) { | ||
super(length, width); | ||
} | ||
} | ||
|
||
@Test | ||
public void testSquare() { | ||
if (verifyNoPropertyViolation()){ | ||
Shape shape = new Square(4); | ||
|
||
// Test Square Instance | ||
assertTrue("",shape instanceof Square); | ||
|
||
// Test Square "area" | ||
assertEquals(16, shape.area()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testRectangle() { | ||
if (verifyNoPropertyViolation()) { | ||
Shape shape = new Rectangle(3, 7); | ||
|
||
// Test Rectangle Instance | ||
assertTrue("", shape instanceof Rectangle); | ||
|
||
// Test Rectangle "area" | ||
assertEquals(21, shape.area()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testCustomRectangle() { | ||
if (verifyNoPropertyViolation()){ | ||
Shape shape = new CustomRectangle(4, 5); | ||
Rectangle rectangle = new CustomRectangle(3,4); | ||
|
||
// Test CustomRectangle Instance | ||
assertTrue("",shape instanceof CustomRectangle); | ||
assertTrue("",rectangle instanceof CustomRectangle); | ||
|
||
// Test CustomRectangle "area" | ||
assertEquals(20, shape.area()); | ||
} | ||
} | ||
} |