Author: jtarrio
Date: Wed May 11 16:44:38 2011
New Revision: 1101968
URL: http://svn.apache.org/viewvc?rev=1101968&view=rev
Log:
Check contents and source of JsContent when adding it to a JsResponse
JsContent objects with empty contents will not be added to a
JsResponseBuilder. Of the remaining objects, those with no name will cause
an IllegalArgumentException to be thrown.
Modified:
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/js/JsContent.java
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/js/JsResponseBuilder.java
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/CajaJsSubtractingProcessorTest.java
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/CompilationProcessorTest.java
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/ExportJsProcessorTest.java
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/GetJsContentProcessorTest.java
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/JsResponseBuilderTest.java
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/SeparatorCommentingProcessorTest.java
Modified:
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/js/JsContent.java
URL:
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/js/JsContent.java?rev=1101968&r1=1101967&r2=1101968&view=diff
==============================================================================
---
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/js/JsContent.java
(original)
+++
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/js/JsContent.java
Wed May 11 16:44:38 2011
@@ -17,6 +17,9 @@
*/
package org.apache.shindig.gadgets.js;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+
import org.apache.shindig.gadgets.features.FeatureRegistry.FeatureBundle;
import org.apache.shindig.gadgets.features.FeatureResource;
@@ -42,6 +45,8 @@ public class JsContent {
private JsContent(String content, String source,
FeatureBundle bundle, FeatureResource resource) {
+ Preconditions.checkArgument(
+ !Strings.isNullOrEmpty(source), "All JS content must have a source");
this.content = content;
this.source = source;
this.bundle = bundle;
Modified:
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/js/JsResponseBuilder.java
URL:
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/js/JsResponseBuilder.java?rev=1101968&r1=1101967&r2=1101968&view=diff
==============================================================================
---
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/js/JsResponseBuilder.java
(original)
+++
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/js/JsResponseBuilder.java
Wed May 11 16:44:38 2011
@@ -70,15 +70,26 @@ public class JsResponseBuilder {
* Prepend a JS to the response.
*/
public JsResponseBuilder prependJs(JsContent jsContent) {
- jsCode.add(0, jsContent);
+ if (canAddContent(jsContent)) {
+ jsCode.add(0, jsContent);
+ }
return this;
}
/**
+ * Prepends JS to the output.
+ */
+ public JsResponseBuilder prependJs(String content, String name) {
+ return prependJs(JsContent.fromText(content, name));
+ }
+
+ /**
* Insert a JS at a specific index.
*/
public JsResponseBuilder insertJsAt(int index, JsContent jsContent) {
- jsCode.add(index, jsContent);
+ if (canAddContent(jsContent)) {
+ jsCode.add(index, jsContent);
+ }
return this;
}
@@ -86,7 +97,9 @@ public class JsResponseBuilder {
* Appends more JS to the response.
*/
public JsResponseBuilder appendJs(JsContent jsContent) {
- jsCode.add(jsContent);
+ if (canAddContent(jsContent)) {
+ jsCode.add(jsContent);
+ }
return this;
}
@@ -108,14 +121,6 @@ public class JsResponseBuilder {
}
/**
- * Prepends JS to the output.
- */
- public JsResponseBuilder prependJs(String content, String name) {
- jsCode.add(0, JsContent.fromText(content, name));
- return this;
- }
-
- /**
* Deletes all JavaScript code in the builder.
*/
public JsResponseBuilder clearJs() {
@@ -255,4 +260,8 @@ public class JsResponseBuilder {
}
return result;
}
+
+ private boolean canAddContent(JsContent jsContent) {
+ return !jsContent.get().isEmpty();
+ }
}
Modified:
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/CajaJsSubtractingProcessorTest.java
URL:
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/CajaJsSubtractingProcessorTest.java?rev=1101968&r1=1101967&r2=1101968&view=diff
==============================================================================
---
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/CajaJsSubtractingProcessorTest.java
(original)
+++
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/CajaJsSubtractingProcessorTest.java
Wed May 11 16:44:38 2011
@@ -42,6 +42,7 @@ public class CajaJsSubtractingProcessorT
private static final List<String> ERRORS = ImmutableList.<String>of();
+ private static final String SOURCE = "source";
private static final String NORMAL_CONTENT_JS = "normal";
private static final String CAJA_CONTENT_JS = "cajoled";
@@ -57,12 +58,12 @@ public class CajaJsSubtractingProcessorT
control = createControl();
contents = Lists.newArrayList();
- contents.add(JsContent.fromFeature(NORMAL_CONTENT_JS, null, null, null));
- contents.add(JsContent.fromFeature(NORMAL_CONTENT_JS, null, null,
+ contents.add(JsContent.fromFeature(NORMAL_CONTENT_JS, SOURCE, null, null));
+ contents.add(JsContent.fromFeature(NORMAL_CONTENT_JS, SOURCE, null,
mockFeatureResource(null)));
- contents.add(JsContent.fromFeature(NORMAL_CONTENT_JS, null, null,
+ contents.add(JsContent.fromFeature(NORMAL_CONTENT_JS, SOURCE, null,
mockFeatureResource(ImmutableMap.of(UriCommon.Param.CAJOLE.getKey(),
"blah"))));
- contents.add(JsContent.fromFeature(CAJA_CONTENT_JS, null, null,
+ contents.add(JsContent.fromFeature(CAJA_CONTENT_JS, SOURCE, null,
mockFeatureResource(ImmutableMap.of(UriCommon.Param.CAJOLE.getKey(),
ATTRIB_VALUE))));
response = new JsResponse(contents, -1, -1, false, ERRORS, null);
Modified:
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/CompilationProcessorTest.java
URL:
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/CompilationProcessorTest.java?rev=1101968&r1=1101967&r2=1101968&view=diff
==============================================================================
---
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/CompilationProcessorTest.java
(original)
+++
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/CompilationProcessorTest.java
Wed May 11 16:44:38 2011
@@ -59,8 +59,8 @@ public class CompilationProcessorTest {
new JsResponseBuilder().setCacheTtlSecs(1234).setStatusCode(200)
.appendJs("content1:", "source1")
.appendJs("content2", "source2")
- .appendJs(JsContent.fromFeature("content3:", null,
mockBundle("extern3"), null))
- .appendJs(JsContent.fromFeature("content4:", null,
mockBundle("extern4"), null));
+ .appendJs(JsContent.fromFeature("content3:", "source3",
mockBundle("extern3"), null))
+ .appendJs(JsContent.fromFeature("content4:", "source4",
mockBundle("extern4"), null));
JsResponse outputResponse = new JsResponseBuilder().appendJs("content3",
"s3").build();
JsRequest request = control.createMock(JsRequest.class);
expect(request.getJsUri()).andReturn(jsUri);
Modified:
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/ExportJsProcessorTest.java
URL:
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/ExportJsProcessorTest.java?rev=1101968&r1=1101967&r2=1101968&view=diff
==============================================================================
---
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/ExportJsProcessorTest.java
(original)
+++
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/ExportJsProcessorTest.java
Wed May 11 16:44:38 2011
@@ -91,11 +91,11 @@ public class ExportJsProcessorTest {
LookupResult lookupMock = mockLookupResult(bundle);
FeatureRegistry featureRegistryMock = mockRegistry(lookupMock);
- textJsContent1 = JsContent.fromText(TEXT_CONTENT_1, null);
- textJsContent2 = JsContent.fromText(TEXT_CONTENT_2, null);
- featureJsContent1 = JsContent.fromFeature(FEATURE_CONTENT_1, null,
mockBundle(EXPORTS_1), null);
- featureJsContent2 = JsContent.fromFeature(FEATURE_CONTENT_2, null,
mockBundle(EXPORTS_2), null);
- featureJsContent3 = JsContent.fromFeature(FEATURE_CONTENT_3, null,
mockBundle(EXPORTS_3), null);
+ textJsContent1 = JsContent.fromText(TEXT_CONTENT_1, "source1");
+ textJsContent2 = JsContent.fromText(TEXT_CONTENT_2, "source2");
+ featureJsContent1 = JsContent.fromFeature(FEATURE_CONTENT_1, "source3",
mockBundle(EXPORTS_1), null);
+ featureJsContent2 = JsContent.fromFeature(FEATURE_CONTENT_2, "source4",
mockBundle(EXPORTS_2), null);
+ featureJsContent3 = JsContent.fromFeature(FEATURE_CONTENT_3, "source5",
mockBundle(EXPORTS_3), null);
compiler = new ExportJsProcessor(featureRegistryMock, contextProviderMock);
}
Modified:
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/GetJsContentProcessorTest.java
URL:
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/GetJsContentProcessorTest.java?rev=1101968&r1=1101967&r2=1101968&view=diff
==============================================================================
---
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/GetJsContentProcessorTest.java
(original)
+++
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/GetJsContentProcessorTest.java
Wed May 11 16:44:38 2011
@@ -124,7 +124,7 @@ public class GetJsContentProcessorTest {
expect(compiler.getJsContent(jsUri, bundle1))
.andReturn(ImmutableList.<JsContent>of(
- JsContent.fromFeature(JS_CODE1, null, null, null)));
+ JsContent.fromFeature(JS_CODE1, "source1", null, null)));
control.replay();
processor.process(request, response);
@@ -147,8 +147,8 @@ public class GetJsContentProcessorTest {
expect(compiler.getJsContent(jsUri, bundle))
.andReturn(ImmutableList.<JsContent>of(
- JsContent.fromFeature(JS_CODE1, null, bundle, resource1),
- JsContent.fromFeature(JS_CODE2, null, bundle, resource2)));
+ JsContent.fromFeature(JS_CODE1, "source1", bundle, resource1),
+ JsContent.fromFeature(JS_CODE2, "source2", bundle, resource2)));
}
private void setupJsUriAndRegistry(UriStatus uriStatus,
Modified:
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/JsResponseBuilderTest.java
URL:
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/JsResponseBuilderTest.java?rev=1101968&r1=1101967&r2=1101968&view=diff
==============================================================================
---
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/JsResponseBuilderTest.java
(original)
+++
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/JsResponseBuilderTest.java
Wed May 11 16:44:38 2011
@@ -18,11 +18,13 @@
*/
package org.apache.shindig.gadgets.js;
-import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
+import java.util.Iterator;
+
public class JsResponseBuilderTest {
private JsResponseBuilder builder;
@@ -49,4 +51,18 @@ public class JsResponseBuilderTest {
"var e = {};\ne.prototype.f = {};\n",
builder.build().getExterns());
}
+
+ @Test
+ public void skipsEmptyContent() throws Exception {
+ builder.appendJs("number 1", "num1");
+ builder.appendJs("", "num2");
+ builder.appendJs("number 3", "num3");
+ builder.prependJs("number 4", "num4");
+ builder.prependJs("", "num5");
+ Iterator<JsContent> allJsContent =
builder.build().getAllJsContent().iterator();
+ assertEquals("num4", allJsContent.next().getSource());
+ assertEquals("num1", allJsContent.next().getSource());
+ assertEquals("num3", allJsContent.next().getSource());
+ assertFalse(allJsContent.hasNext());
+ }
}
Modified:
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/SeparatorCommentingProcessorTest.java
URL:
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/SeparatorCommentingProcessorTest.java?rev=1101968&r1=1101967&r2=1101968&view=diff
==============================================================================
---
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/SeparatorCommentingProcessorTest.java
(original)
+++
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/js/SeparatorCommentingProcessorTest.java
Wed May 11 16:44:38 2011
@@ -60,7 +60,7 @@ public class SeparatorCommentingProcesso
@Test
public void testOneFeature() throws Exception {
- JsContent js = JsContent.fromFeature("content", null,
mockBundle("bundle"), null);
+ JsContent js = JsContent.fromFeature("content", "source",
mockBundle("bundle"), null);
JsResponseBuilder builder = newBuilder(js);
control.replay();
@@ -78,7 +78,7 @@ public class SeparatorCommentingProcesso
@Test
public void testOneText() throws Exception {
- JsContent text1 = JsContent.fromText("text1", null);
+ JsContent text1 = JsContent.fromText("text1", "source");
JsResponseBuilder builder = newBuilder(text1);
control.replay();
@@ -92,8 +92,8 @@ public class SeparatorCommentingProcesso
@Test
public void testMultipleFeaturesWithoutInBetweenTexts() throws Exception {
- JsContent js1 = JsContent.fromFeature("content1", null,
mockBundle("bundle1"), null);
- JsContent js2 = JsContent.fromFeature("content2", null,
mockBundle("bundle2"), null);
+ JsContent js1 = JsContent.fromFeature("content1", "source1",
mockBundle("bundle1"), null);
+ JsContent js2 = JsContent.fromFeature("content2", "source2",
mockBundle("bundle2"), null);
JsResponseBuilder builder = newBuilder(js1, js2);
control.replay();
@@ -115,8 +115,8 @@ public class SeparatorCommentingProcesso
@Test
public void testNeighboringSameFeatures() throws Exception {
FeatureBundle bundle = mockBundle("bundle");
- JsContent js1 = JsContent.fromFeature("content1", null, bundle, null);
- JsContent js2 = JsContent.fromFeature("content2", null, bundle, null);
+ JsContent js1 = JsContent.fromFeature("content1", "source1", bundle, null);
+ JsContent js2 = JsContent.fromFeature("content2", "source2", bundle, null);
JsResponseBuilder builder = newBuilder(js1, js2);
control.replay();
@@ -135,11 +135,11 @@ public class SeparatorCommentingProcesso
@Test
public void testMultipleFeaturesWithInBetweenTexts() throws Exception {
- JsContent text1 = JsContent.fromText("text1", null);
- JsContent text2 = JsContent.fromText("text2", null);
- JsContent text3 = JsContent.fromText("text3", null);
- JsContent js1 = JsContent.fromFeature("content1", null,
mockBundle("bundle1"), null);
- JsContent js2 = JsContent.fromFeature("content2", null,
mockBundle("bundle2"), null);
+ JsContent text1 = JsContent.fromText("text1", "source1");
+ JsContent text2 = JsContent.fromText("text2", "source2");
+ JsContent text3 = JsContent.fromText("text3", "source3");
+ JsContent js1 = JsContent.fromFeature("content1", "source4",
mockBundle("bundle1"), null);
+ JsContent js2 = JsContent.fromFeature("content2", "source5",
mockBundle("bundle2"), null);
JsResponseBuilder builder = newBuilder(text1, js1, text2, js2, text3);
control.replay();