Salome HOME
3657ebe9c154a8031efcce708fec0674a84b4dc2
[modules/kernel.git] / src / Container / TestSalome_file.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "Salome_file_i.hxx"
21 #include <iostream>
22 #include <fstream>
23 #include <sstream>
24 #include "HDFascii.hxx"
25 #include <stdlib.h>
26
27 void print_infos(Engines::file*);
28 void print_state(Engines::SfState*);
29
30 void print_infos(Engines::file * infos) 
31 {
32   std::cerr << "-------------------------------------------------------------------" << std::endl;
33   std::cerr << "file_name = " << infos->file_name << std::endl;
34   std::cerr << "path = " << infos->path << std::endl;
35   std::cerr << "type = " << infos->type << std::endl;
36   std::cerr << "source_file_name = " << infos->source_file_name << std::endl;
37   std::cerr << "status = " << infos->status << std::endl;
38 }
39
40 void print_state(Engines::SfState * state) 
41 {
42   std::cerr << "-------------------------------------------------------------------" << std::endl;
43   std::cerr << "name = " << state->name << std::endl;
44   std::cerr << "hdf5_file_name = " <<  state->hdf5_file_name << std::endl;
45   std::cerr << "number_of_files = " <<  state->number_of_files << std::endl;
46   std::cerr << "files_ok = " <<  state->files_ok << std::endl;
47 }
48
49
50 int main (int argc, char * argv[])
51 {
52   system("rm toto cat test.hdf test2.hdf");
53
54   Salome_file_i file;
55   Salome_file_i file2;
56   Salome_file_i file3;
57   Salome_file_i file4;
58   Salome_file_i file5;
59   Engines::file * infos;
60   Engines::SfState * state;
61   Engines::files * all_infos;
62   PortableServer::POA_var root_poa;
63   PortableServer::POAManager_var pman;
64   CORBA::Object_var obj;
65
66   std::cerr << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << std::endl;
67   std::cerr << "Test of setLocalFile()" << std::endl;
68   file.setLocalFile("/tmp/toto");
69   infos = file.getFileInfos("toto");
70   print_infos(infos);
71
72
73   std::cerr << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << std::endl;
74   std::cerr << "Test of getFilesInfos()" << std::endl;
75   all_infos = file.getFilesInfos();
76   for (int i = 0; i < all_infos->length(); i++)
77   {
78     print_infos(&((*all_infos)[i]));
79   }
80
81   std::cerr << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << std::endl;
82   std::cerr << "Test of getSalome_fileState()" << std::endl;
83   state = file.getSalome_fileState();
84   print_state(state);
85
86   // We start CORBA ...
87   CORBA::ORB_ptr orb = CORBA::ORB_init(argc , argv);
88   obj = orb->resolve_initial_references("RootPOA");
89   root_poa = PortableServer::POA::_narrow(obj);
90   pman = root_poa->the_POAManager();
91   pman->activate();
92
93   file2.setLocalFile("/tmp/toto_distributed_source");
94   Engines::Salome_file_ptr file2_ref = file2._this();
95
96   std::cerr << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << std::endl;
97   std::cerr << "Test of setDistributedFile()" << std::endl;
98   file.setDistributedFile("/tmp/toto_distributed");
99   file.connectDistributedFile("toto_distributed", file2_ref);
100 //  file.setDistributedSourceFile("toto_distributed", "toto_distributed_source");
101   infos = file.getFileInfos("toto_distributed");
102   print_infos(infos);
103
104   // We create the files ... 
105   std::ostringstream oss;
106   oss << "/tmp/toto";
107   std::ofstream f(oss.str().c_str());
108   if (f) 
109     f << "blablabla" << std::endl;
110  
111   std::ostringstream oss2;
112   oss2 << "/tmp/toto_distributed_source";
113   std::ofstream f2(oss2.str().c_str());
114   if (f2) 
115     f2 << "bliblibli" << std::endl;
116
117   try 
118   {
119     file.recvFiles();
120   }
121   catch (SALOME::SALOME_Exception & e)
122   {
123     std::cerr << "Exception : " << e.details.text << std::endl;
124   }
125
126   std::cerr << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << std::endl;
127   std::cerr << "Test of getFilesInfos()" << std::endl;
128   all_infos = file.getFilesInfos();
129   for (int i = 0; i < all_infos->length(); i++)
130   {
131     print_infos(&((*all_infos)[i]));
132   }
133
134   std::cerr << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << std::endl;
135   std::cerr << "Test of getSalome_fileState()" << std::endl;
136   state = file.getSalome_fileState();
137   print_state(state);
138
139   orb->destroy();
140
141   file3.setLocalFile("/tmp/toto");
142   file3.setLocalFile("/bin/cat");
143   state = file3.getSalome_fileState();
144   print_state(state);
145   file3.save_all("test.hdf");
146   file3.setLocalFile("/bin/tutu");
147   file3.save("test2.hdf");
148
149   file4.load("test.hdf");
150   all_infos = file4.getFilesInfos();
151   for (int i = 0; i < all_infos->length(); i++)
152   {
153     print_infos(&((*all_infos)[i]));
154   }
155   state = file4.getSalome_fileState();
156   print_state(state);
157   file5.load("test2.hdf");
158   all_infos = file5.getFilesInfos();
159   for (int i = 0; i < all_infos->length(); i++)
160   {
161     print_infos(&((*all_infos)[i]));
162   }
163   state = file5.getSalome_fileState();
164   print_state(state);
165
166   // Test of ConvertFromHDFToASCII
167   // and ConvertFromASCIIToHDF
168   std::cerr << "Test of ConvertFromASCIIToHDF" << std::endl;
169   HDFascii::ConvertFromASCIIToHDF("/tmp/toto"); // RETURN NULL !
170   std::cerr << "Test of ConvertFromHDFToASCII" << std::endl;
171   std::cerr << HDFascii::ConvertFromHDFToASCII("test2.hdf", false) << std::endl;
172   std::cerr << HDFascii::ConvertFromHDFToASCII("test2.hdf", true) << std::endl;
173
174   std::cerr << "End of tests" << std::endl;
175 }