PLSQL/Execution/test/unit/src/org/netbeans/modules/plsql/execution/TestRecordPlayer.java
author chrislovsund@netbeans.org
Mon, 19 Aug 2013 14:00:24 +0200
changeset 468 935db75997f7
parent 452 b2411fb23674
permissions -rw-r--r--
added test
EADS-4033 - make tests pass
     1 package org.netbeans.modules.plsql.execution;
     2 
     3 import java.io.File;
     4 import java.io.IOException;
     5 import java.util.ArrayList;
     6 import java.util.List;
     7 import static junit.framework.Assert.assertEquals;
     8 import org.apache.commons.io.FileUtils;
     9 import org.netbeans.junit.NbTestCase;
    10 import org.netbeans.modules.plsqlsupport.db.PlsqlExecutableObject;
    11 import org.netbeans.modules.plsqlsupport.db.PlsqlExecutableObjectType;
    12 
    13 /**
    14  * This class contains both the framework for template testing and the test settings.
    15  *
    16  * RUN_LAYER_TEST map states if tests for that particular layer should be run or not. true = all tests for this layer
    17  * are executed, both for test data collection and for test output verification false = the layer is ignored completely,
    18  * both for testing and data collection
    19  *
    20  * RecordTestData is a switch for testing mode true = record test data and write expected data files to their correct
    21  * folders. those files should then be committed to the SVN repository NOTE: this flag should only be set when
    22  * collecting data and the be reset again. Never commit this class to SVN with this flag set to true !!! false = run the
    23  * test as normal
    24  */
    25 public class TestRecordPlayer extends NbTestCase {
    26 
    27     private boolean recordTestData = false; //NOTE: Must be false when committed  !!!
    28 
    29     public TestRecordPlayer(String name) {
    30         super(name);
    31     }
    32 
    33     public void testShouldPassBeforeCommit() {
    34         assertFalse(recordTestData);
    35     }
    36 
    37     public void processExecutableBLocksBlocks(String plsqlFileName, List<PlsqlExecutableObject> exceutableObjects) throws IOException {
    38         List<String> expectedExceutableObjTypes = new ArrayList<String>();
    39         populateListOfExceutableObjects(exceutableObjects, expectedExceutableObjTypes);
    40         if (recordTestData) {
    41             final File expectedDir = new File(getExpectedDir().replace(File.separator + "build" + File.separator, File.separator));
    42             expectedDir.mkdirs();
    43             FileUtils.writeLines(new File(expectedDir, plsqlFileName + ".structure"), expectedExceutableObjTypes);
    44         } else {
    45             File expectedFile = new File(getExpectedDir(), plsqlFileName + ".structure");
    46             final File actualFile = new File(getWorkDir(), plsqlFileName + ".structure");
    47             FileUtils.writeLines(actualFile, expectedExceutableObjTypes);
    48             assertStructure(expectedFile, actualFile);
    49         }
    50     }
    51 
    52     private void populateListOfExceutableObjects(List<PlsqlExecutableObject> exceutableObjects, List<String> expectedExceutableObjTypes) {
    53 
    54         for (PlsqlExecutableObject exceutableObject : exceutableObjects) {
    55             PlsqlExecutableObjectType type = exceutableObject.getType();
    56             if(type == PlsqlExecutableObjectType.BEGINEND) {
    57                 expectedExceutableObjTypes.add("BEGINEND");
    58             }
    59             if(type == PlsqlExecutableObjectType.COLUMNCOMMENT) {
    60                 expectedExceutableObjTypes.add("COLUMNCOMMENT");
    61             }
    62             if(type == PlsqlExecutableObjectType.COMMENT) {
    63                 expectedExceutableObjTypes.add("COMMENT");
    64             }
    65             if(type == PlsqlExecutableObjectType.DECLAREEND) {
    66                 expectedExceutableObjTypes.add("DECLAREEND");
    67             }
    68             if(type == PlsqlExecutableObjectType.FUNCTION) {
    69                 expectedExceutableObjTypes.add("FUNCTION");
    70             }
    71             if(type == PlsqlExecutableObjectType.JAVASOURCE) {
    72                 expectedExceutableObjTypes.add("JAVASOURCE");
    73             }
    74             if(type == PlsqlExecutableObjectType.PACKAGE) {
    75                 expectedExceutableObjTypes.add("PACKAGE");
    76             }
    77             if(type == PlsqlExecutableObjectType.PACKAGEBODY) {
    78                 expectedExceutableObjTypes.add("PACKAGEBODY");
    79             }
    80             if(type == PlsqlExecutableObjectType.PROCEDURE) {
    81                 expectedExceutableObjTypes.add("PROCEDURE");
    82             }
    83             if(type == PlsqlExecutableObjectType.STATEMENT) {
    84                 expectedExceutableObjTypes.add("STATEMENT");
    85             }
    86             if(type == PlsqlExecutableObjectType.TABLECOMMENT) {
    87                 expectedExceutableObjTypes.add("TABLECOMMENT");
    88             }
    89             if(type == PlsqlExecutableObjectType.TRIGGER) {
    90                 expectedExceutableObjTypes.add("TRIGGER");
    91             }
    92             if(type == PlsqlExecutableObjectType.UNKNOWN) {
    93                 expectedExceutableObjTypes.add("UNKNOWN");
    94             }
    95             if(type == PlsqlExecutableObjectType.VIEW) {
    96                 expectedExceutableObjTypes.add("VIEW");
    97             }
    98         }
    99         
   100     }
   101 
   102     public void assertStructure(File expectedFile, File actualFile) throws IOException {
   103         assertLines(FileUtils.readFileToString(expectedFile, "utf-8"), FileUtils.readFileToString(actualFile, "utf-8"));
   104     }
   105 
   106     protected String getExpectedDir() {
   107         return getDataDir().getAbsolutePath() + File.separator + "expected" + File.separator;
   108     }
   109 
   110     public static void assertLines(String expResult, String result) {
   111         String[] expectedLine = expResult.replace("\r", "").split("[\\r\\n]");
   112         String[] resultLine = result.replace("\r", "").split("[\\r\\n]");
   113         for (int i = 0; i < resultLine.length && i < expectedLine.length; i++) {
   114             try {
   115                 assertEquals(expectedLine[i].replaceAll("\\s+", " ").trim(), resultLine[i].replaceAll("\\s+", " ").trim());
   116             } catch (AssertionError e) {
   117                 System.out.println("failed on line " + i);
   118                 System.out.println("EXPECTED==========================================================");
   119                 for (int j = -5; j < 8; j++) {
   120                     if (i < expectedLine.length - j && i + j >= 0) {
   121                         System.out.println((j == 0 ? "--> " : "    ") + expectedLine[i + j]);
   122                     }
   123                 }
   124                 System.out.println("RESULT============================================================");
   125                 for (int j = -5; j < 8; j++) {
   126                     if (i < resultLine.length - j && i + j >= 0) {
   127                         System.out.println((j == 0 ? "--> " : "    ") + resultLine[i + j]);
   128                     }
   129                 }
   130                 System.out.println("==================================================================");
   131                 throw e;
   132             }
   133         }
   134         assertEquals(expectedLine.length, resultLine.length);
   135     }
   136 }