Print
NutsTest.java
package tests.jfun.yan.benchmark;

import jfun.yan.Component;
import jfun.yan.Container;
import jfun.yan.xml.NutsProcessor;
import jfun.yan.xml.nuts.spring.SpringNuts;
import junit.framework.TestCase;

import tests.jfun.yan.benchmark.models.Foo;
import tests.jfun.yan.benchmark.models.Soo;

public class NutsTest extends TestCase {
  private static final boolean spring_involved = true;
  private static final long LOOP = 200000;
  private Container yan = null;
  protected void setUp() throws Exception {
    super.setUp();
    final NutsProcessor processor = new NutsProcessor(getClass().getClassLoader());
    if(spring_involved)
      SpringNuts.setSpringAware("spring integration", processor);
    processor.processResource("tests/jfun/yan/benchmark/yan_component_config.xml");
    this.yan = processor.getContainer();
    Thread.sleep(100);
    System.gc();
    Thread.sleep(100);
    System.gc();
    Thread.sleep(100);
  }
  
  protected void tearDown() throws Exception {
    super.tearDown();
  }
  
  public void testBenchCreateComponentInstance() throws Exception{
    new Benchmark("Nuts: Create bean without injection", LOOP){
      public void run() throws Exception {
        yan.getInstance("bar");
      }
    }.start(true);
    
    Soo soo = (Soo)yan.getInstance("soo");
    assertNotNull(soo.getBar());
  }
  
  public void testBenchConstructorInjection() throws Exception{
    new Benchmark("Nuts: Create bean with Constructor Dependency Injection", LOOP){
      public void run() throws Exception {
        yan.getInstance("foo");
      }
    }.start(true);
    Foo foo = (Foo)yan.getInstance("foo");
    assertNotNull(foo.getBar());
  }
  
  public void testBenchSetterInjectio() throws Exception{
    new Benchmark("Nuts: Create bean with Setter Dependency Injection", LOOP){
      public void run() throws Exception {
        yan.getInstance("soo");
      }
    }.start(true);
    Soo soo = (Soo)yan.getInstance("soo");
    assertNotNull(soo.getBar());
  }
  public void testBenchBytypeSetterInjectio() throws Exception{
    new Benchmark("Nuts: Create bean with bytype autowiring and Setter Dependency Injection", LOOP){
      public void run() throws Exception {
        yan.getInstance("auto_soo");
      }
    }.start(true);
    Soo soo = (Soo)yan.getInstance("auto_soo");
    assertNotNull(soo.getBar());
  }
  public void testBenchSingleton() throws Exception{
    new Benchmark("Nuts: Create singleton bean with Setter Dependency Injection", LOOP*10){
      public void run() throws Exception {
        yan.getInstance("ssoo");
      }
    }.start(true);
    Soo soo = (Soo)yan.getInstance("ssoo");
    assertNotNull(soo.getBar());
  }
  public void testBenchSingletonFactory() throws Exception{
    final jfun.yan.factory.Factory factory = yan.getFactory("ssoo");
     Benchmark bench = new Benchmark("Nuts: Singleton Bean with Factory", LOOP*10){
      
      public void run() throws Exception {
        final Soo foo = (Soo)factory.create();
        foo.noop();
      }
    };
    bench.start(true);
  }
  public void testBenchSingletonCustomFactory() throws Exception{
    final Component ssoo = yan.getComponent("ssoo").factory(Factory.class);
    final Factory factory = (Factory)yan.instantiateComponent(ssoo);
     Benchmark bench = new Benchmark("Nuts: Singleton Bean with custom factory", LOOP*10){
      
      public void run() throws Exception {
        final Soo foo = (Soo)factory.create();
        foo.noop();
      }
    };
    bench.start(true);
  }
  public void testCtorFactory() throws Exception{
    final jfun.yan.factory.Factory factory = yan.getFactory("foo");
     Benchmark bench = new Benchmark("Nuts: Constructor Injection with Factory", LOOP){
      
      public void run() throws Exception {
        final Foo foo = (Foo)factory.create();
        foo.noop();
      }
    };
    bench.start(true);
  }
  public void testSetterFactory() throws Exception{
    final jfun.yan.factory.Factory factory = yan.getFactory("soo");
     Benchmark bench = new Benchmark("Nuts: Setter Injection with Factory", LOOP){
      
      public void run() throws Exception {
        final Soo foo = (Soo)factory.create();
        foo.noop();
      }
    };
    bench.start(true);
  }
  
  public void testBenchEmptyInterceptor() throws Exception{
    if(!spring_involved) return;
     Benchmark bench = new Benchmark("Nuts: Bean method invocation with empty interceptor applied", LOOP * 100){
      Soo foo = (Soo)yan.getInstance("sooProxy");
      public void run() throws Exception {
        foo.noop();
      }
    };
    bench.start(true);
  }
  
  public void testBenchCreateAsectizedBean() throws Exception{
    if(!spring_involved) return;
    new Benchmark("Nuts: Create aspectized bean", LOOP/10 ){
      private Object old = null;
      public void run() throws Exception {
        final Soo nw = (Soo)yan.getInstance("sooProxy");
        if(old!=null){
          assertNotSame(old, nw);
        }
        old = nw;
      }
    }.start(true);
  }
}
Powered by Atlassian Confluence