PLSQL/Execution/test/unit/src/org/netbeans/modules/plsql/execution/PlsqlExecutableBlocksMakerTest.java
author Subhashini Sooriarachchi <subslk@netbeans.org>
Mon, 12 Aug 2013 11:26:54 +0530
changeset 464 e10b2e8563fc
parent 452 b2411fb23674
permissions -rw-r--r--
EADS-3749 encountering issues with the displaying of code in Developer Studio when code folding is enabled
     1 /*
     2  * To change this template, choose Tools | Templates
     3  * and open the template in the editor.
     4  */
     5 package org.netbeans.modules.plsql.execution;
     6 
     7 import java.io.BufferedReader;
     8 import java.io.File;
     9 import java.io.FileInputStream;
    10 import java.io.IOException;
    11 import java.io.InputStream;
    12 import java.io.InputStreamReader;
    13 import java.io.OutputStream;
    14 import java.util.List;
    15 import javax.swing.text.BadLocationException;
    16 import javax.swing.text.Document;
    17 import org.junit.Test;
    18 import static org.junit.Assert.*;
    19 import org.junit.Before;
    20 import org.netbeans.api.lexer.Language;
    21 import org.netbeans.api.lexer.TokenHierarchy;
    22 import org.netbeans.api.lexer.TokenSequence;
    23 import org.netbeans.junit.NbTestCase;
    24 import org.netbeans.modules.plsql.lexer.PlsqlBlockFactory;
    25 import org.netbeans.modules.plsql.lexer.PlsqlTokenId;
    26 import org.netbeans.modules.plsqlsupport.db.PlsqlExecutableObject;
    27 import org.openide.cookies.EditorCookie;
    28 import org.openide.filesystems.FileObject;
    29 import org.openide.filesystems.FileSystem;
    30 import org.openide.filesystems.FileUtil;
    31 import org.openide.loaders.DataFolder;
    32 import org.openide.loaders.DataObject;
    33 import org.openide.util.Exceptions;
    34 import org.openide.util.Task;
    35 
    36 /**
    37  *
    38  * @author subslk
    39  */
    40 public class PlsqlExecutableBlocksMakerTest extends TestRecordPlayer {
    41 
    42     private static FileSystem fs = null;
    43 
    44     public PlsqlExecutableBlocksMakerTest(String name) {
    45         super(name);
    46     }
    47     
    48     @Before
    49     public void setUp() {
    50         fs = FileUtil.createMemoryFileSystem();
    51         assertNotNull(fs);
    52     }
    53 
    54     /**
    55      * Test of makeExceutableObjects method, of class PlsqlExecutableBlocksMaker.
    56      */
    57     @Test
    58     public void testMakeExceutableObjects() throws IOException {
    59 
    60         System.out.println(" Test Exceutable Objects creation");
    61 
    62         final String plsqlFileName = "Actor.apy";
    63         Document doc = null;
    64         FileObject fileObject = null;
    65         try {
    66             fileObject = fs.getRoot().createData(plsqlFileName);
    67             doc = getDocument(fileObject, plsqlFileName);
    68 
    69             PlsqlExecutableBlocksMaker instance = new PlsqlExecutableBlocksMaker(doc);
    70             List<PlsqlExecutableObject> makeExceutableObjects = instance.makeExceutableObjects();
    71             
    72             int size = makeExceutableObjects.size();           
    73             assertEquals(31, size);
    74             processExecutableBLocksBlocks(plsqlFileName,makeExceutableObjects);
    75             
    76         } catch (BadLocationException ex) {
    77             Exceptions.printStackTrace(ex);
    78         } catch (IOException ex) {
    79             Exceptions.printStackTrace(ex);
    80         } finally {
    81             if (fileObject != null) {
    82                 fileObject.delete();
    83             }
    84         }
    85     }
    86     
    87     @Test
    88     public void test1() throws IOException {
    89 
    90         System.out.println("test 1 - api file");
    91 
    92         final String plsqlFileName = "FndEvent.api";
    93         Document doc = null;
    94         FileObject fileObject = null;
    95         try {
    96             fileObject = fs.getRoot().createData(plsqlFileName);
    97             doc = getDocument(fileObject, plsqlFileName);
    98 
    99             PlsqlExecutableBlocksMaker instance = new PlsqlExecutableBlocksMaker(doc);
   100             List<PlsqlExecutableObject> makeExceutableObjects = instance.makeExceutableObjects();
   101             
   102             int size = makeExceutableObjects.size(); 
   103             assertEquals(12, size);
   104             processExecutableBLocksBlocks(plsqlFileName,makeExceutableObjects);
   105             
   106         } catch (BadLocationException ex) {
   107             Exceptions.printStackTrace(ex);
   108         } catch (IOException ex) {
   109             Exceptions.printStackTrace(ex);
   110         } finally {
   111             if (fileObject != null) {
   112                 fileObject.delete();
   113             }
   114         }
   115     }
   116 
   117     @Test
   118     public void test2() throws IOException {
   119 
   120         System.out.println("test 2 - apy file");
   121 
   122         final String plsqlFileName = "FndEvent.apy";
   123         Document doc = null;
   124         FileObject fileObject = null;
   125         try {
   126             fileObject = fs.getRoot().createData(plsqlFileName);
   127             doc = getDocument(fileObject, plsqlFileName);
   128 
   129             PlsqlExecutableBlocksMaker instance = new PlsqlExecutableBlocksMaker(doc);
   130             List<PlsqlExecutableObject> makeExceutableObjects = instance.makeExceutableObjects();
   131             
   132             int size = makeExceutableObjects.size();           
   133             assertEquals(41, size);
   134             processExecutableBLocksBlocks(plsqlFileName,makeExceutableObjects);
   135             
   136         } catch (BadLocationException ex) {
   137             Exceptions.printStackTrace(ex);
   138         } catch (IOException ex) {
   139             Exceptions.printStackTrace(ex);
   140         } finally {
   141             if (fileObject != null) {
   142                 fileObject.delete();
   143             }
   144         }
   145     }
   146     
   147     @Test
   148     public void test3() throws IOException {
   149 
   150         System.out.println("test 3 - rdf file");
   151 
   152         final String plsqlFileName = "FndSecurityPerObject.rdf";
   153         Document doc = null;
   154         FileObject fileObject = null;
   155         try {
   156             fileObject = fs.getRoot().createData(plsqlFileName);
   157             doc = getDocument(fileObject, plsqlFileName);
   158 
   159             PlsqlExecutableBlocksMaker instance = new PlsqlExecutableBlocksMaker(doc);
   160             List<PlsqlExecutableObject> makeExceutableObjects = instance.makeExceutableObjects();
   161             
   162             int size = makeExceutableObjects.size();           
   163             assertEquals(37, size);
   164             processExecutableBLocksBlocks(plsqlFileName,makeExceutableObjects);
   165             
   166         } catch (BadLocationException ex) {
   167             Exceptions.printStackTrace(ex);
   168         } catch (IOException ex) {
   169             Exceptions.printStackTrace(ex);
   170         } finally {
   171             if (fileObject != null) {
   172                 fileObject.delete();
   173             }
   174         }
   175     }
   176     
   177     @Test
   178     public void test4() throws IOException {
   179 
   180         System.out.println("test 4 - ins file");
   181 
   182         final String plsqlFileName = "FndSetting.ins";
   183         Document doc = null;
   184         FileObject fileObject = null;
   185         try {
   186             fileObject = fs.getRoot().createData(plsqlFileName);
   187             doc = getDocument(fileObject, plsqlFileName);
   188 
   189             PlsqlExecutableBlocksMaker instance = new PlsqlExecutableBlocksMaker(doc);
   190             List<PlsqlExecutableObject> makeExceutableObjects = instance.makeExceutableObjects();
   191             
   192             int size = makeExceutableObjects.size();           
   193             assertEquals(7, size);
   194             processExecutableBLocksBlocks(plsqlFileName,makeExceutableObjects);
   195             
   196         } catch (BadLocationException ex) {
   197             Exceptions.printStackTrace(ex);
   198         } catch (IOException ex) {
   199             Exceptions.printStackTrace(ex);
   200         } finally {
   201             if (fileObject != null) {
   202                 fileObject.delete();
   203             }
   204         }
   205     }
   206     
   207     @Test
   208     public void test5() throws IOException {
   209 
   210         System.out.println("test 5 - sql file");
   211 
   212         final String plsqlFileName = "fndbasdr.sql";
   213         Document doc = null;
   214         FileObject fileObject = null;
   215         try {
   216             fileObject = fs.getRoot().createData(plsqlFileName);
   217             doc = getDocument(fileObject, plsqlFileName);
   218 
   219             PlsqlExecutableBlocksMaker instance = new PlsqlExecutableBlocksMaker(doc);
   220             List<PlsqlExecutableObject> makeExceutableObjects = instance.makeExceutableObjects();
   221             
   222             int size = makeExceutableObjects.size();           
   223             assertEquals(81, size);
   224             processExecutableBLocksBlocks(plsqlFileName,makeExceutableObjects);
   225             
   226         } catch (BadLocationException ex) {
   227             Exceptions.printStackTrace(ex);
   228         } catch (IOException ex) {
   229             Exceptions.printStackTrace(ex);
   230         } finally {
   231             if (fileObject != null) {
   232                 fileObject.delete();
   233             }
   234         }
   235     }
   236     
   237     @Test
   238     public void test6() throws IOException {
   239 
   240         System.out.println("test 6 - cdb file");
   241 
   242         final String plsqlFileName = "event.cdb";
   243         Document doc = null;
   244         FileObject fileObject = null;
   245         try {
   246             fileObject = fs.getRoot().createData(plsqlFileName);
   247             doc = getDocument(fileObject, plsqlFileName);
   248 
   249             PlsqlExecutableBlocksMaker instance = new PlsqlExecutableBlocksMaker(doc);
   250             List<PlsqlExecutableObject> makeExceutableObjects = instance.makeExceutableObjects();
   251             
   252             int size = makeExceutableObjects.size();           
   253             assertEquals(9, size);
   254             processExecutableBLocksBlocks(plsqlFileName,makeExceutableObjects);
   255             
   256         } catch (BadLocationException ex) {
   257             Exceptions.printStackTrace(ex);
   258         } catch (IOException ex) {
   259             Exceptions.printStackTrace(ex);
   260         } finally {
   261             if (fileObject != null) {
   262                 fileObject.delete();
   263             }
   264         }
   265     }
   266     
   267     private Document getDocument(FileObject fileObj, String fileName) throws IOException, BadLocationException {
   268         InputStream inputStream = new FileInputStream(new File(getDataDir(), fileName));
   269         InputStreamReader indexReader = new InputStreamReader(inputStream);
   270         BufferedReader input = new BufferedReader(indexReader);
   271         String content = "";
   272         while (input.ready()) {
   273             content = content + input.readLine() + "\n";
   274         }
   275         input.close();
   276         inputStream.close();
   277         indexReader.close();
   278         writeFile(content, fileObj);
   279 
   280         DataObject dataObj = DataFolder.find(fileObj);
   281         EditorCookie ec = dataObj.getLookup().lookup(EditorCookie.class);
   282         assertNotNull(ec);
   283         Task task = ec.prepareDocument();
   284         task.waitFinished();
   285         PlsqlBlockFactory blockFac = dataObj.getLookup().lookup(PlsqlBlockFactory.class);
   286         assertNotNull(blockFac);
   287         Document doc = ec.getDocument();
   288         assertNotNull(doc);
   289         doc.putProperty(Language.class, PlsqlTokenId.language());
   290 
   291         TokenHierarchy tokenHier = TokenHierarchy.get(doc);
   292         assertNotNull(tokenHier);
   293         TokenSequence<PlsqlTokenId> ts = tokenHier.tokenSequence(PlsqlTokenId.language());
   294         assertNotNull(ts);
   295         blockFac.initHierarchy(doc);
   296 
   297         return doc;
   298 
   299     }
   300 
   301     private void writeFile(String content, FileObject file) throws IOException {
   302         OutputStream os = file.getOutputStream();
   303         os.write(content.getBytes("UTF-8"));
   304         os.close();
   305     }
   306 }