TsDataSourceTransactor.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file TsDataSourceTransactor.cpp
22 
23  \brief A Test Suit for the TsDataSourceTransactor interface.
24  */
25 
26 // TerraLib
27 #include <terralib/common.h>
28 #include <terralib/dataaccess.h>
29 #include <terralib/geometry.h>
30 
31 // Unit-Test TerraLib
32 #include "TsDataSourceTransactor.h"
33 #include "TsManagerDataSource.h"
34 
36 
38 {
42  m_capabilit = m_ds->getCapabilities(); //TsManagerDataSource::sm_capabilit;
43 
50 
55 
57 }
58 
60 {
61  m_ds = 0;
62  m_dsType = "";
63  m_pol = 0;
64 }
65 
67 {
68 //#ifdef TE_COMPILE_ALL
69 //#endif
70 }
71 
73 {
74 //#ifdef TE_COMPILE_ALL
75 //#endif
76 }
77 
79 {
80 //#ifdef TE_COMPILE_ALL
81 //#endif
82 }
83 
85 {
86 }
87 
88 
90 {
91 //#ifdef TE_COMPILE_ALL
92 
93 //#endif
94 }
95 
97 {
98 //#ifdef TE_COMPILE_ALL
99 //#endif
100 }
101 
103 {
104 //#ifdef TE_COMPILE_ALL
105  CPPUNIT_ASSERT_NO_THROW(m_ds->open());
106  std::auto_ptr<te::da::DataSourceTransactor> t = m_ds->getTransactor();
107  //CPPUNIT_ASSERT_NO_THROW(t->getDataSource());
108 
109  te::da::DataSource* ds1 = t->getDataSource();
110  CPPUNIT_ASSERT(ds1);
111 
112 //#endif
113 }
114 
116 {
117  std::map< std::string, std::string > cInfo = m_ds->getConnectionInfo();
118 
119  CPPUNIT_ASSERT(m_ds != 0);
120  m_ds->open();
121  std::auto_ptr<te::da::DataSourceTransactor> t = m_ds->getTransactor();
122  CPPUNIT_ASSERT(t.get() != 0);
123 
124  std::vector< std::string > dsNames = t->getDataSetNames();
125  CPPUNIT_ASSERT(dsNames.size() > 0);
126 
127  const std::string oldDsName = dsNames[ 0 ];
128  const std::string newDsName = "newDsName";
129 
130  t->renameDataSet( oldDsName, newDsName );
131 
132  t->renameDataSet( newDsName, oldDsName );
133 }
134 
136 {
137 //#ifdef TE_COMPILE_ALL
138  CPPUNIT_ASSERT(m_ds->isOpened()== true);
139 
140 // get a transactor to retrieve information about the data source
141  CPPUNIT_ASSERT_NO_THROW(m_ds->open());
142  std::auto_ptr<te::da::DataSourceTransactor> t = m_ds->getTransactor();
143 
144  std::vector<std::string> datasets = t->getDataSetNames();
145 
146  CPPUNIT_ASSERT(datasets.size() > 0);
147 
148  std::auto_ptr<te::da::DataSet> dt;
149 
150 // get dataSet by name using transactor
151  try
152  {
153  size_t i=0;
154  while (i < datasets.size() && i < 10) //to save time during test
155  {
156  dt = t->getDataSet(datasets[i]);
157  CPPUNIT_ASSERT_NO_THROW(t->getDataSet(datasets[i]));
158  i++;
159  }
160  }
161  catch (te::common::Exception e)
162  {
163  throw e;
164  }
165  //delete datasets;
166 //#endif
167 }
168 
170 {
171 //#ifdef TE_COMPILE_ALL
172  CPPUNIT_ASSERT(m_ds->isOpened()== true);
173 
174  if (!((m_capabilit.getDataTypeCapabilities()).supportsGeometry()))
175  //CPPUNIT_ASSERT_THROW_MESSAGE("GEOMETRY_DT is not supported by this datasource",(m_capabilit.getDataTypeCapabilities()).supportsGeometry() == 0, te::common::Exception);
176  CPPUNIT_ASSERT_THROW_MESSAGE("GEOMETRY_DT is not supported by this datasource",(m_capabilit.getDataTypeCapabilities()).supportsGeometry(), te::common::Exception);
177 
178 // get a transactor to retrieve information about the data source
179  std::auto_ptr<te::da::DataSourceTransactor> t = m_ds->getTransactor();
180  CPPUNIT_ASSERT(t.get());
181 
182  std::auto_ptr<te::da::DataSet> dt;
183  std::vector<std::string>::iterator it = m_vecDtNames.begin();
184  std::auto_ptr<te::da::DataSetType> datasetType = t->getDataSetType(*it);
185  te::dt::Property* p = datasetType->findFirstPropertyOfType(te::dt::GEOMETRY_TYPE);
186  std::string propname = p->getName();
187 
188  //Testing getDataSet(name,geometry, ...)
189  try
190  {
191  dt = t->getDataSet((*it),propname, &m_pt,te::gm::INTERSECTS);
192  CPPUNIT_ASSERT_NO_THROW(dt = t->getDataSet((*it),propname, &m_pt,te::gm::INTERSECTS)); //point
193  //int nprop = dt->getNumProperties();
194  int pos = static_cast<int>(te::da::GetFirstSpatialPropertyPos(dt.get()));
195  dt->moveNext();
196  m_geom = static_cast<te::gm::Geometry*>(dt->getGeometry(pos)->clone());
197  m_geom->computeMBR(true);
198 
199  std::cout << std::endl << "DataSet Name: " << (*it) << " DataSet Size: " << dt->size() << std::endl ;
200 
201  CPPUNIT_ASSERT_NO_THROW(dt = t->getDataSet((*it),propname, &m_box, te::gm::INTERSECTS)); //box
202  std::cout << std::endl << "DataSet Name: " << (*it) << " DataSet Size: " << dt->size() << std::endl ;
203 
204  CPPUNIT_ASSERT_NO_THROW(dt = t->getDataSet((*it),propname, m_pol, te::gm::INTERSECTS)); //polygon
205  std::cout << std::endl << "DataSet Name: " << (*it) << " DataSet Size: " << dt->size() << std::endl ;
206 
207  CPPUNIT_ASSERT_NO_THROW(dt = t->getDataSet((*it),propname, m_geom , te::gm::INTERSECTS)); //using the geometry recovered above
208  std::cout << std::endl << "DataSet Name: " << (*it) << " DataSet Size: " << dt->size() << std::endl ;
209  }
210  catch (te::common::Exception e)
211  {
212  throw e;
213  }
214 
215 //#endif
216 }
217 
219 {
220 //#ifdef TE_COMPILE_ALL
221  CPPUNIT_ASSERT(m_ds->isOpened()== true);
222 // get a transactor to retrieve information about the data source
223  CPPUNIT_ASSERT_NO_THROW(m_ds->open());
224  std::auto_ptr<te::da::DataSourceTransactor> t = m_ds->getTransactor();
225  CPPUNIT_ASSERT(t.get());
226  if (!((m_capabilit.getDataTypeCapabilities()).supportsGeometry()))
227  //CPPUNIT_ASSERT_THROW_MESSAGE("GEOMETRY_DT is not supported by this datasource", (m_capabilit.getDataTypeCapabilities()).supportsGeometry() == false,te::common::Exception);
228  CPPUNIT_ASSERT_THROW_MESSAGE("GEOMETRY_DT is not supported by this datasource", (m_capabilit.getDataTypeCapabilities()).supportsGeometry(),te::common::Exception);
229 
230  std::vector<std::string> datasets = t->getDataSetNames();
231 
232  std::auto_ptr<te::da::DataSet> dt;
233 
234  std::vector<std::string>::iterator it = m_vecDtNames.begin();
235  std::auto_ptr<te::da::DataSetType> datasetType = t->getDataSetType(*it);
236  te::dt::Property* p = datasetType->findFirstPropertyOfType(te::dt::GEOMETRY_TYPE);
237  std::string propname = p->getName();
238 
239  //Testing getDataSet(datasetname,propertyname,geometry, ...)
240 
241  try
242  {
243  dt = t->getDataSet((*it),propname, &m_pt, te::gm::INTERSECTS);
244  CPPUNIT_ASSERT_NO_THROW(dt = t->getDataSet((*it),propname, &m_pt, te::gm::INTERSECTS)); //point
245  int pos = datasetType->getPropertyPosition(p->getName());
246  dt->moveNext();
247  m_geom = static_cast<te::gm::Geometry*>(dt->getGeometry(pos)->clone());
248  m_geom->computeMBR(true);
249 
250  std::cout << std::endl << "DataSet Name: " << (*it) << " DataSet Size: " << dt->size() << std::endl ;
251 
252  CPPUNIT_ASSERT_NO_THROW(dt = t->getDataSet((*it),propname, &m_box, te::gm::INTERSECTS)); //box
253  std::cout << std::endl << "DataSet Name: " << (*it) << " DataSet Size: " << dt->size() << std::endl ;
254 
255  CPPUNIT_ASSERT_NO_THROW(dt = t->getDataSet((*it),propname, m_pol, te::gm::INTERSECTS)); //polygon
256  std::cout << std::endl << "DataSet Name: " << (*it) << " DataSet Size: " << dt->size() << std::endl ;
257 
258  CPPUNIT_ASSERT_NO_THROW(dt = t->getDataSet((*it),propname, m_geom , te::gm::INTERSECTS)); //using the geometry recovered above
259  std::cout << std::endl << "DataSet Name: " << (*it) << " DataSet Size: " << dt->size() << std::endl ;
260  }
261  catch (te::common::Exception e)
262  {
263  throw e;
264  }
265 
266 //#endif
267 }
268 
270 {
271 //#ifdef TE_COMPILE_ALL
272  CPPUNIT_ASSERT(m_ds->isOpened()== true);
273 // get a transactor to retrieve information about the data source
274  std::auto_ptr<te::da::DataSourceTransactor> t(m_ds->getTransactor());
275  CPPUNIT_ASSERT(t.get());
276 
277  if (!((m_capabilit.getDataTypeCapabilities()).supportsGeometry()))
278  //CPPUNIT_ASSERT_THROW_MESSAGE("GEOMETRY_DT is not supported by this datasource", (m_capabilit.getDataTypeCapabilities()).supportsGeometry() == false,te::common::Exception);
279  CPPUNIT_ASSERT_THROW_MESSAGE("GEOMETRY_DT is not supported by this datasource", (m_capabilit.getDataTypeCapabilities()).supportsGeometry(),te::common::Exception);
280 
281  std::vector<std::string>::iterator it = m_vecDtNames.begin();
282  std::auto_ptr<te::da::DataSetType> datasetType = t->getDataSetType(*it);
283  te::dt::Property* p = datasetType->findFirstPropertyOfType(te::dt::GEOMETRY_TYPE);
284  std::string propname = p->getName();
285 
286  te::gm::Envelope *rec = new te::gm::Envelope(-60.0,-20.0,-35.0,2.0);
287 
288 // Testing getDataSet(name,property,envelope, spatialRelation,...)
289  std::auto_ptr<te::da::DataSet> dtRec;
290 
291  CPPUNIT_ASSERT_NO_THROW(dtRec = t->getDataSet(*it,propname,rec,te::gm::INTERSECTS));
292 
293  if (dtRec.get())
294  {
295  std::auto_ptr<te::gm::Envelope> envRec;
296  int pos = static_cast<int>(te::da::GetFirstSpatialPropertyPos(dtRec.get()));
297  CPPUNIT_ASSERT_NO_THROW(envRec = (dtRec.get())->getExtent(pos));
298  if (envRec.get())
299  {
300  std::cout << std::endl << "DataSetRec Name: " << (*it) << "Size: "<< dtRec->size() << std::endl ;
301  std::cout << "DataSetRec Envelop : " << envRec->m_llx << "," << envRec->m_lly << "," << envRec->m_urx << "," << envRec->m_ury << std::endl;
302  std::cout << "Test Envelop : " << rec->m_llx << "," << rec->m_lly << "," << rec->m_urx << "," << rec->m_ury << std::endl;
303  }
304  }
305 //#endif
306 }
307 
309 {
310 
311 //#ifdef TE_COMPILE_ALL
312  std::auto_ptr<te::da::DataSourceTransactor> t(m_ds->getTransactor());
313  CPPUNIT_ASSERT(t.get());
314 
315  if (!((m_capabilit.getDataTypeCapabilities()).supportsGeometry()))
316  //CPPUNIT_ASSERT_THROW_MESSAGE("GEOMETRY_DT is not supported by this datasource", (m_capabilit.getDataTypeCapabilities()).supportsGeometry() == false,te::common::Exception);
317  CPPUNIT_ASSERT_THROW_MESSAGE("GEOMETRY_DT is not supported by this datasource", (m_capabilit.getDataTypeCapabilities()).supportsGeometry(),te::common::Exception);
318 
319  std::vector<std::string>::iterator it = m_vecDtNames.begin();
320  std::auto_ptr<te::da::DataSetType> datasetType = t->getDataSetType(*it);
321 
322  te::dt::Property* p = datasetType->findFirstPropertyOfType(te::dt::GEOMETRY_TYPE);
323  std::string propname = p->getName();
324 
325  std::auto_ptr<te::da::DataSet> dtRec;
326 
327  std::vector<std::pair<std::string, te::gm::Envelope> >::iterator itpair;
328  for(itpair = m_vecNamesAndRecs.begin(); itpair < m_vecNamesAndRecs.end(); itpair++)
329  {
330  try
331  {
332  CPPUNIT_ASSERT_NO_THROW(dtRec = t->getDataSet( (*itpair).first,propname, &(*itpair).second, te::gm::INTERSECTS ));
333  if (dtRec.get())
334  {
335  std::auto_ptr<te::gm::Envelope> envRec;
336  int pos = static_cast<int>(te::da::GetFirstSpatialPropertyPos(dtRec.get()));
337  CPPUNIT_ASSERT_NO_THROW(envRec = dtRec->getExtent(pos));
338  std::cout << std::endl << "DataSet Name: " << (*itpair).first << " Size: " << dtRec->size() << std::endl ;
339  std::cout << "DataSet Envelop returned: " << envRec->m_llx << "," << envRec->m_lly << "," << envRec->m_urx << "," << envRec->m_ury << std::endl;
340  std::cout << "Envelop Filter : " << (*itpair).second.m_llx << "," << (*itpair).second.m_lly << "," << (*itpair).second.m_urx << "," << (*itpair).second.m_ury << std::endl;
341  }
342  }
343  catch (te::common::Exception e)
344  {
345  throw e;
346  }
347  }
348 //#endif
349 }
350 
352 {
353 //#ifdef TE_COMPILE_ALL
354  CPPUNIT_ASSERT(m_ds->isOpened()== true);
355 // get a transactor to retrieve information about the data source
356  std::auto_ptr<te::da::DataSourceTransactor> t(m_ds->getTransactor());
357  CPPUNIT_ASSERT(t.get());
358 
359  if (!((m_capabilit.getDataTypeCapabilities()).supportsGeometry()))
360  //CPPUNIT_ASSERT_THROW_MESSAGE("DataTypeCapabilites GEOMETRY is not supported by this datasource", (m_capabilit.getDataTypeCapabilities()).supportsGeometry() == false,te::common::Exception);
361  CPPUNIT_ASSERT_THROW_MESSAGE("DataTypeCapabilites GEOMETRY is not supported by this datasource", (m_capabilit.getDataTypeCapabilities()).supportsGeometry(),te::common::Exception);
362 
363  std::auto_ptr<te::da::DataSet> dtRec;
364 
365  std::vector<std::pair<std::string, te::gm::Envelope> >::iterator itpair;
366  std::vector<std::pair<std::string, size_t> >::iterator itsize;
367 
368  for(itpair = m_vecNamesAndRecs.begin(), itsize = m_vecNamesSizesRec.begin() ; itpair < m_vecNamesAndRecs.end() ; itpair++, itsize++)
369  {
370  try
371  {
372  //getting first geometry property name...
373  std::auto_ptr<te::da::DataSetType> datasetType = t->getDataSetType((*itpair).first);
374  te::dt::Property* p = datasetType->findFirstPropertyOfType(te::dt::GEOMETRY_TYPE);
375  std::string propname = p->getName();
376 
377  CPPUNIT_ASSERT_NO_THROW(dtRec = t->getDataSet( (*itpair).first, propname,&(*itpair).second, te::gm::INTERSECTS));
378  size_t ss = dtRec->size();
379  std::cout << std::endl << "DataSet Name: " << (*itpair).first << " DataSet Size: " << dtRec->size() << " Size expected: " << (*itsize).second << std::endl ;
380  CPPUNIT_ASSERT_EQUAL_MESSAGE("DataSet size not equal expected", (*itsize).second, ss);
381  }
382  catch (te::common::Exception e)
383  {
384  throw e;
385  }
386  }
387 
388 //#endif
389 }
390 
392 {
393 }
394 
396 {
397 //#ifdef TE_COMPILE_ALL
398  CPPUNIT_ASSERT(m_ds->isOpened()== true);
399  //SELECT_INTO_QUERY
400 
401  if (!((m_capabilit.getQueryCapabilities()).supportsSelectInto()))
402  //CPPUNIT_ASSERT_THROW_MESSAGE("SELECT_INTO_QUERY is not supported by this datasource", m_capabilit.getQueryCapabilities().supportsSelectInto() == false,te::common::Exception);
403  CPPUNIT_ASSERT_THROW_MESSAGE("SELECT_INTO_QUERY is not supported by this datasource", m_capabilit.getQueryCapabilities().supportsSelectInto(),te::common::Exception);
404 
405  std::vector<std::pair<std::string, size_t> >::iterator it = m_vecNamesSizes.begin();
406  std::string sql = "Select * FROM " + (*it).first;
407  std::auto_ptr<te::da::DataSourceTransactor> dstrans(m_ds->getTransactor());
408  CPPUNIT_ASSERT(dstrans.get());
409 
410  std::auto_ptr<te::da::DataSet> dt;
411  CPPUNIT_ASSERT_NO_THROW(dt = dstrans->query(sql));
412  size_t dtsize = dt->size();
413  CPPUNIT_ASSERT_EQUAL_MESSAGE("Size not equal expected: ", (*it).second, dtsize);
414 
415 //#endif
416 }
417 
419 {
420 }
421 
423 {
424 }
425 
427 {
428 }
429 
431 {
432 }
static te::gm::Polygon * sm_pol
static std::vector< std::pair< std::string, size_t > > sm_vecNamesSizes
std::vector< std::pair< std::string, te::gm::Envelope > > m_vecDtNamesAndEnvelops
static te::gm::Envelope sm_box
CPPUNIT_TEST_SUITE_REGISTRATION(TsDataSourceTransactor)
virtual std::unique_ptr< DataSourceTransactor > getTransactor()=0
It returns the set of parameters used to set up the access channel to the underlying repository...
void tcGetDataSetByEnvRec()
Test Case: get only the objects that intercepts a given rectangle.
virtual void open()=0
It opens the data source and makes it ready for using.
static te::da::DataSource * sm_datasource
double m_urx
Upper right corner x-coordinate.
virtual bool isOpened() const =0
It returns true if the data source is opened, otherwise it returns false.
void tcGetDataSet()
Test Case: get the dataSet using the i-th position in the DataSourceCatalog.
const DataTypeCapabilities & getDataTypeCapabilities() const
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
const QueryCapabilities & getQueryCapabilities() const
It models a property definition.
Definition: Property.h:59
virtual void computeMBR(bool cascade) const _NOEXCEPT_OP(true)=0
It computes the minimum bounding rectangle for the geometry.
virtual const DataSourceCapabilities & getCapabilities() const =0
It returns the known capabilities of the data source.
static std::vector< std::pair< std::string, size_t > > sm_vecNamesSizesRec
double m_llx
Lower left corner x-coordinate.
TEDATAACCESSEXPORT std::size_t GetFirstSpatialPropertyPos(const te::da::DataSet *dataset)
It returns the first dataset spatial property or NULL if none is found.
void tcGetDataSetByOids()
Test Case: It gets a dataset identified by the given name using the set of objects identification...
An Envelope defines a 2D rectangular region.
static size_t sm_nroDataSets
static te::gm::Point sm_pt
static te::dt::TimeDuration dt(20, 30, 50, 11)
static std::vector< std::string > sm_vecDtNames
std::vector< std::string > m_vecDtNames
static te::gm::Geometry * sm_geom
te::gm::Polygon * p
std::vector< std::pair< std::string, size_t > > m_vecNamesSizesRec
A static class for managing the resources used by DataSource test suit.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
static std::map< std::string, std::string > sm_connInfo
void tcBegin()
Test Case: Testing DataSourceTransactor of an given datasource.
double m_lly
Lower left corner y-coordinate.
std::vector< std::pair< std::string, size_t > > m_vecNamesSizes
static std::vector< std::pair< std::string, te::gm::Envelope > > sm_vecNamesAndRecs
void tcGetDataSetByEnvRec2()
Test Case: get only the objects that intercepts a given rectangle.
void tcIsIntransaction()
static std::vector< std::pair< std::string, te::gm::Envelope > > sm_vecDtNamesAndEnvelops
te::da::DataSourceCapabilities m_capabilit
double m_ury
Upper right corner y-coordinate.
const te::core::URI & getConnectionInfo() const
An Uniform Resource Identifier used to describe the datasource connection.
static std::vector< te::gm::Envelope > sm_vecEnvelops
void tcGetDataSetByProperty()
Test Case: It gets the DataSet identified by the given name using a spatial filter over the given geo...
std::map< std::string, std::string > m_connInfo
A test suit for the DataSourceTransactor interface.
This file contains include headers for the TerraLib Common Runtime module.
std::vector< te::gm::Envelope > m_vecEnvelops
This file contains include headers for the Vector Geometry model of TerraLib.
A test suit for the DataSourceTransactor interface.
This file contains include headers for the Data Access module of TerraLib.
void tcGetDataSetByGeometry()
Test Case: get only the objects that intercepts a given geometry (point, box, pol, geom).
static std::string sm_dsType
std::vector< std::pair< std::string, te::gm::Envelope > > m_vecNamesAndRecs
const std::string & getName() const
It returns the property name.
Definition: Property.h:127