Exemplo n.º 1
0
  /**
   * Gets the default locale specified as a global property.
   *
   * @return default locale object.
   * @since 1.5
   * @should not return null if global property does not exist
   * @should not fail with empty global property value
   * @should not fail with bogus global property value
   * @should return locale object for global property
   * @should not cache locale when session is not open
   */
  public static Locale getDefaultLocale() {
    if (defaultLocaleCache == null) {
      if (Context.isSessionOpen()) {
        try {
          String locale =
              Context.getAdministrationService()
                  .getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE);

          if (StringUtils.hasLength(locale)) {
            try {
              defaultLocaleCache = fromSpecification(locale);
            } catch (Exception t) {
              log.warn("Unable to parse default locale global property value: " + locale, t);
            }
          }
        } catch (Exception e) {
          // swallow most of the stack trace for most users
          log.warn("Unable to get locale global property value. " + e.getMessage());
          log.trace("Unable to get locale global property value", e);
        }

        // if we weren't able to load the locale from the global property,
        // use the default one
        if (defaultLocaleCache == null)
          defaultLocaleCache =
              fromSpecification(OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE_DEFAULT_VALUE);
      } else {
        // if session is not open, return the default locale without caching
        return fromSpecification(OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE_DEFAULT_VALUE);
      }
    }

    return defaultLocaleCache;
  }
  /**
   * called before @BeforeTransaction methods
   *
   * @see
   *     org.springframework.test.context.support.AbstractTestExecutionListener#prepareTestInstance(org.springframework.test.context.TestContext)
   */
  @Override
  public void prepareTestInstance(TestContext testContext) throws Exception {
    StartModule startModuleAnnotation = testContext.getTestClass().getAnnotation(StartModule.class);

    // if the developer listed some modules with the @StartModule annotation on the class
    if (startModuleAnnotation != null) {

      if (!lastClassRun.equals(testContext.getTestClass().getSimpleName())) {
        // mark this with our class so that the services are only restarted once
        lastClassRun = testContext.getTestClass().getSimpleName();

        if (!Context.isSessionOpen()) Context.openSession();

        // load the omods that the dev defined for this class
        String modulesToLoad = StringUtils.join(startModuleAnnotation.value(), " ");

        Properties props = BaseContextSensitiveTest.runtimeProperties;
        props.setProperty(ModuleConstants.RUNTIMEPROPERTY_MODULE_LIST_TO_LOAD, modulesToLoad);
        try {
          ModuleUtil.startup(props);
        } catch (Exception e) {
          System.out.println("Error while starting modules: ");
          e.printStackTrace(System.out);
          throw e;
        }
        Assert.assertTrue(
            "Some of the modules did not start successfully for "
                + testContext.getTestClass().getSimpleName()
                + ". Only "
                + ModuleFactory.getStartedModules().size()
                + " modules started instead of "
                + startModuleAnnotation.value().length,
            startModuleAnnotation.value().length <= ModuleFactory.getStartedModules().size());

        /*
         * Refresh spring so the Services are recreated (aka serializer gets put into the SerializationService)
         * To do this, wrap the applicationContext from the testContext into a GenericApplicationContext, allowing
         * loading beans from moduleApplicationContext into it and then calling ctx.refresh()
         * This approach ensures that the application context remains consistent
         */
        GenericApplicationContext ctx =
            new GenericApplicationContext(testContext.getApplicationContext());
        XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
        xmlReader.loadBeanDefinitions(new ClassPathResource("moduleApplicationContext.xml"));
        ctx.refresh();

        // session is closed by the test framework
        // Context.closeSession();
      }
    }
  }
Exemplo n.º 3
0
  /**
   * Gets the default locale specified as a global property.
   *
   * @return default locale object.
   * @since 1.5
   * @should not return null if global property does not exist
   * @should not fail with empty global property value
   * @should not fail with bogus global property value
   * @should return locale object for global property
   */
  public static Locale getDefaultLocale() {
    if (Context.isSessionOpen()) {
      try {
        String locale =
            Context.getAdministrationService()
                .getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE);

        if (StringUtils.hasLength(locale)) {
          try {
            return fromSpecification(locale);
          } catch (Exception t) {
            log.warn("Unable to parse default locale global property value: " + locale, t);
          }
        }
      } catch (Throwable t) {
        // swallow most of the stack trace for most users
        log.warn("Unable to get locale global property value. " + t.getMessage());
        log.trace("Unable to get locale global property value", t);
      }
    }

    return fromSpecification(OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE_DEFAULT_VALUE);
  }
 public boolean isSessionOpen() {
   return Context.isSessionOpen();
 }