PLSQL/Lexer/test/unit/src/org/netbeans/modules/plsql/lexer/PlsqlBlockFactoryTest.java
author Subhashini Sooriarachchi <subslk@netbeans.org>
Mon, 12 Aug 2013 11:26:54 +0530
changeset 464 e10b2e8563fc
parent 405 f949ce7a3fb1
permissions -rw-r--r--
EADS-3749 encountering issues with the displaying of code in Developer Studio when code folding is enabled
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2011 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 2011 Sun Microsystems, Inc.
    41  */
    42 package org.netbeans.modules.plsql.lexer;
    43 
    44 import java.io.BufferedReader;
    45 import java.io.File;
    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;
    67 
    68 /**
    69  *
    70  * @author YADHLK
    71  */
    72 public class PlsqlBlockFactoryTest extends TestRecordPlayer {
    73 
    74     private static FileSystem fs = null;
    75 
    76     @Before
    77     @Override
    78     public void setUp() {
    79         fs = FileUtil.createMemoryFileSystem();
    80         assertNotNull(fs);
    81     }
    82 
    83     public PlsqlBlockFactoryTest(String name) {
    84         super(name);
    85     }
    86 
    87     @Test
    88     public void testBlocksApy() throws IOException, BadLocationException {
    89         System.out.println("Testing blocks of an APY file");
    90         final String plsqlFileName = "test.apy";
    91 
    92         FileObject fileObject = fs.getRoot().createData(plsqlFileName);
    93         assertNotNull(fileObject);
    94         try {
    95             PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
    96             assertNotNull(blockFac);
    97 
    98             List<PlsqlBlock> plsqlBlocks = blockFac.getBlockHierarchy();
    99             //printHierarchy(lstBlockFac, "");
   100             //generateAssert(lstBlockFac, "lstBlockFac", "false");
   101             assertEquals(7, plsqlBlocks.size());
   102             processBlocks(plsqlFileName, plsqlBlocks);
   103         } finally {
   104             if (fileObject != null) {
   105                 fileObject.delete();
   106             }
   107         }
   108     }
   109 
   110     @Test
   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);
   116         try {
   117             PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
   118             assertNotNull(blockFac);
   119 
   120             List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
   121             //printHierarchy(lstBlockFac, "");
   122             assertEquals(3, lstBlockFac.size());
   123             processBlocks(plsqlFileName, lstBlockFac);
   124         } finally {
   125             if (fileObject != null) {
   126                 fileObject.delete();
   127             }
   128         }
   129     }
   130 
   131     @Test
   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);
   137         try {
   138             PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
   139             assertNotNull(blockFac);
   140 
   141             List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
   142             //printHierarchy(lstBlockFac, "");
   143             assertEquals(4, lstBlockFac.size());
   144             processBlocks(plsqlFileName, lstBlockFac);
   145         } finally {
   146             if (fileObject != null) {
   147                 fileObject.delete();
   148             }
   149         }
   150     }
   151 
   152     @Test
   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);
   158         try {
   159             PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
   160             assertNotNull(blockFac);
   161 
   162             List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
   163             //printHierarchy(lstBlockFac, "");
   164             //generateAssert(lstBlockFac, "lstBlockFac", "false");
   165             assertEquals(17, lstBlockFac.size());
   166             processBlocks(plsqlFileName, lstBlockFac);
   167 
   168         } finally {
   169             if (fileObject != null) {
   170                 fileObject.delete();
   171             }
   172         }
   173     }
   174 
   175     @Test
   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);
   181         try {
   182             PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
   183             assertNotNull(blockFac);
   184 
   185             List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
   186             //printHierarchy(lstBlockFac, "");
   187             //generateAssert(lstBlockFac, "lstBlockFac", "false");
   188             processBlocks(plsqlFileName, lstBlockFac);
   189 
   190         } finally {
   191             if (fileObject != null) {
   192                 fileObject.delete();
   193             }
   194         }
   195     }
   196 
   197     @Test
   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);
   203         try {
   204             PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
   205             assertNotNull(blockFac);
   206 
   207             List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
   208 //         printHierarchy(lstBlockFac, "");
   209 //         generateAssert(lstBlockFac, "lstBlockFac", "false");
   210             assertEquals(lstBlockFac.size(), 5);
   211             processBlocks(plsqlFileName, lstBlockFac);
   212         } finally {
   213             if (fileObject != null) {
   214                 fileObject.delete();
   215             }
   216         }
   217     }
   218 
   219     @Test
   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);
   225         try {
   226             PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
   227             assertNotNull(blockFac);
   228 
   229             List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
   230 //         printHierarchy(lstBlockFac, "");
   231 //         generateAssert(lstBlockFac, "lstBlockFac", "false");
   232             assertEquals(lstBlockFac.size(), 3);
   233             processBlocks(plsqlFileName, lstBlockFac);
   234         } finally {
   235             if (fileObject != null) {
   236                 fileObject.delete();
   237             }
   238         }
   239     }
   240 
   241     @Test
   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);
   247         try {
   248             PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
   249             assertNotNull(blockFac);
   250 
   251             List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
   252 //         printHierarchy(lstBlockFac, "");
   253 //         generateAssert(lstBlockFac, "lstBlockFac", "false");
   254             assertEquals(lstBlockFac.size(), 4);
   255             processBlocks(plsqlFileName, lstBlockFac);
   256 
   257         } finally {
   258             if (fileObject != null) {
   259                 fileObject.delete();
   260             }
   261         }
   262     }
   263 
   264     @Test
   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);
   270         try {
   271             PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
   272             assertNotNull(blockFac);
   273 
   274             List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
   275 //         printHierarchy(lstBlockFac, "");
   276 //         generateAssert(lstBlockFac, "lstBlockFac", "false");
   277             assertEquals(lstBlockFac.size(), 8);
   278             processBlocks(plsqlFileName, lstBlockFac);
   279         } finally {
   280             if (fileObject != null) {
   281                 fileObject.delete();
   282             }
   283         }
   284     }
   285 
   286     @Test
   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);
   292         try {
   293             PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
   294             assertNotNull(blockFac);
   295 
   296             List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
   297 //         printHierarchy(lstBlockFac, "");
   298 //         generateAssert(lstBlockFac, "lstBlockFac", "false");
   299             assertEquals(lstBlockFac.size(), 3);
   300             processBlocks(plsqlFileName, lstBlockFac);
   301         } finally {
   302             if (fileObject != null) {
   303                 fileObject.delete();
   304             }
   305         }
   306     }
   307 
   308     @Test
   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);
   314         try {
   315             PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
   316             assertNotNull(blockFac);
   317 
   318             List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
   319 //         printHierarchy(lstBlockFac, "");
   320 //         generateAssert(lstBlockFac, "lstBlockFac", "false");
   321             assertEquals(lstBlockFac.size(), 8);
   322             processBlocks(plsqlFileName, lstBlockFac);
   323         } finally {
   324             if (fileObject != null) {
   325                 fileObject.delete();
   326             }
   327         }
   328     }
   329 
   330     @Test
   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);
   336         try {
   337             PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
   338             assertNotNull(blockFac);
   339 
   340             List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
   341 //         printHierarchy(lstBlockFac, "");
   342 //         generateAssert(lstBlockFac, "lstBlockFac", "false");
   343             assertEquals(54, lstBlockFac.size());
   344             processBlocks(plsqlFileName, lstBlockFac);
   345         } finally {
   346             if (fileObject != null) {
   347                 fileObject.delete();
   348             }
   349         }
   350     }
   351 
   352     @Test
   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);
   358         try {
   359             PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
   360             assertNotNull(blockFac);
   361 
   362             List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
   363 //         printHierarchy(lstBlockFac, "");
   364 //         generateAssert(lstBlockFac, "lstBlockFac", "false");
   365             assertEquals(11, lstBlockFac.size());
   366             processBlocks(plsqlFileName, lstBlockFac);
   367         } finally {
   368             if (fileObject != null) {
   369                 fileObject.delete();
   370             }
   371         }
   372     }
   373 
   374     @Test
   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);
   380         try {
   381             PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
   382             assertNotNull(blockFac);
   383 
   384             List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
   385 //         printHierarchy(lstBlockFac, "");
   386 //         generateAssert(lstBlockFac, "lstBlockFac", "false");
   387             assertEquals(4, lstBlockFac.size());
   388             processBlocks(plsqlFileName, lstBlockFac);
   389         } finally {
   390             if (fileObject != null) {
   391                 fileObject.delete();
   392             }
   393         }
   394     }
   395 
   396     @Test
   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);
   402         try {
   403             PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
   404             assertNotNull(blockFac);
   405 
   406             List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
   407 //         printHierarchy(lstBlockFac, "");
   408 //         generateAssert(lstBlockFac, "lstBlockFac", "false");
   409             assertEquals(24, lstBlockFac.size());
   410             processBlocks(plsqlFileName, lstBlockFac);
   411         } finally {
   412             if (fileObject != null) {
   413                 fileObject.delete();
   414             }
   415         }
   416     }
   417 
   418     @Test
   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);
   424         try {
   425             PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
   426             assertNotNull(blockFac);
   427 
   428             List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
   429 //         printHierarchy(lstBlockFac, "");
   430 //         generateAssert(lstBlockFac, "lstBlockFac", "false");
   431             assertEquals(11, lstBlockFac.size());
   432             processBlocks(plsqlFileName, lstBlockFac);
   433         } finally {
   434             if (fileObject != null) {
   435                 fileObject.delete();
   436             }
   437         }
   438     }
   439 
   440     @Test
   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);
   446         try {
   447             PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
   448             assertNotNull(blockFac);
   449 
   450             List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
   451 //         printHierarchy(lstBlockFac, "");
   452 //         generateAssert(lstBlockFac, "lstBlockFac", "false");
   453             assertEquals(13, lstBlockFac.size());
   454             processBlocks(plsqlFileName, lstBlockFac);
   455         } finally {
   456             if (fileObject != null) {
   457                 fileObject.delete();
   458             }
   459         }
   460     }
   461 
   462     @Test
   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);
   468         try {
   469             PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
   470             assertNotNull(blockFac);
   471 
   472             List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
   473 //         printHierarchy(lstBlockFac, "");
   474 //         generateAssert(lstBlockFac, "lstBlockFac", "false");
   475             processBlocks(plsqlFileName, lstBlockFac);
   476         } finally {
   477             if (fileObject != null) {
   478                 fileObject.delete();
   479             }
   480         }
   481     }
   482 
   483     @Test
   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);
   489         try {
   490             PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
   491             assertNotNull(blockFac);
   492 
   493             List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
   494 //         printHierarchy(lstBlockFac, "");
   495 //         generateAssert(lstBlockFac, "lstBlockFac", "false");
   496             assertEquals(3, lstBlockFac.size());
   497             processBlocks(plsqlFileName, lstBlockFac);
   498         } finally {
   499             if (fileObject != null) {
   500                 fileObject.delete();
   501             }
   502         }
   503     }
   504 
   505     @Test
   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);
   511         try {
   512             PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
   513             assertNotNull(blockFac);
   514 
   515             List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
   516 //         printHierarchy(lstBlockFac, "");
   517 //         generateAssert(lstBlockFac, "lstBlockFac", "false");
   518             assertEquals(4, lstBlockFac.size());
   519             processBlocks(plsqlFileName, lstBlockFac);
   520         } finally {
   521             if (fileObject != null) {
   522                 fileObject.delete();
   523             }
   524         }
   525     }
   526 
   527     @Test
   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);
   533         try {
   534             PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
   535             assertNotNull(blockFac);
   536 
   537             List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
   538 //         printHierarchy(lstBlockFac, "");
   539 //         generateAssert(lstBlockFac, "lstBlockFac", "false");
   540             assertEquals(4, lstBlockFac.size());
   541             processBlocks(plsqlFileName, lstBlockFac);
   542         } finally {
   543             if (fileObject != null) {
   544                 fileObject.delete();
   545             }
   546         }
   547     }
   548     
   549     @Test
   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);
   555         try {
   556             PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
   557             assertNotNull(blockFac);
   558 
   559             List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
   560 //         printHierarchy(lstBlockFac, "");
   561  //          generateAssert(lstBlockFac, "lstBlockFac", "false");
   562  //          assertEquals(2, lstBlockFac.size());
   563             processBlocks(plsqlFileName, lstBlockFac);
   564         } finally {
   565             if (fileObject != null) {
   566                 fileObject.delete();
   567             }
   568         }
   569     }
   570     
   571     @Test
   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);
   577         try {
   578             PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
   579             assertNotNull(blockFac);
   580 
   581             List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
   582             //printHierarchy(lstBlockFac, "");
   583             //assertEquals(2, lstBlockFac.size());
   584             processBlocks(plsqlFileName, lstBlockFac);
   585 
   586         } finally {
   587             if (fileObject != null) {
   588                 fileObject.delete();
   589             }
   590         }
   591     }
   592     
   593    
   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);
   598         String content = "";
   599         while (input.ready()) {
   600             content = content + input.readLine() + "\n";
   601         }
   602         input.close();
   603         inputStream.close();
   604         indexReader.close();
   605         writeFile(content, fileObj);
   606 
   607         DataObject dataObj = DataFolder.find(fileObj);
   608         EditorCookie ec = dataObj.getLookup().lookup(EditorCookie.class);
   609         assertNotNull(ec);
   610         Task task = ec.prepareDocument();
   611         task.waitFinished();
   612         PlsqlBlockFactory blockFac = dataObj.getLookup().lookup(PlsqlBlockFactory.class);
   613         assertNotNull(blockFac);
   614         Document doc = ec.getDocument();
   615         assertNotNull(doc);
   616         doc.putProperty(Language.class, PlsqlTokenId.language());
   617 
   618         TokenHierarchy tokenHier = TokenHierarchy.get(doc);
   619         assertNotNull(tokenHier);
   620         TokenSequence<PlsqlTokenId> ts = tokenHier.tokenSequence(PlsqlTokenId.language());
   621         assertNotNull(ts);
   622         blockFac.initHierarchy(doc);
   623 
   624         return blockFac;
   625     }
   626 
   627     private void writeFile(String content, FileObject file) throws IOException {
   628         OutputStream os = file.getOutputStream();
   629         os.write(content.getBytes("UTF-8"));
   630         os.close();
   631     }
   632 
   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");
   637         }
   638     }
   639 
   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());
   646 
   647         if (parentExisting) {
   648             assertNotNull(block.getParent());
   649         } else {
   650             assertNull(block.getParent());
   651         }
   652     }
   653 
   654     /**
   655      * Method that generates the assert block statements
   656      *
   657      * @param lstBlockFac
   658      * @param txt
   659      * @param isParent
   660      */
   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");
   667         }
   668     }
   669 
   670     private String toOneLine(String name) {
   671         String tmp = "";
   672         int index = name.indexOf("\n");
   673         if (index == -1) {
   674             return name;
   675         }
   676 
   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");
   682                 if (index == -1) {
   683                     tmp = tmp + name + "\\n";  //\n is added here because we substring in the end
   684                 }
   685             } else {
   686                 break;
   687             }
   688         }
   689 
   690         tmp = tmp.substring(0, tmp.length() - 2);
   691         return tmp;
   692     }
   693 }