KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > editor > highlighting > support > PositionsBagRandomTest


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.spi.editor.highlighting.support;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.Random JavaDoc;
25 import javax.swing.text.AttributeSet JavaDoc;
26 import javax.swing.text.PlainDocument JavaDoc;
27 import javax.swing.text.Position JavaDoc;
28 import javax.swing.text.SimpleAttributeSet JavaDoc;
29 import org.netbeans.api.editor.settings.AttributesUtilities;
30 import org.netbeans.junit.NbTestCase;
31 import org.netbeans.spi.editor.highlighting.*;
32
33 /**
34  *
35  * @author Vita Stejskal
36  */

37 public class PositionsBagRandomTest extends NbTestCase {
38     
39     private static final int START = 0;
40     private static final int END = 100;
41
42     private final Random JavaDoc RAND = new Random JavaDoc();
43     private String JavaDoc [] layerNames;
44     private HighlightsContainer[] containers;
45     
46     public PositionsBagRandomTest(String JavaDoc testName) {
47         super(testName);
48     }
49
50     protected void setUp() {
51         RAND.setSeed(System.currentTimeMillis());
52
53         layerNames = new String JavaDoc [] {
54             "layer-A",
55             "layer-B",
56             "layer-C",
57         };
58         
59         containers = new HighlightsContainer [layerNames.length];
60         
61         for (int i = 0; i < layerNames.length; i++) {
62             containers[i] = createRandomBag(layerNames[i]);
63         }
64
65     }
66     
67     public void testMerging() {
68         HighlightsContainer composite = mergeContainers(true, layerNames, containers);
69         
70         for (int pointer = START; pointer <= END; pointer++) {
71             String JavaDoc failMsg = null;
72             Highlight [] highestPair = new Highlight [] { null, null };
73             Highlight [] compositePair = new Highlight [] { null, null };
74             
75             try {
76                 highestPair = new Highlight [] { null, null };
77                 compositePair = new Highlight [] { null, null };
78                 
79                 // Find all highlights at the position
80
ArrayList JavaDoc<AttributeSet JavaDoc> leftHighlights = new ArrayList JavaDoc<AttributeSet JavaDoc>();
81                 ArrayList JavaDoc<AttributeSet JavaDoc> rightHighlights = new ArrayList JavaDoc<AttributeSet JavaDoc>();
82                 for (int i = 0; i < containers.length; i++) {
83                     Highlight [] containerPair = findPair(pointer, containers[i].getHighlights(START, END));
84                     if (containerPair[0] != null) {
85                         leftHighlights.add(containerPair[0].getAttributes());
86                     }
87                     if (containerPair[1] != null) {
88                         rightHighlights.add(containerPair[1].getAttributes());
89                     }
90                 }
91                 
92                 if (!leftHighlights.isEmpty()) {
93                     highestPair[0] = new Highlight(pointer, pointer, AttributesUtilities.createComposite(
94                         leftHighlights.toArray(new AttributeSet JavaDoc[leftHighlights.size()])));
95                 }
96                 if (!rightHighlights.isEmpty()) {
97                     highestPair[1] = new Highlight(pointer, pointer, AttributesUtilities.createComposite(
98                         rightHighlights.toArray(new AttributeSet JavaDoc[rightHighlights.size()])));
99                 }
100                 
101                 // Find the composite container highlight at the position
102
compositePair = findPair(pointer, composite.getHighlights(START, END));
103
104                 for (int i = 0; i < 2; i++) {
105                     if (highestPair[i] != null && compositePair[i] != null) {
106                         // Both highlights exist -> check they are the same
107
if (!highestPair[i].getAttributes().isEqual(compositePair[i].getAttributes())) {
108                             failMsg = (i == 0 ? "Left" : "Right") + "pair attributes do not match";
109                         }
110                     } else if (highestPair[i] != null || compositePair[i] != null) {
111                         // Both highlights should be null otherwise they would not match
112
failMsg = (i == 0 ? "Left" : "Right") + " highlight doesn't match";
113                     }
114                 }
115             } catch (Throwable JavaDoc e) {
116                 failMsg = e.getMessage();
117             }
118             
119             if (failMsg != null) {
120                 dumpAll(pointer, layerNames, containers, composite);
121
122                 // Dump the pair that failed
123
System.out.println("highest pair (pos = " + pointer + ") : " + dumpHighlight(highestPair[0]) + ", " + dumpHighlight(highestPair[1]));
124                 System.out.println(" proxy pair (pos = " + pointer + ") : " + dumpHighlight(compositePair[0]) + ", " + dumpHighlight(compositePair[1]));
125                 
126                 fail(failMsg + " (position = " + pointer + ")");
127             }
128         }
129     }
130
131     public void testTrimming() {
132         HighlightsContainer composite = mergeContainers(false, layerNames, containers);
133
134         for (int pointer = START; pointer <= END; pointer++) {
135             String JavaDoc failMsg = null;
136             Highlight [] highestPair = new Highlight [] { null, null };
137             Highlight [] compositePair = new Highlight [] { null, null };
138             
139             try {
140                 highestPair = new Highlight [] { null, null };
141                 compositePair = new Highlight [] { null, null };
142                 
143                 // Find the highest highlight at the position
144
for (int i = containers.length - 1; i >= 0; i--) {
145                     Highlight [] containerPair = findPair(pointer, containers[i].getHighlights(START, END));
146                     if (highestPair[0] == null) {
147                         highestPair[0] = containerPair[0];
148                     }
149                     if (highestPair[1] == null) {
150                         highestPair[1] = containerPair[1];
151                     }
152                 }
153                 
154                 // Find the composite container highlight at the position
155
compositePair = findPair(pointer, composite.getHighlights(START, END));
156
157                 for (int i = 0; i < 2; i++) {
158                     if (highestPair[i] != null && compositePair[i] != null) {
159                         // Both highlights exist -> check they are the same
160
if (!highestPair[i].getAttributes().isEqual(compositePair[i].getAttributes())) {
161                             failMsg = (i == 0 ? "Left" : "Right") + "pair attributes do not match";
162                         }
163                     } else if (highestPair[i] != null || compositePair[i] != null) {
164                         // Both highlights should be null otherwise they would not match
165
failMsg = (i == 0 ? "Left" : "Right") + " highlight doesn't match";
166                     }
167                 }
168             } catch (Throwable JavaDoc e) {
169                 failMsg = e.getMessage();
170             }
171             
172             if (failMsg != null) {
173                 dumpAll(pointer, layerNames, containers, composite);
174
175                 // Dump the pair that failed
176
System.out.println("highest pair (pos = " + pointer + ") : " + dumpHighlight(highestPair[0]) + ", " + dumpHighlight(highestPair[1]));
177                 System.out.println(" proxy pair (pos = " + pointer + ") : " + dumpHighlight(compositePair[0]) + ", " + dumpHighlight(compositePair[1]));
178                 
179                 fail(failMsg + " (position = " + pointer + ")");
180             }
181         }
182     }
183     
184     private Highlight [] findPair(int offset, HighlightsSequence highlights) {
185         Highlight left = null;
186         Highlight right = null;
187         
188         for ( ; highlights.moveNext(); ) {
189             if (highlights.getStartOffset() == highlights.getEndOffset()) {
190                 // ignore empty offsets
191
continue;
192             }
193             
194             if (offset > highlights.getStartOffset() && offset < highlights.getEndOffset()) {
195                 left = right = copyCurrentHighlight(highlights);
196             } else if (offset == highlights.getEndOffset()) {
197                 left = copyCurrentHighlight(highlights);
198             } else if (offset == highlights.getStartOffset()) {
199                 right = copyCurrentHighlight(highlights);
200             }
201         }
202         
203         return new Highlight [] { left, right };
204     }
205
206     private Highlight copyCurrentHighlight(HighlightsSequence iterator) {
207         return new Highlight(
208             iterator.getStartOffset(),
209             iterator.getEndOffset(),
210             iterator.getAttributes()
211         );
212     }
213     
214     private String JavaDoc dumpHighlight(Highlight h) {
215         if (h == null) {
216             return "< , , >";
217         } else {
218             StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
219
220             sb.append("<");
221             sb.append(h.getStartOffset());
222             sb.append(",");
223             sb.append(h.getEndOffset());
224             sb.append(",");
225             
226             Enumeration JavaDoc en = h.getAttributes().getAttributeNames();
227             while (en.hasMoreElements()) {
228                 Object JavaDoc attrName = en.nextElement();
229                 Object JavaDoc attrValue = h.getAttributes().getAttribute(attrName);
230
231                 sb.append("'");
232                 sb.append(attrName.toString());
233                 sb.append("' = '");
234                 sb.append(attrValue == null ? "null" : attrValue.toString());
235                 sb.append("'");
236                 if (en.hasMoreElements()) {
237                     sb.append(", ");
238                 }
239             }
240
241             sb.append(">");
242             
243             return sb.toString();
244         }
245     }
246
247     private void dumpAll(int position, String JavaDoc [] layerNames, HighlightsContainer[] containers, HighlightsContainer composite) {
248         // Dump the layers
249
System.out.println("Dumping containers:");
250         for (int i = 0; i < containers.length; i++) {
251             System.out.println(" containers[" + i + "] " + layerNames[i] + " {");
252             for (HighlightsSequence highlights = containers[i].getHighlights(START, END); highlights.moveNext(); ) {
253                 Highlight h = copyCurrentHighlight(highlights);
254                 System.out.println(" " + dumpHighlight(h));
255             }
256             System.out.println(" } End of containers[" + i + "] " + layerNames[i] + " -------------------------");
257         }
258         System.out.println("Dumping composite container: {");
259         for (HighlightsSequence proxyHighlights = composite.getHighlights(START, END); proxyHighlights.moveNext(); ) {
260             Highlight h = copyCurrentHighlight(proxyHighlights);
261             System.out.println(" " + dumpHighlight(h));
262         }
263         System.out.println("} End of composite container -----------------------");
264     }
265     
266     private PositionsBag createRandomBag(String JavaDoc bagId) {
267
268         PositionsBag bag = new PositionsBag(new PlainDocument JavaDoc(), false);
269         
270         int attrIdx = 0;
271         int startOffset = START;
272         int endOffset = END;
273
274         int maxGapSize = Math.max((int) (endOffset - startOffset) / 10, 1);
275         int maxHighlightSize = Math.max((int) (endOffset - startOffset) / 2, 1);
276
277         for (int pointer = startOffset + RAND.nextInt(maxGapSize); pointer <= endOffset; ) {
278             int highlightSize = RAND.nextInt(maxHighlightSize);
279             SimpleAttributeSet JavaDoc attributes = new SimpleAttributeSet JavaDoc();
280             attributes.addAttribute("AttrName-" + bagId + "-" + attrIdx, "AttrValue");
281             attrIdx++;
282
283             if (pointer + highlightSize < endOffset) {
284                 bag.addHighlight(
285                     new SimplePosition(pointer), new SimplePosition(pointer + highlightSize), attributes);
286             } else {
287                 bag.addHighlight(
288                     new SimplePosition(pointer), new SimplePosition(endOffset), attributes);
289             }
290
291             // move the pointer
292
pointer += highlightSize + RAND.nextInt(maxGapSize);
293         }
294         
295         return bag;
296     }
297
298     private PositionsBag mergeContainers(boolean merge, String JavaDoc [] layerNames, HighlightsContainer[] containers) {
299         PositionsBag bag = new PositionsBag(new PlainDocument JavaDoc(), merge);
300
301         for (int i = 0; i < containers.length; i++) {
302             HighlightsSequence layerHighlights =
303                 containers[i].getHighlights(START, END);
304             
305             for ( ; layerHighlights.moveNext(); ) {
306                 bag.addHighlight(
307                     new SimplePosition(layerHighlights.getStartOffset()),
308                     new SimplePosition(layerHighlights.getEndOffset()),
309                     layerHighlights.getAttributes());
310             }
311         }
312         
313         return bag;
314     }
315     
316     private static final class Highlight {
317         private int startOffset;
318         private int endOffset;
319         private AttributeSet JavaDoc attributes;
320         
321         public Highlight(int startOffset, int endOffset, AttributeSet JavaDoc attributes) {
322             this.startOffset = startOffset;
323             this.endOffset = endOffset;
324             this.attributes = attributes;
325         }
326
327         public int getStartOffset() {
328             return startOffset;
329         }
330
331         public void setStartOffset(int startOffset) {
332             this.startOffset = startOffset;
333         }
334
335         public int getEndOffset() {
336             return endOffset;
337         }
338
339         public void setEndOffset(int endOffset) {
340             this.endOffset = endOffset;
341         }
342
343         public AttributeSet JavaDoc getAttributes() {
344             return attributes;
345         }
346
347         public void setAttributes(AttributeSet JavaDoc attributes) {
348             this.attributes = attributes;
349         }
350         
351     } // End of H class
352

353     private static final class SimplePosition implements Position JavaDoc {
354         private int offset;
355         
356         public SimplePosition(int offset) {
357             this.offset = offset;
358         }
359         
360         public int getOffset() {
361             return offset;
362         }
363     } // End of SimplePosition class
364

365 }
366
Popular Tags