Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2013 Red Hat, Inc. and/or its affiliates.
   *
   * Licensed under the Eclipse Public License version 1.0, available at
   * http://www.eclipse.org/legal/epl-v10.html
   */
  package org.jboss.forge.arquillian.impl;
  
 
 import  org.jboss.arquillian.container.test.spi.ContainerMethodExecutor;
 import  org.jboss.arquillian.test.spi.TestMethodExecutor;
 import  org.jboss.arquillian.test.spi.TestResult;

Author(s):
Aslak Knutsen
Lincoln Baxter, III
 
 public class FurnaceTestMethodExecutor implements ContainerMethodExecutor
 {
    private Furnace furnace;
 
    {
       Assert.notNull(configFurnaceProtocolConfiguration.class.getName() + " must be provided.");
       Assert.notNull(holderFurnaceHolder.class.getName() + " runtime must be provided");
       this. = holder.getFurnace();
    }
 
    @Override
    public TestResult invoke(final TestMethodExecutor testMethodExecutor)
    {
       TestResult result = null;
       try
       {
          Assert.notNull(testMethodExecutor, TestMethodExecutor.class.getName() + " must be specified");
          final String testClassName = testMethodExecutor.getInstance().getClass().getName();
 
          Object testInstance = null;
          Class<?> testClass = null;
          try
          {
             final AddonRegistry addonRegistry = .getAddonRegistry();
 
             waitUntilStable();
             ..println("Furnace test harness is searching for test [" + testClassName + "]");
 
             for (Addon addon : addonRegistry.getAddons())
             {
                if (addon.getStatus().isStarted())
                {
                   ServiceRegistry registry = addon.getServiceRegistry();
                   ExportedInstance<?> exportedInstance = registry.getExportedInstance(testClassName);
 
                   if (exportedInstance != null)
                   {
                      if (testInstance == null)
                      {
                         testInstance = exportedInstance.get();
                         testClass = ClassLoaders.loadClass(addon.getClassLoader(), testClassName);
                      }
                      else
                      {
                         throw new IllegalStateException(
                                  "Multiple test classes found in deployed addons. " +
                                           "You must have only one @Deployment(testable=true\"); deployment");
                      }
                   }
                }
             }
          }
          catch (Exception e)
          {
             String message = "Furnace test harness encountered an error while launching test: "
                      + testMethodExecutor.getInstance().getClass().getName() + "."
                      + testMethodExecutor.getMethod().getName() + "()";
             ..println(message);
             throw new IllegalStateException(messagee);
          }
 
          if (testInstance != null)
          {
             try
            {
               try
               {
                  try
                  {
                     testInstance = ClassLoaderAdapterBuilder.callingLoader(getClass().getClassLoader())
                              .delegateLoader(testInstance.getClass().getClassLoader())
                              .enhance(testInstancetestClass);
                  }
                  catch (Exception e)
                  {
                     .
                              .println("Furnace test harness could not enhance test class. Falling back to un-proxied invocation.");
                  }
                  Method method = testInstance.getClass().getMethod(testMethodExecutor.getMethod().getName());
                  Annotation[] annotations = method.getAnnotations();
                  for (Annotation annotation : annotations)
                  {
                     if ("org.junit.Ignore".equals(annotation.getClass().getName()))
                     {
                        result = TestResult.skipped(null);
                     }
                  }
                  if (result == null)
                  {
                     try
                     {
                        ..println("Furnace test harness is executing test method: "
                                 + testMethodExecutor.getInstance().getClass().getName() + "."
                                 + testMethodExecutor.getMethod().getName() + "()");
                        try
                        {
                           invokeBefore(testInstance.getClass(), testInstance);
                           method.invoke(testInstance);
                           result = TestResult.passed();
                        }
                        catch (Exception e)
                        {
                           /*
                            * https://issues.jboss.org/browse/FORGE-1677
                            */
                           Throwable rootCause = getRootCause(e);
                           if (rootCause != null
                                    && Proxies.isForgeProxy(rootCause)
                                    && "org.junit.internal.AssumptionViolatedException".equals(Proxies
                                             .unwrap(rootCause).getClass()
                                             .getName()))
                           {
                              try
                              {
                                 /*
                                  * Due to ClassLoader and serialization restrictions, we need to create a new instance
                                  * of this class.
                                  */
                                 Throwable thisClassloaderException = (Throwable) Class
                                          .forName("org.junit.internal.AssumptionViolatedException")
                                          .getConstructor(String.class).newInstance(rootCause.getMessage());
                                 thisClassloaderException.setStackTrace(rootCause.getStackTrace());
                                 result = TestResult.skipped(thisClassloaderException);
                              }
                              catch (Exception ex)
                              {
                                 // Ignore failure to create a new exception, just pass through the original.
                                 result = TestResult.skipped(e);
                              }
                           }
                           else
                           {
                              throw e;
                           }
                        }
                        finally
                        {
                           invokeAfter(testInstance.getClass(), testInstance);
                        }
                     }
                     catch (InvocationTargetException e)
                     {
                        if (e.getCause() != null && e.getCause() instanceof Exception)
                           throw (Exceptione.getCause();
                        else
                           throw e;
                     }
                  }
               }
               catch (AssertionError e)
               {
                  result = TestResult.failed(e);
               }
               catch (Exception e)
               {
                  result = TestResult.failed(e);
                  Throwable cause = e.getCause();
                  while (cause != null)
                  {
                     if (cause instanceof AssertionError)
                     {
                        result = TestResult.failed(cause);
                        break;
                     }
                     cause = cause.getCause();
                  }
               }
               if (TestResult.Status.FAILED.equals(result.getStatus()))
               {
                  printGraphToStream(.);
               }
               return result;
            }
            catch (Exception e)
            {
               String message = "Error launching test "
                        + testMethodExecutor.getInstance().getClass().getName() + "."
                        + testMethodExecutor.getMethod().getName() + "()";
               ..println(message);
               throw new IllegalStateException(messagee);
            }
         }
         else
         {
            for (Addon addon : .getAddonRegistry().getAddons())
            {
               try
               {
                  addon.getFuture().get();
               }
               catch (InterruptedException e)
               {
                  // Do nothing
               }
               catch (ExecutionException e)
               {
                  throw new IllegalStateException(
                           "Test runner could not locate test class [" + testClassName + "] in any deployed Addon.",
                           e.getCause());
               }
            }
            for (Addon addon : .getAddonRegistry().getAddons())
            {
               if (addon.getClassLoader() != null)
               {
                  try
                  {
                     Class<?> clazz = addon.getClassLoader().loadClass(testClassName);
                     clazz.newInstance();
                  }
                  catch (ClassNotFoundException e)
                  {
                     if (e.getCause() != e && e.getCause() != null)
                     {
                        throw new IllegalStateException("Test runner could not locate test class [" + testClassName
                                 + "] in any deployed Addon."e.getCause());
                     }
                  }
                  catch (Exception e)
                  {
                     throw new IllegalStateException("Test runner could not locate test class [" + testClassName
                              + "] in any deployed Addon."e.getCause());
                  }
               }
            }
            throw new IllegalStateException("Test runner could not locate test class [" + testClassName
                     + "] in any deployed Addon - Reason unknown.");
         }
      }
      catch (RuntimeException e)
      {
         printGraphToStream(.);
         throw e;
      }
      finally
      {
          = null;
      }
   }
   private void printGraphToStream(PrintStream stream)
   {
      stream.println("Test failed - printing current Addon graph:");
      stream.println(.getAddonRegistry().toString());
   }
   @SuppressWarnings("unchecked")
   private void invokeBefore(Class<?> clazzObject instancethrows Exception
   {
      if (clazz.getSuperclass() != null && !Object.class.equals(clazz.getSuperclass()))
         invokeBefore(clazz.getSuperclass(), instance);
      for (Method m : clazz.getMethods())
      {
         if (Annotations.isAnnotationPresent(m,
                  (Class<? extends Annotation>) clazz.getClassLoader().loadClass("org.junit.Before")))
         {
            m.invoke(instance);
         }
      }
   }
   @SuppressWarnings("unchecked")
   private void invokeAfter(Class<?> clazzObject instancethrows Exception
   {
      for (Method m : clazz.getMethods())
      {
         if (Annotations.isAnnotationPresent(m,
                  (Class<? extends Annotation>) clazz.getClassLoader().loadClass("org.junit.After")))
         {
            m.invoke(instance);
         }
      }
      if (clazz.getSuperclass() != null && !Object.class.equals(clazz.getSuperclass()))
         invokeAfter(clazz.getSuperclass(), instance);
   }
   private Throwable getRootCause(Throwable t)
   {
      Throwable cause = t;
      Throwable result = t;
      while (cause != null)
      {
         result = cause;
         cause = cause.getCause();
      }
      return result;
   }
   private void waitUntilStable(Furnace furnacethrows InterruptedException
   {
      while (furnace.getStatus().isStarting())
      {
         Thread.sleep(10);
      }
   }
New to GrepCode? Check out our FAQ X