Skip to content

Commit

Permalink
Move helper method to TeztHelper to allow reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
ash2k committed Sep 22, 2013
1 parent c660535 commit 183bd2c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
Expand Up @@ -13,14 +13,17 @@
*/
package ch.qos.logback.classic.spi;

import static ch.qos.logback.classic.util.TeztHelper.addSuppressed;
import static org.junit.Assert.assertEquals;
import static org.junit.Assume.assumeNotNull;
import static org.junit.Assume.assumeTrue;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import ch.qos.logback.classic.util.TeztHelper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -30,25 +33,6 @@ public class ThrowableProxyTest {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);

private static final Method ADD_SUPPRESSED_METHOD;

static {
Method method = null;
try {
method = Throwable.class.getMethod("addSuppressed", Throwable.class);
} catch (NoSuchMethodException e) {
// ignore, will get thrown in Java < 7
}
ADD_SUPPRESSED_METHOD = method;
}

private static void addSuppressed(Throwable outer, Throwable suppressed) throws InvocationTargetException, IllegalAccessException
{
if(ADD_SUPPRESSED_METHOD != null) {
ADD_SUPPRESSED_METHOD.invoke(outer, suppressed);
}
}

@Before
public void setUp() throws Exception {
}
Expand Down Expand Up @@ -96,7 +80,7 @@ public void nested() {
@Test
public void suppressed() throws InvocationTargetException, IllegalAccessException
{
assumeNotNull(ADD_SUPPRESSED_METHOD); // only execute on Java 7, would work anyway but doesn't make sense.
assumeTrue(TeztHelper.suppressedSupported()); // only execute on Java 7, would work anyway but doesn't make sense.
Exception ex = null;
try {
someMethod();
Expand All @@ -113,7 +97,7 @@ public void suppressed() throws InvocationTargetException, IllegalAccessExceptio
@Test
public void suppressedWithCause() throws InvocationTargetException, IllegalAccessException
{
assumeNotNull(ADD_SUPPRESSED_METHOD); // only execute on Java 7, would work anyway but doesn't make sense.
assumeTrue(TeztHelper.suppressedSupported()); // only execute on Java 7, would work anyway but doesn't make sense.
Exception ex = null;
try {
someMethod();
Expand All @@ -130,7 +114,7 @@ public void suppressedWithCause() throws InvocationTargetException, IllegalAcces
@Test
public void suppressedWithSuppressed() throws Exception
{
assumeNotNull(ADD_SUPPRESSED_METHOD); // only execute on Java 7, would work anyway but doesn't make sense.
assumeTrue(TeztHelper.suppressedSupported()); // only execute on Java 7, would work anyway but doesn't make sense.
Exception ex = null;
try {
someMethod();
Expand Down
Expand Up @@ -13,9 +13,33 @@
*/
package ch.qos.logback.classic.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class TeztHelper {


private static final Method ADD_SUPPRESSED_METHOD;

static {
Method method = null;
try {
method = Throwable.class.getMethod("addSuppressed", Throwable.class);
} catch (NoSuchMethodException e) {
// ignore, will get thrown in Java < 7
}
ADD_SUPPRESSED_METHOD = method;
}

public static boolean suppressedSupported() {
return ADD_SUPPRESSED_METHOD != null;
}

public static void addSuppressed(Throwable outer, Throwable suppressed) throws InvocationTargetException, IllegalAccessException {
if(suppressedSupported()) {
ADD_SUPPRESSED_METHOD.invoke(outer, suppressed);
}
}

static public Throwable makeNestedException(int level) {
if (level == 0) {
return new Exception("nesting level=" + level);
Expand Down

0 comments on commit 183bd2c

Please sign in to comment.