From 19078705f85e1ba8529032857d3b1396e9502a39 Mon Sep 17 00:00:00 2001
From: Janik Zikovsky <zikovskyjl@ornl.gov>
Date: Wed, 12 Jan 2011 20:13:31 +0000
Subject: [PATCH] Refs #2167: ManagedWorkspaceTest failed if run in a different
 folder.

---
 .../MantidDataObjects/ManagedWorkspace2D.h    |  2 +
 .../DataObjects/src/ManagedWorkspace2D.cpp    | 41 +++++++++++--------
 .../DataObjects/test/ManagedWorkspace2DTest.h |  5 ++-
 3 files changed, 28 insertions(+), 20 deletions(-)

diff --git a/Code/Mantid/Framework/DataObjects/inc/MantidDataObjects/ManagedWorkspace2D.h b/Code/Mantid/Framework/DataObjects/inc/MantidDataObjects/ManagedWorkspace2D.h
index f830a643eff..2ef5e06c966 100644
--- a/Code/Mantid/Framework/DataObjects/inc/MantidDataObjects/ManagedWorkspace2D.h
+++ b/Code/Mantid/Framework/DataObjects/inc/MantidDataObjects/ManagedWorkspace2D.h
@@ -59,6 +59,8 @@ public:
   virtual size_t getMemorySize() const;
 	virtual bool threadSafe() const { return false; }
 
+	std::string get_filename() const;
+
 protected:
 
   /// Reads in a data block.
diff --git a/Code/Mantid/Framework/DataObjects/src/ManagedWorkspace2D.cpp b/Code/Mantid/Framework/DataObjects/src/ManagedWorkspace2D.cpp
index 7f73b36541d..431ed6cec11 100644
--- a/Code/Mantid/Framework/DataObjects/src/ManagedWorkspace2D.cpp
+++ b/Code/Mantid/Framework/DataObjects/src/ManagedWorkspace2D.cpp
@@ -105,29 +105,29 @@ void ManagedWorkspace2D::init(const int &NVectors, const int &XLength, const int
   std::string fullPath = path + m_filename;
 
   {
-  std::string fileToOpen = fullPath + "0";
+    std::string fileToOpen = fullPath; //+ "0";
 
-  // Create the temporary file
-  m_datafile[0] = new std::fstream(fileToOpen.c_str(), std::ios::in | std::ios::out | std::ios::binary | std::ios::trunc);
+    // Create the temporary file
+    m_datafile[0] = new std::fstream(fileToOpen.c_str(), std::ios::in | std::ios::out | std::ios::binary | std::ios::trunc);
 
-  if ( ! *m_datafile[0] )
-  {
-    m_datafile[0]->clear();
-    // Try to open in current working directory instead
-    std::string file = m_filename + "0";
-    m_datafile[0]->open(file.c_str(), std::ios::in | std::ios::out | std::ios::binary | std::ios::trunc);
-
-    // Throw an exception if it still doesn't work
     if ( ! *m_datafile[0] )
     {
-      g_log.error("Unable to open temporary data file");
-      throw std::runtime_error("ManagedWorkspace2D: Unable to open temporary data file");
+      m_datafile[0]->clear();
+      // Try to open in current working directory instead
+      std::string file = m_filename + "0";
+      m_datafile[0]->open(file.c_str(), std::ios::in | std::ios::out | std::ios::binary | std::ios::trunc);
+
+      // Throw an exception if it still doesn't work
+      if ( ! *m_datafile[0] )
+      {
+        g_log.error("Unable to open temporary data file");
+        throw std::runtime_error("ManagedWorkspace2D: Unable to open temporary data file");
+      }
+    }
+    else
+    {
+      m_filename = fullPath;
     }
-  }
-  else
-  {
-    m_filename = fullPath;
-  }
   } // block to restrict scope of fileToOpen
 
   // Set exception flags for fstream so that any problems from now on will throw
@@ -262,6 +262,11 @@ size_t ManagedWorkspace2D::getMemorySize() const
     return (size_t)(double(m_vectorSize)/1024)*m_bufferedData.size()*m_vectorsPerBlock;
 }
 
+/// Return the full path to the file used.
+std::string ManagedWorkspace2D::get_filename() const
+{
+  return this->m_filename;
+}
 
 } // namespace DataObjects
 } // namespace Mantid
diff --git a/Code/Mantid/Framework/DataObjects/test/ManagedWorkspace2DTest.h b/Code/Mantid/Framework/DataObjects/test/ManagedWorkspace2DTest.h
index 31323b8ac4a..301aa476c62 100644
--- a/Code/Mantid/Framework/DataObjects/test/ManagedWorkspace2DTest.h
+++ b/Code/Mantid/Framework/DataObjects/test/ManagedWorkspace2DTest.h
@@ -60,8 +60,9 @@ public:
     }
 
     // Test all is as it should be with the temporary file
-    fstream file("WS2D3testInit.tmp0", ios::in | ios::binary);
-    TS_ASSERT(file);;
+    std::string filename = ws.get_filename();
+    fstream file(filename.c_str(), ios::in | ios::binary);
+    TSM_ASSERT(filename, file);;
     
     double temp;
     file.read((char *) &temp, sizeof(double));
-- 
GitLab