655 lines
24 KiB
Java
655 lines
24 KiB
Java
import java.lang.reflect.Field;
|
|
import java.lang.reflect.InvocationTargetException;
|
|
import java.lang.reflect.Method;
|
|
import java.lang.reflect.Modifier;
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
import java.util.Optional;
|
|
|
|
import de.uni_due.s3.jack2.backend.checkers.tracingchecker.framework.TracingFramework;
|
|
import de.uni_due.s3.jack2.backend.checkers.tracingchecker.framework.TracingFramework.Test;
|
|
|
|
public class Tests {
|
|
|
|
public static void main(String[] args) {
|
|
Class<Tests> clazz = Tests.class;
|
|
Tests test = new Tests();
|
|
System.out.println(String.format("Tests: %s", test.getClass().getName()));
|
|
System.out.println();
|
|
for (Method method : clazz.getMethods()) {
|
|
if (method.isAnnotationPresent(Test.class)) {
|
|
System.out.println(String.format("Currently testing: %s", method.getName()));
|
|
int size = test.testPoints.size();
|
|
try {
|
|
method.invoke(test);
|
|
if (test.testPoints.size() != size + 1) {
|
|
System.err.println("ERROR: Test yields no or multiple results. Erroneous test.");
|
|
} else {
|
|
System.out.println(
|
|
String.format("PASS: Test passed with %d points!",
|
|
test.testPoints.get(test.testPoints.size() - 1)));
|
|
}
|
|
} catch (Exception e) {
|
|
System.err.println(String.format("ERROR: Test failed with exception: %s", e.getMessage()));
|
|
e.printStackTrace();
|
|
}
|
|
System.out.println();
|
|
}
|
|
|
|
}
|
|
System.out.println();
|
|
System.out.println(String.format("Total points: %d", test.getResult()));
|
|
|
|
}
|
|
|
|
@Test(name = "Aufgabe 1 - MediaLibrary - getDuration - 10 Punkte")
|
|
public void testMediaLibraryLasting() {
|
|
int points = 10;
|
|
if (!implementsInterface(MediaLibrary.class, Lasting.class)) {
|
|
points -= 5;
|
|
TracingFramework
|
|
.printError("MediaLibrary should implement interface Lasting, but does not. Deducting 5 points.");
|
|
}
|
|
Optional<Method> method = findMethod(MediaLibrary.class, "getDuration", int.class);
|
|
if (method.isEmpty()) {
|
|
points -= 5;
|
|
TracingFramework.printError(
|
|
"MediaLibrary should implement method getDuration with return type int, but does not. Deducting 5 points.");
|
|
} else {
|
|
Method getDuration = method.get();
|
|
MediaLibrary mediaLibrary = new MediaLibrary(10, 500);
|
|
mediaLibrary.addItem(new MediaItem(100, "A", 0));
|
|
mediaLibrary.addItem(new MediaItem(100, "B", 0));
|
|
mediaLibrary.addItem(new MediaItem(100, "C", 0));
|
|
|
|
try {
|
|
int duration = (int) getDuration.invoke(mediaLibrary);
|
|
if (duration != 300) {
|
|
points -= 3;
|
|
TracingFramework.printError(
|
|
"A media library with 3 x 100 min media items should have a duration of 300 min but was "
|
|
+ duration);
|
|
}
|
|
} catch (IllegalAccessException | InvocationTargetException e) {
|
|
points -= 3;
|
|
TracingFramework.printError("Error invoking getDuration method: " + e.getMessage());
|
|
}
|
|
}
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 1 - MediaLibrary - addItem - 15 Punkte")
|
|
public void testMediaLibraryAddItem() {
|
|
int points = 15;
|
|
|
|
// initialize media library and items
|
|
MediaLibrary mediaLibrary = new MediaLibrary(10, 10);
|
|
Series a = new Series(1, "A", 0);
|
|
MediaItem[] items = new MediaItem[] {
|
|
a, new Series(1, "B", 0), new Series(1, "C", 0)
|
|
};
|
|
setItems(mediaLibrary, items);
|
|
|
|
// test for duplicates
|
|
// should return false and not add the item
|
|
boolean duplicates = mediaLibrary.addItem(a);
|
|
if (getItems(mediaLibrary).length == 3) {
|
|
if (duplicates) {
|
|
points -= 3;
|
|
TracingFramework.printError("Note: The media library should return false for duplicates.");
|
|
}
|
|
} else {
|
|
points -= 6;
|
|
TracingFramework.printError("The media library does not check for duplicates.");
|
|
}
|
|
|
|
// test for duration exceeding
|
|
// should return false and not add the item
|
|
setItems(mediaLibrary, new MediaItem[] { a, new Series(1, "B", 0), new Series(1, "C", 0) });
|
|
boolean duration = mediaLibrary.addItem(new Series(8, "D", 0));
|
|
if (getItems(mediaLibrary).length == 3) {
|
|
if (duration) {
|
|
points -= 3;
|
|
TracingFramework.printError("Note: The media library should return false for exceeded duration.");
|
|
}
|
|
} else {
|
|
points -= 6;
|
|
TracingFramework.printError("The media library does not check the duration.");
|
|
}
|
|
|
|
// max duration not actually exceeded
|
|
setItems(mediaLibrary, new MediaItem[] { a, new Series(1, "B", 0), new Series(1, "C", 0) });
|
|
boolean duration2 = mediaLibrary.addItem(new Series(7, "G", 0));
|
|
if (getItems(mediaLibrary).length == 3) {
|
|
points -= 3;
|
|
TracingFramework
|
|
.printError(
|
|
"Note: The media library doesn't add an item when it hits exactly the maximum duration.");
|
|
}
|
|
|
|
addPoints(points);
|
|
}
|
|
|
|
private static TraceableMediaLibrary createTraceableMediaLibrary() {
|
|
TraceableMediaLibrary mediaLibrary = new TraceableMediaLibrary(10, 10);
|
|
mediaLibrary.addItem(new Series(1, "A", 0));
|
|
mediaLibrary.addItem(new Series(1, "B", 0));
|
|
mediaLibrary.addItem(new Series(1, "C", 0));
|
|
mediaLibrary.addItem(new Series(1, "A", 0));
|
|
mediaLibrary.addItem(new Series(1, "B", 0));
|
|
mediaLibrary.addItem(new Series(1, "C", 0));
|
|
mediaLibrary.addItem(new Series(1, "A", 0));
|
|
mediaLibrary.addItem(new Series(1, "B", 0));
|
|
mediaLibrary.addItem(new Series(1, "C", 0));
|
|
return mediaLibrary;
|
|
}
|
|
|
|
private static long countRecursionSteps(TraceableMediaLibrary mediaLibrary) {
|
|
List<StackTraceElement> stackTrace = mediaLibrary.getStackTrace();
|
|
return stackTrace.stream()
|
|
.filter(element -> element.getClassName().equals("MediaLibrary")
|
|
&& element.getMethodName().equals("lastIndexOf"))
|
|
.count();
|
|
}
|
|
|
|
@Test(name = "Aufgabe 1 - MediaLibrary - lastIndexOf - not existing item - 10 Punkte")
|
|
public void testMediaLibraryLastIndexOfNotExisting() {
|
|
int points = 10;
|
|
TraceableMediaLibrary mediaLibrary = createTraceableMediaLibrary();
|
|
|
|
// test for not existing item
|
|
int index = mediaLibrary.lastIndexOf("D", 0);
|
|
long recursionSteps = countRecursionSteps(mediaLibrary);
|
|
if (recursionSteps == 0) {
|
|
points -= 10;
|
|
TracingFramework.printError("The media library should use recursion to find the index of an item.");
|
|
} else if (index != -1) {
|
|
points -= 10;
|
|
TracingFramework.printError("The media library should return -1 for not existing items.");
|
|
}
|
|
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 1 - MediaLibrary - lastIndexOf - existing item - 5 Punkte")
|
|
public void testMediaLibraryLastIndexOfExisting() {
|
|
int points = 5;
|
|
TraceableMediaLibrary mediaLibrary = createTraceableMediaLibrary();
|
|
|
|
// test for existing item
|
|
int index = mediaLibrary.lastIndexOf("B", 8);
|
|
long recursionSteps = countRecursionSteps(mediaLibrary);
|
|
if (recursionSteps == 0) {
|
|
points -= 5;
|
|
TracingFramework
|
|
.printError(
|
|
"The media library should use recursion to find the index of an item. Deducting 10 points.");
|
|
} else if (index != 7) {
|
|
points -= 5;
|
|
TracingFramework.printError(
|
|
"The media library should return the index of the first occurrence of an item. Deducting 10 points.");
|
|
}
|
|
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 1 - MediaLibrary - lastIndexOf - shifted start - 10 Punkte")
|
|
public void testMediaLibraryLastIndexOfShiftedStart() {
|
|
int points = 10;
|
|
TraceableMediaLibrary mediaLibrary = createTraceableMediaLibrary();
|
|
|
|
// test for shifted start
|
|
int index = mediaLibrary.lastIndexOf("C", 7);
|
|
long recursionSteps = countRecursionSteps(mediaLibrary);
|
|
if (recursionSteps == 0) {
|
|
points -= 10;
|
|
TracingFramework
|
|
.printError(
|
|
"The media library should use recursion to find the index of an item. Deducting 10 points.");
|
|
} else if (index != 5) {
|
|
points -= 10;
|
|
TracingFramework.printError(
|
|
"The media library should return the index of the first occurrence of an item before start index. Deducting 10 points.");
|
|
}
|
|
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 2 - Deque - addFirst - valid values - 10 Punkte")
|
|
public void testDequeAddFirst() {
|
|
int points = 10;
|
|
Deque deque = new Deque();
|
|
String[] values = { "A", "B", "C", "D", "E" };
|
|
for (String value : values) {
|
|
deque.addFirst(value);
|
|
String firstValue = peekFirst(deque);
|
|
if (!value.equals(firstValue)) {
|
|
String firstValueFromFront = peekFirstFromBack(deque);
|
|
if (value.equals(firstValueFromFront)) {
|
|
points -= 1;
|
|
TracingFramework.printError(
|
|
"The pointers inside the deque are not set correctly, but the first value is still added properly. Deducting 1 point.");
|
|
} else {
|
|
points -= 2;
|
|
TracingFramework.printError(
|
|
"The first value is not added properly. Expected: " + value + ", but got: " + firstValue);
|
|
}
|
|
}
|
|
}
|
|
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 2 - Deque - addFirst - null value - 3 Punkte")
|
|
public void testDequeAddFirstNull() {
|
|
int points = 3;
|
|
Deque deque = new Deque();
|
|
deque.addFirst(null);
|
|
if (getFront(deque) != null || getBack(deque) != null) {
|
|
points -= 3;
|
|
TracingFramework.printError("The deque should not add null values. Deducting 3 points.");
|
|
}
|
|
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 2 - Deque - addFirst - empty value - 2 Punkte")
|
|
public void testDequeAddFirstEmpty() {
|
|
int points = 2;
|
|
Deque deque = new Deque();
|
|
deque.addFirst("");
|
|
if (getFront(deque) != null || getBack(deque) != null) {
|
|
points -= 2;
|
|
TracingFramework.printError("The deque should not add empty values. Deducting 2 points.");
|
|
}
|
|
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 2 - Deque - peekLast - filled deque - 10 Punkte")
|
|
public void testDequePeekLastFilled() {
|
|
int points = 10;
|
|
Deque deque = buildDeque("Das", "kann", "man", "nicht", "raten");
|
|
|
|
String beforeStudentPeek = peekLast(deque);
|
|
|
|
String lastValue = deque.peekLast();
|
|
if (!"raten".equals(lastValue)) {
|
|
points -= 10;
|
|
TracingFramework
|
|
.printError("The last value should be 'raten', but got: " + lastValue + ". Deducting 10 points.");
|
|
} else {
|
|
String afterStudentPeek = peekLast(deque);
|
|
if (!beforeStudentPeek.equals(afterStudentPeek)) {
|
|
points -= 5;
|
|
TracingFramework.printError("The peekLast method should not alter the deque. Deducting 5 points.");
|
|
}
|
|
}
|
|
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 2 - Deque - peekLast - empty deque - 5 Punkte")
|
|
public void testDequePeekLastEmpty() {
|
|
int points = 5;
|
|
Deque deque = buildDeque("A", "B", "C", "D", "E");
|
|
if (hasInvalidLastPeeking()) {
|
|
points -= 5;
|
|
TracingFramework.printError(
|
|
"The peekLast method should not return null for a non-empty deque, assuming it always returns null. Deducting 5 points.");
|
|
} else {
|
|
// empty the deque
|
|
pollFirst(deque);
|
|
pollFirst(deque);
|
|
pollFirst(deque);
|
|
pollFirst(deque);
|
|
pollFirst(deque);
|
|
String value = deque.peekLast();
|
|
if (value != null) {
|
|
points -= 5;
|
|
TracingFramework
|
|
.printError("The peekLast method should return null for an empty deque. Deducting 5 points.");
|
|
}
|
|
}
|
|
|
|
addPoints(points);
|
|
}
|
|
|
|
private boolean hasInvalidLastPeeking() {
|
|
Deque deque = buildDeque("A", "B", "C", "D", "E");
|
|
String value = deque.peekLast();
|
|
// if value is null, we assume that the return value of peekLast is always null
|
|
// without checking would always return null, and therefore granting full points
|
|
return value == null;
|
|
}
|
|
|
|
@Test(name = "Aufgabe 2 - Deque - pollFirst - filled deque - 10 Punkte")
|
|
public void testDequePollFirstFilled() {
|
|
int points = 10;
|
|
|
|
Deque deque = buildDeque("Das", "kann", "man", "nicht", "raten");
|
|
|
|
String value = deque.pollFirst();
|
|
if (!"Das".equals(value)) {
|
|
points -= 5;
|
|
TracingFramework.printError(
|
|
"The pollFirst method should return 'Das', but got: " + value + ". Deducting 5 points.");
|
|
}
|
|
|
|
value = deque.pollFirst();
|
|
if (!"kann".equals(value)) {
|
|
points -= 5;
|
|
TracingFramework.printError("The pollFirst method should return 'kann', but got: " + value
|
|
+ ". Polling should remove the first item. Deducting 5 points.");
|
|
}
|
|
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 2 - Deque - pollFirst - empty deque - 5 Punkte")
|
|
public void testDequePollFirstEmpty() {
|
|
int points = 5;
|
|
Deque deque = buildDeque("A", "B", "C", "D", "E");
|
|
if (hasInvalidFirstPolling()) {
|
|
points -= 5;
|
|
TracingFramework.printError(
|
|
"The pollFirst method returns null for an non empty deque, assuming it always returns null. Deducting 5 points.");
|
|
} else {
|
|
// empty the deque
|
|
pollFirst(deque);
|
|
pollFirst(deque);
|
|
pollFirst(deque);
|
|
pollFirst(deque);
|
|
pollFirst(deque);
|
|
String value = deque.pollFirst();
|
|
if (value != null) {
|
|
points -= 5;
|
|
TracingFramework
|
|
.printError("The pollFirst method should return null for an empty deque. Deducting 5 points.");
|
|
}
|
|
}
|
|
|
|
addPoints(points);
|
|
}
|
|
|
|
public boolean hasInvalidFirstPolling() {
|
|
Deque deque = buildDeque("A", "B", "C", "D", "E");
|
|
String value = deque.pollFirst();
|
|
// if value is null, we assume that the return value of pollFirst is always null
|
|
// without checking would always return null, and therefore granting full points
|
|
return value == null;
|
|
}
|
|
|
|
@Test(name = "Aufgabe 2 - Deque - size - filled deque - 3 Punkte")
|
|
public void testDequeSizeFilled() {
|
|
int points = 3;
|
|
Deque deque = buildDeque("A", "B", "C", "D", "E");
|
|
int size = deque.size();
|
|
if (size != 5) {
|
|
points -= 3;
|
|
TracingFramework.printError("The size method should return 5, but got: " + size + ". Deducting 3 points.");
|
|
}
|
|
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 2 - Deque - size - empty deque - 2 Punkte")
|
|
public void testDequeSizeEmpty() {
|
|
int points = 2;
|
|
Deque deque = buildDeque("A", "B", "C", "D", "E");
|
|
int size = deque.size();
|
|
if (size == 0) {
|
|
points -= 2;
|
|
TracingFramework.printError("The size method should not return 0 for a non-empty deque, but got: " + size
|
|
+ ". Assuming it always returns 0. Deducting 2 points.");
|
|
} else {
|
|
// empty the deque
|
|
pollFirst(deque);
|
|
pollFirst(deque);
|
|
pollFirst(deque);
|
|
pollFirst(deque);
|
|
pollFirst(deque);
|
|
size = deque.size();
|
|
if (size != 0) {
|
|
points -= 2;
|
|
TracingFramework.printError("The size method should return 0 for an empty deque, but got: " + size
|
|
+ ". Deducting 2 points.");
|
|
}
|
|
}
|
|
|
|
addPoints(points);
|
|
}
|
|
|
|
private Deque buildDeque(String... values) {
|
|
Deque deque = new Deque();
|
|
for (String value : values) {
|
|
addLast(deque, value);
|
|
}
|
|
return deque;
|
|
}
|
|
|
|
private String pollFirst(Deque deque) {
|
|
Item first = getFront(deque);
|
|
if (first == null) {
|
|
return null;
|
|
}
|
|
String value = first.getValue();
|
|
if (first.getNext() != null) {
|
|
setFront(deque, first.getNext());
|
|
first.getNext().setPrev(null);
|
|
} else {
|
|
setFront(deque, null);
|
|
setBack(deque, null);
|
|
}
|
|
return value;
|
|
}
|
|
|
|
private String peekFirst(Deque deque) {
|
|
Item first = getFront(deque);
|
|
return first != null ? first.getValue() : null;
|
|
}
|
|
|
|
private String peekLast(Deque deque) {
|
|
Item last = getBack(deque);
|
|
return last != null ? last.getValue() : null;
|
|
}
|
|
|
|
private String peekLastFromFront(Deque deque) {
|
|
Item last = getBackFromFront(deque);
|
|
return last != null ? last.getValue() : null;
|
|
}
|
|
|
|
private String peekFirstFromBack(Deque deque) {
|
|
Item first = getFrontFromBack(deque);
|
|
return first != null ? first.getValue() : null;
|
|
}
|
|
|
|
private Item getBackFromFront(Deque deque) {
|
|
Item current = getFront(deque);
|
|
while (current != null && current.getNext() != null) {
|
|
current = current.getNext();
|
|
}
|
|
return current;
|
|
}
|
|
|
|
private Item getFrontFromBack(Deque deque) {
|
|
Item current = getBack(deque);
|
|
while (current != null && current.getPrev() != null) {
|
|
current = current.getPrev();
|
|
}
|
|
return current;
|
|
}
|
|
|
|
private void addLast(Deque deque, String value) {
|
|
Item item = new Item(value);
|
|
if (isEmpty(deque)) {
|
|
setFront(deque, item);
|
|
setBack(deque, item);
|
|
} else {
|
|
Item back = getBack(deque);
|
|
back.setNext(item);
|
|
item.setPrev(back);
|
|
setBack(deque, item);
|
|
}
|
|
}
|
|
|
|
private boolean isEmpty(Deque deque) {
|
|
return getFront(deque) == null && getBack(deque) == null;
|
|
}
|
|
|
|
private Item getFront(Deque deque) {
|
|
try {
|
|
Method method = Deque.class.getDeclaredMethod("getFront");
|
|
method.setAccessible(true);
|
|
return (Item) method.invoke(deque);
|
|
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
|
|
throw new RuntimeException("Error accessing getFront method", e);
|
|
}
|
|
}
|
|
|
|
private Item getBack(Deque deque) {
|
|
try {
|
|
Method method = Deque.class.getDeclaredMethod("getBack");
|
|
method.setAccessible(true);
|
|
return (Item) method.invoke(deque);
|
|
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
|
|
throw new RuntimeException("Error accessing getBack method", e);
|
|
}
|
|
}
|
|
|
|
private void setFront(Deque deque, Item front) {
|
|
try {
|
|
Method method = Deque.class.getDeclaredMethod("setFront", Item.class);
|
|
method.setAccessible(true);
|
|
method.invoke(deque, front);
|
|
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
|
|
throw new RuntimeException("Error accessing setFront method", e);
|
|
}
|
|
}
|
|
|
|
private void setBack(Deque deque, Item back) {
|
|
try {
|
|
Method method = Deque.class.getDeclaredMethod("setBack", Item.class);
|
|
method.setAccessible(true);
|
|
method.invoke(deque, back);
|
|
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
|
|
throw new RuntimeException("Error accessing setBack method", e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Sets the media library items.
|
|
*
|
|
* @param mediaLibrary The media library.
|
|
* @param items The items.
|
|
*/
|
|
private void setItems(MediaLibrary mediaLibrary, MediaItem[] items) {
|
|
try {
|
|
Field f = MediaLibrary.class.getDeclaredField("items");
|
|
f.setAccessible(true);
|
|
f.set(mediaLibrary, items);
|
|
} catch (Exception e) {
|
|
TracingFramework.printError("Cannot set media library items (" + e + ").");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The points of each test case.
|
|
*/
|
|
private List<Integer> testPoints = new ArrayList<>();
|
|
|
|
/**
|
|
* This method will be called by jack after all
|
|
* test have been executed to get the resulting
|
|
* points.
|
|
*
|
|
* @return The points resulting from the test.
|
|
*/
|
|
public int getResult() {
|
|
int sum = 0;
|
|
for (Integer p : testPoints) {
|
|
sum += p;
|
|
}
|
|
return sum;
|
|
}
|
|
|
|
/**
|
|
* Adds points for a test case. This class assumes
|
|
* that the method is called <b>exactly</b> once
|
|
* per test case.
|
|
*
|
|
* @param value The points to add.
|
|
*/
|
|
protected void addPoints(int value) {
|
|
testPoints.add(value);
|
|
}
|
|
|
|
public static boolean hasVariableOfType(Class<?> clazz, String fieldName, Class<?> fieldType) {
|
|
try {
|
|
Field field = clazz.getDeclaredField(fieldName);
|
|
return field.getType().equals(fieldType);
|
|
} catch (NoSuchFieldException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static boolean hasPrivateVariable(Class<?> clazz, String fieldName) {
|
|
try {
|
|
Field field = clazz.getDeclaredField(fieldName);
|
|
return Modifier.isPrivate(field.getModifiers());
|
|
} catch (NoSuchFieldException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static Optional<Method> findMethod(Class<?> clazz, String methodName, Class<?> returnType,
|
|
Class<?>... parameterTypes) {
|
|
try {
|
|
Method method = clazz.getDeclaredMethod(methodName, parameterTypes);
|
|
if (method.getReturnType().equals(returnType)) {
|
|
return Optional.of(method);
|
|
}
|
|
} catch (NoSuchMethodException e) {
|
|
// Method not found
|
|
}
|
|
return Optional.empty();
|
|
}
|
|
|
|
public static boolean implementsInterface(Class<?> clazz, Class<?> interfaceClass) {
|
|
Class<?>[] interfaces = clazz.getInterfaces();
|
|
return Arrays.stream(interfaces).anyMatch(interfaceClass::equals);
|
|
}
|
|
|
|
public static boolean extendsClass(Class<?> clazz, Class<?> superclass) {
|
|
Class<?> currentSuperclass = clazz.getSuperclass();
|
|
|
|
while (currentSuperclass != null) {
|
|
if (currentSuperclass.equals(superclass)) {
|
|
return true;
|
|
}
|
|
currentSuperclass = currentSuperclass.getSuperclass();
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Returns the media library items via reflection.
|
|
*
|
|
* @param mediaLibrary The media library.
|
|
* @return The media library's items.
|
|
*/
|
|
private MediaItem[] getItems(MediaLibrary mediaLibrary) {
|
|
try {
|
|
Field f = MediaLibrary.class.getDeclaredField("items");
|
|
f.setAccessible(true);
|
|
return (MediaItem[]) f.get(mediaLibrary);
|
|
} catch (Exception e) {
|
|
TracingFramework.printError("Cannot get media library items (" + e + ").");
|
|
return new MediaItem[0];
|
|
}
|
|
}
|
|
}
|