001: // ========================================================================
002: // Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
003: // ------------------------------------------------------------------------
004: // Licensed under the Apache License, Version 2.0 (the "License");
005: // you may not use this file except in compliance with the License.
006: // You may obtain a copy of the License at
007: // http://www.apache.org/licenses/LICENSE-2.0
008: // Unless required by applicable law or agreed to in writing, software
009: // distributed under the License is distributed on an "AS IS" BASIS,
010: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: // See the License for the specific language governing permissions and
012: // limitations under the License.
013: // ========================================================================
014:
015: package org.mortbay.io;
016:
017: import junit.framework.TestCase;
018:
019: /* ------------------------------------------------------------------------------- */
020: /**
021: *
022: * @author gregw
023: */
024: public class BufferCacheTest extends TestCase {
025: final static String[] S = { "S0", "S1", "s2", "s3" };
026:
027: BufferCache cache;
028:
029: public BufferCacheTest(String arg0) {
030: super (arg0);
031: }
032:
033: public static void main(String[] args) {
034: junit.textui.TestRunner.run(BufferCacheTest.class);
035: }
036:
037: /**
038: * @see TestCase#setUp()
039: */
040: protected void setUp() throws Exception {
041: super .setUp();
042: cache = new BufferCache();
043: cache.add(S[1], 1);
044: cache.add(S[2], 2);
045: cache.add(S[3], 3);
046: }
047:
048: /**
049: * @see TestCase#tearDown()
050: */
051: protected void tearDown() throws Exception {
052: super .tearDown();
053: }
054:
055: public void testLookupIndex() {
056: for (int i = 0; i < S.length; i++) {
057: String s = "S0S1s2s3";
058: ByteArrayBuffer buf = new ByteArrayBuffer(s.getBytes(),
059: i * 2, 2);
060: BufferCache.CachedBuffer b = cache.get(buf);
061: int index = b == null ? -1 : b.getOrdinal();
062:
063: if (i > 0)
064: assertEquals(i, index);
065: else
066: assertEquals(-1, index);
067: }
068: }
069:
070: public void testGetBuffer() {
071: for (int i = 0; i < S.length; i++) {
072: String s = "S0S1s2s3";
073: ByteArrayBuffer buf = new ByteArrayBuffer(s.getBytes(),
074: i * 2, 2);
075: Buffer b = cache.get(buf);
076:
077: if (i > 0)
078: assertEquals(i, b.peek(1) - '0');
079: else
080: assertEquals(null, b);
081: }
082: }
083:
084: public void testLookupBuffer() {
085: for (int i = 0; i < S.length; i++) {
086: String s = "S0S1s2s3";
087: ByteArrayBuffer buf = new ByteArrayBuffer(s.getBytes(),
088: i * 2, 2);
089: Buffer b = cache.lookup(buf);
090:
091: assertEquals(S[i], b.toString());
092: if (i > 0)
093: assertTrue("" + i, S[i] == b.toString());
094: else {
095: assertTrue("" + i, S[i] != b.toString());
096: assertEquals("" + i, S[i], b.toString());
097: }
098: }
099: }
100:
101: public void testInsensitiveLookupBuffer() {
102: for (int i = 0; i < S.length; i++) {
103: String s = "s0s1S2S3";
104: ByteArrayBuffer buf = new ByteArrayBuffer(s.getBytes(),
105: i * 2, 2);
106: Buffer b = cache.lookup(buf);
107:
108: assertTrue("test" + i, S[i].equalsIgnoreCase(b.toString()));
109: if (i > 0)
110: assertTrue("test" + i, S[i] == b.toString());
111: else
112: assertTrue("test" + i, S[i] != b.toString());
113: }
114: }
115:
116: public void testToString() {
117: for (int i = 0; i < S.length; i++) {
118: String s = "S0S1s2s3";
119: ByteArrayBuffer buf = new ByteArrayBuffer(s.getBytes(),
120: i * 2, 2);
121: String b = cache.toString(buf);
122:
123: assertEquals(S[i], b);
124: if (i > 0)
125: assertTrue(S[i] == b);
126: else
127: assertTrue(S[i] != b);
128: }
129: }
130:
131: }
|