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