admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * To change this template, choose Tools | Templates |
| 3 | * and open the template in the editor. |
| 4 | */ |
| 5 | package tai_ofa; |
| 6 | |
| 7 | import java.io.BufferedReader; |
| 8 | import java.io.DataInputStream; |
| 9 | import java.io.File; |
| 10 | import java.io.FileInputStream; |
| 11 | import java.io.FileReader; |
| 12 | import java.io.FileWriter; |
| 13 | import java.io.IOException; |
| 14 | import java.io.InputStreamReader; |
| 15 | import java.util.ArrayList; |
| 16 | import java.util.Iterator; |
| 17 | import java.util.Locale; |
| 18 | import java.util.Map; |
| 19 | import java.util.TreeMap; |
| 20 | import java.util.regex.Matcher; |
| 21 | import java.util.regex.Pattern; |
| 22 | import javafx.application.Application; |
| 23 | import javafx.beans.value.ChangeListener; |
| 24 | import javafx.beans.value.ObservableValue; |
| 25 | import javafx.collections.FXCollections; |
| 26 | import javafx.collections.ObservableList; |
| 27 | import javafx.event.ActionEvent; |
| 28 | import javafx.event.EventHandler; |
| 29 | import javafx.geometry.Insets; |
| 30 | import javafx.scene.Scene; |
| 31 | import javafx.scene.control.Button; |
| 32 | import javafx.scene.control.CheckBox; |
| 33 | import javafx.scene.control.Label; |
| 34 | import javafx.scene.control.TableColumn; |
| 35 | import javafx.scene.control.TableView; |
| 36 | import javafx.scene.control.cell.PropertyValueFactory; |
| 37 | import javafx.scene.image.ImageView; |
| 38 | import javafx.scene.layout.GridPane; |
| 39 | import javafx.scene.layout.HBox; |
| 40 | import javafx.scene.layout.StackPane; |
| 41 | import javafx.scene.text.Text; |
| 42 | import javafx.stage.Stage; |
| 43 | |
| 44 | /** |
| 45 | * |
| 46 | * @author Raghav Kashyap (raghavkashyap@paxterrasolutions.com) |
| 47 | |
| 48 | * TestON is free software: you can redistribute it and/or modify |
| 49 | * it under the terms of the GNU General Public License as published by |
| 50 | * the Free Software Foundation, either version 2 of the License, or |
| 51 | * (at your option) any later version. |
| 52 | |
| 53 | * TestON is distributed in the hope that it will be useful, |
| 54 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 55 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 56 | * GNU General Public License for more details. |
| 57 | |
| 58 | * You should have received a copy of the GNU General Public License |
| 59 | * along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 60 | |
| 61 | */ |
| 62 | public class OFATestCaseSelection extends Application { |
| 63 | |
| 64 | String driverFile; |
| 65 | String paramFileName; |
| 66 | String pythonFile; |
| 67 | TAILocale label = new TAILocale(Locale.ENGLISH); |
| 68 | |
| 69 | OFATestCaseSelection(String fileName, String paramsFileName) { |
| 70 | driverFile = label.hierarchyTestON + "/tests/" + fileName + "/" + fileName + ".ospk"; |
| 71 | pythonFile = label.hierarchyTestON + "/tests/" + fileName + "/" + fileName + ".py"; |
| 72 | paramFileName = label.hierarchyTestON + "/tests/" + fileName + "/" + fileName + ".params"; |
| 73 | } |
| 74 | ObservableList<TestCaseSelectionTable> data; |
| 75 | ObservableList<String> testSelected; |
| 76 | TableView<TestCaseSelectionTable> deviceTable; |
| 77 | TableColumn selectCaseColumn; |
| 78 | TableColumn testCaseIdColumn; |
| 79 | TableColumn testCaseNameColumn; |
| 80 | TreeMap<String, String> testCaseIdAndName = new TreeMap<String, String>(); |
| 81 | String caseId, caseName; |
| 82 | GridPane testCaseSelectionGrid = new GridPane(); |
| 83 | TreeMap<String, String> stepHash = new TreeMap<String, String>(); |
| 84 | TableColumn stepId, stepName; |
| 85 | TableView<TestSelectStepTable> stepTable; |
| 86 | ObservableList<TestSelectStepTable> stepData; |
| 87 | |
| 88 | /** |
| 89 | * @param args the command line arguments |
| 90 | */ |
| 91 | public static void main(String[] args) { |
| 92 | launch(args); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public void start(final Stage primaryStage) { |
| 97 | primaryStage.setTitle("TestCase Selection"); |
| 98 | primaryStage.setResizable(false); |
| 99 | testSelected = FXCollections.observableArrayList(); |
| 100 | stepData = FXCollections.observableArrayList(); |
| 101 | |
| 102 | testCaseSelectionGrid.setPadding(new Insets(0, 0, 0, 15)); |
| 103 | testCaseSelectionGrid.setVgap(10); |
| 104 | testCaseSelectionGrid.setHgap(20); |
| 105 | final CheckBox selectTestCase = new CheckBox(); |
| 106 | Label testCaseId = new Label(""); |
| 107 | Label testCaseName = new Label(""); |
| 108 | |
| 109 | stepTable = new TableView<TestSelectStepTable>(); |
| 110 | deviceTable = new TableView<TestCaseSelectionTable>(); |
| 111 | data = FXCollections.observableArrayList(new TestCaseSelectionTable(selectTestCase, testCaseId, testCaseName)); |
| 112 | deviceTable.setMinWidth(430); |
| 113 | deviceTable.setMaxHeight(300); |
| 114 | testCaseIdColumn = new TableColumn(); |
| 115 | |
| 116 | testCaseIdColumn.setCellValueFactory(new PropertyValueFactory<TestCaseSelectionTable, CheckBox>("testCaseCheckBox")); |
| 117 | testCaseIdColumn.setMinWidth(90); |
| 118 | testCaseIdColumn.setResizable(false); |
| 119 | selectCaseColumn = new TableColumn("TestCase Id"); |
| 120 | selectCaseColumn.setSortable(true); |
| 121 | selectCaseColumn.setCellValueFactory(new PropertyValueFactory<TestCaseSelectionTable, Label>("testCaseId")); |
| 122 | selectCaseColumn.setMinWidth(130); |
| 123 | selectCaseColumn.setResizable(false); |
| 124 | testCaseNameColumn = new TableColumn("TestCase Name"); |
| 125 | testCaseNameColumn.setCellValueFactory(new PropertyValueFactory<TestCaseSelectionTable, Label>("testCaseName")); |
| 126 | testCaseNameColumn.setMinWidth(130); |
| 127 | testCaseNameColumn.setResizable(false); |
| 128 | deviceTable.setItems(data); |
| 129 | deviceTable.getColumns().addAll(testCaseIdColumn, selectCaseColumn, testCaseNameColumn); |
| 130 | stepTable.setMinWidth(620); |
| 131 | stepTable.setMaxHeight(330); |
| 132 | |
| 133 | stepId = new TableColumn("ID"); |
| 134 | stepId.setCellValueFactory(new PropertyValueFactory<TestSelectStepTable, Label>("testStepId")); |
| 135 | stepId.setMinWidth(10); |
| 136 | stepId.setResizable(true); |
| 137 | |
| 138 | stepName = new TableColumn("Name"); |
| 139 | stepName.setCellValueFactory(new PropertyValueFactory<TestSelectStepTable, Label>("testStepName")); |
| 140 | stepName.setMinWidth(400); |
| 141 | stepName.setResizable(true); |
| 142 | |
| 143 | |
| 144 | stepTable.getColumns().addAll(stepId, stepName); |
| 145 | stepTable.setItems(stepData); |
| 146 | driverFunctionName(); |
| 147 | |
| 148 | Iterator driverFileIterator = testCaseIdAndName.entrySet().iterator(); |
| 149 | while (driverFileIterator.hasNext()) { |
| 150 | Map.Entry testCaseDetail = (Map.Entry) driverFileIterator.next(); |
| 151 | final CheckBox selectcase = new CheckBox(); |
| 152 | final Label id = new Label((String) testCaseDetail.getKey()); |
| 153 | Label name = new Label((String) testCaseDetail.getValue()); |
| 154 | selectTestCase.selectedProperty().addListener(new ChangeListener<Boolean>() { |
| 155 | @Override |
| 156 | public void changed(ObservableValue<? extends Boolean> arg0, Boolean arg1, Boolean arg2) { |
| 157 | selectcase.setSelected(true); |
| 158 | if (selectTestCase.isSelected() == false) { |
| 159 | selectcase.setSelected(false); |
| 160 | } |
| 161 | } |
| 162 | }); |
| 163 | |
| 164 | selectcase.selectedProperty().addListener(new ChangeListener<Boolean>() { |
| 165 | @Override |
| 166 | public void changed(ObservableValue<? extends Boolean> arg0, Boolean arg1, Boolean arg2) { |
| 167 | if (selectcase.isSelected() == true) { |
| 168 | stepData.clear(); |
| 169 | for (int i = 0; i < deviceTable.getItems().size(); i++) { |
| 170 | if (deviceTable.getItems().get(i).testCaseId.getText().equals(id.getText())) { |
| 171 | deviceTable.getSelectionModel().select(i); |
| 172 | Pattern caseNumberPattern = Pattern.compile("CASE\\s*(\\d+)"); |
| 173 | Matcher caseNumberMatcher = caseNumberPattern.matcher(deviceTable.getItems().get(i).testCaseId.getText()); |
| 174 | String caseNumber = ""; |
| 175 | if (caseNumberMatcher.find()) { |
| 176 | caseNumber = caseNumberMatcher.group(1); |
| 177 | } |
| 178 | |
| 179 | getTestSteps(caseNumber); |
| 180 | testSelected.add(caseNumber); |
| 181 | |
| 182 | Iterator entries = stepHash.entrySet().iterator(); |
| 183 | while (entries.hasNext()) { |
| 184 | Map.Entry entry = (Map.Entry) entries.next(); |
| 185 | String key = (String) entry.getKey(); |
| 186 | String value = (String) entry.getValue(); |
| 187 | stepData.add(new TestSelectStepTable(new Label(key), new Label(value))); |
| 188 | } |
| 189 | |
| 190 | stepTable.setItems(stepData); |
| 191 | stepTable.setVisible(true); |
| 192 | try { |
| 193 | testCaseSelectionGrid.add(new Text("Test Steps :"), 0, 3); |
| 194 | testCaseSelectionGrid.add(stepTable, 0, 4); |
| 195 | } catch (Exception e) { |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | if (deviceTable.getSelectionModel().getSelectedItem().getTestCaseCheckBox().isSelected() == true) { |
| 202 | } |
| 203 | } |
| 204 | }); |
| 205 | |
| 206 | data.add(new TestCaseSelectionTable(selectcase, id, name)); |
| 207 | testCaseIdColumn.setCellValueFactory(new PropertyValueFactory<TestCaseSelectionTable, CheckBox>("testCaseCheckBox")); |
| 208 | testCaseIdColumn.setMinWidth(50); |
| 209 | testCaseIdColumn.setResizable(false); |
| 210 | |
| 211 | selectCaseColumn.setCellValueFactory(new PropertyValueFactory<TestCaseSelectionTable, Label>("testCaseId")); |
| 212 | selectCaseColumn.setMinWidth(100); |
| 213 | selectCaseColumn.setResizable(false); |
| 214 | |
| 215 | testCaseNameColumn.setCellValueFactory(new PropertyValueFactory<TestCaseSelectionTable, Label>("testCaseName")); |
| 216 | testCaseNameColumn.setMinWidth(292); |
| 217 | testCaseNameColumn.setResizable(false); |
| 218 | deviceTable.setItems(data); |
| 219 | } |
| 220 | |
| 221 | testCaseSelectionGrid.add(deviceTable, 0, 1); |
| 222 | |
| 223 | HBox optionButton = new HBox(5); |
| 224 | optionButton.setPadding(new Insets(0, 0, 0, 0)); |
| 225 | |
| 226 | Button startTest = new Button("Save"); |
| 227 | |
| 228 | startTest.setOnAction(new EventHandler<ActionEvent>() { |
| 229 | @Override |
| 230 | public void handle(ActionEvent t) { |
| 231 | for (int i = 0; i < deviceTable.getItems().size(); i++) { |
| 232 | |
| 233 | if (deviceTable.getItems().get(i).testCaseIdCheck.isSelected()) { |
| 234 | deviceTable.getSelectionModel().select(i); |
| 235 | } |
| 236 | |
| 237 | if (deviceTable.getSelectionModel().getSelectedItem().getTestCaseCheckBox().isSelected() == true) { |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | StringBuilder testcases = new StringBuilder(); |
| 242 | for (String s : testSelected) { |
| 243 | testcases.append(s).append(','); |
| 244 | } |
| 245 | primaryStage.close(); |
| 246 | } |
| 247 | }); |
| 248 | |
| 249 | Button modifyParams = new Button("Modify Params"); |
| 250 | Button cancelButton = new Button("Cancel"); |
| 251 | optionButton.getChildren().addAll(new Label(" "), startTest, modifyParams, cancelButton); |
| 252 | testCaseSelectionGrid.add(optionButton, 0, 5); |
| 253 | |
| 254 | StackPane root = new StackPane(); |
| 255 | root.getChildren().add(testCaseSelectionGrid); |
| 256 | primaryStage.setScene(new Scene(root, 650, 400)); |
| 257 | primaryStage.show(); |
| 258 | } |
| 259 | |
| 260 | public void driverFunctionName() { |
| 261 | try { |
| 262 | FileInputStream fstream = new FileInputStream(driverFile); |
| 263 | ArrayList<String> driverFunctionName = new ArrayList<String>(); |
| 264 | DataInputStream in = new DataInputStream(fstream); |
| 265 | BufferedReader br = new BufferedReader(new InputStreamReader(in)); |
| 266 | String strLine; |
| 267 | while ((strLine = br.readLine()) != null) { |
| 268 | Pattern casePattern = Pattern.compile("^CASE\\s+(\\d+)"); |
| 269 | Matcher match = casePattern.matcher(strLine); |
| 270 | while (match.find()) { |
| 271 | driverFunctionName.add(match.group()); |
| 272 | caseId = match.group(); |
| 273 | strLine = br.readLine(); |
| 274 | casePattern = Pattern.compile("NAME\\s+(\\\"+(.*)\\\")"); |
| 275 | match = casePattern.matcher(strLine); |
| 276 | if (match.find()) { |
| 277 | caseName = match.group(2); |
| 278 | } |
| 279 | testCaseIdAndName.put(caseId, caseName); |
| 280 | } |
| 281 | } |
| 282 | } catch (Exception e) { |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | public void getParamsUpdate(String testcases) { |
| 287 | try { |
| 288 | File file = new File(paramFileName); |
| 289 | BufferedReader reader = new BufferedReader(new FileReader(file)); |
| 290 | String line = "", oldtext = ""; |
| 291 | while ((line = reader.readLine()) != null) { |
| 292 | oldtext += line + "\r\n"; |
| 293 | } |
| 294 | reader.close(); |
| 295 | String newtext = oldtext.replaceAll("<testcases>\\s*(\\d+)</testcases>", "<testcases>" + testcases + "</testcases>"); |
| 296 | FileWriter writer = new FileWriter(paramFileName); |
| 297 | writer.write(newtext); |
| 298 | writer.close(); |
| 299 | } catch (IOException ioe) { |
| 300 | ioe.printStackTrace(); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | public TreeMap getCaseIdAndName() { |
| 305 | return testCaseIdAndName; |
| 306 | } |
| 307 | |
| 308 | public void getTestSteps(String caseNumber) { |
| 309 | OFAFileOperations fileOperation = new OFAFileOperations(); |
| 310 | int stepCount = 0; |
| 311 | String stepCounter = ""; |
| 312 | BufferedReader input = null; |
| 313 | ArrayList<String> contents = new ArrayList<String>(); |
| 314 | File scriptName = new File(driverFile); |
| 315 | if (scriptName.exists()) { |
| 316 | try { |
| 317 | //use buffering, reading one line at a time |
| 318 | //FileReader always assumes default encoding is OK! |
| 319 | try { |
| 320 | input = new BufferedReader(new FileReader(scriptName)); |
| 321 | } catch (Exception e) { |
| 322 | } |
| 323 | |
| 324 | try { |
| 325 | String line = null; //not declared within while loop |
| 326 | while ((line = input.readLine()) != null) { |
| 327 | contents.add(line); |
| 328 | } |
| 329 | } finally { |
| 330 | try { |
| 331 | input.close(); |
| 332 | } catch (Exception e) { |
| 333 | } |
| 334 | } |
| 335 | } catch (IOException ex) { |
| 336 | ex.printStackTrace(); |
| 337 | } |
| 338 | |
| 339 | for (int i = 0; i < contents.size(); i++) { |
| 340 | Pattern casePattern = Pattern.compile("\\s*CASE\\s*(\\d+)\\s*"); |
| 341 | Matcher caseMatcher = casePattern.matcher(contents.get(i)); |
| 342 | if (caseMatcher.find()) { |
| 343 | if (caseMatcher.group(1).equals(caseNumber)) { |
| 344 | i++; |
| 345 | Pattern casePatterns = Pattern.compile("\\s*CASE\\s*(\\d+)\\s*"); |
| 346 | Matcher caseMatchers = casePatterns.matcher(contents.get(i)); |
| 347 | while (!caseMatchers.find() && i < contents.size()) { |
| 348 | Pattern casesPatterns = Pattern.compile("\\s*CASE\\s*(\\d+)\\s*"); |
| 349 | Matcher casesMatchers = casesPatterns.matcher(contents.get(i)); |
| 350 | if (casesMatchers.find()) { |
| 351 | break; |
| 352 | } else { |
| 353 | Pattern stepPattern = Pattern.compile("\\s*STEP\\s+\"\\s*(.*)\\s*\"\\s*"); |
| 354 | Matcher stepMatcher = stepPattern.matcher(contents.get(i)); |
| 355 | try { |
| 356 | if (stepMatcher.find()) { |
| 357 | stepCount++; |
| 358 | stepCounter = caseNumber + "." + String.valueOf(stepCount); |
| 359 | stepHash.put(stepCounter, stepMatcher.group(1)); |
| 360 | } |
| 361 | } catch (Exception e) { |
| 362 | break; |
| 363 | } |
| 364 | i++; |
| 365 | } |
| 366 | } |
| 367 | i--; |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | } else { |
| 372 | try { |
| 373 | //use buffering, reading one line at a time |
| 374 | //FileReader always assumes default encoding is OK! |
| 375 | try { |
| 376 | input = new BufferedReader(new FileReader(pythonFile)); |
| 377 | } catch (Exception e) { |
| 378 | } |
| 379 | |
| 380 | try { |
| 381 | String line = null; //not declared within while loop |
| 382 | while ((line = input.readLine()) != null) { |
| 383 | contents.add(line); |
| 384 | } |
| 385 | } finally { |
| 386 | try { |
| 387 | input.close(); |
| 388 | } catch (Exception e) { |
| 389 | } |
| 390 | } |
| 391 | } catch (IOException ex) { |
| 392 | ex.printStackTrace(); |
| 393 | } |
| 394 | |
| 395 | for (int i = 0; i < contents.size(); i++) { |
| 396 | Pattern casePattern = Pattern.compile("\\s*def\\s+CASE(\\d+)\\s*\\(\\s*(.*)\\s*\\)\\s*:\\s*"); |
| 397 | Matcher caseMatcher = casePattern.matcher(contents.get(i)); |
| 398 | if (caseMatcher.find()) { |
| 399 | if (caseMatcher.group(1).equals(caseNumber)) { |
| 400 | i++; |
| 401 | Pattern casePatterns = Pattern.compile("\\s*def\\s+CASE(\\d+)\\s*\\(\\s*(.*)\\s*\\)\\s*:\\s*"); |
| 402 | Matcher caseMatchers = casePatterns.matcher(contents.get(i)); |
| 403 | while (!caseMatchers.find() && i < contents.size()) { |
| 404 | Pattern casesPatterns = Pattern.compile("\\s*def\\s+CASE(\\d+)\\s*\\(\\s*(.*)\\s*\\)\\s*:\\s*"); |
| 405 | Matcher casesMatchers = casesPatterns.matcher(contents.get(i)); |
| 406 | if (casesMatchers.find()) { |
| 407 | break; |
| 408 | } else { |
| 409 | Pattern stepPattern = Pattern.compile("\\s*main.step\\(\\s*\"\\s*(.*)\\s*\"\\s*\\)\\s*"); |
| 410 | Matcher stepMatcher = stepPattern.matcher(contents.get(i)); |
| 411 | try { |
| 412 | if (stepMatcher.find()) { |
| 413 | stepCount++; |
| 414 | stepCounter = caseNumber + "." + String.valueOf(stepCount); |
| 415 | stepHash.put(stepCounter, stepMatcher.group(1)); |
| 416 | } |
| 417 | } catch (Exception e) { |
| 418 | break; |
| 419 | } |
| 420 | i++; |
| 421 | } |
| 422 | } |
| 423 | i--; |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | } |