public void testToSqlDate() throws BirtException { java.sql.Date date = DataTypeUtil.toSqlDate("1999-2-11"); Calendar cal = Calendar.getInstance(); cal.clear(); cal.set(1999, 1, 11); cal.getTime(); assertEquals(cal.getTime(), date); try { java.sql.Date date1 = DataTypeUtil.toSqlDate("99-2-11"); cal.set(99, 1, 11); assertEquals(cal.getTime(), date1); } catch (BirtException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { java.sql.Date date1 = DataTypeUtil.toSqlDate("9921111"); assertEquals(cal.getTime(), date1); fail("Should not arrive here"); } catch (BirtException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
/** * set parameter choice value. The string value is in English locale, and needs to be parsed back * into object value based on the data type. * * @param value the string value for the object * @param type the parameter data type */ public void setValue(String value, int type) { try { switch (type) { case IScalarParameterDefn.TYPE_BOOLEAN: this.value = DataTypeUtil.toBoolean(value); break; case IScalarParameterDefn.TYPE_DATE_TIME: this.value = DataTypeUtil.toDate(value); break; case IScalarParameterDefn.TYPE_DECIMAL: this.value = DataTypeUtil.toBigDecimal(value); break; case IScalarParameterDefn.TYPE_FLOAT: this.value = DataTypeUtil.toDouble(value); break; case IScalarParameterDefn.TYPE_INTEGER: this.value = DataTypeUtil.toInteger(value); break; case IScalarParameterDefn.TYPE_DATE: this.value = DataTypeUtil.toSqlDate(value); break; case IScalarParameterDefn.TYPE_TIME: this.value = DataTypeUtil.toSqlTime(value); break; case IScalarParameterDefn.TYPE_STRING: default: this.value = DataTypeUtil.toString(value); break; } } catch (BirtException e) { log.log(Level.SEVERE, e.getLocalizedMessage(), e); this.value = null; } }
/** Test passing reportContext */ @SuppressWarnings("unchecked") @Test public void testExecute1() { try { final IReportEngine reportEngine = ReportEngine.getReportEngine(); final InputStream is = this.getClass().getResourceAsStream("/reports/test_display_parameters.rptdesign"); final IReportRunnable design = reportEngine.openReportDesign(is); final IGetParameterDefinitionTask paramTask = reportEngine.createGetParameterDefinitionTask(design); List<EngineException> errors = null; try { final IRunAndRenderTask rrTask = reportEngine.createRunAndRenderTask(design); final Map<String, Object> appContext = rrTask.getAppContext(); final ClassLoader classLoader = getClass().getClassLoader(); appContext.put(EngineConstants.APPCONTEXT_CLASSLOADER_KEY, classLoader); // rrTask.setAppContext(appContext); try { final ByteArrayOutputStream os = new ByteArrayOutputStream(); final RenderOption options = new HTMLRenderOption(); options.setOutputFormat("HTML"); options.setOutputStream(os); rrTask.setRenderOption(options); rrTask.run(); errors = rrTask.getErrors(); String output = os.toString("utf-8"); System.out.println(output); Assert.assertTrue(output.indexOf("Australian Collectors, Co.") >= 0); Assert.assertTrue(output.indexOf("NewParameter") >= 0); Assert.assertTrue(output.indexOf("abc") >= 0); } finally { rrTask.close(); } } finally { paramTask.close(); } if (errors != null) { Iterator<EngineException> iterator = errors.iterator(); if (iterator.hasNext()) { EngineException error = iterator.next(); Assert.fail("Engine exception: " + error.getMessage()); } } } catch (UnsupportedEncodingException e) { e.printStackTrace(); Assert.fail(e.toString()); } catch (BirtException e) { e.printStackTrace(); Assert.fail(e.toString()); } }
/** @throws Exception */ @Test public void testInvalidBinding() throws Exception { QueryDefinition queryDefn = newReportQuery(); // column mapping String[] name = new String[] {"testColumn1"}; ScriptExpression[] se = new ScriptExpression[name.length]; se[0] = new ScriptExpression("dataSetRow.COUNTRY"); for (int i = 0; i < name.length; i++) queryDefn.addBinding(new Binding(name[i], se[i])); IResultIterator ri = executeQuery(queryDefn); if (ri.next()) { try { ri.getValue(name[0]); } catch (BirtException e) { assertTrue(e.getErrorCode() == ResourceConstants.INVALID_JS_EXPR); } } ri.close(); }
/** layout page footer area */ protected void layoutFooter() { IContent footerContent = pageContent.getPageFooter(); if (footerContent != null) { DimensionType h = pageContent.getFooterHeight(); if (h == null) { h = new DimensionType(0.5f, DimensionType.UNITS_IN); } footerContent.setHeight(h); footer.content = footerContent; boolean autoPageBreak = context.isAutoPageBreak(); context.setAutoPageBreak(false); RegionLayoutEngine rle = new RegionLayoutEngine(footer, context); try { rle.layout(footerContent); } catch (BirtException e) { logger.log(Level.WARNING, e.getMessage(), e); } context.setAutoPageBreak(autoPageBreak); } }
@SuppressWarnings("unchecked") public static void addBirtException( IReportContext reportContext, String errorMessage, Integer severity) { BirtException be = new BirtException("org.eclipse.birt.report.engine", errorMessage, new Object[] {""}); be.setSeverity(severity); try { // get the protect field 'context' from reportContext @SuppressWarnings("rawtypes") Class rciClass = reportContext.getClass(); Field fieldFromScript = rciClass.getDeclaredField("context"); if (fieldFromScript == null) { return; } // instantiate the ExecutionContext object that // populates the context field fieldFromScript.setAccessible(true); Object execContext = fieldFromScript.get(reportContext); // now get a handle to the addException method on ExecutionObject @SuppressWarnings("rawtypes") Class execClass = execContext.getClass(); Method addMethod = execClass.getMethod("addException", new Class[] {BirtException.class}); // finally invoke the method which will add the BirtException // to the report addMethod.setAccessible(true); addMethod.invoke(execContext, new Object[] {be}); // Lots of ways for this to break... } catch (Exception e) { logger.warning(e.getMessage()); e.printStackTrace(); } return; }
public void testToBoolean() { Boolean result; for (int i = 0; i < testObject.length; i++) { System.out.println(i); try { result = DataTypeUtil.toBoolean(testObject[i]); if (resultBoolean[i] instanceof Exception) fail("Should throw Exception."); assertEquals(result, resultBoolean[i]); } catch (BirtException dteEx) { if (!(resultBoolean[i] instanceof Exception)) fail("Should not throw Exception."); } } try { assertTrue(DataTypeUtil.toBoolean(new Double(0.1)).booleanValue()); assertTrue(DataTypeUtil.toBoolean(new Double(-0.1)).booleanValue()); assertTrue(DataTypeUtil.toBoolean(new Double(1)).booleanValue()); assertTrue(DataTypeUtil.toBoolean(new Double(1)).booleanValue()); assertFalse(DataTypeUtil.toBoolean(new Double(0)).booleanValue()); } catch (BirtException e) { // TODO Auto-generated catch block e.printStackTrace(); } }