Permalink
Browse files

Merged changes

  • Loading branch information...
2 parents 7e8206e + 10c0fb0 commit 606b972b21d70c9314143ea51602411836ba2010 @jroper jroper committed Jul 5, 2011
@@ -13,7 +13,7 @@
private final char[] buffer;
private final int length;
- private final List<SitemeshBufferFragment> chainedBuffers;
+ private final List<SitemeshBufferFragment> bufferFragments;
public DefaultSitemeshBuffer(char[] buffer) {
this(buffer, buffer.length);
@@ -23,16 +23,16 @@ public DefaultSitemeshBuffer(char[] buffer, int length) {
this(buffer, length, Collections.<SitemeshBufferFragment>emptyList());
}
- public DefaultSitemeshBuffer(char[] buffer, int length, List<SitemeshBufferFragment> chainedBuffers) {
+ public DefaultSitemeshBuffer(char[] buffer, int length, List<SitemeshBufferFragment> bufferFragments) {
this.buffer = buffer;
this.length = length;
- this.chainedBuffers = new ArrayList<SitemeshBufferFragment>(chainedBuffers);
- Collections.sort(chainedBuffers);
+ this.bufferFragments = new ArrayList<SitemeshBufferFragment>(bufferFragments);
+ Collections.sort(bufferFragments);
}
public void writeTo(Writer writer, int start, int length) throws IOException {
int pos = start;
- for (SitemeshBufferFragment fragment : chainedBuffers) {
+ for (SitemeshBufferFragment fragment : bufferFragments) {
if (fragment.getPosition() < pos) {
continue;
}
@@ -59,7 +59,7 @@ public int getTotalLength() {
public int getTotalLength(int start, int length) {
int total = length;
- for (SitemeshBufferFragment fragment : chainedBuffers) {
+ for (SitemeshBufferFragment fragment : bufferFragments) {
if (fragment.getPosition() < start) {
continue;
}
@@ -80,6 +80,6 @@ public int getBufferLength() {
}
public boolean hasFragments() {
- return !chainedBuffers.isEmpty();
+ return !bufferFragments.isEmpty();
}
}
@@ -7,53 +7,78 @@
import com.opensymphony.module.sitemesh.SitemeshBuffer;
import com.opensymphony.module.sitemesh.SitemeshBufferFragment;
+import com.opensymphony.module.sitemesh.SitemeshBufferWriter;
import junit.framework.TestCase;
/**
*/
-public class ChainingBufferTest extends TestCase
-{
- public void testSimpleChain() throws Exception
- {
- SitemeshBuffer buffer = newSitemeshBuffer("aaaa", newBufferFragment("bb", 2));
- assertEquals("aabbaa", getContent(buffer));
+public class ChainingBufferTest extends TestCase {
+ public void testSimpleChain() throws Exception {
+ SitemeshBuffer buffer = newSitemeshBuffer("1234", newBufferFragment("ab", 2));
+ assertEquals("12ab34", getContent(buffer));
}
- public void testBefore() throws Exception
- {
- SitemeshBuffer buffer = newSitemeshBuffer("aaaa", newBufferFragment("bb", 2));
- assertEquals("a", getContent(buffer, 0, 1));
- assertEquals("aa", getContent(buffer, 0, 2));
+ public void testBefore() throws Exception {
+ SitemeshBuffer buffer = newSitemeshBuffer("1234", newBufferFragment("ab", 2));
+ assertEquals("1", getContent(buffer, 0, 1));
+ assertEquals("12", getContent(buffer, 0, 2));
}
- public void testAfter() throws Exception
- {
- SitemeshBuffer buffer = newSitemeshBuffer("aaaa", newBufferFragment("bb", 2));
- assertEquals("bbaa", getContent(buffer, 2, 2));
- assertEquals("a", getContent(buffer, 3, 1));
+ public void testAfter() throws Exception {
+ SitemeshBuffer buffer = newSitemeshBuffer("1234", newBufferFragment("ab", 2));
+ assertEquals("ab34", getContent(buffer, 2, 2));
+ assertEquals("4", getContent(buffer, 3, 1));
}
- private String getContent(SitemeshBuffer buffer) throws Exception
- {
+ public void testFragment() throws Exception {
+ SitemeshBuffer buffer = newSitemeshBuffer("1234", newBufferFragment("abcd", 1, 2, 2));
+ assertEquals("12bc34", getContent(buffer));
+ }
+
+ public void testDeepFragments() throws Exception {
+ SitemeshBuffer buffer = newSitemeshBuffer("123456789",
+ newBufferFragment("abcdefg", 3,
+ newBufferFragment("hijklm", 1, 1, 4),
+ newBufferFragment("nopqr", 1, 4, 5)),
+ newBufferFragment("tuzwx", 0, 2, 8));
+ assertEquals("123abcdieopqrfg45678tu9", getContent(buffer));
+ }
+
+ public void testWriter() throws Exception {
+ SitemeshBuffer buffer = newSitemeshBuffer("123456");
+ SitemeshBufferWriter writer = new SitemeshBufferWriter();
+ writer.write("abc");
+ writer.writeSitemeshBuffer(buffer, 1, 4);
+ writer.write("def");
+ assertEquals("abcdef", writer.toString());
+ assertEquals("abc2345def", getContent(writer.getSitemeshBuffer()));
+ }
+
+ private String getContent(SitemeshBuffer buffer) throws Exception {
CharArrayWriter writer = new CharArrayWriter();
buffer.writeTo(writer, 0, buffer.getBufferLength());
return writer.toString();
}
- private String getContent(SitemeshBuffer buffer, int start, int length) throws Exception
- {
+ private String getContent(SitemeshBuffer buffer, int start, int length) throws Exception {
CharArrayWriter writer = new CharArrayWriter();
buffer.writeTo(writer, start, length);
return writer.toString();
}
- private SitemeshBuffer newSitemeshBuffer(String content, SitemeshBufferFragment... fragments)
- {
+ private SitemeshBuffer newSitemeshBuffer(String content, SitemeshBufferFragment... fragments) {
return new DefaultSitemeshBuffer(content.toCharArray(), content.length(), Arrays.asList(fragments));
}
- private SitemeshBufferFragment newBufferFragment(String content, int position)
- {
+ private SitemeshBufferFragment newBufferFragment(String content, int position) {
return new SitemeshBufferFragment(newSitemeshBuffer(content), 0, content.length(), position);
}
+
+ private SitemeshBufferFragment newBufferFragment(String content, int start, int length, int position) {
+ return new SitemeshBufferFragment(newSitemeshBuffer(content), start, length, position);
+ }
+
+ private SitemeshBufferFragment newBufferFragment(String content, int position, SitemeshBufferFragment... fragments) {
+ return new SitemeshBufferFragment(newSitemeshBuffer(content, fragments), 0, content.length(), position);
+ }
}

0 comments on commit 606b972

Please sign in to comment.