KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.swing.text.AbstractDocument JavaDoc;
23 import javax.swing.text.BadLocationException JavaDoc;
24 import junit.framework.TestCase;
25 import org.netbeans.api.editor.fold.Fold;
26 import org.netbeans.api.editor.fold.FoldType;
27 import org.netbeans.api.editor.fold.FoldHierarchy;
28 import org.netbeans.spi.editor.fold.FoldHierarchyTransaction;
29 import org.netbeans.spi.editor.fold.FoldManager;
30 import org.netbeans.spi.editor.fold.FoldManagerFactory;
31
32 /**
33  *
34  * @author mmetelka
35  */

36 public class NestedFoldManagerTest extends TestCase {
37     
38     static final int FOLD_START_OFFSET_OUTER = 5;
39     static final int FOLD_END_OFFSET_OUTER = 10;
40     static final int FOLD_START_OFFSET_INNER = 6;
41     static final int FOLD_END_OFFSET_INNER = 8;
42     
43     public NestedFoldManagerTest(String JavaDoc testName) {
44         super(testName);
45     }
46     
47     public void test() {
48         test(true);
49         test(false);
50     }
51
52     /**
53      * Test the creation of several folds.
54      */

55     public void test(boolean outerFirst) {
56         FoldHierarchyTestEnv env = new FoldHierarchyTestEnv(new NestedFoldManagerFactory(outerFirst));
57
58         FoldHierarchy hierarchy = env.getHierarchy();
59         AbstractDocument JavaDoc doc = env.getDocument();
60         doc.readLock();
61         try {
62             hierarchy.lock();
63             try {
64                 Fold rootFold = hierarchy.getRootFold();
65                 int foldCount = rootFold.getFoldCount();
66                 int expectedFoldCount = 1;
67                 assertTrue("Incorrect fold count " + foldCount, // NOI18N
68
(foldCount == expectedFoldCount)
69                 );
70                 
71                 Fold fold = rootFold.getFold(0);
72                 FoldType foldType = fold.getType();
73                 int foldStartOffset = fold.getStartOffset();
74                 int foldEndOffset = fold.getEndOffset();
75                 assertTrue("Incorrect fold type " + foldType, // NOI18N
76
(foldType == AbstractFoldManager.REGULAR_FOLD_TYPE));
77                 assertTrue("Incorrect fold start offset " + foldStartOffset, // NOI18N
78
(foldStartOffset == FOLD_START_OFFSET_OUTER));
79                 assertTrue("Incorrect fold end offset " + foldEndOffset, // NOI18N
80
(foldEndOffset == FOLD_END_OFFSET_OUTER));
81                 
82                 // Test inner fold
83
Fold outerFold = fold;
84                 foldCount = outerFold.getFoldCount();
85                 expectedFoldCount = 1;
86                 assertTrue("Incorrect fold count " + foldCount, // NOI18N
87
(foldCount == expectedFoldCount)
88                 );
89                 
90                 fold = outerFold.getFold(0);
91                 assertTrue("Folds must differ", (fold != outerFold)); // NOI18N
92
foldType = fold.getType();
93                 foldStartOffset = fold.getStartOffset();
94                 foldEndOffset = fold.getEndOffset();
95                 assertTrue("Incorrect fold type " + foldType, // NOI18N
96
(foldType == AbstractFoldManager.REGULAR_FOLD_TYPE));
97                 assertTrue("Incorrect fold start offset " + foldStartOffset, // NOI18N
98
(foldStartOffset == FOLD_START_OFFSET_INNER));
99                 assertTrue("Incorrect fold end offset " + foldEndOffset, // NOI18N
100
(foldEndOffset == FOLD_END_OFFSET_INNER));
101
102             } finally {
103                 hierarchy.unlock();
104             }
105         } finally {
106             doc.readUnlock();
107         }
108     }
109     
110     
111     final class NestedFoldManager extends AbstractFoldManager {
112         
113         private boolean outerFirst;
114         
115         NestedFoldManager(boolean outerFirst) {
116             this.outerFirst = outerFirst;
117         }
118         
119         private void addOuter(FoldHierarchyTransaction transaction) throws BadLocationException JavaDoc {
120             getOperation().addToHierarchy(
121                 REGULAR_FOLD_TYPE,
122                 null,
123                 false,
124                 FOLD_START_OFFSET_OUTER, FOLD_END_OFFSET_OUTER, 1, 1,
125                 null,
126                 transaction
127             );
128         }
129         
130         private void addInner(FoldHierarchyTransaction transaction) throws BadLocationException JavaDoc{
131             getOperation().addToHierarchy(
132                 REGULAR_FOLD_TYPE,
133                 null,
134                 false,
135                 FOLD_START_OFFSET_INNER, FOLD_END_OFFSET_INNER, 1, 1,
136                 null,
137                 transaction
138             );
139         }
140         
141         public void initFolds(FoldHierarchyTransaction transaction) {
142             try {
143                 if (outerFirst) {
144                     addOuter(transaction);
145                 }
146                 addInner(transaction);
147                 if (!outerFirst) {
148                     addOuter(transaction);
149                 }
150             } catch (BadLocationException JavaDoc e) {
151                 e.printStackTrace();
152                 fail();
153             }
154         }
155         
156     }
157
158     public final class NestedFoldManagerFactory implements FoldManagerFactory {
159         
160         private boolean outerFirst;
161         
162         NestedFoldManagerFactory(boolean outerFirst) {
163             this.outerFirst = outerFirst;
164         }
165         
166         public FoldManager createFoldManager() {
167             return new NestedFoldManager(outerFirst);
168         }
169         
170     }
171     
172 }
173
Popular Tags