001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.editor.fold;
043:
044: import java.util.Collections;
045: import javax.swing.text.AbstractDocument;
046: import javax.swing.text.BadLocationException;
047: import javax.swing.text.Position;
048: import org.netbeans.api.editor.fold.Fold;
049: import org.netbeans.api.editor.fold.FoldType;
050: import org.netbeans.api.editor.fold.FoldHierarchy;
051: import org.netbeans.junit.MemoryFilter;
052: import org.netbeans.junit.NbTestCase;
053: import org.netbeans.spi.editor.fold.FoldHierarchyTransaction;
054: import org.netbeans.spi.editor.fold.FoldManager;
055: import org.netbeans.spi.editor.fold.FoldManagerFactory;
056:
057: /**
058: *
059: * @author mmetelka
060: */
061: public class SimpleFoldManagerTest extends NbTestCase {
062:
063: private static final int MAX_FOLD_MEMORY_SIZE = 64;
064:
065: static final int FOLD_START_OFFSET_1 = 5;
066: static final int FOLD_END_OFFSET_1 = 10;
067:
068: public SimpleFoldManagerTest(String testName) {
069: super (testName);
070: }
071:
072: /**
073: * Test the creation of several folds.
074: */
075: public void test() {
076: FoldHierarchyTestEnv env = new FoldHierarchyTestEnv(
077: new SimpleFoldManagerFactory());
078:
079: FoldHierarchy hierarchy = env.getHierarchy();
080: AbstractDocument doc = env.getDocument();
081: doc.readLock();
082: try {
083: hierarchy.lock();
084: try {
085:
086: Fold rootFold = hierarchy.getRootFold();
087: int foldCount = rootFold.getFoldCount();
088: int expectedFoldCount = 1;
089: assertTrue("Incorrect fold count " + foldCount, // NOI18N
090: (foldCount == expectedFoldCount));
091:
092: Fold fold = rootFold.getFold(0);
093: FoldType foldType = fold.getType();
094: int foldStartOffset = fold.getStartOffset();
095: int foldEndOffset = fold.getEndOffset();
096: assertTrue(
097: "Incorrect fold type " + foldType, // NOI18N
098: (foldType == AbstractFoldManager.REGULAR_FOLD_TYPE));
099: assertTrue("Incorrect fold start offset "
100: + foldStartOffset, // NOI18N
101: (foldStartOffset == FOLD_START_OFFSET_1));
102: assertTrue(
103: "Incorrect fold end offset " + foldEndOffset, // NOI18N
104: (foldEndOffset == FOLD_END_OFFSET_1));
105:
106: // Check fold size
107: assertSize("Size of the fold ",
108: Collections.singleton(fold), // NOI18N
109: MAX_FOLD_MEMORY_SIZE,
110: new FoldMemoryFilter(fold));
111:
112: } finally {
113: hierarchy.unlock();
114: }
115: } finally {
116: doc.readUnlock();
117: }
118: }
119:
120: final class SimpleFoldManager extends AbstractFoldManager {
121:
122: public void initFolds(FoldHierarchyTransaction transaction) {
123: try {
124: getOperation().addToHierarchy(REGULAR_FOLD_TYPE,
125: "...", // non-null to properly count fold's size (non-null desc gets set) // NOI18N
126: false, FOLD_START_OFFSET_1, FOLD_END_OFFSET_1,
127: 1, 1, null, transaction);
128: } catch (BadLocationException e) {
129: e.printStackTrace();
130: fail();
131: }
132: }
133:
134: }
135:
136: public final class SimpleFoldManagerFactory implements
137: FoldManagerFactory {
138:
139: public FoldManager createFoldManager() {
140: return new SimpleFoldManager();
141: }
142:
143: }
144:
145: private final class FoldMemoryFilter implements MemoryFilter {
146:
147: private Fold fold;
148:
149: FoldMemoryFilter(Fold fold) {
150: this .fold = fold;
151: }
152:
153: public boolean reject(Object o) {
154: return (o == fold.getType())
155: || (o == fold.getDescription()) // requires non-null description during construction
156: || (o == fold.getParent())
157: || (o instanceof FoldOperationImpl)
158: || (o instanceof Position);
159:
160: // Will count possible FoldChildren and ExtraInfo
161: }
162:
163: }
164:
165: }
|