Beispiel #1
0
 @Test(expected = IllegalArgumentException.class)
 public void testReplaceMissingResource() {
   final Group group = new Group("group");
   group.replace(
       Resource.create("/path", ResourceType.JS),
       Arrays.asList(Resource.create("", ResourceType.JS)));
 }
Beispiel #2
0
 /**
  * Creates a resource from a given resourceElement. It can be css, js. If resource tag name is
  * group-ref, the method will start a recursive computation.
  *
  * @param resourceElement
  * @param resources list of parsed resources where the parsed resource is added.
  */
 private void parseResource(
     final Element resourceElement,
     final Collection<Resource> resources,
     final Collection<Group> groups) {
   ResourceType type = null;
   final String tagName = resourceElement.getTagName();
   final String uri = resourceElement.getTextContent();
   if (TAG_JS.equals(tagName)) {
     type = ResourceType.JS;
   } else if (TAG_CSS.equals(tagName)) {
     type = ResourceType.CSS;
   } else if (TAG_GROUP_REF.equals(tagName)) {
     // uri in this case is the group name
     final Element groupElement = allGroupElements.get(uri);
     resources.addAll(parseGroup(groupElement, groups));
   }
   if (type != null) {
     final String minimizeAsString = resourceElement.getAttribute(ATTR_MINIMIZE);
     final boolean minimize =
         StringUtils.isEmpty(minimizeAsString)
             ? true
             : Boolean.valueOf(resourceElement.getAttribute(ATTR_MINIMIZE));
     final Resource resource = Resource.create(uri, type);
     resource.setMinimize(minimize);
     resources.add(resource);
   }
 }
 /** {@inheritDoc} */
 @Override
 public void process(final Resource resource, final Reader reader, final Writer writer)
     throws IOException {
   final String content = IOUtils.toString(reader);
   final AbstractLinter linter = enginePool.getObject();
   try {
     // TODO investigate why linter fails when trying to reuse the same instance twice
     linter.setOptions(options).validate(content);
   } catch (final LinterException e) {
     onLinterException(e, resource);
   } catch (final WroRuntimeException e) {
     onException(e);
     final String resourceUri =
         resource == null ? StringUtils.EMPTY : "[" + resource.getUri() + "]";
     LOG.warn(
         "Exception while applying "
             + getClass().getSimpleName()
             + " processor on the "
             + resourceUri
             + " resource, no processing applied...",
         e);
   } finally {
     // don't change the processed content no matter what happens.
     writer.write(content);
     reader.close();
     writer.close();
     enginePool.returnObject(linter);
   }
 }
Beispiel #4
0
 @SuppressWarnings("unchecked")
 @Test
 public void testReplaceWithEmptyCollection() {
   final Group group = new Group("group");
   final Resource resource = Resource.create("/path", ResourceType.JS);
   group.addResource(resource);
   group.replace(resource, Collections.EMPTY_LIST);
   Assert.assertTrue(group.getResources().isEmpty());
 }
Beispiel #5
0
 @Test
 public void testResoruceOfTypeFound() {
   final Group group = new Group("group");
   final List<Resource> resources = new ArrayList<Resource>();
   resources.add(Resource.create("/some.css", ResourceType.CSS));
   group.setResources(resources);
   Assert.assertEquals(true, group.hasResourcesOfType(ResourceType.CSS));
   Assert.assertEquals(false, group.hasResourcesOfType(ResourceType.JS));
 }
Beispiel #6
0
 /**
  * Check if the resource was changed from previous run. The implementation uses resource content
  * digest (hash) to check for change.
  *
  * @param resource the {@link Resource} to check.
  * @return true if the resource was changed.
  */
 private boolean isChanged(final Resource resource, final String groupName) {
   LOG.debug("Check change for resource {}", resource.getUri());
   try {
     final String uri = resource.getUri();
     // using AtomicBoolean because we need to mutate this variable inside an anonymous class.
     final AtomicBoolean changeDetected =
         new AtomicBoolean(getResourceChangeDetector().checkChangeForGroup(uri, groupName));
     if (!changeDetected.get() && resource.getType() == ResourceType.CSS) {
       final Reader reader = new InputStreamReader(locatorFactory.locate(uri));
       LOG.debug("Check @import directive from {}", resource);
       createCssImportProcessor(changeDetected, groupName)
           .process(resource, reader, new StringWriter());
     }
     return changeDetected.get();
   } catch (final IOException e) {
     LOG.debug(
         "[FAIL] Cannot check {} resource (Exception message: {}). Assuming it is unchanged...",
         resource,
         e.getMessage());
     return false;
   }
 }
 /** {@inheritDoc} */
 @Override
 protected void doContribution(
     final Group group, final ModelAndView modelAndView, final Map<String, Group> additionalGroups)
     throws IOException {
   String bundle = group.getName();
   Map<String, Object> model = modelAndView.getModel();
   StringBuilder buffer = new StringBuilder();
   if (useCache()) {
     buffer.append(script("/bundle/" + bundle + ".js?v=" + version));
   } else {
     List<Resource> candidates = group.getResources();
     List<Resource> resources = new ArrayList<Resource>();
     for (Resource resource : candidates) {
       if (resource.getType() == ResourceType.JS) {
         buffer.append(script(resource.getUri()));
         resources.add(resource);
       }
     }
     model.put(resourcesVarName(), resources);
   }
   // 2. Publish as a model attribute.
   model.put(varName(), buffer.toString());
   logger.trace("Publishing {}:\n{}", varName(), buffer);
 }
Beispiel #8
0
 @Test
 public void testReplaceWithFewResources() {
   final Group group = new Group("group");
   final Resource resource = Resource.create("/static/*", ResourceType.JS);
   resource.setMinimize(false);
   group.addResource(resource);
   group.replace(
       resource,
       Arrays.asList(
           Resource.create("/static/one.js", ResourceType.JS),
           Resource.create("/static/two.js", ResourceType.JS)));
   Assert.assertEquals(2, group.getResources().size());
   Assert.assertEquals(resource.isMinimize(), group.getResources().get(0).isMinimize());
 }
Beispiel #9
0
 /** {@inheritDoc} */
 public void process(final Resource resource, final Reader reader, final Writer writer)
     throws IOException {
   final String content = IOUtils.toString(reader);
   try {
     writer.write(doProcess(content));
   } catch (final WroRuntimeException e) {
     onException(e);
     writer.write(content);
     final String resourceUri =
         resource == null ? StringUtils.EMPTY : "[" + resource.getUri() + "]";
     LOG.warn(
         "Exception while  applying lessCss processor on the "
             + resourceUri
             + " resource, no processing applied...",
         e);
   } finally {
     reader.close();
     writer.close();
   }
 }