IronJacamarWithByteman.java

/*
 * IronJacamar, a Java EE Connector Architecture implementation
 * Copyright 2015, Red Hat Inc, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the Eclipse Public License 1.0 as
 * published by the Free Software Foundation.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Eclipse
 * Public License for more details.
 *
 * You should have received a copy of the Eclipse Public License 
 * along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.ironjacamar.embedded.byteman;

import org.ironjacamar.embedded.junit4.IronJacamar;

import java.util.ArrayList;
import java.util.List;

import org.jboss.byteman.agent.submit.ScriptText;
import org.jboss.byteman.agent.submit.Submit;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.TestClass;

/**
 * IronJacamar with Byteman JUnit 4 runner
 */
public class IronJacamarWithByteman extends IronJacamar
{
   /**
    * Constructor
    * @param clz The class
    * @exception InitializationError If the test can't be initialized
    */
   public IronJacamarWithByteman(Class<?> clz) throws InitializationError
   {
      super(clz);
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public void extensionStart(TestClass tc) throws Exception
   {
      List<ScriptText> scripts = new ArrayList<ScriptText>();
      int key = 1;
      
      BMRules rules = tc.getAnnotation(BMRules.class);
      if (rules != null)
      {
         for (BMRule rule : rules.value())
         {
            scripts.add(createScriptText(key, rule));
            key++;
         }
      }

      BMRule rule = tc.getAnnotation(BMRule.class);
      if (rule != null)
      {
         scripts.add(createScriptText(key, rule));
      }

      if (!scripts.isEmpty())
      {
         Submit submit = new Submit();
         String result = submit.addScripts(scripts);
      }
   }
   
   /**
    * {@inheritDoc}
    */
   @Override
   public void extensionStop(TestClass tc) throws Exception
   {
      List<ScriptText> scripts = new ArrayList<ScriptText>();
      int key = 1;
      
      BMRules rules = tc.getAnnotation(BMRules.class);
      if (rules != null)
      {
         for (BMRule rule : rules.value())
         {
            scripts.add(createScriptText(key, rule));
            key++;
         }
      }

      BMRule rule = tc.getAnnotation(BMRule.class);
      if (rule != null)
      {
         scripts.add(createScriptText(key, rule));
      }

      if (!scripts.isEmpty())
      {
         Submit submit = new Submit();
         String result = submit.deleteScripts(scripts);
      }
   }

   /**
    * Create a ScriptText instance
    * @param key The key
    * @param rule The rule
    * @return The ScriptText instance
    */
   private ScriptText createScriptText(int key, BMRule rule)
   {
      StringBuilder builder = new StringBuilder();
      builder.append("# BMUnit autogenerated script: ").append(rule.name());

      builder.append("\nRULE ");
      builder.append(rule.name());
      if (rule.isInterface())
      {
         builder.append("\nINTERFACE ");
      }
      else
      {
         builder.append("\nCLASS ");
      }

      builder.append(rule.targetClass());
      builder.append("\nMETHOD ");
      builder.append(rule.targetMethod());

      String location = rule.targetLocation();
      if (location != null && location.length() > 0)
      {
         builder.append("\nAT ");
         builder.append(location);
      }

      String binding = rule.binding();
      if (binding != null && binding.length() > 0)
      {
         builder.append("\nBIND ");
         builder.append(binding);
      }

      String helper = rule.helper();
      if (helper != null && helper.length() > 0)
      {
         builder.append("\nHELPER ");
         builder.append(helper);
      }

      builder.append("\nIF ");
      builder.append(rule.condition());
      builder.append("\nDO ");
      builder.append(rule.action());
      builder.append("\nENDRULE\n");

      return new ScriptText("IronJacamarWithByteman" + key, builder.toString());
   }
}