PLSQL/Execution/test/unit/src/org/netbeans/modules/plsql/execution/TestRecordPlayer.java
1 package org.netbeans.modules.plsql.execution;
4 import java.io.IOException;
5 import java.util.ArrayList;
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;
14 * This class contains both the framework for template testing and the test settings.
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
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
25 public class TestRecordPlayer extends NbTestCase {
27 private boolean recordTestData = false; //NOTE: Must be false when committed !!!
29 public TestRecordPlayer(String name) {
33 public void testShouldPassBeforeCommit() {
34 assertFalse(recordTestData);
37 public void processExecutableBLocksBlocks(String plsqlFileName, List<PlsqlExecutableObject> exceutableObjects) throws IOException {
38 List<String> expectedExceutableObjTypes = new ArrayList<String>();
39 populateListOfExceutableObjects(exceutableObjects, expectedExceutableObjTypes);
41 final File expectedDir = new File(getExpectedDir().replace(File.separator + "build" + File.separator, File.separator));
43 FileUtils.writeLines(new File(expectedDir, plsqlFileName + ".structure"), expectedExceutableObjTypes);
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);
52 private void populateListOfExceutableObjects(List<PlsqlExecutableObject> exceutableObjects, List<String> expectedExceutableObjTypes) {
54 for (PlsqlExecutableObject exceutableObject : exceutableObjects) {
55 PlsqlExecutableObjectType type = exceutableObject.getType();
56 if(type == PlsqlExecutableObjectType.BEGINEND) {
57 expectedExceutableObjTypes.add("BEGINEND");
59 if(type == PlsqlExecutableObjectType.COLUMNCOMMENT) {
60 expectedExceutableObjTypes.add("COLUMNCOMMENT");
62 if(type == PlsqlExecutableObjectType.COMMENT) {
63 expectedExceutableObjTypes.add("COMMENT");
65 if(type == PlsqlExecutableObjectType.DECLAREEND) {
66 expectedExceutableObjTypes.add("DECLAREEND");
68 if(type == PlsqlExecutableObjectType.FUNCTION) {
69 expectedExceutableObjTypes.add("FUNCTION");
71 if(type == PlsqlExecutableObjectType.JAVASOURCE) {
72 expectedExceutableObjTypes.add("JAVASOURCE");
74 if(type == PlsqlExecutableObjectType.PACKAGE) {
75 expectedExceutableObjTypes.add("PACKAGE");
77 if(type == PlsqlExecutableObjectType.PACKAGEBODY) {
78 expectedExceutableObjTypes.add("PACKAGEBODY");
80 if(type == PlsqlExecutableObjectType.PROCEDURE) {
81 expectedExceutableObjTypes.add("PROCEDURE");
83 if(type == PlsqlExecutableObjectType.STATEMENT) {
84 expectedExceutableObjTypes.add("STATEMENT");
86 if(type == PlsqlExecutableObjectType.TABLECOMMENT) {
87 expectedExceutableObjTypes.add("TABLECOMMENT");
89 if(type == PlsqlExecutableObjectType.TRIGGER) {
90 expectedExceutableObjTypes.add("TRIGGER");
92 if(type == PlsqlExecutableObjectType.UNKNOWN) {
93 expectedExceutableObjTypes.add("UNKNOWN");
95 if(type == PlsqlExecutableObjectType.VIEW) {
96 expectedExceutableObjTypes.add("VIEW");
102 public void assertStructure(File expectedFile, File actualFile) throws IOException {
103 assertLines(FileUtils.readFileToString(expectedFile, "utf-8"), FileUtils.readFileToString(actualFile, "utf-8"));
106 protected String getExpectedDir() {
107 return getDataDir().getAbsolutePath() + File.separator + "expected" + File.separator;
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++) {
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]);
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]);
130 System.out.println("==================================================================");
134 assertEquals(expectedLine.length, resultLine.length);