/* 
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; version 2 of the
 * License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

// test model file 

#include "tut_stdafx.h"

#include "workbench/wb_model_file.h"

#include "wb_helpers.h"
#include "grt_test_utility.h"

#define TMP_DIR "/tmp"


using namespace wb;



BEGIN_TEST_DATA_CLASS(wb_model_file)
END_TEST_DATA_CLASS;





TEST_MODULE(wb_model_file, "tests for WB model file");


TEST_FUNCTION(1)
{
  WBTester tester;
  bec::GRTManager *grtm;
  grt::GRT *grt= tester.wb->get_grt();

  grtm= bec::GRTManager::get_instance_for(grt);
  std::string tmpDir = TMP_DIR;

  {
    ModelFile mf(tmpDir);

    workbench_DocumentRef doc(grt);

    // create a test file, change it and then save_as
    
    mf.create(grtm);

    doc->name("t1");
    mf.store_document(grt, doc);
    mf.save_to("t1.mwb");

    doc->name("t2");
    mf.store_document(grt, doc);
    mf.save_to("t2.mwb");
  }

  {
    ModelFile mf1(tmpDir);
    ModelFile mf2(tmpDir);

    mf1.open("t1.mwb", grtm);
    mf2.open("t2.mwb", grtm);

    workbench_DocumentRef d1, d2;

    d1= mf1.retrieve_document(grt);
    d2= mf2.retrieve_document(grt);

    ensure_equals("document 1 content", *d1->name(), "t1");
    ensure_equals("document 2 content", *d2->name(), "t2");
  }
}


TEST_FUNCTION(2)
{
  WBTester tester;
  bec::GRTManager *grtm;
  grt::GRT *grt= tester.wb->get_grt();
  grtm= bec::GRTManager::get_instance_for(grt);

  // load sakile a bunch of times
  std::string tmpDir = TMP_DIR;

  ModelFile m(tmpDir);

  for (int i= 0; i < 10; i++)
    m.open("data/workbench/sakila.mwb", grtm);
}

TEST_FUNCTION(3)
{
  WBTester wb;
  wb.wb->open_document("data/workbench/sakila_full.mwb");
  wb.wb->close_document();
}


END_TESTS
