Functions

mitkPicFileWriterTest.cpp File Reference

#include "mitkPicFileWriter.h"
#include "mitkDataNodeFactory.h"
#include "mitkTestingMacros.h"

Go to the source code of this file.

Functions

int mitkPicFileWriterTest (int argc, char *argv[])

Function Documentation

int mitkPicFileWriterTest ( int  argc,
char *  argv[] 
)

Simple example for a test for the (non-existent) class "PicFileWriter".

argc and argv are the command line parameters which were passed to the ADD_TEST command in the CMakeLists.txt file. For the automatic tests, argv is either empty for the simple tests or contains the filename of a test image for the image tests (see CMakeLists.txt).

Definition at line 30 of file mitkPicFileWriterTest.cpp.

References EXIT_FAILURE, EXIT_SUCCESS, MITK_TEST_BEGIN, MITK_TEST_CONDITION_REQUIRED, MITK_TEST_END, MITK_TEST_FOR_EXCEPTION_BEGIN, MITK_TEST_FOR_EXCEPTION_END, mitk::DataNodeFactory::New(), and mitk::PicFileWriter::New().

{
  // always start with this!
  MITK_TEST_BEGIN("PicFileWriter")

    // let's create an object of our class  
    mitk::PicFileWriter::Pointer myPicFileWriter = mitk::PicFileWriter::New();

  // first test: did this work?
  // using MITK_TEST_CONDITION_REQUIRED makes the test stop after failure, since
  // it makes no sense to continue without an object.
  MITK_TEST_CONDITION_REQUIRED(myPicFileWriter.IsNotNull(),"Testing instantiation") 

    // load image
    std::cout << "Loading file: " << std::flush;
  if(argc==0)
  {
    std::cout<<"no file specified [FAILED]"<<std::endl;
    return EXIT_FAILURE;
  }
  mitk::Image::Pointer image = NULL;

  try{  
    // test for exception handling of NULL image
    std::cout << "Testing handling of NULL image " << std::flush;
    MITK_TEST_FOR_EXCEPTION_BEGIN(itk::ExceptionObject)

    myPicFileWriter->SetInput(image);
    myPicFileWriter->SetFileName("/usr/bin");
    myPicFileWriter->Update(); 
    MITK_TEST_FOR_EXCEPTION_END(itk::ExceptionObject)
  }
  catch(...) {
    std::cout << "Success: Writer warns on NULL image." << std::endl;
  }

  mitk::DataNodeFactory::Pointer factory = mitk::DataNodeFactory::New();
  try
  {
    std::cout<<argv[1]<<std::endl;
    factory->SetFileName( argv[1] );
    factory->Update();

    if(factory->GetNumberOfOutputs()<1)
    {
      std::cout<<"file could not be loaded [FAILED]"<<std::endl;
      return EXIT_FAILURE;
    }
    mitk::DataNode::Pointer node = factory->GetOutput( 0 );
    image = dynamic_cast<mitk::Image*>(node->GetData());
    if(image.IsNull())
    {
      std::cout<<"file "<< argv[1]<< "is not an image - test will not be applied [PASSED]"<<std::endl;
      std::cout<<"[TEST DONE]"<<std::endl;
      return EXIT_SUCCESS;
    }
  }
  catch ( itk::ExceptionObject & ex )
  {
    std::cout << "Exception: " << ex << "[FAILED]" << std::endl;
    return EXIT_FAILURE;
  }

  MITK_TEST_CONDITION_REQUIRED(image.IsNotNull(),"loaded image not NULL")

  try{  
    // test for exception handling
    MITK_TEST_FOR_EXCEPTION_BEGIN(itk::ExceptionObject)

    myPicFileWriter->SetInput(image);
    myPicFileWriter->SetFileName("/usr/bin");
    myPicFileWriter->Update(); 
    MITK_TEST_FOR_EXCEPTION_END(itk::ExceptionObject)
  }
  catch(...) {
    //this means that a wrong exception (i.e. no itk:Exception) has been thrown 
    std::cout << "Wrong exception (i.e. no itk:Exception) caught during write [FAILED]" << std::endl;
    return EXIT_FAILURE;
  }

  // always end with this!
  MITK_TEST_END()
}
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines