Commit 3fbae751 authored by Bill Lorensen's avatar Bill Lorensen Committed by Ben Boeckel
Browse files

Testing: catch and check error messages

These tests purposefully trigger errors in order to test that they
occur. Use vtkTestErrorObserver to catch these errors rather than
printing them as output and causing the test to fail.
parent f9301564
......@@ -22,10 +22,29 @@
#include <vtkArrayPrint.h>
#include <vtkSparseArray.h>
#include <vtkSmartPointer.h>
#include <vtkTestErrorObserver.h>
#include <iostream>
#include <stdexcept>
#define CHECK_ERROR_MSG(observer, msg) \
{ \
std::string expectedMsg(msg); \
if (!observer->GetError()) \
{ \
std::cout << "ERROR: Failed to catch any error. Expected the error message to contain \"" << expectedMsg << std::endl; \
} \
else \
{ \
std::string gotMsg(observer->GetErrorMessage()); \
if (gotMsg.find(expectedMsg) == std::string::npos) \
{ \
std::cout << "ERROR: Error message does not contain \"" << expectedMsg << "\" got \n\"" << gotMsg << std::endl; \
} \
} \
} \
observer->Clear()
#define test_expression(expression) \
{ \
if(!(expression)) \
......@@ -49,11 +68,16 @@ int TestSparseArrayValidation(int vtkNotUsed(argc), char *vtkNotUsed(argv)[])
array->AddValue(0, 1, 3);
test_expression(array->Validate());
vtkSmartPointer<vtkTest::ErrorObserver> errorObserver =
vtkSmartPointer<vtkTest::ErrorObserver>::New();
array->AddObserver(vtkCommand::ErrorEvent, errorObserver);
array->Clear();
array->AddValue(0, 0, 1);
array->AddValue(1, 2, 2);
array->AddValue(0, 0, 4);
test_expression(!array->Validate());
CHECK_ERROR_MSG(errorObserver,
"Array contains 1 duplicate coordinates");
array->Clear();
array->AddValue(0, 0, 1);
......
......@@ -14,12 +14,16 @@
=========================================================================*/
#include "vtkGenericCell.h"
#include "vtkMath.h"
#include "vtkSmartPointer.h"
#include "vtkTestErrorObserver.h"
int TestGenericCell(int , char *[])
{
int rval = 0;
vtkGenericCell *cell = vtkGenericCell::New();
vtkSmartPointer<vtkTest::ErrorObserver> errorObserver =
vtkSmartPointer<vtkTest::ErrorObserver>::New();
cell->AddObserver(vtkCommand::ErrorEvent, errorObserver);
for(int i=0; i<VTK_NUMBER_OF_CELL_TYPES;++i)
{
cell->SetCellType( i );
......
......@@ -21,6 +21,7 @@
#include "vtkRegressionTestImage.h"
#include "vtkTestUtilities.h"
#include "vtkTestErrorObserver.h"
#include "vtkExecutive.h"
#include "vtkGlyph3D.h"
#include "vtkSmartPointer.h"
#include "vtkDoubleArray.h"
......@@ -29,6 +30,24 @@
#include "vtkCamera.h"
#include "vtkCommand.h"
#define CHECK_ERROR_MSG(observer, msg) \
{ \
std::string expectedMsg(msg); \
if (!observer->GetError()) \
{ \
std::cout << "ERROR: Failed to catch any error. Expected the error message to contain \"" << expectedMsg << std::endl; \
} \
else \
{ \
std::string gotMsg(observer->GetErrorMessage()); \
if (gotMsg.find(expectedMsg) == std::string::npos) \
{ \
std::cout << "ERROR: Error message does not contain \"" << expectedMsg << "\" got \n\"" << gotMsg << std::endl; \
} \
} \
} \
observer->Clear()
static bool TestGlyph3D_WithBadArray()
{
vtkSmartPointer<vtkDoubleArray> vectors =
......@@ -58,12 +77,16 @@ static bool TestGlyph3D_WithBadArray()
glyph3D->SetInputData(polydata);
glyph3D->SetInputArrayToProcess(1,0,0,vtkDataObject::FIELD_ASSOCIATION_POINTS,"Normals");
glyph3D->SetVectorModeToUseVector();
vtkSmartPointer<vtkTest::ErrorObserver> errorObserver =
vtkSmartPointer<vtkTest::ErrorObserver> errorObserver1 =
vtkSmartPointer<vtkTest::ErrorObserver>::New();
vtkSmartPointer<vtkTest::ErrorObserver> errorObserver2 =
vtkSmartPointer<vtkTest::ErrorObserver>::New();
glyph3D->AddObserver(vtkCommand::ErrorEvent,errorObserver);
glyph3D->AddObserver(vtkCommand::ErrorEvent,errorObserver1);
glyph3D->GetExecutive()->AddObserver(vtkCommand::ErrorEvent,errorObserver2);
glyph3D->Update();
bool res = errorObserver->GetError();
return res;
CHECK_ERROR_MSG(errorObserver1, "vtkDataArray Normals has more than 3 components");
CHECK_ERROR_MSG(errorObserver2, "Algorithm vtkGlyph3D");
return true;
}
int TestGlyph3D(int argc, char* argv[])
......
......@@ -25,6 +25,8 @@
#include "vtkObjectFactory.h"
#include "vtkTestUtilities.h"
#include "vtkTestErrorObserver.h"
#include "vtkNew.h"
#include "vtkRegressionTestImage.h"
#include "vtkCompositeRenderManager.h"
#include "vtkDataSetReader.h"
......@@ -224,7 +226,10 @@ void MyProcess::Execute()
vtkClearZPass *clearZ=vtkClearZPass::New();
clearZ->SetDepth(0.9);
vtkNew<vtkTest::ErrorObserver> errorObserver1;
vtkCompositeRGBAPass *compositeRGBAPass=vtkCompositeRGBAPass::New();
compositeRGBAPass->AddObserver(vtkCommand::ErrorEvent,
errorObserver1.GetPointer());
compositeRGBAPass->SetController(this->Controller);
compositeRGBAPass->SetKdtree(dd->GetKdtree());
vtkRenderPassCollection *passes=vtkRenderPassCollection::New();
......@@ -306,6 +311,17 @@ void MyProcess::Execute()
}
else
{
std::string gotMsg(errorObserver1->GetErrorMessage());
std::string expectedMsg("Missing required OpenGL extensions");
if (gotMsg.find(expectedMsg) == std::string::npos) \
{
std::cout << "ERROR: Error message does not contain \"" << expectedMsg << "\" got \n\"" << gotMsg << std::endl;
this->ReturnValue=vtkTesting::FAILED;
}
else
{
std::cout << expectedMsg << std::endl;
}
this->ReturnValue=vtkTesting::PASSED; // not supported.
}
......
......@@ -5,6 +5,27 @@
#include "vtkStatisticsAlgorithm.h"
#include "vtkTable.h"
#include "vtkTestErrorObserver.h"
#include "vtkExecutive.h"
#define CHECK_ERROR_MSG(observer, msg) \
{ \
std::string expectedMsg(msg); \
if (!observer->GetError()) \
{ \
std::cout << "ERROR: Failed to catch any error. Expected the error message to contain \"" << expectedMsg << std::endl; \
} \
else \
{ \
std::string gotMsg(observer->GetErrorMessage()); \
if (gotMsg.find(expectedMsg) == std::string::npos) \
{ \
std::cout << "ERROR: Error message does not contain \"" << expectedMsg << "\" got \n\"" << gotMsg << std::endl; \
} \
} \
} \
observer->Clear()
//----------------------------------------------------------------------------
int TestComputeQuartiles(int , char * [])
{
......@@ -49,8 +70,11 @@ int TestComputeQuartiles(int , char * [])
// Run Compute Quantiles
vtkNew<vtkComputeQuartiles> quartiles;
vtkNew<vtkTest::ErrorObserver> errorObserver1;
// First verify that absence of input does not cause trouble
quartiles->GetExecutive()->AddObserver(vtkCommand::ErrorEvent,errorObserver1.GetPointer());
quartiles->Update();
CHECK_ERROR_MSG(errorObserver1, "Input port 0 of algorithm vtkComputeQuartiles");
// Now set the real input table
quartiles->SetInputData(vtkStatisticsAlgorithm::INPUT_DATA, table.GetPointer());
......
......@@ -22,8 +22,29 @@
#include "vtkStringArray.h"
#include "vtkTable.h"
#include "vtkTestErrorObserver.h"
#include "vtkExecutive.h"
#include <sstream>
#define CHECK_ERROR_MSG(observer, msg) \
{ \
std::string expectedMsg(msg); \
if (!observer->GetError()) \
{ \
std::cout << "ERROR: Failed to catch any error. Expected the error message to contain \"" << expectedMsg << std::endl; \
} \
else \
{ \
std::string gotMsg(observer->GetErrorMessage()); \
if (gotMsg.find(expectedMsg) == std::string::npos) \
{ \
std::cout << "Error message does not contain \"" << expectedMsg << "\" got \n\"" << gotMsg << std::endl; \
} \
} \
} \
observer->Clear()
//----------------------------------------------------------------------------
const double densities[] = {
0.00013383,
......@@ -99,8 +120,11 @@ int TestExtractFunctionalBagPlot(int , char * [])
vtkNew<vtkExtractFunctionalBagPlot> ebp;
vtkNew<vtkTest::ErrorObserver> errorObserver1;
// First verify that absence of input does not cause trouble
ebp->GetExecutive()->AddObserver(vtkCommand::ErrorEvent,errorObserver1.GetPointer());
ebp->Update();
CHECK_ERROR_MSG(errorObserver1, "Input port 0 of algorithm vtkExtractFunctionalBagPlot");
ebp->SetInputData(0, table.GetPointer());
ebp->SetInputData(1, inTableDensity.GetPointer());
......
/*=========================================================================
Program: Visualization Toolkit
Module: TestSimplePointsReaderWriter.cxx
Module: TestMFIXReader.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
......@@ -13,6 +13,7 @@
=========================================================================*/
#include <vtkSmartPointer.h>
#include <vtkExecutive.h>
#include <vtkDataSetMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
......@@ -26,34 +27,44 @@
#include <vtkTestErrorObserver.h>
#include <vtkRegressionTestImage.h>
#define CHECK_ERROR_MSG(observer, msg) \
{ \
std::string expectedMsg(msg); \
if (!observer->GetError()) \
{ \
std::cout << "ERROR: Failed to catch any error. Expected the error message to contain \"" << expectedMsg << std::endl; \
} \
else \
{ \
std::string gotMsg(observer->GetErrorMessage()); \
if (gotMsg.find(expectedMsg) == std::string::npos) \
{ \
std::cout << "ERROR: Error message does not contain \"" << expectedMsg << "\" got \n\"" << gotMsg << std::endl; \
} \
} \
} \
observer->Clear()
int TestMFIXReader(int argc, char *argv[])
{
// Read file name.
char* filename =
vtkTestUtilities::ExpandDataFileName(argc, argv, "Data/MFIXReader/BUB01.RES");
vtkSmartPointer<vtkTest::ErrorObserver> errorObserver =
vtkSmartPointer<vtkTest::ErrorObserver> errorObserver1 =
vtkSmartPointer<vtkTest::ErrorObserver>::New();
vtkSmartPointer<vtkTest::ErrorObserver> errorObserver2 =
vtkSmartPointer<vtkTest::ErrorObserver>::New();
vtkSmartPointer<vtkMFIXReader> reader =
vtkSmartPointer<vtkMFIXReader>::New();
reader->AddObserver(vtkCommand::ErrorEvent, errorObserver);
reader->AddObserver(vtkCommand::ErrorEvent, errorObserver1);
reader->GetExecutive()->AddObserver(vtkCommand::ErrorEvent, errorObserver2);
// Update without a filename should cause an error
reader->Update();
// Check for model bounds error
if (errorObserver->GetError())
{
std::cout << "Caught expected error: "
<< errorObserver->GetErrorMessage();
}
else
{
std::cout << "Failed to catch expected error regarding missing file name" << std::endl;
return EXIT_FAILURE;
}
errorObserver->Clear();
CHECK_ERROR_MSG(errorObserver1,
"No filename specified");
reader->SetFileName(filename);
delete [] filename;
......
......@@ -19,13 +19,33 @@
-------------------------------------------------------------------------*/
#include <vtkFixedWidthTextReader.h>
#include <vtkNew.h>
#include <vtkStringArray.h>
#include <vtkTable.h>
#include <vtkVariant.h>
#include <vtkVariantArray.h>
#include <vtkTestUtilities.h>
#include <vtkTestErrorObserver.h>
#include <vtkIOStream.h>
#define CHECK_ERROR_MSG(observer, msg) \
{ \
std::string expectedMsg(msg); \
if (!observer->GetError()) \
{ \
std::cout << "ERROR: Failed to catch any error. Expected the error message to contain \"" << expectedMsg << std::endl; \
} \
else \
{ \
std::string gotMsg(observer->GetErrorMessage()); \
if (gotMsg.find(expectedMsg) == std::string::npos) \
{ \
std::cout << "ERROR: Error message does not contain \"" << expectedMsg << "\" got \n\"" << gotMsg << std::endl; \
} \
} \
} \
observer->Clear()
int TestFixedWidthTextReader(int argc, char *argv[])
{
std::cout << "### Pass 1: No headers, field width 10, do not strip whitespace" << std::endl;
......@@ -36,12 +56,17 @@ int TestFixedWidthTextReader(int argc, char *argv[])
std::cout << "Filename: " << filename << std::endl;
vtkNew<vtkTest::ErrorObserver> errorObserver1;
vtkFixedWidthTextReader *reader = vtkFixedWidthTextReader::New();
reader->SetHaveHeaders(false);
reader->SetFieldWidth(10);
reader->StripWhiteSpaceOff();
reader->SetFileName(filename);
reader->SetTableErrorObserver(errorObserver1.GetPointer());
reader->Update();
CHECK_ERROR_MSG(errorObserver1,
"Incorrect number of tuples in SetRow. Expected 4, but got 6");
std::cout << "Printing reader info..." << std::endl;
reader->Print(std::cout);
......@@ -93,7 +118,11 @@ int TestFixedWidthTextReader(int argc, char *argv[])
reader->SetFieldWidth(10);
reader->StripWhiteSpaceOn();
reader->SetFileName(filename);
reader->SetTableErrorObserver(errorObserver1.GetPointer());
reader->Update();
CHECK_ERROR_MSG(errorObserver1,
"Incorrect number of tuples in SetRow. Expected 4, but got 6");
table = reader->GetOutput();
......
......@@ -22,6 +22,7 @@
// this test.
#include "vtkSQLiteDatabase.h"
#include "vtkSmartPointer.h"
#include "vtkSQLQuery.h"
#include "vtkSQLDatabaseSchema.h"
#include "vtkRowQueryToTable.h"
......@@ -31,8 +32,28 @@
#include "vtkVariantArray.h"
#include "DatabaseSchemaWith2Tables.h"
#include "vtkTestErrorObserver.h"
#include <vector>
#define CHECK_ERROR_MSG(observer, msg) \
{ \
std::string expectedMsg(msg); \
if (!observer->GetError()) \
{ \
std::cout << "ERROR: Failed to catch any error. Expected the error message to contain \"" << expectedMsg << std::endl; \
} \
else \
{ \
std::string gotMsg(observer->GetErrorMessage()); \
if (gotMsg.find(expectedMsg) == std::string::npos) \
{ \
std::cout << "ERROR: Error message does not contain \"" << expectedMsg << "\" got \n\"" << gotMsg << std::endl; \
} \
} \
} \
observer->Clear()
int TestSQLiteDatabase( int /*argc*/, char* /*argv*/[])
{
bool status;
......@@ -49,7 +70,13 @@ int TestSQLiteDatabase( int /*argc*/, char* /*argv*/[])
cerr << ">>>>> Testing creation modes." << endl;
vtkSmartPointer<vtkTest::ErrorObserver> errorObserver =
vtkSmartPointer<vtkTest::ErrorObserver>::New();
vtkSmartPointer<vtkTest::ErrorObserver> queryObserver =
vtkSmartPointer<vtkTest::ErrorObserver>::New();
vtkSQLiteDatabase* db1 = vtkSQLiteDatabase::SafeDownCast( vtkSQLDatabase::CreateFromURL( "sqlite://local.db" ) );
status = db1->Open("", vtkSQLiteDatabase::CREATE_OR_CLEAR);
vtkSQLQuery* query1 = db1->GetQueryInstance();
query1->SetQuery("CREATE TABLE test (id INTEGER)");
......@@ -67,12 +94,15 @@ int TestSQLiteDatabase( int /*argc*/, char* /*argv*/[])
query1->Delete();
vtkSQLiteDatabase* db2 = vtkSQLiteDatabase::SafeDownCast( vtkSQLDatabase::CreateFromURL( "sqlite://local.db" ) );
db2->AddObserver(vtkCommand::ErrorEvent, errorObserver);
status = db2->Open("", vtkSQLiteDatabase::CREATE);
if ( status )
{
cerr << "Using CREATE on an existing file should have failed but did not.\n";
return 1;
}
CHECK_ERROR_MSG(errorObserver,
"You specified creating a database but the file exists");
db2->Delete();
vtkSQLiteDatabase* db3 = vtkSQLiteDatabase::SafeDownCast( vtkSQLDatabase::CreateFromURL( "sqlite://local.db" ) );
......@@ -100,12 +130,15 @@ int TestSQLiteDatabase( int /*argc*/, char* /*argv*/[])
return 1;
}
vtkSQLQuery* query4 = db4->GetQueryInstance();
query4->AddObserver(vtkCommand::ErrorEvent, queryObserver);
query4->SetQuery("SELECT * from test");
if (query4->Execute())
{
cerr << "Select query succeeded when it shouldn't have." << endl;
return 1;
}
CHECK_ERROR_MSG(queryObserver,
"Query is not null but prepared statement is");
db4->Delete();
query4->Delete();
......
......@@ -15,6 +15,26 @@
#include <vtkSmartPointer.h>
#include <vtkXMLGenericDataObjectReader.h>
#include <vtkXMLDataParser.h>
#include "vtkTestErrorObserver.h"
#define CHECK_ERROR_MSG(observer, msg) \
{ \
std::string expectedMsg(msg); \
if (!observer->GetError()) \
{ \
std::cout << "ERROR: Failed to catch any error. Expected the error message to contain \"" << expectedMsg << std::endl; \
} \
else \
{ \
std::string gotMsg(observer->GetErrorMessage()); \
if (gotMsg.find(expectedMsg) == std::string::npos) \
{ \
std::cout << "ERROR: Error message does not contain \"" << expectedMsg << "\" got \n\"" << gotMsg << std::endl; \
} \
} \
} \
observer->Clear()
int TestXMLReaderBadData(int argc, char* argv[])
{
......@@ -28,11 +48,23 @@ int TestXMLReaderBadData(int argc, char* argv[])
std::string inputFilename = argv[1];
// Observe errors
vtkSmartPointer<vtkTest::ErrorObserver> errorObserver0 =
vtkSmartPointer<vtkTest::ErrorObserver>::New();
vtkSmartPointer<vtkTest::ErrorObserver> errorObserver1 =
vtkSmartPointer<vtkTest::ErrorObserver>::New();
vtkSmartPointer<vtkTest::ErrorObserver> errorObserver2 =
vtkSmartPointer<vtkTest::ErrorObserver>::New();
// Read the file
vtkSmartPointer<vtkXMLGenericDataObjectReader> reader =
vtkSmartPointer<vtkXMLGenericDataObjectReader>::New();
reader->SetFileName(inputFilename.c_str());
reader->AddObserver(vtkCommand::ErrorEvent, errorObserver0);
reader->SetReaderErrorObserver(errorObserver1);
reader->SetParserErrorObserver(errorObserver2);
reader->Update();
CHECK_ERROR_MSG(errorObserver2,
"vtkXMLDataParser");
return EXIT_SUCCESS;
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkSeedRepresentationTest1.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkSeedRepresentation.h"
#include <cstdlib>
......@@ -8,10 +22,29 @@
#include "vtkProperty2D.h"
#include "WidgetTestingMacros.h"
#include "vtkTestErrorObserver.h"
#include <vtkTextActor.h>
#include <vtkPointHandleRepresentation3D.h>
#define CHECK_ERROR_MSG(observer, msg) \
{ \
std::string expectedMsg(msg); \
if (!observer->GetError()) \
{ \
std::cout << "Failed to catch any error. Expected the error message to contain \"" << expectedMsg << std::endl; \
} \
else \
{ \
std::string gotMsg(observer->GetErrorMessage()); \
if (gotMsg.find(expectedMsg) == std::string::npos) \
{ \
std::cout << "Error message does not contain \"" << expectedMsg << "\" got \n\"" << gotMsg << std::endl; \
} \
} \
} \
observer->Clear()
int vtkSeedRepresentationTest1(int , char * [] )
{
vtkSmartPointer< vtkSeedRepresentation > node1 = vtkSmartPointer< vtkSeedRepresentation >::New();
......@@ -23,10 +56,18 @@ int vtkSeedRepresentationTest1(int , char * [] )
double pos[3] = {1.0, 2.0, -3.0};
double pos2[3];
int s = 0;
// set/get display and world should fail without seeds
vtkSmartPointer<vtkTest::ErrorObserver> errorObserver =
vtkSmartPointer<vtkTest::ErrorObserver>::New();
node1->AddObserver(vtkCommand::ErrorEvent, errorObserver);
node1->SetSeedDisplayPosition(s, pos);
CHECK_ERROR_MSG(errorObserver, "Trying to access non-existent handle");
node1->GetSeedWorldPosition(s, pos2);
CHECK_ERROR_MSG(errorObserver, "Trying to access non-existent handle");
node1->GetSeedDisplayPosition(s,pos);
CHECK_ERROR_MSG(errorObserver, "Trying to access non-existent handle");
// set/get display and world position will fail without seeds having been
// created, so add some and then do the testing of return values.
......
......@@ -32,6 +32,7 @@
#include "vtkTestUtilities.h"
#include "vtkRegressionTestImage.h"
#include "vtkNew.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderWindow.h"
......@@ -81,6 +82,8 @@
#include "vtkTreeCompositer.h"
#include "vtkOpenGLRenderWindow.h"
#include "vtkTestErrorObserver.h"
namespace
{
......@@ -177,21 +180,27 @@ void MyProcess::Execute()
vtkCameraPass *opaqueCameraPass=vtkCameraPass::New();
opaqueCameraPass->SetDelegatePass(opaqueSequence);
vtkNew<vtkTest::ErrorObserver> errorObserver2;
vtkShadowMapBakerPass *shadowsBaker=vtkShadowMapBakerPass::New();
shadowsBaker->SetOpaquePass(opaqueCameraPass);
shadowsBaker->SetResolution(1024);
// To cancel self-shadowing.
shadowsBaker->SetPolygonOffsetFactor(3.1f);
shadowsBaker->SetPolygonOffsetUnits(10.0f);
shadowsBaker->AddObserver(
vtkCommand::ErrorEvent, errorObserver2.GetPointer());