/**
   * 初始化当前测试类用到的spring application context对象
   *
   * @param testedObject
   * @param contextFactory
   * @return does initial spring context successfully
   */
  @SuppressWarnings("rawtypes")
  public static JTesterBeanFactory initSpringContext(
      Class testClazz, ApplicationContextFactory contextFactory) {
    JTesterBeanFactory beanFactory = (JTesterBeanFactory) TestedObject.getSpringBeanFactory();
    if (beanFactory != null) {
      return beanFactory;
    }
    SpringApplicationContext annotation =
        AnnotationUtils.getClassLevelAnnotation(SpringApplicationContext.class, testClazz);
    if (annotation == null) {
      return null;
    }

    long startTime = System.currentTimeMillis();

    String[] locations = annotation.value();
    boolean ignoreNoSuchBean = annotation.ignoreNoSuchBean();
    JTesterSpringContext context =
        contextFactory.createApplicationContext(Arrays.asList(locations), ignoreNoSuchBean);

    context.refresh();
    long duration = System.currentTimeMillis() - startTime;
    JTesterLogger.warn(
        String.format(
            "take %d ms to init spring context for test obejct[%s]",
            duration, testClazz.getName()));

    beanFactory = context.getJTesterBeanFactory();
    TestedObject.setSpringContext(beanFactory);
    return beanFactory;
  }
 /**
  * 获得当前测试类spring容器中名称为beanname的spring bean
  *
  * @param beanName
  * @return
  */
 public static Object getBeanByName(String beanname) {
   BeanFactory factory = (BeanFactory) TestedObject.getSpringBeanFactory();
   if (factory == null) {
     throw new RuntimeException(
         "can't find SpringApplicationContext for tested class:"
             + TestedObject.currTestedClazzName());
   } else {
     Object bean = factory.getBean(beanname);
     return bean;
   }
 }
Exemple #3
0
  @Test
  public void test_db_load_mysql() {
    ArbitrateConfigRegistry.regist(configClientService);
    dbLoadAction = (DbLoadAction) TestedObject.getSpringBeanFactory().getBean("dbLoadAction");

    final Channel channel = new Channel();
    channel.setId(1L);

    final Pipeline pipeline = new Pipeline();
    pipeline.setId(100L);
    List<DataMediaPair> pairs = generatorDataMediaPairForMysql(20);
    pipeline.setPairs(pairs);
    pipeline.getParameters().merge(new SystemParameter());
    pipeline.getParameters().merge(new ChannelParameter());
    // pipeline.getParameters().setChannelInfo("LJH_DEMO");

    // final Pipeline oppositePipeline = new Pipeline();
    // oppositePipeline.setId(101L);
    channel.setPipelines(Arrays.asList(pipeline));

    final Node currentNode = new Node();
    currentNode.setId(1L);
    new NonStrictExpectations() {

      {
        configClientService.findChannel(anyLong);
        returns(channel);
        configClientService.findPipeline(anyLong);
        returns(pipeline);
        configClientService.currentNode();
        returns(currentNode);
      }
    };

    Identity identity = new Identity();
    identity.setChannelId(100L);
    identity.setPipelineId(100L);
    identity.setProcessId(100L);

    RowBatch rowBatch = new RowBatch();
    rowBatch.setIdentity(identity);
    List<EventData> eventDatas = generatorEventDataForMysql(0, 20, EventType.INSERT);
    for (EventData eventData : eventDatas) {
      rowBatch.merge(eventData);
    }
    eventDatas = generatorEventDataForMysql(10, 10, EventType.INSERT);
    for (EventData eventData : eventDatas) {
      rowBatch.merge(eventData);
    }
    eventDatas = generatorEventDataForMysql(19, 1, EventType.DELETE);
    for (EventData eventData : eventDatas) {
      rowBatch.merge(eventData);
    }

    WeightController controller = new WeightController(1);
    dbLoadAction.load(rowBatch, controller);
  }
 /**
  * 释放测试类的spring容器
  *
  * @param springContext AbstractApplicationContext实例,这里定义为Object是方便其它模块脱离spring依赖
  */
 public static void closeSpringContext(Object beanFactory) {
   if (beanFactory == null) {
     return;
   }
   if (beanFactory instanceof JTesterBeanFactory) {
     ((JTesterBeanFactory) beanFactory).destroySingletons();
     JTesterLogger.warn("close spring context for class:" + TestedObject.currTestedClazzName());
   } else {
     String error =
         String.format(
             "there must be something error, the type[%s] object isn't a spring context.",
             beanFactory.getClass().getName());
     throw new RuntimeException(error);
   }
 }