PLSQL/Lexer/test/unit/src/org/netbeans/modules/plsql/lexer/PlsqlBlockFactoryTest.java
EADS-3749 encountering issues with the displaying of code in Developer Studio when code folding is enabled
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
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]"
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.
40 * Portions Copyrighted 2011 Sun Microsystems, Inc.
42 package org.netbeans.modules.plsql.lexer;
44 import java.io.BufferedReader;
46 import java.io.FileInputStream;
47 import java.io.IOException;
48 import java.io.InputStream;
49 import java.io.InputStreamReader;
50 import java.io.OutputStream;
51 import java.util.List;
52 import javax.swing.text.BadLocationException;
53 import javax.swing.text.Document;
54 import static org.junit.Assert.*;
55 import org.junit.Before;
56 import org.junit.Test;
57 import org.netbeans.api.lexer.Language;
58 import org.netbeans.api.lexer.TokenHierarchy;
59 import org.netbeans.api.lexer.TokenSequence;
60 import org.openide.cookies.EditorCookie;
61 import org.openide.filesystems.FileObject;
62 import org.openide.filesystems.FileSystem;
63 import org.openide.filesystems.FileUtil;
64 import org.openide.loaders.DataFolder;
65 import org.openide.loaders.DataObject;
66 import org.openide.util.Task;
72 public class PlsqlBlockFactoryTest extends TestRecordPlayer {
74 private static FileSystem fs = null;
79 fs = FileUtil.createMemoryFileSystem();
83 public PlsqlBlockFactoryTest(String name) {
88 public void testBlocksApy() throws IOException, BadLocationException {
89 System.out.println("Testing blocks of an APY file");
90 final String plsqlFileName = "test.apy";
92 FileObject fileObject = fs.getRoot().createData(plsqlFileName);
93 assertNotNull(fileObject);
95 PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
96 assertNotNull(blockFac);
98 List<PlsqlBlock> plsqlBlocks = blockFac.getBlockHierarchy();
99 //printHierarchy(lstBlockFac, "");
100 //generateAssert(lstBlockFac, "lstBlockFac", "false");
101 assertEquals(7, plsqlBlocks.size());
102 processBlocks(plsqlFileName, plsqlBlocks);
104 if (fileObject != null) {
111 public void testBlocksApi() throws IOException, BadLocationException {
112 System.out.println("Testing blocks of an API file");
113 final String plsqlFileName = "test.api";
114 FileObject fileObject = fs.getRoot().createData(plsqlFileName);
115 assertNotNull(fileObject);
117 PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
118 assertNotNull(blockFac);
120 List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
121 //printHierarchy(lstBlockFac, "");
122 assertEquals(3, lstBlockFac.size());
123 processBlocks(plsqlFileName, lstBlockFac);
125 if (fileObject != null) {
132 public void testBlocksUpg() throws IOException, BadLocationException {
133 System.out.println("Testing blocks of an UPG file");
134 final String plsqlFileName = "test.upg";
135 FileObject fileObject = fs.getRoot().createData(plsqlFileName);
136 assertNotNull(fileObject);
138 PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
139 assertNotNull(blockFac);
141 List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
142 //printHierarchy(lstBlockFac, "");
143 assertEquals(4, lstBlockFac.size());
144 processBlocks(plsqlFileName, lstBlockFac);
146 if (fileObject != null) {
153 public void testAdvanced1() throws IOException, BadLocationException {
154 System.out.println("Advanced test case 1");
155 final String plsqlFileName = "test1.apy";
156 FileObject fileObject = fs.getRoot().createData(plsqlFileName);
157 assertNotNull(fileObject);
159 PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
160 assertNotNull(blockFac);
162 List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
163 //printHierarchy(lstBlockFac, "");
164 //generateAssert(lstBlockFac, "lstBlockFac", "false");
165 assertEquals(17, lstBlockFac.size());
166 processBlocks(plsqlFileName, lstBlockFac);
169 if (fileObject != null) {
176 public void testAdvanced2() throws IOException, BadLocationException {
177 System.out.println("Advanced test case 2");
178 final String plsqlFileName = "test2.apy";
179 FileObject fileObject = fs.getRoot().createData(plsqlFileName);
180 assertNotNull(fileObject);
182 PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
183 assertNotNull(blockFac);
185 List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
186 //printHierarchy(lstBlockFac, "");
187 //generateAssert(lstBlockFac, "lstBlockFac", "false");
188 processBlocks(plsqlFileName, lstBlockFac);
191 if (fileObject != null) {
198 public void testAdvanced3() throws IOException, BadLocationException {
199 System.out.println("Advanced test case 3");
200 final String plsqlFileName = "080617_75132_fndbas.cdb";
201 FileObject fileObject = fs.getRoot().createData(plsqlFileName);
202 assertNotNull(fileObject);
204 PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
205 assertNotNull(blockFac);
207 List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
208 // printHierarchy(lstBlockFac, "");
209 // generateAssert(lstBlockFac, "lstBlockFac", "false");
210 assertEquals(lstBlockFac.size(), 5);
211 processBlocks(plsqlFileName, lstBlockFac);
213 if (fileObject != null) {
220 public void testAdvanced4() throws IOException, BadLocationException {
221 System.out.println("Advanced test case 4");
222 final String plsqlFileName = "081114_78488_VMOSFA.cdb";
223 FileObject fileObject = fs.getRoot().createData(plsqlFileName);
224 assertNotNull(fileObject);
226 PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
227 assertNotNull(blockFac);
229 List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
230 // printHierarchy(lstBlockFac, "");
231 // generateAssert(lstBlockFac, "lstBlockFac", "false");
232 assertEquals(lstBlockFac.size(), 3);
233 processBlocks(plsqlFileName, lstBlockFac);
235 if (fileObject != null) {
242 public void testAdvanced5() throws IOException, BadLocationException {
243 System.out.println("Advanced test case 5");
244 final String plsqlFileName = "Dictionary.apy";
245 FileObject fileObject = fs.getRoot().createData(plsqlFileName);
246 assertNotNull(fileObject);
248 PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
249 assertNotNull(blockFac);
251 List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
252 // printHierarchy(lstBlockFac, "");
253 // generateAssert(lstBlockFac, "lstBlockFac", "false");
254 assertEquals(lstBlockFac.size(), 4);
255 processBlocks(plsqlFileName, lstBlockFac);
258 if (fileObject != null) {
265 public void testAdvanced6() throws IOException, BadLocationException {
266 System.out.println("Advanced test case 6");
267 final String plsqlFileName = "FavoriteSchedules.apy";
268 FileObject fileObject = fs.getRoot().createData(plsqlFileName);
269 assertNotNull(fileObject);
271 PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
272 assertNotNull(blockFac);
274 List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
275 // printHierarchy(lstBlockFac, "");
276 // generateAssert(lstBlockFac, "lstBlockFac", "false");
277 assertEquals(lstBlockFac.size(), 8);
278 processBlocks(plsqlFileName, lstBlockFac);
280 if (fileObject != null) {
287 public void testAdvanced7() throws IOException, BadLocationException {
288 System.out.println("Advanced test case 7");
289 final String plsqlFileName = "FndEvent.api";
290 FileObject fileObject = fs.getRoot().createData(plsqlFileName);
291 assertNotNull(fileObject);
293 PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
294 assertNotNull(blockFac);
296 List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
297 // printHierarchy(lstBlockFac, "");
298 // generateAssert(lstBlockFac, "lstBlockFac", "false");
299 assertEquals(lstBlockFac.size(), 3);
300 processBlocks(plsqlFileName, lstBlockFac);
302 if (fileObject != null) {
309 public void testAdvanced8() throws IOException, BadLocationException {
310 System.out.println("Advanced test case 8");
311 final String plsqlFileName = "FndEvent.apy";
312 FileObject fileObject = fs.getRoot().createData(plsqlFileName);
313 assertNotNull(fileObject);
315 PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
316 assertNotNull(blockFac);
318 List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
319 // printHierarchy(lstBlockFac, "");
320 // generateAssert(lstBlockFac, "lstBlockFac", "false");
321 assertEquals(lstBlockFac.size(), 8);
322 processBlocks(plsqlFileName, lstBlockFac);
324 if (fileObject != null) {
331 public void testAdvanced9() throws IOException, BadLocationException {
332 System.out.println("Advanced test case 9");
333 final String plsqlFileName = "IdentityPayInfo.apy";
334 FileObject fileObject = fs.getRoot().createData(plsqlFileName);
335 assertNotNull(fileObject);
337 PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
338 assertNotNull(blockFac);
340 List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
341 // printHierarchy(lstBlockFac, "");
342 // generateAssert(lstBlockFac, "lstBlockFac", "false");
343 assertEquals(54, lstBlockFac.size());
344 processBlocks(plsqlFileName, lstBlockFac);
346 if (fileObject != null) {
353 public void testAdvanced10() throws IOException, BadLocationException {
354 System.out.println("Advanced test case 10");
355 final String plsqlFileName = "InstallationSite.apy";
356 FileObject fileObject = fs.getRoot().createData(plsqlFileName);
357 assertNotNull(fileObject);
359 PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
360 assertNotNull(blockFac);
362 List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
363 // printHierarchy(lstBlockFac, "");
364 // generateAssert(lstBlockFac, "lstBlockFac", "false");
365 assertEquals(11, lstBlockFac.size());
366 processBlocks(plsqlFileName, lstBlockFac);
368 if (fileObject != null) {
375 public void testAdvanced11() throws IOException, BadLocationException {
376 System.out.println("Advanced test case 11");
377 final String plsqlFileName = "OpPersDiaryCalculation.apy";
378 FileObject fileObject = fs.getRoot().createData(plsqlFileName);
379 assertNotNull(fileObject);
381 PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
382 assertNotNull(blockFac);
384 List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
385 // printHierarchy(lstBlockFac, "");
386 // generateAssert(lstBlockFac, "lstBlockFac", "false");
387 assertEquals(4, lstBlockFac.size());
388 processBlocks(plsqlFileName, lstBlockFac);
390 if (fileObject != null) {
397 public void testAdvanced12() throws IOException, BadLocationException {
398 System.out.println("Advanced test case 12");
399 final String plsqlFileName = "ScorecardInputValue.apy";
400 FileObject fileObject = fs.getRoot().createData(plsqlFileName);
401 assertNotNull(fileObject);
403 PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
404 assertNotNull(blockFac);
406 List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
407 // printHierarchy(lstBlockFac, "");
408 // generateAssert(lstBlockFac, "lstBlockFac", "false");
409 assertEquals(24, lstBlockFac.size());
410 processBlocks(plsqlFileName, lstBlockFac);
412 if (fileObject != null) {
419 public void testAdvanced13() throws IOException, BadLocationException {
420 System.out.println("Advanced test case 13");
421 final String plsqlFileName = "Trainer.apy";
422 FileObject fileObject = fs.getRoot().createData(plsqlFileName);
423 assertNotNull(fileObject);
425 PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
426 assertNotNull(blockFac);
428 List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
429 // printHierarchy(lstBlockFac, "");
430 // generateAssert(lstBlockFac, "lstBlockFac", "false");
431 assertEquals(11, lstBlockFac.size());
432 processBlocks(plsqlFileName, lstBlockFac);
434 if (fileObject != null) {
441 public void testAdvanced14() throws IOException, BadLocationException {
442 System.out.println("Advanced test case 14");
443 final String plsqlFileName = "fndbas_ora.cre";
444 FileObject fileObject = fs.getRoot().createData(plsqlFileName);
445 assertNotNull(fileObject);
447 PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
448 assertNotNull(blockFac);
450 List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
451 // printHierarchy(lstBlockFac, "");
452 // generateAssert(lstBlockFac, "lstBlockFac", "false");
453 assertEquals(13, lstBlockFac.size());
454 processBlocks(plsqlFileName, lstBlockFac);
456 if (fileObject != null) {
463 public void testAdvanced15() throws IOException, BadLocationException {
464 System.out.println("Advanced test case 15");
465 final String plsqlFileName = "person.ins";
466 FileObject fileObject = fs.getRoot().createData(plsqlFileName);
467 assertNotNull(fileObject);
469 PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
470 assertNotNull(blockFac);
472 List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
473 // printHierarchy(lstBlockFac, "");
474 // generateAssert(lstBlockFac, "lstBlockFac", "false");
475 processBlocks(plsqlFileName, lstBlockFac);
477 if (fileObject != null) {
484 public void testAdvanced16() throws IOException, BadLocationException {
485 System.out.println("Advanced test case 16");
486 final String plsqlFileName = "sys_IalObjectSlave.api";
487 FileObject fileObject = fs.getRoot().createData(plsqlFileName);
488 assertNotNull(fileObject);
490 PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
491 assertNotNull(blockFac);
493 List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
494 // printHierarchy(lstBlockFac, "");
495 // generateAssert(lstBlockFac, "lstBlockFac", "false");
496 assertEquals(3, lstBlockFac.size());
497 processBlocks(plsqlFileName, lstBlockFac);
499 if (fileObject != null) {
506 public void testAdvanced17() throws IOException, BadLocationException {
507 System.out.println("Advanced test case 17");
508 final String plsqlFileName = "sys_IalObjectSlave.apy";
509 FileObject fileObject = fs.getRoot().createData(plsqlFileName);
510 assertNotNull(fileObject);
512 PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
513 assertNotNull(blockFac);
515 List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
516 // printHierarchy(lstBlockFac, "");
517 // generateAssert(lstBlockFac, "lstBlockFac", "false");
518 assertEquals(4, lstBlockFac.size());
519 processBlocks(plsqlFileName, lstBlockFac);
521 if (fileObject != null) {
528 public void testAdvanced18() throws IOException, BadLocationException {
529 System.out.println("Advanced test case 18");
530 final String plsqlFileName = "txtser.ins";
531 FileObject fileObject = fs.getRoot().createData(plsqlFileName);
532 assertNotNull(fileObject);
534 PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
535 assertNotNull(blockFac);
537 List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
538 // printHierarchy(lstBlockFac, "");
539 // generateAssert(lstBlockFac, "lstBlockFac", "false");
540 assertEquals(4, lstBlockFac.size());
541 processBlocks(plsqlFileName, lstBlockFac);
543 if (fileObject != null) {
550 public void testSimpleCaseStatement() throws IOException, BadLocationException {
551 System.out.println("testSimpleCaseStatement");
552 final String plsqlFileName = "case.upg";
553 FileObject fileObject = fs.getRoot().createData(plsqlFileName);
554 assertNotNull(fileObject);
556 PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
557 assertNotNull(blockFac);
559 List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
560 // printHierarchy(lstBlockFac, "");
561 // generateAssert(lstBlockFac, "lstBlockFac", "false");
562 // assertEquals(2, lstBlockFac.size());
563 processBlocks(plsqlFileName, lstBlockFac);
565 if (fileObject != null) {
572 public void testForComments() throws IOException, BadLocationException {
573 System.out.println("Test for Comment blocks ");
574 final String plsqlFileName = "TestComments.apy";
575 FileObject fileObject = fs.getRoot().createData(plsqlFileName);
576 assertNotNull(fileObject);
578 PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
579 assertNotNull(blockFac);
581 List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
582 //printHierarchy(lstBlockFac, "");
583 //assertEquals(2, lstBlockFac.size());
584 processBlocks(plsqlFileName, lstBlockFac);
587 if (fileObject != null) {
594 private PlsqlBlockFactory loadAsTmpFile(FileObject fileObj, String fileName) throws IOException, BadLocationException {
595 InputStream inputStream = new FileInputStream(new File(getDataDir(), fileName));
596 InputStreamReader indexReader = new InputStreamReader(inputStream);
597 BufferedReader input = new BufferedReader(indexReader);
599 while (input.ready()) {
600 content = content + input.readLine() + "\n";
605 writeFile(content, fileObj);
607 DataObject dataObj = DataFolder.find(fileObj);
608 EditorCookie ec = dataObj.getLookup().lookup(EditorCookie.class);
610 Task task = ec.prepareDocument();
612 PlsqlBlockFactory blockFac = dataObj.getLookup().lookup(PlsqlBlockFactory.class);
613 assertNotNull(blockFac);
614 Document doc = ec.getDocument();
616 doc.putProperty(Language.class, PlsqlTokenId.language());
618 TokenHierarchy tokenHier = TokenHierarchy.get(doc);
619 assertNotNull(tokenHier);
620 TokenSequence<PlsqlTokenId> ts = tokenHier.tokenSequence(PlsqlTokenId.language());
622 blockFac.initHierarchy(doc);
627 private void writeFile(String content, FileObject file) throws IOException {
628 OutputStream os = file.getOutputStream();
629 os.write(content.getBytes("UTF-8"));
633 private void printHierarchy(List<PlsqlBlock> lstBlockFac, String txt) {
634 for (PlsqlBlock block : lstBlockFac) {
635 System.out.println(txt + "Block Name:" + block.getName() + " Type:" + block.getType() + " Start:" + block.getStartOffset() + " End:" + block.getEndOffset() + " Children:" + block.getChildBlocks().size());
636 printHierarchy(block.getChildBlocks(), txt + "\t");
640 private void assertBlock(List<PlsqlBlock> lstBlock, int index, PlsqlBlockType type, String name, boolean parentExisting, int childCount) {
641 PlsqlBlock block = lstBlock.get(index);
642 assertNotNull(block);
643 assertEquals(type, block.getType());
644 assertEquals(name, block.getName());
645 assertEquals(childCount, block.getChildCount());
647 if (parentExisting) {
648 assertNotNull(block.getParent());
650 assertNull(block.getParent());
655 * Method that generates the assert block statements
661 private void generateAssert(List<PlsqlBlock> lstBlockFac, String txt, String isParent) {
662 for (int i = 0; i < lstBlockFac.size(); i++) {
663 PlsqlBlock block = lstBlockFac.get(i);
664 System.out.println("assertBlock(" + txt + ", " + i + ", PlsqlBlockType." + block.getType() + ", \"" + toOneLine(block.getName())
665 + "\", " + isParent + ", " + block.getStartOffset() + ", " + block.getEndOffset() + ", " + block.getChildBlocks().size() + ");");
666 generateAssert(block.getChildBlocks(), txt + ".get(" + i + ").getChildBlocks()", "true");
670 private String toOneLine(String name) {
672 int index = name.indexOf("\n");
677 while (index != -1) {
678 tmp = tmp + name.substring(0, index) + "\\n";
679 if (name.length() > index + 1) {
680 name = name.substring(index + 1);
681 index = name.indexOf("\n");
683 tmp = tmp + name + "\\n"; //\n is added here because we substring in the end
690 tmp = tmp.substring(0, tmp.length() - 2);