Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2013 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2013 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.openide.windows; |
43 |
|
44 |
import java.io.Reader; |
45 |
import static org.junit.Assert.*; |
46 |
import org.junit.Test; |
47 |
import org.openide.util.Lookup; |
48 |
import org.openide.util.lookup.Lookups; |
49 |
|
50 |
/** |
51 |
* |
52 |
* @author jhavlin |
53 |
*/ |
54 |
public class IOFoldingTest { |
55 |
|
56 |
@Test |
57 |
public void testStartFold() { |
58 |
InputOutputWithFolding io = new InputOutputWithFolding(); |
59 |
FoldHandle fold1 = IOFolding.startFold(io, true); |
60 |
assertEquals(1, io.currentLevel); |
61 |
FoldHandle fold2 = fold1.startFold(true); |
62 |
assertEquals(2, io.currentLevel); |
63 |
fold2.finish(); |
64 |
assertEquals(1, io.currentLevel); |
65 |
fold1.finish(); |
66 |
assertEquals(0, io.currentLevel); |
67 |
} |
68 |
|
69 |
@Test(expected = UnsupportedOperationException.class) |
70 |
public void testStartFoldedUnsupported() { |
71 |
FoldHandle f = IOFolding.startFold(new DummyInputOutput(), true); |
72 |
} |
73 |
|
74 |
@Test(expected = IllegalStateException.class) |
75 |
public void testStartFoldIllegal() { |
76 |
InputOutputWithFolding io = new InputOutputWithFolding(); |
77 |
FoldHandle f1 = IOFolding.startFold(io, true); |
78 |
FoldHandle f2 = IOFolding.startFold(io, true); |
79 |
} |
80 |
|
81 |
@Test(expected = IllegalStateException.class) |
82 |
public void testFinishIllegal() { |
83 |
InputOutputWithFolding io = new InputOutputWithFolding(); |
84 |
FoldHandle f1 = IOFolding.startFold(io, true); |
85 |
FoldHandle f2 = f1.startFold(true); |
86 |
f1.finish(); |
87 |
} |
88 |
|
89 |
@Test |
90 |
public void testIsSupported() { |
91 |
assertTrue(IOFolding.isSupported(new InputOutputWithFolding())); |
92 |
} |
93 |
|
94 |
@Test |
95 |
public void testIsSupportedUnsupported() { |
96 |
assertFalse(IOFolding.isSupported(new DummyInputOutput())); |
97 |
} |
98 |
|
99 |
/** |
100 |
* Dummy InputOutput that supports folding. |
101 |
*/ |
102 |
private static class InputOutputWithFolding extends DummyInputOutput |
103 |
implements Lookup.Provider { |
104 |
|
105 |
private final Lookup lookup = Lookups.fixed(new DummyIOFolding()); |
106 |
|
107 |
private DummyIOFolding.DummyFoldHandleDef currentFold = null; |
108 |
private int currentLevel = 0; |
109 |
|
110 |
@Override |
111 |
public Lookup getLookup() { |
112 |
return lookup; |
113 |
} |
114 |
|
115 |
private class DummyIOFolding extends IOFolding { |
116 |
|
117 |
@Override |
118 |
protected FoldHandleDefinition startFold(boolean expanded) { |
119 |
if (currentFold != null) { |
120 |
throw new IllegalStateException("Last fold not finished"); |
121 |
} else { |
122 |
currentFold = new DummyFoldHandleDef(null); |
123 |
currentLevel++; |
124 |
return currentFold; |
125 |
} |
126 |
} |
127 |
|
128 |
private class DummyFoldHandleDef extends FoldHandleDefinition { |
129 |
|
130 |
private DummyFoldHandleDef nested = null; |
131 |
private final DummyFoldHandleDef parent; |
132 |
|
133 |
public DummyFoldHandleDef(DummyFoldHandleDef parent) { |
134 |
this.parent = parent; |
135 |
} |
136 |
|
137 |
@Override |
138 |
public void finish() { |
139 |
if (nested != null) { |
140 |
throw new IllegalStateException("A nested fold exists."); |
141 |
} else if (parent == null) { |
142 |
currentFold = null; |
143 |
} else { |
144 |
parent.nested = null; |
145 |
} |
146 |
currentLevel--; |
147 |
} |
148 |
|
149 |
@Override |
150 |
public FoldHandleDefinition startFold(boolean expanded) { |
151 |
if (nested != null) { |
152 |
throw new IllegalStateException("Last fold not finished."); |
153 |
} else { |
154 |
nested = new DummyFoldHandleDef(this); |
155 |
currentLevel++; |
156 |
return nested; |
157 |
} |
158 |
} |
159 |
|
160 |
@Override |
161 |
public void setExpanded(boolean expanded) { |
162 |
} |
163 |
} |
164 |
} |
165 |
} |
166 |
|
167 |
/** |
168 |
* Dummy implementation of InputOutput. |
169 |
*/ |
170 |
private static class DummyInputOutput implements InputOutput { |
171 |
|
172 |
@Override |
173 |
public OutputWriter getOut() { |
174 |
throw new UnsupportedOperationException("Not supported yet."); |
175 |
} |
176 |
|
177 |
@Override |
178 |
public Reader getIn() { |
179 |
throw new UnsupportedOperationException("Not supported yet."); |
180 |
} |
181 |
|
182 |
@Override |
183 |
public OutputWriter getErr() { |
184 |
throw new UnsupportedOperationException("Not supported yet."); |
185 |
} |
186 |
|
187 |
@Override |
188 |
public void closeInputOutput() { |
189 |
throw new UnsupportedOperationException("Not supported yet."); |
190 |
} |
191 |
|
192 |
@Override |
193 |
public boolean isClosed() { |
194 |
throw new UnsupportedOperationException("Not supported yet."); |
195 |
} |
196 |
|
197 |
@Override |
198 |
public void setOutputVisible(boolean value) { |
199 |
throw new UnsupportedOperationException("Not supported yet."); |
200 |
} |
201 |
|
202 |
@Override |
203 |
public void setErrVisible(boolean value) { |
204 |
throw new UnsupportedOperationException("Not supported yet."); |
205 |
} |
206 |
|
207 |
@Override |
208 |
public void setInputVisible(boolean value) { |
209 |
throw new UnsupportedOperationException("Not supported yet."); |
210 |
} |
211 |
|
212 |
@Override |
213 |
public void select() { |
214 |
throw new UnsupportedOperationException("Not supported yet."); |
215 |
} |
216 |
|
217 |
@Override |
218 |
public boolean isErrSeparated() { |
219 |
throw new UnsupportedOperationException("Not supported yet."); |
220 |
} |
221 |
|
222 |
@Override |
223 |
public void setErrSeparated(boolean value) { |
224 |
throw new UnsupportedOperationException("Not supported yet."); |
225 |
} |
226 |
|
227 |
@Override |
228 |
public boolean isFocusTaken() { |
229 |
throw new UnsupportedOperationException("Not supported yet."); |
230 |
} |
231 |
|
232 |
@Override |
233 |
public void setFocusTaken(boolean value) { |
234 |
throw new UnsupportedOperationException("Not supported yet."); |
235 |
} |
236 |
|
237 |
@Override |
238 |
@Deprecated |
239 |
public Reader flushReader() { |
240 |
throw new UnsupportedOperationException("Not supported yet."); |
241 |
} |
242 |
} |
243 |
} |