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
jrechtacek@0
     1
/*
jrechtacek@0
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jrechtacek@0
     3
 *
jrechtacek@0
     4
 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
jrechtacek@0
     5
 *
jrechtacek@0
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jrechtacek@0
     7
 * Other names may be trademarks of their respective owners.
jrechtacek@0
     8
 *
jrechtacek@0
     9
 * The contents of this file are subject to the terms of either the GNU
jrechtacek@0
    10
 * General Public License Version 2 only ("GPL") or the Common
jrechtacek@0
    11
 * Development and Distribution License("CDDL") (collectively, the
jrechtacek@0
    12
 * "License"). You may not use this file except in compliance with the
jrechtacek@0
    13
 * License. You can obtain a copy of the License at
jrechtacek@0
    14
 * http://www.netbeans.org/cddl-gplv2.html
jrechtacek@0
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jrechtacek@0
    16
 * specific language governing permissions and limitations under the
jrechtacek@0
    17
 * License.  When distributing the software, include this License Header
jrechtacek@0
    18
 * Notice in each file and include the License file at
jrechtacek@0
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jrechtacek@0
    20
 * particular file as subject to the "Classpath" exception as provided
jrechtacek@0
    21
 * by Oracle in the GPL Version 2 section of the License file that
jrechtacek@0
    22
 * accompanied this code. If applicable, add the following below the
jrechtacek@0
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jrechtacek@0
    24
 * your own identifying information:
jrechtacek@0
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jrechtacek@0
    26
 *
jrechtacek@0
    27
 * If you wish your version of this file to be governed by only the CDDL
jrechtacek@0
    28
 * or only the GPL Version 2, indicate your decision by adding
jrechtacek@0
    29
 * "[Contributor] elects to include this software in this distribution
jrechtacek@0
    30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jrechtacek@0
    31
 * single choice of license, a recipient has the option to distribute
jrechtacek@0
    32
 * your version of this file under either the CDDL, the GPL Version 2 or
jrechtacek@0
    33
 * to extend the choice of license to its licensees as provided above.
jrechtacek@0
    34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jrechtacek@0
    35
 * Version 2 license, then the option applies only if the new code is
jrechtacek@0
    36
 * made subject to such option by the copyright holder.
jrechtacek@0
    37
 *
jrechtacek@0
    38
 * Contributor(s):
jrechtacek@0
    39
 *
jrechtacek@0
    40
 * Portions Copyrighted 2011 Sun Microsystems, Inc.
jrechtacek@0
    41
 */
jrechtacek@0
    42
package org.netbeans.modules.plsql.lexer;
jrechtacek@0
    43
jrechtacek@0
    44
import java.io.BufferedReader;
chrislovsund@399
    45
import java.io.File;
chrislovsund@399
    46
import java.io.FileInputStream;
jrechtacek@0
    47
import java.io.IOException;
jrechtacek@0
    48
import java.io.InputStream;
jrechtacek@0
    49
import java.io.InputStreamReader;
jrechtacek@0
    50
import java.io.OutputStream;
jrechtacek@0
    51
import java.util.List;
jrechtacek@0
    52
import javax.swing.text.BadLocationException;
jrechtacek@0
    53
import javax.swing.text.Document;
chrislovsund@399
    54
import static org.junit.Assert.*;
jrechtacek@0
    55
import org.junit.Before;
jrechtacek@0
    56
import org.junit.Test;
jrechtacek@0
    57
import org.netbeans.api.lexer.Language;
jrechtacek@0
    58
import org.netbeans.api.lexer.TokenHierarchy;
jrechtacek@0
    59
import org.netbeans.api.lexer.TokenSequence;
jrechtacek@0
    60
import org.openide.cookies.EditorCookie;
jrechtacek@0
    61
import org.openide.filesystems.FileObject;
jrechtacek@0
    62
import org.openide.filesystems.FileSystem;
jrechtacek@0
    63
import org.openide.filesystems.FileUtil;
jrechtacek@0
    64
import org.openide.loaders.DataFolder;
jrechtacek@0
    65
import org.openide.loaders.DataObject;
jrechtacek@0
    66
import org.openide.util.Task;
jrechtacek@0
    67
jrechtacek@0
    68
/**
jrechtacek@0
    69
 *
jrechtacek@0
    70
 * @author YADHLK
jrechtacek@0
    71
 */
chrislovsund@399
    72
public class PlsqlBlockFactoryTest extends TestRecordPlayer {
jrechtacek@0
    73
chrislovsund@371
    74
    private static FileSystem fs = null;
jrechtacek@0
    75
chrislovsund@399
    76
    @Before
chrislovsund@399
    77
    @Override
chrislovsund@399
    78
    public void setUp() {
chrislovsund@371
    79
        fs = FileUtil.createMemoryFileSystem();
chrislovsund@371
    80
        assertNotNull(fs);
chrislovsund@371
    81
    }
jrechtacek@0
    82
chrislovsund@399
    83
    public PlsqlBlockFactoryTest(String name) {
chrislovsund@399
    84
        super(name);
chrislovsund@371
    85
    }
jrechtacek@0
    86
chrislovsund@371
    87
    @Test
chrislovsund@371
    88
    public void testBlocksApy() throws IOException, BadLocationException {
chrislovsund@371
    89
        System.out.println("Testing blocks of an APY file");
chrislovsund@399
    90
        final String plsqlFileName = "test.apy";
chrislovsund@399
    91
chrislovsund@399
    92
        FileObject fileObject = fs.getRoot().createData(plsqlFileName);
chrislovsund@371
    93
        assertNotNull(fileObject);
chrislovsund@371
    94
        try {
chrislovsund@399
    95
            PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
chrislovsund@371
    96
            assertNotNull(blockFac);
jrechtacek@0
    97
chrislovsund@399
    98
            List<PlsqlBlock> plsqlBlocks = blockFac.getBlockHierarchy();
chrislovsund@371
    99
            //printHierarchy(lstBlockFac, "");
chrislovsund@371
   100
            //generateAssert(lstBlockFac, "lstBlockFac", "false");
chrislovsund@399
   101
            assertEquals(7, plsqlBlocks.size());
chrislovsund@399
   102
            processBlocks(plsqlFileName, plsqlBlocks);
chrislovsund@371
   103
        } finally {
chrislovsund@371
   104
            if (fileObject != null) {
chrislovsund@371
   105
                fileObject.delete();
chrislovsund@371
   106
            }
chrislovsund@371
   107
        }
chrislovsund@371
   108
    }
jrechtacek@0
   109
chrislovsund@371
   110
    @Test
chrislovsund@371
   111
    public void testBlocksApi() throws IOException, BadLocationException {
chrislovsund@371
   112
        System.out.println("Testing blocks of an API file");
chrislovsund@399
   113
        final String plsqlFileName = "test.api";
chrislovsund@399
   114
        FileObject fileObject = fs.getRoot().createData(plsqlFileName);
chrislovsund@371
   115
        assertNotNull(fileObject);
chrislovsund@371
   116
        try {
chrislovsund@399
   117
            PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
chrislovsund@371
   118
            assertNotNull(blockFac);
jrechtacek@0
   119
chrislovsund@371
   120
            List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
chrislovsund@371
   121
            //printHierarchy(lstBlockFac, "");
chrislovsund@399
   122
            assertEquals(3, lstBlockFac.size());
chrislovsund@399
   123
            processBlocks(plsqlFileName, lstBlockFac);
chrislovsund@371
   124
        } finally {
chrislovsund@371
   125
            if (fileObject != null) {
chrislovsund@371
   126
                fileObject.delete();
chrislovsund@371
   127
            }
chrislovsund@371
   128
        }
chrislovsund@371
   129
    }
jrechtacek@0
   130
chrislovsund@371
   131
    @Test
chrislovsund@371
   132
    public void testBlocksUpg() throws IOException, BadLocationException {
chrislovsund@371
   133
        System.out.println("Testing blocks of an UPG file");
chrislovsund@399
   134
        final String plsqlFileName = "test.upg";
chrislovsund@399
   135
        FileObject fileObject = fs.getRoot().createData(plsqlFileName);
chrislovsund@371
   136
        assertNotNull(fileObject);
chrislovsund@371
   137
        try {
chrislovsund@399
   138
            PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
chrislovsund@371
   139
            assertNotNull(blockFac);
jrechtacek@0
   140
chrislovsund@371
   141
            List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
chrislovsund@371
   142
            //printHierarchy(lstBlockFac, "");
chrislovsund@399
   143
            assertEquals(4, lstBlockFac.size());
chrislovsund@399
   144
            processBlocks(plsqlFileName, lstBlockFac);
chrislovsund@371
   145
        } finally {
chrislovsund@371
   146
            if (fileObject != null) {
chrislovsund@371
   147
                fileObject.delete();
chrislovsund@371
   148
            }
chrislovsund@371
   149
        }
chrislovsund@371
   150
    }
jrechtacek@0
   151
chrislovsund@371
   152
    @Test
chrislovsund@371
   153
    public void testAdvanced1() throws IOException, BadLocationException {
chrislovsund@371
   154
        System.out.println("Advanced test case 1");
chrislovsund@399
   155
        final String plsqlFileName = "test1.apy";
chrislovsund@399
   156
        FileObject fileObject = fs.getRoot().createData(plsqlFileName);
chrislovsund@371
   157
        assertNotNull(fileObject);
chrislovsund@371
   158
        try {
chrislovsund@399
   159
            PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
chrislovsund@371
   160
            assertNotNull(blockFac);
jrechtacek@0
   161
chrislovsund@371
   162
            List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
chrislovsund@371
   163
            //printHierarchy(lstBlockFac, "");
chrislovsund@371
   164
            //generateAssert(lstBlockFac, "lstBlockFac", "false");
chrislovsund@399
   165
            assertEquals(17, lstBlockFac.size());
chrislovsund@399
   166
            processBlocks(plsqlFileName, lstBlockFac);
chrislovsund@399
   167
chrislovsund@371
   168
        } finally {
chrislovsund@371
   169
            if (fileObject != null) {
chrislovsund@371
   170
                fileObject.delete();
chrislovsund@371
   171
            }
chrislovsund@371
   172
        }
chrislovsund@371
   173
    }
jrechtacek@0
   174
chrislovsund@371
   175
    @Test
chrislovsund@371
   176
    public void testAdvanced2() throws IOException, BadLocationException {
chrislovsund@371
   177
        System.out.println("Advanced test case 2");
chrislovsund@399
   178
        final String plsqlFileName = "test2.apy";
chrislovsund@399
   179
        FileObject fileObject = fs.getRoot().createData(plsqlFileName);
chrislovsund@371
   180
        assertNotNull(fileObject);
chrislovsund@371
   181
        try {
chrislovsund@399
   182
            PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
chrislovsund@371
   183
            assertNotNull(blockFac);
jrechtacek@0
   184
chrislovsund@371
   185
            List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
chrislovsund@371
   186
            //printHierarchy(lstBlockFac, "");
chrislovsund@371
   187
            //generateAssert(lstBlockFac, "lstBlockFac", "false");
chrislovsund@399
   188
            processBlocks(plsqlFileName, lstBlockFac);
chrislovsund@399
   189
chrislovsund@371
   190
        } finally {
chrislovsund@371
   191
            if (fileObject != null) {
chrislovsund@371
   192
                fileObject.delete();
chrislovsund@371
   193
            }
chrislovsund@371
   194
        }
chrislovsund@371
   195
    }
jrechtacek@0
   196
chrislovsund@371
   197
    @Test
chrislovsund@371
   198
    public void testAdvanced3() throws IOException, BadLocationException {
chrislovsund@371
   199
        System.out.println("Advanced test case 3");
chrislovsund@399
   200
        final String plsqlFileName = "080617_75132_fndbas.cdb";
chrislovsund@399
   201
        FileObject fileObject = fs.getRoot().createData(plsqlFileName);
chrislovsund@371
   202
        assertNotNull(fileObject);
chrislovsund@371
   203
        try {
chrislovsund@399
   204
            PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
chrislovsund@371
   205
            assertNotNull(blockFac);
jrechtacek@0
   206
chrislovsund@371
   207
            List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
jrechtacek@0
   208
//         printHierarchy(lstBlockFac, "");
jrechtacek@0
   209
//         generateAssert(lstBlockFac, "lstBlockFac", "false");
chrislovsund@399
   210
            assertEquals(lstBlockFac.size(), 5);
chrislovsund@399
   211
            processBlocks(plsqlFileName, lstBlockFac);
chrislovsund@371
   212
        } finally {
chrislovsund@371
   213
            if (fileObject != null) {
chrislovsund@371
   214
                fileObject.delete();
chrislovsund@371
   215
            }
chrislovsund@371
   216
        }
chrislovsund@371
   217
    }
jrechtacek@0
   218
chrislovsund@371
   219
    @Test
chrislovsund@371
   220
    public void testAdvanced4() throws IOException, BadLocationException {
chrislovsund@371
   221
        System.out.println("Advanced test case 4");
chrislovsund@399
   222
        final String plsqlFileName = "081114_78488_VMOSFA.cdb";
chrislovsund@399
   223
        FileObject fileObject = fs.getRoot().createData(plsqlFileName);
chrislovsund@371
   224
        assertNotNull(fileObject);
chrislovsund@371
   225
        try {
chrislovsund@399
   226
            PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
chrislovsund@371
   227
            assertNotNull(blockFac);
jrechtacek@0
   228
chrislovsund@371
   229
            List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
jrechtacek@0
   230
//         printHierarchy(lstBlockFac, "");
jrechtacek@0
   231
//         generateAssert(lstBlockFac, "lstBlockFac", "false");
chrislovsund@399
   232
            assertEquals(lstBlockFac.size(), 3);
chrislovsund@399
   233
            processBlocks(plsqlFileName, lstBlockFac);
chrislovsund@371
   234
        } finally {
chrislovsund@371
   235
            if (fileObject != null) {
chrislovsund@371
   236
                fileObject.delete();
chrislovsund@371
   237
            }
chrislovsund@371
   238
        }
chrislovsund@371
   239
    }
jrechtacek@0
   240
chrislovsund@371
   241
    @Test
chrislovsund@371
   242
    public void testAdvanced5() throws IOException, BadLocationException {
chrislovsund@371
   243
        System.out.println("Advanced test case 5");
chrislovsund@399
   244
        final String plsqlFileName = "Dictionary.apy";
chrislovsund@399
   245
        FileObject fileObject = fs.getRoot().createData(plsqlFileName);
chrislovsund@371
   246
        assertNotNull(fileObject);
chrislovsund@371
   247
        try {
chrislovsund@399
   248
            PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
chrislovsund@371
   249
            assertNotNull(blockFac);
jrechtacek@0
   250
chrislovsund@371
   251
            List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
jrechtacek@0
   252
//         printHierarchy(lstBlockFac, "");
jrechtacek@0
   253
//         generateAssert(lstBlockFac, "lstBlockFac", "false");
chrislovsund@399
   254
            assertEquals(lstBlockFac.size(), 4);
chrislovsund@399
   255
            processBlocks(plsqlFileName, lstBlockFac);
chrislovsund@399
   256
chrislovsund@371
   257
        } finally {
chrislovsund@371
   258
            if (fileObject != null) {
chrislovsund@371
   259
                fileObject.delete();
chrislovsund@371
   260
            }
chrislovsund@371
   261
        }
chrislovsund@371
   262
    }
jrechtacek@0
   263
chrislovsund@371
   264
    @Test
chrislovsund@371
   265
    public void testAdvanced6() throws IOException, BadLocationException {
chrislovsund@371
   266
        System.out.println("Advanced test case 6");
chrislovsund@399
   267
        final String plsqlFileName = "FavoriteSchedules.apy";
chrislovsund@399
   268
        FileObject fileObject = fs.getRoot().createData(plsqlFileName);
chrislovsund@371
   269
        assertNotNull(fileObject);
chrislovsund@371
   270
        try {
chrislovsund@399
   271
            PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
chrislovsund@371
   272
            assertNotNull(blockFac);
jrechtacek@0
   273
chrislovsund@371
   274
            List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
jrechtacek@0
   275
//         printHierarchy(lstBlockFac, "");
jrechtacek@0
   276
//         generateAssert(lstBlockFac, "lstBlockFac", "false");
chrislovsund@399
   277
            assertEquals(lstBlockFac.size(), 8);
chrislovsund@399
   278
            processBlocks(plsqlFileName, lstBlockFac);
chrislovsund@371
   279
        } finally {
chrislovsund@371
   280
            if (fileObject != null) {
chrislovsund@371
   281
                fileObject.delete();
chrislovsund@371
   282
            }
chrislovsund@371
   283
        }
chrislovsund@371
   284
    }
jrechtacek@0
   285
chrislovsund@371
   286
    @Test
chrislovsund@371
   287
    public void testAdvanced7() throws IOException, BadLocationException {
chrislovsund@371
   288
        System.out.println("Advanced test case 7");
chrislovsund@399
   289
        final String plsqlFileName = "FndEvent.api";
chrislovsund@399
   290
        FileObject fileObject = fs.getRoot().createData(plsqlFileName);
chrislovsund@371
   291
        assertNotNull(fileObject);
chrislovsund@371
   292
        try {
chrislovsund@399
   293
            PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
chrislovsund@371
   294
            assertNotNull(blockFac);
jrechtacek@0
   295
chrislovsund@371
   296
            List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
jrechtacek@0
   297
//         printHierarchy(lstBlockFac, "");
jrechtacek@0
   298
//         generateAssert(lstBlockFac, "lstBlockFac", "false");
chrislovsund@399
   299
            assertEquals(lstBlockFac.size(), 3);
chrislovsund@399
   300
            processBlocks(plsqlFileName, lstBlockFac);
chrislovsund@371
   301
        } finally {
chrislovsund@371
   302
            if (fileObject != null) {
chrislovsund@371
   303
                fileObject.delete();
chrislovsund@371
   304
            }
chrislovsund@371
   305
        }
chrislovsund@371
   306
    }
jrechtacek@0
   307
chrislovsund@371
   308
    @Test
chrislovsund@371
   309
    public void testAdvanced8() throws IOException, BadLocationException {
chrislovsund@371
   310
        System.out.println("Advanced test case 8");
chrislovsund@399
   311
        final String plsqlFileName = "FndEvent.apy";
chrislovsund@399
   312
        FileObject fileObject = fs.getRoot().createData(plsqlFileName);
chrislovsund@371
   313
        assertNotNull(fileObject);
chrislovsund@371
   314
        try {
chrislovsund@399
   315
            PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
chrislovsund@371
   316
            assertNotNull(blockFac);
jrechtacek@0
   317
chrislovsund@371
   318
            List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
jrechtacek@0
   319
//         printHierarchy(lstBlockFac, "");
jrechtacek@0
   320
//         generateAssert(lstBlockFac, "lstBlockFac", "false");
chrislovsund@399
   321
            assertEquals(lstBlockFac.size(), 8);
chrislovsund@399
   322
            processBlocks(plsqlFileName, lstBlockFac);
chrislovsund@371
   323
        } finally {
chrislovsund@371
   324
            if (fileObject != null) {
chrislovsund@371
   325
                fileObject.delete();
chrislovsund@371
   326
            }
chrislovsund@371
   327
        }
chrislovsund@371
   328
    }
jrechtacek@0
   329
chrislovsund@371
   330
    @Test
chrislovsund@371
   331
    public void testAdvanced9() throws IOException, BadLocationException {
chrislovsund@371
   332
        System.out.println("Advanced test case 9");
chrislovsund@399
   333
        final String plsqlFileName = "IdentityPayInfo.apy";
chrislovsund@399
   334
        FileObject fileObject = fs.getRoot().createData(plsqlFileName);
chrislovsund@371
   335
        assertNotNull(fileObject);
chrislovsund@371
   336
        try {
chrislovsund@399
   337
            PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
chrislovsund@371
   338
            assertNotNull(blockFac);
jrechtacek@0
   339
chrislovsund@371
   340
            List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
jrechtacek@0
   341
//         printHierarchy(lstBlockFac, "");
jrechtacek@0
   342
//         generateAssert(lstBlockFac, "lstBlockFac", "false");
chrislovsund@399
   343
            assertEquals(54, lstBlockFac.size());
chrislovsund@399
   344
            processBlocks(plsqlFileName, lstBlockFac);
chrislovsund@371
   345
        } finally {
chrislovsund@371
   346
            if (fileObject != null) {
chrislovsund@371
   347
                fileObject.delete();
chrislovsund@371
   348
            }
chrislovsund@371
   349
        }
chrislovsund@371
   350
    }
jrechtacek@0
   351
chrislovsund@371
   352
    @Test
chrislovsund@371
   353
    public void testAdvanced10() throws IOException, BadLocationException {
chrislovsund@371
   354
        System.out.println("Advanced test case 10");
chrislovsund@399
   355
        final String plsqlFileName = "InstallationSite.apy";
chrislovsund@399
   356
        FileObject fileObject = fs.getRoot().createData(plsqlFileName);
chrislovsund@371
   357
        assertNotNull(fileObject);
chrislovsund@371
   358
        try {
chrislovsund@399
   359
            PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
chrislovsund@371
   360
            assertNotNull(blockFac);
jrechtacek@0
   361
chrislovsund@371
   362
            List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
jrechtacek@0
   363
//         printHierarchy(lstBlockFac, "");
jrechtacek@0
   364
//         generateAssert(lstBlockFac, "lstBlockFac", "false");
chrislovsund@399
   365
            assertEquals(11, lstBlockFac.size());
chrislovsund@399
   366
            processBlocks(plsqlFileName, lstBlockFac);
chrislovsund@371
   367
        } finally {
chrislovsund@371
   368
            if (fileObject != null) {
chrislovsund@371
   369
                fileObject.delete();
chrislovsund@371
   370
            }
chrislovsund@371
   371
        }
chrislovsund@371
   372
    }
jrechtacek@0
   373
chrislovsund@371
   374
    @Test
chrislovsund@371
   375
    public void testAdvanced11() throws IOException, BadLocationException {
chrislovsund@371
   376
        System.out.println("Advanced test case 11");
chrislovsund@399
   377
        final String plsqlFileName = "OpPersDiaryCalculation.apy";
chrislovsund@399
   378
        FileObject fileObject = fs.getRoot().createData(plsqlFileName);
chrislovsund@371
   379
        assertNotNull(fileObject);
chrislovsund@371
   380
        try {
chrislovsund@399
   381
            PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
chrislovsund@371
   382
            assertNotNull(blockFac);
jrechtacek@0
   383
chrislovsund@371
   384
            List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
jrechtacek@0
   385
//         printHierarchy(lstBlockFac, "");
jrechtacek@0
   386
//         generateAssert(lstBlockFac, "lstBlockFac", "false");
chrislovsund@399
   387
            assertEquals(4, lstBlockFac.size());
chrislovsund@399
   388
            processBlocks(plsqlFileName, lstBlockFac);
chrislovsund@371
   389
        } finally {
chrislovsund@371
   390
            if (fileObject != null) {
chrislovsund@371
   391
                fileObject.delete();
chrislovsund@371
   392
            }
chrislovsund@371
   393
        }
chrislovsund@371
   394
    }
jrechtacek@0
   395
chrislovsund@371
   396
    @Test
chrislovsund@371
   397
    public void testAdvanced12() throws IOException, BadLocationException {
chrislovsund@371
   398
        System.out.println("Advanced test case 12");
chrislovsund@399
   399
        final String plsqlFileName = "ScorecardInputValue.apy";
chrislovsund@399
   400
        FileObject fileObject = fs.getRoot().createData(plsqlFileName);
chrislovsund@371
   401
        assertNotNull(fileObject);
chrislovsund@371
   402
        try {
chrislovsund@399
   403
            PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
chrislovsund@371
   404
            assertNotNull(blockFac);
jrechtacek@0
   405
chrislovsund@371
   406
            List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
jrechtacek@0
   407
//         printHierarchy(lstBlockFac, "");
jrechtacek@0
   408
//         generateAssert(lstBlockFac, "lstBlockFac", "false");
chrislovsund@399
   409
            assertEquals(24, lstBlockFac.size());
chrislovsund@399
   410
            processBlocks(plsqlFileName, lstBlockFac);
chrislovsund@371
   411
        } finally {
chrislovsund@371
   412
            if (fileObject != null) {
chrislovsund@371
   413
                fileObject.delete();
chrislovsund@371
   414
            }
chrislovsund@371
   415
        }
chrislovsund@371
   416
    }
jrechtacek@0
   417
chrislovsund@371
   418
    @Test
chrislovsund@371
   419
    public void testAdvanced13() throws IOException, BadLocationException {
chrislovsund@371
   420
        System.out.println("Advanced test case 13");
chrislovsund@399
   421
        final String plsqlFileName = "Trainer.apy";
chrislovsund@399
   422
        FileObject fileObject = fs.getRoot().createData(plsqlFileName);
chrislovsund@371
   423
        assertNotNull(fileObject);
chrislovsund@371
   424
        try {
chrislovsund@399
   425
            PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
chrislovsund@371
   426
            assertNotNull(blockFac);
jrechtacek@0
   427
chrislovsund@371
   428
            List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
jrechtacek@0
   429
//         printHierarchy(lstBlockFac, "");
jrechtacek@0
   430
//         generateAssert(lstBlockFac, "lstBlockFac", "false");
chrislovsund@399
   431
            assertEquals(11, lstBlockFac.size());
chrislovsund@399
   432
            processBlocks(plsqlFileName, lstBlockFac);
chrislovsund@371
   433
        } finally {
chrislovsund@371
   434
            if (fileObject != null) {
chrislovsund@371
   435
                fileObject.delete();
chrislovsund@371
   436
            }
chrislovsund@371
   437
        }
chrislovsund@371
   438
    }
jrechtacek@0
   439
chrislovsund@371
   440
    @Test
chrislovsund@371
   441
    public void testAdvanced14() throws IOException, BadLocationException {
chrislovsund@371
   442
        System.out.println("Advanced test case 14");
chrislovsund@399
   443
        final String plsqlFileName = "fndbas_ora.cre";
chrislovsund@399
   444
        FileObject fileObject = fs.getRoot().createData(plsqlFileName);
chrislovsund@371
   445
        assertNotNull(fileObject);
chrislovsund@371
   446
        try {
chrislovsund@399
   447
            PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
chrislovsund@371
   448
            assertNotNull(blockFac);
jrechtacek@0
   449
chrislovsund@371
   450
            List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
jrechtacek@0
   451
//         printHierarchy(lstBlockFac, "");
jrechtacek@0
   452
//         generateAssert(lstBlockFac, "lstBlockFac", "false");
chrislovsund@399
   453
            assertEquals(13, lstBlockFac.size());
chrislovsund@399
   454
            processBlocks(plsqlFileName, lstBlockFac);
chrislovsund@371
   455
        } finally {
chrislovsund@371
   456
            if (fileObject != null) {
chrislovsund@371
   457
                fileObject.delete();
chrislovsund@371
   458
            }
chrislovsund@371
   459
        }
chrislovsund@371
   460
    }
jrechtacek@0
   461
chrislovsund@371
   462
    @Test
chrislovsund@371
   463
    public void testAdvanced15() throws IOException, BadLocationException {
chrislovsund@371
   464
        System.out.println("Advanced test case 15");
chrislovsund@399
   465
        final String plsqlFileName = "person.ins";
chrislovsund@399
   466
        FileObject fileObject = fs.getRoot().createData(plsqlFileName);
chrislovsund@371
   467
        assertNotNull(fileObject);
chrislovsund@371
   468
        try {
chrislovsund@399
   469
            PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
chrislovsund@371
   470
            assertNotNull(blockFac);
jrechtacek@0
   471
chrislovsund@371
   472
            List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
jrechtacek@0
   473
//         printHierarchy(lstBlockFac, "");
jrechtacek@0
   474
//         generateAssert(lstBlockFac, "lstBlockFac", "false");
chrislovsund@399
   475
            processBlocks(plsqlFileName, lstBlockFac);
chrislovsund@371
   476
        } finally {
chrislovsund@371
   477
            if (fileObject != null) {
chrislovsund@371
   478
                fileObject.delete();
chrislovsund@371
   479
            }
chrislovsund@371
   480
        }
chrislovsund@371
   481
    }
jrechtacek@0
   482
chrislovsund@371
   483
    @Test
chrislovsund@371
   484
    public void testAdvanced16() throws IOException, BadLocationException {
chrislovsund@371
   485
        System.out.println("Advanced test case 16");
chrislovsund@399
   486
        final String plsqlFileName = "sys_IalObjectSlave.api";
chrislovsund@399
   487
        FileObject fileObject = fs.getRoot().createData(plsqlFileName);
chrislovsund@371
   488
        assertNotNull(fileObject);
chrislovsund@371
   489
        try {
chrislovsund@399
   490
            PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
chrislovsund@371
   491
            assertNotNull(blockFac);
jrechtacek@0
   492
chrislovsund@371
   493
            List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
jrechtacek@0
   494
//         printHierarchy(lstBlockFac, "");
jrechtacek@0
   495
//         generateAssert(lstBlockFac, "lstBlockFac", "false");
chrislovsund@399
   496
            assertEquals(3, lstBlockFac.size());
chrislovsund@399
   497
            processBlocks(plsqlFileName, lstBlockFac);
chrislovsund@371
   498
        } finally {
chrislovsund@371
   499
            if (fileObject != null) {
chrislovsund@371
   500
                fileObject.delete();
chrislovsund@371
   501
            }
chrislovsund@371
   502
        }
chrislovsund@371
   503
    }
jrechtacek@0
   504
chrislovsund@371
   505
    @Test
chrislovsund@371
   506
    public void testAdvanced17() throws IOException, BadLocationException {
chrislovsund@371
   507
        System.out.println("Advanced test case 17");
chrislovsund@399
   508
        final String plsqlFileName = "sys_IalObjectSlave.apy";
chrislovsund@399
   509
        FileObject fileObject = fs.getRoot().createData(plsqlFileName);
chrislovsund@371
   510
        assertNotNull(fileObject);
chrislovsund@371
   511
        try {
chrislovsund@399
   512
            PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
chrislovsund@371
   513
            assertNotNull(blockFac);
jrechtacek@0
   514
chrislovsund@371
   515
            List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
jrechtacek@0
   516
//         printHierarchy(lstBlockFac, "");
jrechtacek@0
   517
//         generateAssert(lstBlockFac, "lstBlockFac", "false");
chrislovsund@399
   518
            assertEquals(4, lstBlockFac.size());
chrislovsund@399
   519
            processBlocks(plsqlFileName, lstBlockFac);
chrislovsund@371
   520
        } finally {
chrislovsund@371
   521
            if (fileObject != null) {
chrislovsund@371
   522
                fileObject.delete();
chrislovsund@371
   523
            }
chrislovsund@371
   524
        }
chrislovsund@371
   525
    }
jrechtacek@0
   526
chrislovsund@371
   527
    @Test
chrislovsund@371
   528
    public void testAdvanced18() throws IOException, BadLocationException {
chrislovsund@371
   529
        System.out.println("Advanced test case 18");
chrislovsund@399
   530
        final String plsqlFileName = "txtser.ins";
chrislovsund@399
   531
        FileObject fileObject = fs.getRoot().createData(plsqlFileName);
chrislovsund@371
   532
        assertNotNull(fileObject);
chrislovsund@371
   533
        try {
chrislovsund@399
   534
            PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
chrislovsund@371
   535
            assertNotNull(blockFac);
jrechtacek@0
   536
chrislovsund@371
   537
            List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
jrechtacek@0
   538
//         printHierarchy(lstBlockFac, "");
jrechtacek@0
   539
//         generateAssert(lstBlockFac, "lstBlockFac", "false");
chrislovsund@399
   540
            assertEquals(4, lstBlockFac.size());
chrislovsund@399
   541
            processBlocks(plsqlFileName, lstBlockFac);
chrislovsund@371
   542
        } finally {
chrislovsund@371
   543
            if (fileObject != null) {
chrislovsund@371
   544
                fileObject.delete();
chrislovsund@371
   545
            }
chrislovsund@371
   546
        }
chrislovsund@371
   547
    }
subslk@405
   548
    
subslk@405
   549
    @Test
subslk@405
   550
    public void testSimpleCaseStatement() throws IOException, BadLocationException {
subslk@405
   551
        System.out.println("testSimpleCaseStatement");
subslk@405
   552
        final String plsqlFileName = "case.upg";
subslk@405
   553
        FileObject fileObject = fs.getRoot().createData(plsqlFileName);
subslk@405
   554
        assertNotNull(fileObject);
subslk@405
   555
        try {
subslk@405
   556
            PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
subslk@405
   557
            assertNotNull(blockFac);
jrechtacek@0
   558
subslk@405
   559
            List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
subslk@405
   560
//         printHierarchy(lstBlockFac, "");
subslk@405
   561
 //          generateAssert(lstBlockFac, "lstBlockFac", "false");
subslk@405
   562
 //          assertEquals(2, lstBlockFac.size());
subslk@405
   563
            processBlocks(plsqlFileName, lstBlockFac);
subslk@405
   564
        } finally {
subslk@405
   565
            if (fileObject != null) {
subslk@405
   566
                fileObject.delete();
subslk@405
   567
            }
subslk@405
   568
        }
subslk@405
   569
    }
subslk@405
   570
    
subslk@464
   571
    @Test
subslk@464
   572
    public void testForComments() throws IOException, BadLocationException {
subslk@464
   573
        System.out.println("Test for Comment blocks ");
subslk@464
   574
        final String plsqlFileName = "TestComments.apy";
subslk@464
   575
        FileObject fileObject = fs.getRoot().createData(plsqlFileName);
subslk@464
   576
        assertNotNull(fileObject);
subslk@464
   577
        try {
subslk@464
   578
            PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
subslk@464
   579
            assertNotNull(blockFac);
subslk@464
   580
subslk@464
   581
            List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
subslk@464
   582
            //printHierarchy(lstBlockFac, "");
subslk@464
   583
            //assertEquals(2, lstBlockFac.size());
subslk@464
   584
            processBlocks(plsqlFileName, lstBlockFac);
subslk@464
   585
subslk@464
   586
        } finally {
subslk@464
   587
            if (fileObject != null) {
subslk@464
   588
                fileObject.delete();
subslk@464
   589
            }
subslk@464
   590
        }
subslk@464
   591
    }
subslk@464
   592
    
subslk@405
   593
   
chrislovsund@371
   594
    private PlsqlBlockFactory loadAsTmpFile(FileObject fileObj, String fileName) throws IOException, BadLocationException {
chrislovsund@399
   595
        InputStream inputStream = new FileInputStream(new File(getDataDir(), fileName));
chrislovsund@371
   596
        InputStreamReader indexReader = new InputStreamReader(inputStream);
chrislovsund@371
   597
        BufferedReader input = new BufferedReader(indexReader);
chrislovsund@371
   598
        String content = "";
chrislovsund@371
   599
        while (input.ready()) {
chrislovsund@371
   600
            content = content + input.readLine() + "\n";
chrislovsund@371
   601
        }
chrislovsund@371
   602
        input.close();
chrislovsund@371
   603
        inputStream.close();
chrislovsund@371
   604
        indexReader.close();
chrislovsund@371
   605
        writeFile(content, fileObj);
jrechtacek@0
   606
chrislovsund@371
   607
        DataObject dataObj = DataFolder.find(fileObj);
chrislovsund@371
   608
        EditorCookie ec = dataObj.getLookup().lookup(EditorCookie.class);
chrislovsund@371
   609
        assertNotNull(ec);
chrislovsund@371
   610
        Task task = ec.prepareDocument();
chrislovsund@371
   611
        task.waitFinished();
chrislovsund@371
   612
        PlsqlBlockFactory blockFac = dataObj.getLookup().lookup(PlsqlBlockFactory.class);
chrislovsund@371
   613
        assertNotNull(blockFac);
chrislovsund@371
   614
        Document doc = ec.getDocument();
chrislovsund@371
   615
        assertNotNull(doc);
chrislovsund@371
   616
        doc.putProperty(Language.class, PlsqlTokenId.language());
jrechtacek@0
   617
chrislovsund@371
   618
        TokenHierarchy tokenHier = TokenHierarchy.get(doc);
chrislovsund@371
   619
        assertNotNull(tokenHier);
chrislovsund@371
   620
        TokenSequence<PlsqlTokenId> ts = tokenHier.tokenSequence(PlsqlTokenId.language());
chrislovsund@371
   621
        assertNotNull(ts);
chrislovsund@371
   622
        blockFac.initHierarchy(doc);
jrechtacek@0
   623
chrislovsund@371
   624
        return blockFac;
chrislovsund@371
   625
    }
jrechtacek@0
   626
chrislovsund@371
   627
    private void writeFile(String content, FileObject file) throws IOException {
chrislovsund@371
   628
        OutputStream os = file.getOutputStream();
chrislovsund@371
   629
        os.write(content.getBytes("UTF-8"));
chrislovsund@371
   630
        os.close();
chrislovsund@371
   631
    }
jrechtacek@0
   632
chrislovsund@371
   633
    private void printHierarchy(List<PlsqlBlock> lstBlockFac, String txt) {
chrislovsund@371
   634
        for (PlsqlBlock block : lstBlockFac) {
chrislovsund@371
   635
            System.out.println(txt + "Block Name:" + block.getName() + " Type:" + block.getType() + " Start:" + block.getStartOffset() + " End:" + block.getEndOffset() + " Children:" + block.getChildBlocks().size());
chrislovsund@371
   636
            printHierarchy(block.getChildBlocks(), txt + "\t");
chrislovsund@371
   637
        }
chrislovsund@371
   638
    }
jrechtacek@0
   639
chrislovsund@399
   640
    private void assertBlock(List<PlsqlBlock> lstBlock, int index, PlsqlBlockType type, String name, boolean parentExisting, int childCount) {
chrislovsund@371
   641
        PlsqlBlock block = lstBlock.get(index);
chrislovsund@371
   642
        assertNotNull(block);
chrislovsund@399
   643
        assertEquals(type, block.getType());
chrislovsund@399
   644
        assertEquals(name, block.getName());
chrislovsund@399
   645
        assertEquals(childCount, block.getChildCount());
jrechtacek@0
   646
chrislovsund@371
   647
        if (parentExisting) {
chrislovsund@371
   648
            assertNotNull(block.getParent());
chrislovsund@371
   649
        } else {
chrislovsund@371
   650
            assertNull(block.getParent());
chrislovsund@371
   651
        }
chrislovsund@371
   652
    }
jrechtacek@0
   653
chrislovsund@371
   654
    /**
chrislovsund@371
   655
     * Method that generates the assert block statements
chrislovsund@371
   656
     *
chrislovsund@371
   657
     * @param lstBlockFac
chrislovsund@371
   658
     * @param txt
chrislovsund@371
   659
     * @param isParent
chrislovsund@371
   660
     */
chrislovsund@371
   661
    private void generateAssert(List<PlsqlBlock> lstBlockFac, String txt, String isParent) {
chrislovsund@371
   662
        for (int i = 0; i < lstBlockFac.size(); i++) {
chrislovsund@371
   663
            PlsqlBlock block = lstBlockFac.get(i);
chrislovsund@371
   664
            System.out.println("assertBlock(" + txt + ", " + i + ", PlsqlBlockType." + block.getType() + ", \"" + toOneLine(block.getName())
chrislovsund@371
   665
                    + "\", " + isParent + ", " + block.getStartOffset() + ", " + block.getEndOffset() + ", " + block.getChildBlocks().size() + ");");
chrislovsund@371
   666
            generateAssert(block.getChildBlocks(), txt + ".get(" + i + ").getChildBlocks()", "true");
chrislovsund@371
   667
        }
chrislovsund@371
   668
    }
jrechtacek@0
   669
chrislovsund@371
   670
    private String toOneLine(String name) {
chrislovsund@371
   671
        String tmp = "";
chrislovsund@371
   672
        int index = name.indexOf("\n");
chrislovsund@371
   673
        if (index == -1) {
chrislovsund@371
   674
            return name;
chrislovsund@371
   675
        }
jrechtacek@0
   676
chrislovsund@371
   677
        while (index != -1) {
chrislovsund@371
   678
            tmp = tmp + name.substring(0, index) + "\\n";
chrislovsund@371
   679
            if (name.length() > index + 1) {
chrislovsund@371
   680
                name = name.substring(index + 1);
chrislovsund@371
   681
                index = name.indexOf("\n");
chrislovsund@371
   682
                if (index == -1) {
chrislovsund@371
   683
                    tmp = tmp + name + "\\n";  //\n is added here because we substring in the end
chrislovsund@371
   684
                }
chrislovsund@371
   685
            } else {
chrislovsund@371
   686
                break;
jrechtacek@0
   687
            }
chrislovsund@371
   688
        }
jrechtacek@0
   689
chrislovsund@371
   690
        tmp = tmp.substring(0, tmp.length() - 2);
chrislovsund@371
   691
        return tmp;
chrislovsund@371
   692
    }
jrechtacek@0
   693
}