private void init(String testClass, String testName, TestState state) { if (writeToFile) { File fileToWrite = createFile(testClass, testName); if (fileToWrite == null) { System.err.println( "Cannot create file for " + testClass + "." + testName + ". Writing to STDOUT"); state.printStream = System.out; } else { try { state.printStream = new PrintStream(new FileOutputStream(fileToWrite)); System.out.println( testClass + "." + testName + " writing to " + fileToWrite.getAbsolutePath()); } catch (FileNotFoundException e) { e.printStackTrace(); state.printStream = System.out; } } } else { state.printStream = System.out; } // sanity check if (state.printStream == null) { throw new IllegalStateException("PrintStream cannot be null!!"); } }
public static boolean isFailed(MPSTreeNode node) { if (node == null || !(node.isLeaf())) { return false; } TestMethodTreeNode leaf = (TestMethodTreeNode) node; TestState state = leaf.getState(); return state.equals(TestState.ERROR) || state.equals(TestState.FAILED); }
/** ***************************************************************************** */ synchronized String getToolTip() { StringBuffer buf = new StringBuffer(); buf.append("<html>"); buf.append("<c><b>TEST "); buf.append(test_name); buf.append("</b></c><hr>"); buf.append("<table cellpadding=0 cellspacing=1 align=left >"); buf.append("<tr><td>STATUS </td><td>"); buf.append(test_status.toString()); buf.append("</td></tr>"); buf.append("<tr><td>STATE</td><td>"); buf.append(test_state.toString()); buf.append("</td></tr>"); if (fail_message != null) { buf.append("<tr><td>ERROR</td><td>"); buf.append(fail_message); buf.append("</td></tr>"); } if (fail_trace != null) { StringTokenizer tok = new StringTokenizer(fail_trace, "\n\t"); String s1 = tok.nextToken(); buf.append("<tr><td>TRACE</td><td>" + s1 + "</td></tr>"); while (tok.hasMoreTokens()) { String s = tok.nextToken(); buf.append("<tr><td></td><td> " + s + "</td></tr>"); } } buf.append("</table>"); return buf.toString(); }
@Override public void analyze(LogEntry logEntry) { String testClass = classMatch(logEntry.thread()); if (testClass == null) { return; } TestState state = getOrCreate(testClass); if (state.started) { state.printStream.println(logEntry.prettyPrint()); state.finished = testFinished(logEntry.message()); if (state.finished) { endTest(testClass, state); } } else { String testMethod = extractTestMethodIfStarting(logEntry.message()); if (state.started = testStarted(testMethod)) { init(testClass, testMethod, state); state.printStream.println(logEntry.prettyPrint()); } } }
public static Context post(URI uri, Object payload, TestState testState, PostFactory postFactory) throws IOException { if (postFactory == null) { postFactory = ApiTesterModule.createPostFactory(); } if (testState == null) { testState = ApiTesterModule.createTestState(); } HttpPost request = postFactory.createPostRequest(uri, payload); // IMPORTANT: we have to get the cookies from the testState before making the request // because this request could add some cookie to the testState (e.g: the response could have // a Set-Cookie header) ApiRequest apiRequest = ApiTestUtil.convertToApiRequest(uri, request, testState.getCookies()); HttpResponse response = testState.client.execute(request); ApiResponse apiResponse = ApiTestUtil.convertToApiResponse(response); return new Context(apiResponse, apiRequest); }
private void test(TestState currentState) throws InterruptedException, InvocationTargetException { synchronized (LOCK) { this.currentState = currentState; System.out.println(this.currentState); List list; if (currentState.getMultiple()) { list = multiple; } else { list = single; } Robot r; try { r = new Robot(); } catch (AWTException e) { throw new RuntimeException(e.getMessage()); } r.delay(10); Point loc = this.getLocationOnScreen(); r.mouseMove(loc.x + 10, loc.y + 10); r.mousePress(InputEvent.BUTTON1_MASK); r.delay(10); r.mouseRelease(InputEvent.BUTTON1_MASK); r.delay(10); list.requestFocusInWindow(); LOCK.wait(ACTION_TIMEOUT); if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != list) { throw new RuntimeException("Test failed - list isn't focus owner."); } list.deselect(0); list.deselect(1); list.deselect(2); list.deselect(3); list.deselect(4); list.deselect(5); list.deselect(6); list.deselect(7); list.deselect(8); int selectIndex = 0; int visibleIndex = 0; if (currentState.getScrollMoved()) { if (currentState.getKeyID() == KeyEvent.VK_PAGE_UP || currentState.getKeyID() == KeyEvent.VK_HOME) { selectIndex = 8; visibleIndex = 8; } else if (currentState.getKeyID() == KeyEvent.VK_PAGE_DOWN || currentState.getKeyID() == KeyEvent.VK_END) { selectIndex = 0; visibleIndex = 0; } } else { if (currentState.getKeyID() == KeyEvent.VK_PAGE_UP || currentState.getKeyID() == KeyEvent.VK_HOME) { if (currentState.getSelectedMoved()) { selectIndex = 1; visibleIndex = 0; } else { selectIndex = 0; visibleIndex = 0; } } else if (currentState.getKeyID() == KeyEvent.VK_PAGE_DOWN || currentState.getKeyID() == KeyEvent.VK_END) { if (currentState.getSelectedMoved()) { selectIndex = 7; visibleIndex = 8; } else { selectIndex = 8; visibleIndex = 8; } } } list.select(selectIndex); list.makeVisible(visibleIndex); r.delay(10); if (currentState.getKeyID() == KeyEvent.VK_HOME || currentState.getKeyID() == KeyEvent.VK_END) { r.keyPress(KeyEvent.VK_CONTROL); } r.delay(10); r.keyPress(currentState.getKeyID()); r.delay(10); r.keyRelease(currentState.getKeyID()); r.delay(10); if (currentState.getKeyID() == KeyEvent.VK_HOME || currentState.getKeyID() == KeyEvent.VK_END) { r.keyRelease(KeyEvent.VK_CONTROL); } r.waitForIdle(); r.delay(200); if (currentState.getTemplate() != currentState.getAction()) throw new RuntimeException("Test failed."); } }
/** ***************************************************************************** */ synchronized boolean handleTestState(Element e) { boolean chng = false; class_name = IvyXml.getAttrString(e, "CLASS"); method_name = IvyXml.getAttrString(e, "METHOD"); test_name = IvyXml.getAttrString(e, "NAME"); test_class = IvyXml.getAttrString(e, "TCLASS"); TestStatus osts = test_status; TestState ost = test_state; boolean ignore = false; annotation_types.clear(); for (Element ane : IvyXml.children(e, "ANNOT")) { String ant = IvyXml.getText(ane); annotation_types.add(ant); if (ant.startsWith("@org.junit.Ignore")) ignore = true; } if (IvyXml.getAttrBool(e, "EMPTY")) ignore = true; String sts = IvyXml.getAttrString(e, "STATUS"); if (sts.equals("FAILURE")) { test_status = TestStatus.FAILURE; if (test_state == TestState.RUNNING || test_state == TestState.UNKNOWN) test_state = TestState.UP_TO_DATE; } else if (sts.equals("SUCCESS")) { test_status = TestStatus.SUCCESS; if (test_state == TestState.RUNNING || test_state == TestState.UNKNOWN) test_state = TestState.UP_TO_DATE; } else { if (ignore) test_state = TestState.IGNORED; test_status = TestStatus.UNKNOWN; count_data = null; } if (osts != test_status) chng = true; if (test_status == TestStatus.FAILURE) { String omsg = fail_message; fail_message = IvyXml.getTextElement(e, "EXCEPTION"); fail_trace = IvyXml.getTextElement(e, "TRACE"); if (fail_trace != null && fail_message == null) { int idx = fail_trace.indexOf("\n"); if (idx < 0) fail_message = fail_trace; else fail_message = fail_trace.substring(0, idx); } if (omsg == null && fail_message != null) chng = true; else if (omsg != null && fail_message == null) chng = true; else if (omsg != null && !omsg.equals(fail_message)) chng = true; } else { fail_message = null; fail_trace = null; } String st = IvyXml.getAttrString(e, "STATE"); if (st != null) { try { test_state = TestState.valueOf(st); } catch (IllegalArgumentException ex) { } } if (ost != test_state) chng = true; Element xe = IvyXml.getChild(e, "COVERAGE"); if (xe != null) count_data = new CountData(xe); if (chng) update(); return chng; }
@Test public void minimumRequiredIsValidSql() throws Exception { ChangeFactory changeFactory = ChangeFactory.getInstance(); for (String changeName : changeFactory.getDefinedChanges()) { if (changeName.equals("addDefaultValue")) { continue; // need to better handle strange "one of defaultValue* is required" logic } if (changeName.equals("changeWithNestedTags") || changeName.equals("sampleChange")) { continue; // not a real change } for (Database database : DatabaseFactory.getInstance().getImplementedDatabases()) { if (database.getShortName() == null) { continue; } TestState state = new TestState( name.getMethodName(), changeName, database.getShortName(), TestState.Type.SQL); state.addComment("Database: " + database.getShortName()); Change change = changeFactory.create(changeName); if (!change.supports(database)) { continue; } if (change.generateStatementsVolatile(database)) { continue; } ChangeMetaData changeMetaData = ChangeFactory.getInstance().getChangeMetaData(change); change.setResourceAccessor(new JUnitResourceAccessor()); for (String paramName : new TreeSet<String>(changeMetaData.getRequiredParameters(database).keySet())) { ChangeParameterMetaData param = changeMetaData.getParameters().get(paramName); Object paramValue = param.getExampleValue(); String serializedValue; serializedValue = formatParameter(paramValue); state.addComment("Change Parameter: " + param.getParameterName() + "=" + serializedValue); param.setValue(change, paramValue); } ValidationErrors errors = change.validate(database); assertFalse( "Validation errors for " + changeMetaData.getName() + " on " + database.getShortName() + ": " + errors.toString(), errors.hasErrors()); SqlStatement[] sqlStatements = change.generateStatements(database); for (SqlStatement statement : sqlStatements) { Sql[] sql = SqlGeneratorFactory.getInstance().generateSql(statement, database); if (sql == null) { System.out.println("Null sql for " + statement + " on " + database.getShortName()); } else { for (Sql line : sql) { String sqlLine = line.toSql(); assertFalse( "Change " + changeMetaData.getName() + " contains 'null' for " + database.getShortName() + ": " + sqlLine, sqlLine.contains(" null ")); state.addValue(sqlLine + ";"); } } } state.test(); } } }
@Test public void extraParamsIsValidSql() throws Exception { ChangeFactory changeFactory = ChangeFactory.getInstance(); for (String changeName : changeFactory.getDefinedChanges()) { if (changeName.equals("addDefaultValue")) { continue; // need to better handle strange "one of defaultValue* is required" logic } for (Database database : DatabaseFactory.getInstance().getImplementedDatabases()) { if (database.getShortName() == null) { continue; } TestState state = new TestState( name.getMethodName(), changeName, database.getShortName(), TestState.Type.SQL); state.addComment("Database: " + database.getShortName()); Change baseChange = changeFactory.create(changeName); if (!baseChange.supports(database)) { continue; } if (baseChange.generateStatementsVolatile(database)) { continue; } ChangeMetaData changeMetaData = ChangeFactory.getInstance().getChangeMetaData(baseChange); ArrayList<String> optionalParameters = new ArrayList<String>(changeMetaData.getOptionalParameters(database).keySet()); Collections.sort(optionalParameters); List<List<String>> paramLists = powerSet(optionalParameters); Collections.sort( paramLists, new Comparator<List<String>>() { @Override public int compare(List<String> o1, List<String> o2) { int comp = Integer.valueOf(o1.size()).compareTo(o2.size()); if (comp == 0) { comp = StringUtils.join(o1, ",").compareTo(StringUtils.join(o2, ",")); } return comp; } }); for (List<String> permutation : paramLists) { Change change = changeFactory.create(changeName); change.setResourceAccessor(new JUnitResourceAccessor()); // for (String paramName : new TreeSet<String>(changeMetaData.getRequiredParameters(database).keySet())) { ChangeParameterMetaData param = changeMetaData.getParameters().get(paramName); Object paramValue = param.getExampleValue(); String serializedValue; serializedValue = formatParameter(paramValue); state.addComment( "Required Change Parameter: " + param.getParameterName() + "=" + serializedValue); param.setValue(change, paramValue); } for (String paramName : permutation) { ChangeParameterMetaData param = changeMetaData.getParameters().get(paramName); if (!param.supports(database)) { continue; } Object paramValue = param.getExampleValue(); String serializedValue; serializedValue = formatParameter(paramValue); state.addComment( "Optional Change Parameter: " + param.getParameterName() + "=" + serializedValue); param.setValue(change, paramValue); } ValidationErrors errors = change.validate(database); assertFalse( "Validation errors for " + changeMetaData.getName() + " on " + database.getShortName() + ": " + errors.toString(), errors.hasErrors()); // // SqlStatement[] sqlStatements = change.generateStatements(database); // for (SqlStatement statement : sqlStatements) { // Sql[] sql = // SqlGeneratorFactory.getInstance().generateSql(statement, database); // if (sql == null) { // System.out.println("Null sql for "+statement+" on // "+database.getShortName()); // } else { // for (Sql line : sql) { // state.addValue(line.toSql()+";"); // } // } // } // state.test(); } } } }