From 8f2927c5422b3fd63f8d3f6924583f006ce73bde Mon Sep 17 00:00:00 2001
From: Kasper Marstal <kaspermarstal@gmail.com>
Date: Thu, 30 Apr 2015 16:17:18 +0200
Subject: [PATCH] COMP: ELASTIX-1 Add support for downloading test data at
 build time

---
 CMakeLists.txt                                | 21 ++++------
 SuperBuild/CMakeLists.txt                     |  6 +--
 Testing/CMakeLists.txt                        | 40 ++++++++++++++++---
 .../BrainProtonDensitySliceR10X13Y17.png.md5  |  1 +
 Testing/elxExampleTest.cxx                    | 16 +++++++-
 5 files changed, 60 insertions(+), 24 deletions(-)
 create mode 100644 Testing/Data/Input/BrainProtonDensitySliceR10X13Y17.png.md5

diff --git a/CMakeLists.txt b/CMakeLists.txt
index c825fcac..ec355d9a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -10,34 +10,29 @@ find_package( ITK REQUIRED )
 include( ${ITK_USE_FILE} )
 include( "${CMAKE_CURRENT_SOURCE_DIR}/CMake/elxRequiredITKModules.cmake" )
 
+#---------------------------------------------------------------------
+# Build Elastix
+
 #---------------------------------------------------------------------
 # Testing
 
 # Do not build tests by default since this requires GoogleTest submodule
-# to be initialized. This saves users that just clone and build from 
-# seeing FATAL_ERRORs out of the box. 
+# to be initialized. Otherwise users may see FATAL_ERRORs out of the box.
+# The SuperBuild will correctly initialize the GoogleTest submodule and 
+# build tests by default.
 option( ELASTIX_BUILD_TESTING "Enable building tests." OFF )
 
 if( ELASTIX_BUILD_TESTING )
   enable_testing()
   add_subdirectory( Testing )
-
-  mark_as_advanced( ELASTIX_BUILD_CTESTS )
-  option( ELASTIX_BUILD_CTESTS "Enable CTests." ON )
-  if( ELASTIX_BUILD_CTESTS )
-    include( CTest )
-  endif()
 endif()
 
-#---------------------------------------------------------------------
-# Build Elastix
-
 #---------------------------------------------------------------------
 # Build Documentation
 
 mark_as_advanced( ELASTIX_BUILD_DOXYGEN )
-option( BUILD_DOXYGEN "Enable building Doxygen documentation." OFF )
+option( ELASTIX_BUILD_DOXYGEN "Enable building Doxygen documentation." OFF )
 
 mark_as_advanced( ELASTIX_BUILD_READTHEDOCS )
-option( BUILD_READTHEDOCS "Enable building readthedocs.org documentation." OFF )
+option( ELASTX_BUILD_READTHEDOCS "Enable building readthedocs.org documentation." OFF )
 
diff --git a/SuperBuild/CMakeLists.txt b/SuperBuild/CMakeLists.txt
index c719db98..12a9ed5a 100644
--- a/SuperBuild/CMakeLists.txt
+++ b/SuperBuild/CMakeLists.txt
@@ -25,10 +25,8 @@ endif()
 # Examples will be build as an external project to verify the installation of elastix
 option( ELASTIX_BUILD_EXAMPLES "Enable building examples." ON )
 
-# Do not build tests by default since this requires GoogleTest submodule
-# to be initialized. This saves users that just clone and build from 
-# seeing FATAL_ERRORs out of the box.
-option( ELASTIX_BUILD_TESTING "Enable building tests." OFF )
+# Build tests by default
+option( ELASTIX_BUILD_TESTING "Enable building tests." ON )
 
 if( ELASTIX_BUILD_TESTING )
   mark_as_advanced( ELASTIX_BUILD_CTESTS )
diff --git a/Testing/CMakeLists.txt b/Testing/CMakeLists.txt
index 83be7813..7343cdc1 100644
--- a/Testing/CMakeLists.txt
+++ b/Testing/CMakeLists.txt
@@ -6,7 +6,7 @@ if( NOT EXISTS "${CMAKE_SOURCE_DIR}/Testing/GoogleTest/.git" )
 git submodule update --init in the source directory to compile tests." )
 endif()
 
-if( ${CMAKE_CXX_COMPILER_ID} STREQUAL "MSVC")
+if( ${CMAKE_CXX_COMPILER_ID} STREQUAL "MSVC" )
   include( ${CMAKE_SOURCE_DIR}/CMake/elxGoogleTestWinConfig.cmake )
 endif()
 
@@ -28,12 +28,40 @@ set( ElastixUnitTestSource
   elxExampleTest.cxx
 )
 
+#---------------------------------------------------------------------
+# Setup ExternalData
+
+# ExternalData module was introduced in CMake version 2.8.11
+cmake_minimum_required( VERSION 2.8.11 )
+
+include( ExternalData )
+
+list( APPEND ExternalData_URL_TEMPLATES
+  "http://midas3.kitware.com/midas/api/rest?method=midas.bitstream.download&checksum=%(hash)&algorithm=%(algo)"
+  "https://midas3.kitware.com/midas/api/rest?method=midas.bitstream.download&checksum=%(hash)&algorithm=%(algo)"
+)
+
+#---------------------------------------------------------------------
+# To add data to a tests, create a list "${filename}Data" (without
+# the filename extension) and add data to this list.
+
+set( elxExampleTestData
+  DATA{Data/Input/BrainProtonDensitySliceR10X13Y17.png}
+)
+
 #---------------------------------------------------------------------
 # Build test executables
 
 foreach( ElastixUnitTestFilename ${ElastixUnitTestSource} )
-  string( REPLACE ".cxx" "" ElastixUnitTestExe ${ElastixUnitTestFilename} )
-  add_executable( ${ElastixUnitTestExe} ${ElastixUnitTestFilename} )
-  target_link_libraries( ${ElastixUnitTestExe} ${ELASTIX_LIBRARIES} ${TEST_LIBRARIES} )
-  add_test( ElastixUnitTests ${ElastixUnitTestExe} )
-endforeach()
\ No newline at end of file
+  string( REPLACE ".cxx" "" ElastixUnitTest ${ElastixUnitTestFilename} )
+  add_executable( ${ElastixUnitTest} ${ElastixUnitTestFilename} )
+  target_link_libraries( ${ElastixUnitTest} ${ELASTIX_LIBRARIES} ${ITK_LIBRARIES} ${TEST_LIBRARIES} )
+
+  ExternalData_Add_Test( "${ElastixUnitTest}ExternalDataTarget"
+    NAME ${ElastixUnitTest}
+    COMMAND ${ElastixUnitTest} ${${ElastixUnitTest}Data}
+  )
+
+  ExternalData_Add_Target( "${ElastixUnitTest}ExternalDataTarget"} )
+endforeach()
+
diff --git a/Testing/Data/Input/BrainProtonDensitySliceR10X13Y17.png.md5 b/Testing/Data/Input/BrainProtonDensitySliceR10X13Y17.png.md5
new file mode 100644
index 00000000..93f3cd12
--- /dev/null
+++ b/Testing/Data/Input/BrainProtonDensitySliceR10X13Y17.png.md5
@@ -0,0 +1 @@
+610392a128986d934dfc0a1b0dc27e91
\ No newline at end of file
diff --git a/Testing/elxExampleTest.cxx b/Testing/elxExampleTest.cxx
index 9c678ed9..1b3cfafc 100644
--- a/Testing/elxExampleTest.cxx
+++ b/Testing/elxExampleTest.cxx
@@ -1,5 +1,19 @@
+#include "itkImage.h"
+#include "itkImageFileReader.h"
+#include "itkImageFileWriter.h"
+
 #include "gtest/gtest.h"
 
-TEST( ExampleGoogleTest, Assert ) {
+TEST( GoogleTest, GoogleTest ) {
+  /**
+   * This example demonstrates the GoogleTest framework.
+   *
+   * When the test is built, the ExternalData CMake module 
+   * scans the Testing/Data directory for content links on  
+   * the form "[filename].[ext].md5" (text files with hashes).
+   * The content links are replaced with files from the file
+   * repository and checked for hash consistensy.
+   */
+
   ASSERT_TRUE( true );
 }
\ No newline at end of file
-- 
GitLab