KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > fold > SimpleFoldManagerTest


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.modules.editor.fold;
21
22 import java.util.Collections JavaDoc;
23 import javax.swing.text.AbstractDocument JavaDoc;
24 import javax.swing.text.BadLocationException JavaDoc;
25 import javax.swing.text.Position JavaDoc;
26 import org.netbeans.api.editor.fold.Fold;
27 import org.netbeans.api.editor.fold.FoldType;
28 import org.netbeans.api.editor.fold.FoldHierarchy;
29 import org.netbeans.junit.MemoryFilter;
30 import org.netbeans.junit.NbTestCase;
31 import org.netbeans.spi.editor.fold.FoldHierarchyTransaction;
32 import org.netbeans.spi.editor.fold.FoldManager;
33 import org.netbeans.spi.editor.fold.FoldManagerFactory;
34
35 /**
36  *
37  * @author mmetelka
38  */

39 public class SimpleFoldManagerTest extends NbTestCase {
40     
41     private static final int MAX_FOLD_MEMORY_SIZE = 64;
42     
43     static final int FOLD_START_OFFSET_1 = 5;
44     static final int FOLD_END_OFFSET_1 = 10;
45     
46     public SimpleFoldManagerTest(String JavaDoc testName) {
47         super(testName);
48     }
49     
50     /**
51      * Test the creation of several folds.
52      */

53     public void test() {
54         FoldHierarchyTestEnv env = new FoldHierarchyTestEnv(new SimpleFoldManagerFactory());
55
56         FoldHierarchy hierarchy = env.getHierarchy();
57         AbstractDocument JavaDoc doc = env.getDocument();
58         doc.readLock();
59         try {
60             hierarchy.lock();
61             try {
62                 
63                 Fold rootFold = hierarchy.getRootFold();
64                 int foldCount = rootFold.getFoldCount();
65                 int expectedFoldCount = 1;
66                 assertTrue("Incorrect fold count " + foldCount, // NOI18N
67
(foldCount == expectedFoldCount)
68                 );
69                 
70                 Fold fold = rootFold.getFold(0);
71                 FoldType foldType = fold.getType();
72                 int foldStartOffset = fold.getStartOffset();
73                 int foldEndOffset = fold.getEndOffset();
74                 assertTrue("Incorrect fold type " + foldType, // NOI18N
75
(foldType == AbstractFoldManager.REGULAR_FOLD_TYPE));
76                 assertTrue("Incorrect fold start offset " + foldStartOffset, // NOI18N
77
(foldStartOffset == FOLD_START_OFFSET_1));
78                 assertTrue("Incorrect fold end offset " + foldEndOffset, // NOI18N
79
(foldEndOffset == FOLD_END_OFFSET_1));
80                 
81                 // Check fold size
82
assertSize("Size of the fold " , Collections.singleton(fold), // NOI18N
83
MAX_FOLD_MEMORY_SIZE, new FoldMemoryFilter(fold));
84                 
85             } finally {
86                 hierarchy.unlock();
87             }
88         } finally {
89             doc.readUnlock();
90         }
91     }
92     
93     
94     final class SimpleFoldManager extends AbstractFoldManager {
95         
96         public void initFolds(FoldHierarchyTransaction transaction) {
97             try {
98                 getOperation().addToHierarchy(
99                     REGULAR_FOLD_TYPE,
100                     "...", // non-null to properly count fold's size (non-null desc gets set) // NOI18N
101
false,
102                     FOLD_START_OFFSET_1, FOLD_END_OFFSET_1, 1, 1,
103                     null,
104                     transaction
105                 );
106             } catch (BadLocationException JavaDoc e) {
107                 e.printStackTrace();
108                 fail();
109             }
110         }
111         
112     }
113
114     public final class SimpleFoldManagerFactory implements FoldManagerFactory {
115         
116         public FoldManager createFoldManager() {
117             return new SimpleFoldManager();
118         }
119         
120     }
121
122     private final class FoldMemoryFilter implements MemoryFilter {
123         
124         private Fold fold;
125         
126         FoldMemoryFilter(Fold fold) {
127             this.fold = fold;
128         }
129         
130         public boolean reject(Object JavaDoc o) {
131             return (o == fold.getType())
132                 || (o == fold.getDescription()) // requires non-null description during construction
133
|| (o == fold.getParent())
134                 || (o instanceof FoldOperationImpl)
135                 || (o instanceof Position JavaDoc);
136             
137             // Will count possible FoldChildren and ExtraInfo
138
}
139
140     }
141
142 }
143
Popular Tags