Skip to content

Commit

Permalink
Refactor localization and simplify throwing localized assertion failures
Browse files Browse the repository at this point in the history
  • Loading branch information
MaisiKoleni committed Apr 1, 2022
1 parent 0c36486 commit 5512ea1
Show file tree
Hide file tree
Showing 23 changed files with 186 additions and 203 deletions.
17 changes: 8 additions & 9 deletions src/main/java/de/tum/in/test/api/dynamic/Check.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.tum.in.test.api.dynamic;

import static de.tum.in.test.api.localization.Messages.formatLocalized;
import static org.junit.jupiter.api.Assertions.fail;
import static de.tum.in.test.api.localization.Messages.localizedFailure;

import java.lang.reflect.Modifier;
import java.util.function.Supplier;
Expand All @@ -16,49 +15,49 @@ public enum Check {
@Override
void checkModifiers(int modifiers, Supplier<String> desc) {
if (!Modifier.isStatic(modifiers))
fail(formatLocalized("dynamics.check.not_static", desc.get())); //$NON-NLS-1$
throw localizedFailure("dynamics.check.not_static", desc.get()); //$NON-NLS-1$
}
},
NOT_STATIC {
@Override
void checkModifiers(int modifiers, Supplier<String> desc) {
if (Modifier.isStatic(modifiers))
fail(formatLocalized("dynamics.check.static", desc.get())); //$NON-NLS-1$
throw localizedFailure("dynamics.check.static", desc.get()); //$NON-NLS-1$
}
},
FINAL {
@Override
void checkModifiers(int modifiers, Supplier<String> desc) {
if (!Modifier.isFinal(modifiers))
fail(formatLocalized("dynamics.check.not_final", desc.get())); //$NON-NLS-1$
throw localizedFailure("dynamics.check.not_final", desc.get()); //$NON-NLS-1$
}
},
NOT_FINAL {
@Override
void checkModifiers(int modifiers, Supplier<String> desc) {
if (Modifier.isFinal(modifiers))
fail(formatLocalized("dynamics.check.final", desc.get())); //$NON-NLS-1$
throw localizedFailure("dynamics.check.final", desc.get()); //$NON-NLS-1$
}
},
PUBLIC {
@Override
void checkModifiers(int modifiers, Supplier<String> desc) {
if (!Modifier.isPublic(modifiers))
fail(formatLocalized("dynamics.check.not_public", desc.get())); //$NON-NLS-1$
throw localizedFailure("dynamics.check.not_public", desc.get()); //$NON-NLS-1$
}
},
NOT_PUBLIC {
@Override
void checkModifiers(int modifiers, Supplier<String> desc) {
if (Modifier.isPublic(modifiers))
fail(formatLocalized("dynamics.check.public", desc.get())); //$NON-NLS-1$
throw localizedFailure("dynamics.check.public", desc.get()); //$NON-NLS-1$
}
},
PACKAGE_PRIVATE {
@Override
void checkModifiers(int modifiers, Supplier<String> desc) {
if (Modifier.isPublic(modifiers) || Modifier.isPrivate(modifiers) || Modifier.isProtected(modifiers))
fail(formatLocalized("dynamics.check.not_package", desc.get())); //$NON-NLS-1$
throw localizedFailure("dynamics.check.not_package", desc.get()); //$NON-NLS-1$
}
};

Expand Down
17 changes: 8 additions & 9 deletions src/main/java/de/tum/in/test/api/dynamic/DynamicClass.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.tum.in.test.api.dynamic;

import static de.tum.in.test.api.localization.Messages.formatLocalized;
import static org.junit.jupiter.api.Assertions.fail;
import static de.tum.in.test.api.localization.Messages.*;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -45,7 +44,7 @@ public Class<T> toClass() {
try {
clazz = (Class<T>) Class.forName(name);
} catch (ClassNotFoundException e) {
fail(formatLocalized("dynamics.class.not_found", name), e); //$NON-NLS-1$
throw localizedFailure(e, "dynamics.class.not_found", name); //$NON-NLS-1$
}
}
return clazz;
Expand Down Expand Up @@ -87,7 +86,7 @@ public T cast(Object obj) {
return null;
if (rClass.isPrimitive()) {
if (obj == null)
throw new NullPointerException(formatLocalized("dynamics.class.null", getName())); //$NON-NLS-1$
throw new NullPointerException(localized("dynamics.class.null", getName())); //$NON-NLS-1$
Class<?> objClass = obj.getClass();
Class<?> wrapper = primitiveWrappers.get(rClass);
if (objClass.equals(wrapper))
Expand Down Expand Up @@ -160,7 +159,7 @@ public void check(Check... checks) {
toClass();
int modifiers = toClass().getModifiers();
for (Check check : checks)
check.checkModifiers(modifiers, () -> formatLocalized("dynamics.class.name", this)); //$NON-NLS-1$
check.checkModifiers(modifiers, () -> localized("dynamics.class.name", this)); //$NON-NLS-1$
}

public int checkForPublicOrProtectedMethods(DynamicMethod<?>... exceptions) {
Expand All @@ -178,12 +177,12 @@ public int checkForPublicOrProtectedMethods(List<DynamicMethod<?>> exceptions) {
String sig = DynamicMethod.signatureOf(m);
if (!"main(java.lang.String[])".endsWith(sig) && !publicMethods.contains(sig) //$NON-NLS-1$
&& !objectMethods.contains(sig))
fail(formatLocalized("dynamics.class.method_public", sig)); //$NON-NLS-1$
throw localizedFailure("dynamics.class.method_public", sig); //$NON-NLS-1$
}
if (Modifier.isProtected(m.getModifiers())) {
String sig = DynamicMethod.signatureOf(m);
if (!objectMethods.contains(sig))
fail(formatLocalized("dynamics.class.method_protected", sig)); //$NON-NLS-1$
throw localizedFailure("dynamics.class.method_protected", sig); //$NON-NLS-1$
}
checked++;
}
Expand All @@ -196,7 +195,7 @@ public int checkForNonPrivateFields() {
if (f.isSynthetic())
continue;
if (!Modifier.isPrivate(f.getModifiers()))
fail(formatLocalized("dynamics.class.field_private", f)); //$NON-NLS-1$
throw localizedFailure("dynamics.class.field_private", f); //$NON-NLS-1$
checked++;
}
return checked;
Expand All @@ -208,7 +207,7 @@ public int checkForNonFinalFields() {
if (f.isSynthetic())
continue;
if (!Modifier.isFinal(f.getModifiers()))
fail(formatLocalized("dynamics.class.field_final", f)); //$NON-NLS-1$
throw localizedFailure("dynamics.class.field_final", f); //$NON-NLS-1$
checked++;
}
return checked;
Expand Down
14 changes: 6 additions & 8 deletions src/main/java/de/tum/in/test/api/dynamic/DynamicConstructor.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package de.tum.in.test.api.dynamic;

import static de.tum.in.test.api.dynamic.DynamicMethod.*;
import static de.tum.in.test.api.localization.Messages.formatLocalized;
import static org.junit.jupiter.api.Assertions.fail;
import static de.tum.in.test.api.localization.Messages.*;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -31,7 +30,7 @@ public Constructor<T> toConstructor() {
constructor = owner.toClass().getDeclaredConstructor(DynamicClass.resolveAll(parameters));
constructor.trySetAccessible();
} catch (NoSuchMethodException e) {
fail(formatLocalized("dynamics.constructor.not_found", owner, descParams(this.parameters)), e); //$NON-NLS-1$
throw localizedFailure(e, "dynamics.constructor.not_found", owner, descParams(this.parameters)); //$NON-NLS-1$
}
}
return constructor;
Expand All @@ -54,17 +53,16 @@ public T newInstance(Object... params) {
try {
return toConstructor().newInstance(params);
} catch (InstantiationException e) {
fail(formatLocalized("dynamics.constructor.abstract", owner), e); //$NON-NLS-1$
throw localizedFailure(e, "dynamics.constructor.abstract", owner); //$NON-NLS-1$
} catch (IllegalAccessException e) {
fail(formatLocalized("dynamics.constructor.access", this), e); //$NON-NLS-1$
throw localizedFailure(e, "dynamics.constructor.access", this); //$NON-NLS-1$
} catch (IllegalArgumentException e) {
fail(formatLocalized("dynamics.constructor.arguments", this, descArgs(params)), e); //$NON-NLS-1$
throw localizedFailure(e, "dynamics.constructor.arguments", this, descArgs(params)); //$NON-NLS-1$
} catch (InvocationTargetException e) {
if (e.getTargetException() instanceof RuntimeException)
throw (RuntimeException) e.getTargetException();
throw UnexpectedExceptionError.wrap(e.getTargetException());
}
return null; // unreachable
}

@Override
Expand All @@ -77,6 +75,6 @@ public void check(Check... checks) {
toConstructor();
int modifiers = toConstructor().getModifiers();
for (Check check : checks)
check.checkModifiers(modifiers, () -> formatLocalized("dynamics.constructor.name", this)); //$NON-NLS-1$
check.checkModifiers(modifiers, () -> localized("dynamics.constructor.name", this)); //$NON-NLS-1$
}
}
24 changes: 10 additions & 14 deletions src/main/java/de/tum/in/test/api/dynamic/DynamicField.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.tum.in.test.api.dynamic;

import static de.tum.in.test.api.localization.Messages.formatLocalized;
import static org.junit.jupiter.api.Assertions.fail;
import static de.tum.in.test.api.localization.Messages.*;

import java.lang.reflect.Field;
import java.util.ArrayList;
Expand Down Expand Up @@ -40,12 +39,11 @@ public DynamicField(DynamicClass<?> dClass, DynamicClass<T> fType, boolean ignor
public Field toField() {
if (field == null) {
var of = findField(owner.toClass());
if (of.isPresent()) {
field = of.get();
field.trySetAccessible();
} else {
fail(formatLocalized("dynamics.field.not_found", name)); //$NON-NLS-1$
if (!of.isPresent()) {
throw localizedFailure("dynamics.field.not_found", name); //$NON-NLS-1$
}
field = of.get();
field.trySetAccessible();
}
return field;
}
Expand All @@ -67,22 +65,20 @@ public T getOf(Object o) {
try {
return type.cast(toField().get(o));
} catch (IllegalAccessException e) {
fail(formatLocalized("dynamics.field.access", name, owner), e); //$NON-NLS-1$
throw localizedFailure(e, "dynamics.field.access", name, owner); //$NON-NLS-1$
} catch (IllegalArgumentException e) {
fail(formatLocalized("dynamics.field.target", name, owner), e); //$NON-NLS-1$
throw localizedFailure(e, "dynamics.field.target", name, owner); //$NON-NLS-1$
} catch (ClassCastException e) {
fail(formatLocalized("dynamics.field.cast", name, owner, type.getName()), e); //$NON-NLS-1$
throw localizedFailure(e, "dynamics.field.cast", name, owner, type.getName()); //$NON-NLS-1$
}
return null; // unreachable
}

public T getStatic() {
try {
return getOf(null);
} catch (NullPointerException e) {
fail(formatLocalized("dynamics.field.static", name, owner), e); //$NON-NLS-1$
throw localizedFailure(e, "dynamics.field.static", name, owner); //$NON-NLS-1$
}
return null; // unreachable
}

private Optional<Field> findField(Class<?> c) {
Expand Down Expand Up @@ -111,6 +107,6 @@ public String toString() {
public void check(Check... checks) {
int modifiers = toField().getModifiers();
for (Check check : checks)
check.checkModifiers(modifiers, () -> formatLocalized("dynamics.field.name", this)); //$NON-NLS-1$
check.checkModifiers(modifiers, () -> localized("dynamics.field.name", this)); //$NON-NLS-1$
}
}
22 changes: 10 additions & 12 deletions src/main/java/de/tum/in/test/api/dynamic/DynamicMethod.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.tum.in.test.api.dynamic;

import static de.tum.in.test.api.localization.Messages.formatLocalized;
import static org.junit.jupiter.api.Assertions.fail;
import static de.tum.in.test.api.localization.Messages.*;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -47,9 +46,9 @@ public Method toMethod() {
m = owner.toClass().getDeclaredMethod(name, DynamicClass.resolveAll(parameters));
m.trySetAccessible();
if (!returnType.toClass().isAssignableFrom(m.getReturnType()))
fail(formatLocalized("dynamics.method.return", this, returnType)); //$NON-NLS-1$
throw localizedFailure("dynamics.method.return", this, returnType); //$NON-NLS-1$
} catch (NoSuchMethodException e) {
fail(formatLocalized("dynamics.method.not_found", returnType, this), e); //$NON-NLS-1$
throw localizedFailure(e, "dynamics.method.not_found", returnType, this); //$NON-NLS-1$
}
}
return m;
Expand All @@ -74,25 +73,24 @@ public T invokeOn(Object o, Object... params) {
try {
return returnType.cast(toMethod().invoke(o, params));
} catch (NullPointerException e) {
fail(formatLocalized("dynamics.method.null", this), e); //$NON-NLS-1$
throw localizedFailure(e, "dynamics.method.null", this); //$NON-NLS-1$
} catch (IllegalAccessException e) {
fail(formatLocalized("dynamics.method.access", this), e); //$NON-NLS-1$
throw localizedFailure(e, "dynamics.method.access", this); //$NON-NLS-1$
} catch (IllegalArgumentException e) {
fail(formatLocalized("dynamics.method.arguments", this, descArgs(params), o.getClass().getCanonicalName()), //$NON-NLS-1$
e);
throw localizedFailure(e, "dynamics.method.arguments", this, descArgs(params), //$NON-NLS-1$
o.getClass().getCanonicalName());
} catch (InvocationTargetException e) {
if (e.getTargetException() instanceof RuntimeException)
throw (RuntimeException) e.getTargetException();
throw UnexpectedExceptionError.wrap(e.getTargetException());
} catch (ClassCastException e) {
fail(formatLocalized("dynamics.method.cast", this, returnType), e); //$NON-NLS-1$
throw localizedFailure(e, "dynamics.method.cast", this, returnType); //$NON-NLS-1$
}
return null; // unreachable
}

public T invokeStatic(Object... params) {
if (!Modifier.isStatic(toMethod().getModifiers()))
fail(formatLocalized("dynamics.method.static", this)); //$NON-NLS-1$
throw localizedFailure("dynamics.method.static", this); //$NON-NLS-1$
return invokeOn(null, params);
}

Expand Down Expand Up @@ -137,6 +135,6 @@ public static String descParams(DynamicClass<?>... params) {
public void check(Check... checks) {
int modifiers = toMethod().getModifiers();
for (Check check : checks)
check.checkModifiers(modifiers, () -> formatLocalized("dynamics.method.name", this)); //$NON-NLS-1$
check.checkModifiers(modifiers, () -> localized("dynamics.method.name", this)); //$NON-NLS-1$
}
}
6 changes: 3 additions & 3 deletions src/main/java/de/tum/in/test/api/internal/ReportingUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package de.tum.in.test.api.internal;

import static de.tum.in.test.api.localization.Messages.formatLocalized;
import static de.tum.in.test.api.localization.Messages.localized;

import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -79,7 +79,7 @@ private static String transformMessage(ThrowableInfo info) {
private static Throwable handleSanitizationFailure(String name, Throwable error) {
var info = BlacklistedInvoker.invokeOrElse(error::toString, () -> error.getClass().toString());
LOG.error("Sanitization failed for {} with error {}", name, info); //$NON-NLS-1$
return new SecurityException(formatLocalized("sanitization.sanitization_failure", name, info)); //$NON-NLS-1$
return new SecurityException(localized("sanitization.sanitization_failure", name, info)); //$NON-NLS-1$
}

private static void addStackframeInfoToMessage(ThrowableInfo info) {
Expand All @@ -88,7 +88,7 @@ private static void addStackframeInfoToMessage(ThrowableInfo info) {
if (first.isPresent()) {
var call = first.get().toString();
info.setMessage(Objects.toString(info.getMessage(), "") + "\n" //$NON-NLS-1$ //$NON-NLS-2$
+ formatLocalized("reporting.problem_location_hint", call)); //$NON-NLS-1$
+ localized("reporting.problem_location_hint", call)); //$NON-NLS-1$
}
}
}
21 changes: 9 additions & 12 deletions src/main/java/de/tum/in/test/api/internal/TestGuardUtils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.tum.in.test.api.internal;

import static de.tum.in.test.api.localization.Messages.*;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.platform.commons.support.AnnotationSupport.findAnnotation;

import java.lang.annotation.Annotation;
Expand Down Expand Up @@ -53,7 +52,7 @@ public static void checkForHidden(TestContext context) {
// check if there are both, that would be a mistake
if (hasAnnotationType(context, TestType.PUBLIC))
throw new AnnotationFormatError(
formatLocalized("test_guard.test_cannot_be_public_and_hidden", context.displayName())); //$NON-NLS-1$
localized("test_guard.test_cannot_be_public_and_hidden", context.displayName())); //$NON-NLS-1$

var now = ZonedDateTime.now();
var finalDeadline = extractDeadline(context);
Expand All @@ -64,10 +63,11 @@ public static void checkForHidden(TestContext context) {
Optional<ZonedDateTime> activationBefore = extractActivationBefore(context);
if (activationBefore.map(now::isBefore).orElse(false))
return;
fail(localized("test_guard.hidden_test_before_deadline_message")); //$NON-NLS-1$
} else if (hasAnnotation(context, Deadline.class) || hasAnnotation(context, ExtendedDeadline.class)) {
throw localizedFailure("test_guard.hidden_test_before_deadline_message"); //$NON-NLS-1$
}
if (hasAnnotation(context, Deadline.class) || hasAnnotation(context, ExtendedDeadline.class)) {
throw new AnnotationFormatError(
formatLocalized("test_guard.public_test_cannot_have_deadline", context.displayName())); //$NON-NLS-1$
localized("test_guard.public_test_cannot_have_deadline", context.displayName())); //$NON-NLS-1$
}
}

Expand All @@ -84,8 +84,7 @@ public static ZonedDateTime extractDeadline(TestContext context) {
var deadline = extractDeadline(context.testClass(), context.testMethod());
if (deadline.isPresent())
return deadline.get();
throw new AnnotationFormatError(
formatLocalized("test_guard.hidden_test_missing_deadline", context.displayName())); //$NON-NLS-1$
throw new AnnotationFormatError(localized("test_guard.hidden_test_missing_deadline", context.displayName())); //$NON-NLS-1$
}

public static Optional<ZonedDateTime> extractDeadline(Optional<Class<?>> testClass, Optional<Method> testMethod) {
Expand Down Expand Up @@ -147,7 +146,7 @@ public static ZonedDateTime parseDeadline(String deadlineString) {
}
return dateTime.atZone(zone);
} catch (DateTimeParseException e) {
throw new AnnotationFormatError(formatLocalized("test_guard.invalid_deadline_format", deadlineString), //$NON-NLS-1$
throw new AnnotationFormatError(localized("test_guard.invalid_deadline_format", deadlineString), //$NON-NLS-1$
e);
}
}
Expand Down Expand Up @@ -187,15 +186,13 @@ private static String[] splitIntoDateTimeAndZone(String deadlineString) {
public static Duration parseDuration(String durationString) {
var matcher = DURATION_PATTERN.matcher(durationString);
if (!matcher.matches())
throw new AnnotationFormatError(
formatLocalized("test_guard.invalid_extended_deadline_format", durationString)); //$NON-NLS-1$
throw new AnnotationFormatError(localized("test_guard.invalid_extended_deadline_format", durationString)); //$NON-NLS-1$
int d = Optional.ofNullable(matcher.group("d")).map(Integer::parseInt).orElse(0); //$NON-NLS-1$
int h = Optional.ofNullable(matcher.group("h")).map(Integer::parseInt).orElse(0); //$NON-NLS-1$
int m = Optional.ofNullable(matcher.group("m")).map(Integer::parseInt).orElse(0); //$NON-NLS-1$
var duration = Duration.ofDays(d).plusHours(h).plusMinutes(m);
if (duration.isZero() || duration.isNegative())
throw new AnnotationFormatError(
formatLocalized("test_guard.extended_deadline_zero_or_negative", durationString)); //$NON-NLS-1$
throw new AnnotationFormatError(localized("test_guard.extended_deadline_zero_or_negative", durationString)); //$NON-NLS-1$
return duration;
}
}
Loading

0 comments on commit 5512ea1

Please sign in to comment.