-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySQLTest.java
More file actions
192 lines (170 loc) · 6.05 KB
/
MySQLTest.java
File metadata and controls
192 lines (170 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import java.io.*;
import java.util.ArrayList;
/**
* Test class that will test the output of the Application.
*
* @author Brendon Strowe
* @author Brett Phillips
* @author Steven Ricci
* @author Louie Trapani
*
*/
public class MySQLTest {
private BufferedReader br;
public static final String EDGE_FILE_ID = "EDGE Diagram File"; //first line of .edg files should be this
private File edgeFile = new File("./Courses.edg");
private ArrayList<String> tableNames = new ArrayList<String>();
//private EdgeConvertFileParser ecfp;
//private CreateDDLMySQL testObj;
/**
* Set up the test object before running each test
*
* @throws Exception
*/
public static void main(String args[]) {
MySQLTest test = new MySQLTest();
}
public MySQLTest() {
// Test if output file exists
File currentDirectory = new File(".");
File[] files = currentDirectory.listFiles();
boolean fileExists = false;
File sqlFile = new File(".");
String databaseName = "";
for (File file : files) {
if (file.getName().indexOf(".sql") > -1) {
fileExists = true;
sqlFile = new File(file.getPath());
break;
}
}
if (!fileExists) {
// Test fails
System.out.println("TEST FAILED: No output SQL file exists in the running directory.");
System.exit(0);
} else {
System.out.println("Found output SQL file: " + sqlFile.getName());
try {
br = new BufferedReader(new FileReader(sqlFile));
} catch(FileNotFoundException e) {
}
String currentLine = "";
try {
while ((currentLine = br.readLine()) != null) {
currentLine = currentLine.trim();
if (currentLine.startsWith("CREATE TABLE ")) { //this has the name of the Database
System.out.println(currentLine);
databaseName = currentLine.substring(currentLine.indexOf("CREATE TABLE"), currentLine.indexOf("(") - 1); //get the name of the database
break;
}
}
if (databaseName.equals("")) {
System.out.println("TEST FAILED: No database defined in Output file.");
System.exit(0);
}
} catch(IOException e) {
}
}
// Get the table names out of the `courses.edg` file.
openFile(edgeFile);
// Run `mysqlcheck -u myusername -p -c [output SQL file]`
// Check if it passes with an `OK` message or an `ERROR` message from
// the command output.
try {
BufferedReader br;
System.out.println("Running `mysql` command to attempt to create database from output SQL file…\n");
Process mysqlProcess = Runtime.getRuntime().exec("mysql -u root -p < " + sqlFile.getName());
mysqlProcess.waitFor();
Process mysqlcheckProcess = Runtime.getRuntime().exec("mysqlcheck -u root -p -c CoursesDB");
br = new BufferedReader(new InputStreamReader(mysqlcheckProcess.getInputStream()));
System.out.println("`mysqltest` requires a password to login to the root user account.");
mysqlcheckProcess.waitFor();
System.out.println("Running `mysqltest`.");
String line = br.readLine();
System.out.println("");
// Check if output matches expected
while (line != null) {
System.out.println(line);
line = br.readLine();
if (line.toUpperCase().indexOf("ERROR") != -1) {
System.out.println("TEST FAILED: Error in creating database from Output file.");
System.exit(0);
} else {
String tableNameSql = line.substring(line.indexOf(databaseName) + 1, line.indexOf(" "));
System.out.println(tableNameSql);
boolean match = false;
for (String tableName : tableNames) {
if (tableName.toUpperCase().equals(tableNameSql.toUpperCase())) {
match = true;
break;
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void openFile(File inputFile) {
try {
BufferedReader br = new BufferedReader(new FileReader(inputFile));
//test for what kind of file we have
String currentLine = br.readLine().trim();
if (currentLine.startsWith(EDGE_FILE_ID)) {
this.parseEdgeFile(inputFile); //parse the file
br.close();
}
} // try
catch (FileNotFoundException fnfe) {
System.out.println("Cannot find edge file \"" + inputFile.getName() + "\".");
System.exit(0);
} // catch FileNotFoundException
catch (IOException ioe) {
System.out.println(ioe);
System.exit(0);
} // catch IOException
} // openFile()
public void parseEdgeFile(File inputFile) throws IOException {
String currentLine = "";
String tableName = "";
boolean isEntity = false;
int numFigure = 0;
BufferedReader br = new BufferedReader(new FileReader(inputFile));
while ((currentLine = br.readLine()) != null) {
currentLine = currentLine.trim();
if (currentLine.startsWith("Figure ")) { //this is the start of a Figure entry
numFigure = Integer.parseInt(currentLine.substring(currentLine.indexOf(" ") + 1)); //get the Figure number
currentLine = br.readLine().trim(); // this should be "{"
currentLine = br.readLine().trim();
if (!currentLine.startsWith("Style")) { // this is to weed out other Figures, like Labels
continue;
} else {
String style = currentLine.substring(currentLine.indexOf("\"") + 1, currentLine.lastIndexOf("\"")); //get the Style parameter
if (style.startsWith("Entity")) {
isEntity = true;
} else {
continue;
}
currentLine = br.readLine().trim(); //this should be Text
tableName = currentLine.substring(currentLine.indexOf("\"") + 1, currentLine.lastIndexOf("\"")).replaceAll(" ", ""); //get the Text parameter
if (tableName.equals("")) {
// Test failed with malformed SQL
System.out.println("There are entities or attributes with blank names in this diagram.");
break;
}
}
int escape = tableName.indexOf("\\");
if (escape > 0) { //Edge denotes a line break as "\line", disregard anything after a backslash
tableName = tableName.substring(0, escape);
}
if (isEntity) { //create a new EdgeTable object and add it to the alTables ArrayList
tableNames.add(tableName);
}
//reset flags
isEntity = false;
} // if("Figure")
} // while()
} // parseEdgeFile()
}