AthenaContext.java

  1. /*
  2.  * @copyright defined in LICENSE.txt
  3.  */

  4. package ship.test;

  5. import static org.slf4j.LoggerFactory.getLogger;

  6. import lombok.Getter;
  7. import lombok.Setter;
  8. import org.luaj.vm2.LuaValue;
  9. import org.luaj.vm2.lib.OneArgFunction;
  10. import org.luaj.vm2.lib.TwoArgFunction;
  11. import org.slf4j.Logger;

  12. public class AthenaContext {

  13.   protected static final ThreadLocal<AthenaContext> cabinet =
  14.       new InheritableThreadLocal<AthenaContext>() {
  15.         @Override
  16.         protected AthenaContext initialValue() {
  17.           return new AthenaContext();
  18.         }
  19.       };

  20.   public static AthenaContext getContext() {
  21.     return cabinet.get();
  22.   }

  23.   protected final transient Logger logger = getLogger(getClass());

  24.   @Getter
  25.   @Setter
  26.   protected TestResultCollector testReporter = new TestResultCollector();

  27.   public OneArgFunction startSuite = new OneArgFunction() {
  28.     @Override
  29.     public LuaValue call(final LuaValue name) {
  30.       testReporter.startSuite(name.tojstring());
  31.       return null;
  32.     }
  33.   };

  34.   public OneArgFunction endSuite = new OneArgFunction() {
  35.     @Override
  36.     public LuaValue call(final LuaValue name) {
  37.       testReporter.endSuite(name.tojstring());
  38.       return null;
  39.     }
  40.   };

  41.   public OneArgFunction startTest = new OneArgFunction() {
  42.     @Override
  43.     public LuaValue call(final LuaValue name) {
  44.       logger.trace("Starting {}...", name);
  45.       testReporter.startCase(name.tojstring());
  46.       return null;
  47.     }
  48.   };
  49.   public OneArgFunction endTest = new OneArgFunction() {
  50.     @Override
  51.     public LuaValue call(final LuaValue name) {
  52.       logger.trace("{} end", name);
  53.       testReporter.endCase(name.tojstring());
  54.       return null;
  55.     }
  56.   };
  57.   public TwoArgFunction recordError = new TwoArgFunction() {
  58.     @Override
  59.     public LuaValue call(LuaValue name, LuaValue error) {
  60.       logger.info("{} throw {}", name, error);
  61.       testReporter.error(name.tojstring(), error.tojstring());
  62.       return null;
  63.     }
  64.   };

  65.   public static void clear() {
  66.     cabinet.remove();
  67.   }
  68. }