AipoでDataContextをTomcat起動以外から作成する

Tomcatは起動しないですが、ファイルパスの関係上Tomcatが起動できる状態にする必要はあるので一旦「mvn install」でビルドします。

  public static void initialize() {
    System.setProperty("catalina.home", System.getProperty("user.dir"));

    ServletContext servletContext = EasyMock.createMock(ServletContext.class);

    WebApplicationResourceLocator locator = new WebApplicationResourceLocator();
    locator.setSkipAbsolutePath(true);
    locator.setSkipClasspath(true);
    locator.setSkipCurrentDirectory(true);
    locator.setSkipHomeDirectory(true);
    locator.addFilesystemPath(System.getProperty("user.dir")
      + FS
      + fileSeparatorJoin("war", "target", "aipo", "WEB-INF"));

    DefaultConfiguration conf =
      new DefaultConfiguration(Configuration.DEFAULT_DOMAIN_FILE, locator);
    Configuration.initializeSharedConfiguration(conf);
    Database.initialize(servletContext);

    ServletConfig config = EasyMock.createNiceMock(ServletConfig.class);
    ServletContext context = EasyMock.createNiceMock(ServletContext.class);

    HttpServletRequest request =
      EasyMock.createNiceMock(HttpServletRequest.class);
    HttpServletResponse response =
      EasyMock.createNiceMock(HttpServletResponse.class);

    EasyMock.expect(config.getServletName()).andStubReturn("Aipo");
    EasyMock.expect(config.getServletContext()).andStubReturn(context);
    EasyMock
      .expect(config.getInitParameter(TurbineConstants.APPLICATION_ROOT))
      .andStubReturn(
        System.getProperty("user.dir")
          + FS
          + fileSeparatorJoin("war", "target", "aipo"));
    EasyMock
      .expect(config.getInitParameter(TurbineServices.PROPERTIES_PATH_KEY))
      .andStubReturn(
        FS
          + fileSeparatorJoin(
            "WEB-INF",
            "conf",
            "JetspeedJResources.properties"));
    EasyMock.expect(request.getMethod()).andStubReturn("POST");
    EasyMock.expect(request.getContextPath()).andStubReturn("/context");
    EasyMock.expect(request.getRequestURI()).andStubReturn("");
    EasyMock.expect(context.getRealPath("/")).andStubReturn(getRoot());

    EasyMock.replay(config);
    EasyMock.replay(context);
    EasyMock.replay(request);
    EasyMock.replay(response);

    Turbine turbine = new Turbine();
    JetspeedResourceService service = new JetspeedResourceService();
    try {
      turbine.init(config);
      service.init(config);
    } catch (ServletException e) {
      e.printStackTrace();
    } catch (InitializationException e) {
      e.printStackTrace();
    }

    DataContext datacontext = null;
    try {
      datacontext = Database.createDataContext(Database.DEFAULT_ORG);
    } catch (Exception e) {
      e.printStackTrace();
    }

    DataContext.bindThreadDataContext(datacontext);
  }

  public static void finalizeTest() {

  }

  private static String getRoot() {
    return System.getProperty("user.dir")
      + FS
      + fileSeparatorJoin("war", "target", "aipo");
  }

以下のように実行できます。

public static void main(String[] args) {

  initialize();
  EipMConfig config = Database.create(EipMConfig.class);
  config.setName("commitTest");
  config.setValue("testValue");
  Database.commit();

}