public void testValidateInvalidHTMLQuestion() throws Exception { // HTMLQuestion has invalid FrameHeight try { QAPValidator.validateFile(defaultInvalidHTMLQuestionFileName); fail("Expected ValidationException when previewing a HIT with an invalid HTMLQuestion"); } catch (ValidationException e) { // Expected exception assertContains("ValidationFailure was not caused by an invalid frame height.", "'I am not a number; I am a free man!' is not a valid value for 'integer'", e.getMessage()); } }
/** * Returns a preview of the HIT as HTML * * @param input the input needed for the HIT * @param props the properties of the HIT * @param question the question asked in the HIT * @return an HTML preview of the HIT * @throws Exception */ public String previewHIT(HITDataInput input, HITProperties props, HITQuestion question) throws Exception { if (props == null || question == null) throw new IllegalArgumentException(); String questionXML = null; if (input != null) { Map<String, String> inputMap = input.getRowAsMap(1); questionXML = question.getQuestion(inputMap); } else { questionXML = question.getQuestion(); } // Validate question before preview QAPValidator.validate(questionXML); String questionPreview = XslTransformer.convertQAPtoHTML(questionXML); InputStream headerURL = this.getClass().getResourceAsStream("previewHITHeader.xml"); InputStream footerURL = this.getClass().getResourceAsStream("previewHITFooter.xml"); if (headerURL == null) { log.error("Error reading the preview header file."); } if (footerURL == null) { log.error("Error reading the preview footer file."); } BufferedReader headerReader = new BufferedReader(new InputStreamReader(headerURL)); BufferedReader footerReader = new BufferedReader(new InputStreamReader(footerURL)); String thisLine = null; String header = ""; String footer = ""; while ((thisLine = headerReader.readLine()) != null) { header += thisLine + System.getProperty("line.separator"); } while ((thisLine = footerReader.readLine()) != null) { footer += thisLine + System.getProperty("line.separator"); } headerReader.close(); footerReader.close(); NumberFormat rewardFormatter = NumberFormat.getInstance(); rewardFormatter.setMaximumFractionDigits(2); rewardFormatter.setMinimumFractionDigits(2); Map<String, String> headerMap = new HashMap<String, String>(); headerMap.put("requester", "[Your Requester Name Here]"); headerMap.put("title", props.getTitle()); headerMap.put("description", props.getDescription()); headerMap.put("keywords", props.getKeywords()); headerMap.put("reward", rewardFormatter.format(props.getRewardAmount())); String mergedHeader = VelocityUtil.doMerge(header, headerMap); String previewString = mergedHeader + questionPreview + footer; return previewString; }
public void testValidateHTMLQuestion() throws Exception { // Note that the SDK does not validate the HTML CDATA; // it only validates the surrounding XML. QAPValidator.validateFile(defaultHTMLQuestionFileName); }