KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > editor > util > FlyOffsetGapListTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.editor.util;
21
22 import org.netbeans.junit.NbTestCase;
23
24 /**
25  * Test of FlyOffsetGapList correctness.
26  *
27  * @author mmetelka
28  */

29 public class FlyOffsetGapListTest extends NbTestCase {
30
31     public FlyOffsetGapListTest(java.lang.String JavaDoc testName) {
32         super(testName);
33     }
34
35     @SuppressWarnings JavaDoc("unchecked")
36     public void test() throws Exception JavaDoc {
37         FOGL fogl = new FOGL(3); // start offset is 3
38
assertEquals(3, fogl.elementOrEndOffset(0));
39
40         try {
41             fogl.elementOffset(0);
42             fail("Exception not thrown");
43         } catch (IndexOutOfBoundsException JavaDoc e) {
44             // Expected
45
}
46         try {
47             fogl.elementOrEndOffset(1);
48             fail("Exception not thrown");
49         } catch (IndexOutOfBoundsException JavaDoc e) {
50             // Expected
51
}
52         try {
53             fogl.elementOrEndOffset(-1);
54             fail("Exception not thrown");
55         } catch (IndexOutOfBoundsException JavaDoc e) {
56             // Expected
57
}
58         
59         fogl.add(new Element(-1, 5));
60         fogl.defaultInsertUpdate(3, 5);
61         assertEquals(3, fogl.elementOrEndOffset(0));
62         assertEquals(8, fogl.elementOrEndOffset(1));
63     }
64     
65     private static final class FOGL extends FlyOffsetGapList<Element> {
66         
67         private int startOffset;
68         
69         FOGL(int startOffset) {
70             this.startOffset = startOffset;
71         }
72         
73         @Override JavaDoc
74         protected int startOffset() {
75             return startOffset;
76         }
77
78         protected boolean isElementFlyweight(Element elem) {
79             return elem.isFlyweight();
80         }
81         
82         protected int elementLength(Element elem) {
83             return elem.length();
84         }
85
86         protected int elementRawOffset(Element elem) {
87             return elem.rawOffset();
88         }
89         
90         protected void setElementRawOffset(Element elem, int rawOffset) {
91             elem.setRawOffset(rawOffset);
92         }
93
94     }
95     
96     private static final class Element {
97         
98         private int rawOffset;
99         
100         private final int length;
101         
102         public Element(int rawOffset, int length) {
103             this.rawOffset = rawOffset;
104             this.length = length;
105         }
106         
107         public int rawOffset() {
108             return rawOffset;
109         }
110         
111         public void setRawOffset(int rawOffset) {
112             this.rawOffset = rawOffset;
113         }
114         
115         public int length() {
116             return length;
117         }
118         
119         public boolean isFlyweight() {
120             return (rawOffset == -1);
121         }
122
123     }
124     
125 }
126
Popular Tags