예제 #1
0
  /** Proves that the model reload has effect. */
  @Test
  public void modelShouldBeReloadedWhenReloadIsTriggered() throws Exception {
    final WroManagerFactory wroManagerFactory =
        new BaseWroManagerFactory()
            .setModelFactory(
                new WroModelFactory() {
                  private boolean wasCreated = false;

                  public WroModel create() {
                    if (!wasCreated) {
                      wasCreated = true;
                      // return model with no groups defined
                      return new WroModel();
                    }
                    // second time when created add one group
                    return new WroModel().addGroup(new Group("g1"));
                  }

                  public void destroy() {}
                });
    Context.set(Context.standaloneContext());

    final WroFilter filter =
        new WroFilter() {
          @Override
          protected WroManagerFactory newWroManagerFactory() {
            return wroManagerFactory;
          }

          @Override
          protected ObjectFactory<WroConfiguration> newWroConfigurationFactory(
              final FilterConfig filterConfig) {
            return new ObjectFactory<WroConfiguration>() {
              public WroConfiguration create() {
                return Context.get().getConfig();
              }
            };
          }
        };
    filter.init(mockFilterConfig);
    final WroModelFactory modelFactory = wroManagerFactory.create().getModelFactory();

    assertTrue(modelFactory.create().getGroups().isEmpty());

    // reload model
    Context.get().getConfig().reloadModel();
    // the second time should have one group
    assertEquals(1, modelFactory.create().getGroups().size());
  }
예제 #2
0
 /**
  * Check if resources from a group were changed. If a change is detected, the changeListener will
  * be invoked.
  *
  * @param cacheEntry the cache key which was requested. The key contains the groupName which has
  *     to be checked for changes.
  */
 public void check(final CacheKey cacheEntry) {
   Validate.notNull(cacheEntry);
   LOG.debug("ResourceWatcher started...");
   final StopWatch watch = new StopWatch();
   watch.start("detect changes");
   try {
     final Group group =
         new WroModelInspector(modelFactory.create()).getGroupByName(cacheEntry.getGroupName());
     if (isGroupChanged(group.collectResourcesOfType(cacheEntry.getType()))) {
       onGroupChanged(cacheEntry);
     }
     changeDetector.reset();
   } catch (final Exception e) {
     onException(e);
   } finally {
     watch.stop();
     LOG.debug("resource watcher info: {}", watch.prettyPrint());
   }
 }
예제 #3
0
 /**
  * Check if resources from a group were changed. If a change is detected, the changeListener will
  * be invoked.
  *
  * @param cacheKey the cache key which was requested. The key contains the groupName which has to
  *     be checked for changes.
  */
 public void check(final CacheKey cacheKey, final Callback callback) {
   notNull(cacheKey);
   LOG.debug("started");
   final StopWatch watch = new StopWatch();
   watch.start("detect changes");
   try {
     final Group group =
         new WroModelInspector(modelFactory.create()).getGroupByName(cacheKey.getGroupName());
     if (isGroupChanged(group.collectResourcesOfType(cacheKey.getType()), callback)) {
       callback.onGroupChanged(cacheKey);
       cacheStrategy.put(cacheKey, null);
     }
     resourceChangeDetector.reset();
   } catch (final Exception e) {
     onException(e);
   } finally {
     watch.stop();
     LOG.debug("resource watcher info: {}", watch.prettyPrint());
   }
 }