Permalink
Browse files

Merge branch 'issue518' into 1.4.x

  • Loading branch information...
2 parents 3e8e117 + 451a441 commit f33a5decfcda48cd04e26459f23bb7aa7380216a @alexo committed Aug 15, 2012
Showing with 498 additions and 30 deletions.
  1. +0 −1 wro4j-core/src/main/java/ro/isdc/wro/manager/factory/ConfigurableWroManagerFactory.java
  2. +7 −5 wro4j-core/src/main/java/ro/isdc/wro/model/group/processor/InjectorBuilder.java
  3. +8 −1 wro4j-core/src/main/java/ro/isdc/wro/model/resource/support/hash/ConfigurableHashStrategy.java
  4. +7 −2 wro4j-core/src/main/java/ro/isdc/wro/model/resource/support/naming/ConfigurableNamingStrategy.java
  5. +58 −0 ...ore/src/main/java/ro/isdc/wro/model/resource/support/naming/DefaultHashEncoderNamingStrategy.java
  6. +2 −1 ...j-core/src/main/java/ro/isdc/wro/model/resource/support/naming/DefaultNamingStrategyProvider.java
  7. +52 −0 ...core/src/main/java/ro/isdc/wro/model/resource/support/naming/FolderHashEncoderNamingStrategy.java
  8. +13 −0 wro4j-core/src/main/java/ro/isdc/wro/model/resource/support/naming/HashEncoderNamingStrategy.java
  9. +4 −0 wro4j-core/src/test/java/ro/isdc/wro/model/resource/support/hash/TestConfigurableHashStrategy.java
  10. +4 −0 ...-core/src/test/java/ro/isdc/wro/model/resource/support/naming/TestConfigurableNamingStrategy.java
  11. +75 −0 ...src/test/java/ro/isdc/wro/model/resource/support/naming/TestDefaultHashEncoderNamingStrategy.java
  12. +75 −0 .../src/test/java/ro/isdc/wro/model/resource/support/naming/TestFolderHashEncoderNamingStrategy.java
  13. +75 −0 ...j-core/src/test/java/ro/isdc/wro/model/resource/support/naming/TestHashEncoderNamingStrategy.java
  14. +15 −3 wro4j-core/src/test/java/ro/isdc/wro/model/resource/support/naming/TestNamingStrategy.java
  15. +17 −1 wro4j-examples/wro4j-standalone/pom.xml
  16. +4 −2 wro4j-examples/wro4j-standalone/src/main/webapp/WEB-INF/wro.properties
  17. +5 −0 wro4j-maven-plugin/src/main/java/ro/isdc/wro/maven/plugin/Wro4jMojo.java
  18. +46 −8 ...-plugin/src/main/java/ro/isdc/wro/maven/plugin/manager/factory/ConfigurableWroManagerFactory.java
  19. +31 −6 wro4j-maven-plugin/src/test/java/ro/isdc/wro/maven/plugin/TestWro4jMojo.java
@@ -147,7 +147,6 @@ protected Properties newProperties() {
final Properties props = new Properties();
updatePropertiesWithConfiguration(props, ConfigurableHashStrategy.KEY);
return props;
-
}
};
}
@@ -172,6 +172,13 @@ public NamingStrategy create() {
return namingStrategy;
}
});
+ map.put(HashStrategy.class, new InjectorObjectFactory<HashStrategy>() {
+ public HashStrategy create() {
+ final HashStrategy hashStrategy = managerFactory.create().getHashStrategy();
+ injector.inject(hashStrategy);
+ return hashStrategy;
+ }
+ });
map.put(ReadOnlyContext.class, createReadOnlyContextProxy());
map.put(WroConfiguration.class, new InjectorObjectFactory<WroConfiguration>() {
public WroConfiguration create() {
@@ -190,11 +197,6 @@ public ResourceAuthorizationManager create() {
return authorizationManager;
}
});
- map.put(HashStrategy.class, new InjectorObjectFactory<HashStrategy>() {
- public HashStrategy create() {
- return managerFactory.create().getHashStrategy();
- }
- });
}
/**
@@ -4,7 +4,10 @@
import java.io.InputStream;
import java.util.Map;
+import ro.isdc.wro.model.group.Inject;
+import ro.isdc.wro.model.group.processor.Injector;
import ro.isdc.wro.model.resource.support.AbstractConfigurableSingleStrategy;
+import ro.isdc.wro.model.resource.support.naming.NamingStrategy;
/**
@@ -21,13 +24,17 @@
* Property name to specify alias.
*/
public static final String KEY = "hashStrategy";
+ @Inject
+ private Injector injector;
/**
* {@inheritDoc}
*/
public String getHash(final InputStream inputStream)
throws IOException {
- return getConfiguredStrategy().getHash(inputStream);
+ final HashStrategy hashStrategy = getConfiguredStrategy();
+ injector.inject(hashStrategy);
+ return hashStrategy.getHash(inputStream);
}
/**
@@ -4,6 +4,8 @@
import java.io.InputStream;
import java.util.Map;
+import ro.isdc.wro.model.group.Inject;
+import ro.isdc.wro.model.group.processor.Injector;
import ro.isdc.wro.model.resource.support.AbstractConfigurableSingleStrategy;
@@ -21,13 +23,16 @@
* Property name to specify namingStrategy alias.
*/
public static final String KEY = "namingStrategy";
-
+ @Inject
+ private Injector injector;
/**
* {@inheritDoc}
*/
public String rename(String originalName, InputStream inputStream)
throws IOException {
- return getConfiguredStrategy().rename(originalName, inputStream);
+ final NamingStrategy namingStrategy = getConfiguredStrategy();
+ injector.inject(namingStrategy);
+ return namingStrategy.rename(originalName, inputStream);
}
/**
@@ -0,0 +1,58 @@
+package ro.isdc.wro.model.resource.support.naming;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.commons.io.FilenameUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.Validate;
+
+import ro.isdc.wro.model.group.Inject;
+import ro.isdc.wro.model.resource.support.hash.HashStrategy;
+
+
+/**
+ * Encodes the hash into the folder where the resources is located.
+ * Example:
+ * original name: group.js
+ * renamed: group-a912810be321.js
+ *
+ * @author Alex Objelean
+ * @created 15 Aug 2012
+ * @since 1.4.9
+ */
+public class DefaultHashEncoderNamingStrategy
+ implements NamingStrategy {
+ public static final String ALIAS = "hashEncoder";
+ @Inject
+ private HashStrategy hashStrategy;
+
+
+ /**
+ * @return the {@link HashStrategy} to use for renaming. By default the used strategy is the same as the one
+ * configured by wro4j. Override this method to provide a custom {@link HashStrategy}.
+ */
+ protected HashStrategy getHashStrategy() {
+ return hashStrategy;
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public String rename(final String originalName, final InputStream inputStream)
+ throws IOException {
+ Validate.notNull(originalName);
+ Validate.notNull(inputStream);
+ final String baseName = FilenameUtils.getBaseName(originalName);
+ int baseNameIndex = originalName.indexOf(baseName);
+ final String path = originalName.substring(0, baseNameIndex);
+ final String extension = FilenameUtils.getExtension(originalName);
+ final String hash = getHashStrategy().getHash(inputStream);
+ final StringBuilder sb = new StringBuilder(path).append(baseName).append("-").append(hash);
+ if (!StringUtils.isEmpty(extension)) {
+ sb.append(".").append(extension);
+ }
+ return sb.toString();
+ }
+}
@@ -21,8 +21,9 @@
final Map<String, NamingStrategy> map = new HashMap<String, NamingStrategy>();
map.put(TimestampNamingStrategy.ALIAS, new TimestampNamingStrategy());
map.put(NoOpNamingStrategy.ALIAS, new NoOpNamingStrategy());
+ map.put(DefaultHashEncoderNamingStrategy.ALIAS, new DefaultHashEncoderNamingStrategy());
map.put(HashEncoderNamingStrategy.ALIAS, new HashEncoderNamingStrategy());
+ map.put(FolderHashEncoderNamingStrategy.ALIAS, new FolderHashEncoderNamingStrategy());
return map;
}
-
}
@@ -0,0 +1,52 @@
+/**
+ * Copyright Alex Objelean
+ */
+package ro.isdc.wro.model.resource.support.naming;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.commons.lang3.Validate;
+
+import ro.isdc.wro.model.group.Inject;
+import ro.isdc.wro.model.resource.support.hash.HashStrategy;
+
+
+/**
+ * Encodes the hash into the folder where the resources is located.
+ * Example:
+ * original name: group.js
+ * renamed: a912810be321/group.js
+ *
+ * @author Alex Objelean
+ * @created 15 Aug 2012
+ * @since 1.4.9
+ */
+public class FolderHashEncoderNamingStrategy
+ implements NamingStrategy {
+ public static final String ALIAS = "folderHashEncoder";
+ @Inject
+ private HashStrategy hashStrategy;
+
+ /**
+ * @return the {@link HashStrategy} to use for renaming. By default the used strategy is the same as the one
+ * configured by wro4j. Override this method to provide a custom {@link HashStrategy}.
+ */
+ protected HashStrategy getHashStrategy() {
+ return hashStrategy;
+ }
+
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public String rename(final String originalName, final InputStream inputStream)
+ throws IOException {
+ Validate.notNull(originalName);
+ Validate.notNull(inputStream);
+ final String hash = getHashStrategy().getHash(inputStream);
+ final StringBuilder sb = new StringBuilder(hash).append("/").append(originalName);
+ return sb.toString();
+ }
+}
@@ -8,6 +8,7 @@
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.Validate;
import ro.isdc.wro.model.resource.support.hash.CRC32HashStrategy;
import ro.isdc.wro.model.resource.support.hash.HashStrategy;
@@ -20,6 +21,7 @@
*
* @author Alex Objelean
* @created 15 Aug 2010
+ * @deprecated prefer Using {@link DefaultHashEncoderNamingStrategy}.
*/
public class HashEncoderNamingStrategy
implements NamingStrategy {
@@ -33,12 +35,23 @@
protected HashStrategy newHashStrategy() {
return new CRC32HashStrategy();
}
+
+ /**
+ * @return the {@link HashStrategy} to use for renaming. By default the used strategy is the same as the one
+ * configured by wro4j. Override this method to provide a custom {@link HashStrategy}.
+ */
+ protected HashStrategy getHashStrategy() {
+ return hashStrategy;
+ }
+
/**
* {@inheritDoc}
*/
public String rename(final String originalName, final InputStream inputStream)
throws IOException {
+ Validate.notNull(originalName);
+ Validate.notNull(inputStream);
final String baseName = FilenameUtils.getBaseName(originalName);
final String extension = FilenameUtils.getExtension(originalName);
final String hash = hashStrategy.getHash(inputStream);
@@ -12,6 +12,8 @@
import org.mockito.MockitoAnnotations;
import ro.isdc.wro.WroRuntimeException;
+import ro.isdc.wro.config.Context;
+import ro.isdc.wro.util.WroTestUtils;
/**
* @author Alex Objelean
@@ -27,6 +29,8 @@
public void setUp() {
MockitoAnnotations.initMocks(this);
victim = new ConfigurableHashStrategy();
+ Context.set(Context.standaloneContext());
+ WroTestUtils.createInjector().inject(victim);
}
@Test(expected = NullPointerException.class)
@@ -11,6 +11,8 @@
import org.mockito.MockitoAnnotations;
import ro.isdc.wro.WroRuntimeException;
+import ro.isdc.wro.config.Context;
+import ro.isdc.wro.util.WroTestUtils;
/**
@@ -25,6 +27,8 @@
public void setUp() {
MockitoAnnotations.initMocks(this);
victim = new ConfigurableNamingStrategy();
+ Context.set(Context.standaloneContext());
+ WroTestUtils.createInjector().inject(victim);
}
@Test(expected = NullPointerException.class)
@@ -0,0 +1,75 @@
+/*
+* Copyright 2011 France Télécom
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*
+* This was inspired by TestFingerprintCreatorNamingStrategy.
+*/
+
+package ro.isdc.wro.model.resource.support.naming;
+
+import static junit.framework.Assert.assertEquals;
+
+import java.io.ByteArrayInputStream;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import ro.isdc.wro.config.Context;
+import ro.isdc.wro.util.WroTestUtils;
+import ro.isdc.wro.util.WroUtil;
+
+/**
+ * Test class for {@link DefaultHashEncoderNamingStrategy}
+ *
+ * @author Alex Objelean
+ * @created 15 Aug 2012
+ */
+public class TestDefaultHashEncoderNamingStrategy {
+ private NamingStrategy namingStrategy;
+
+ @Before
+ public void setUp() {
+ Context.set(Context.standaloneContext());
+ namingStrategy = new DefaultHashEncoderNamingStrategy();
+ WroTestUtils.createInjector().inject(namingStrategy);
+ }
+
+ @Test(expected=NullPointerException.class)
+ public void cannotAcceptNullResourceName() throws Exception {
+ namingStrategy.rename(null, WroUtil.EMPTY_STREAM);
+ }
+
+ @Test(expected=NullPointerException.class)
+ public void cannotAcceptNullStream() throws Exception {
+ namingStrategy.rename("fileName.js", null);
+ }
+
+ @Test
+ public void shouldRenameResourceWithEmptyContent() throws Exception {
+ final String result = namingStrategy.rename("fileName", WroUtil.EMPTY_STREAM);
+ assertEquals("fileName-da39a3ee5e6b4b0d3255bfef95601890afd80709", result);
+ }
+
+ @Test
+ public void shouldRenameResourceWithSomeContent() throws Exception {
+ final String result = namingStrategy.rename("anotherFile.js", new ByteArrayInputStream("someContent".getBytes()));
+ assertEquals("anotherFile-99ef8ae827896f2af4032d5dab9298ec86309abf.js", result);
+ }
+
+ @Test
+ public void shouldRenameResourceContainedInAFolder() throws Exception {
+ final String result = namingStrategy.rename("folder1/folder2/resource.css", new ByteArrayInputStream("someContent".getBytes()));
+ assertEquals("folder1/folder2/resource-99ef8ae827896f2af4032d5dab9298ec86309abf.css", result);
+ }
+}
Oops, something went wrong.

0 comments on commit f33a5de

Please sign in to comment.