Skip to content

Commit

Permalink
Update ogcutils tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rldhont committed Oct 7, 2016
1 parent 1ab83dc commit 409a183
Showing 1 changed file with 137 additions and 7 deletions.
144 changes: 137 additions & 7 deletions tests/src/core/testqgsogcutils.cpp
Expand Up @@ -89,12 +89,126 @@ void TestQgsOgcUtils::testGeometryFromGML()
QVERIFY( geomBox->wkbType() == QGis::WKBPolygon );
}

static bool compareElements( QDomElement& element1, QDomElement& element2 )
{
QString tag1 = element1.tagName();
tag1.replace( QRegExp( ".*:" ), "" );
QString tag2 = element2.tagName();
tag2.replace( QRegExp( ".*:" ), "" );
if ( tag1 != tag2 )
{
qDebug( "Different tag names: %s, %s", tag1.toAscii().data(), tag2.toAscii().data() );
return false ;
}

if ( element1.hasAttributes() != element2.hasAttributes() )
{
qDebug( "Different hasAttributes: %s, %s", tag1.toAscii().data(), tag2.toAscii().data() );
return false;
}

if ( element1.hasAttributes() )
{
QDomNamedNodeMap attrs1 = element1.attributes();
QDomNamedNodeMap attrs2 = element2.attributes();

if ( attrs1.size() != attrs2.size() )
{
qDebug( "Different attributes size: %s, %s", tag1.toAscii().data(), tag2.toAscii().data() );
return false;
}

for ( int i = 0 ; i < attrs1.size() ; ++i )
{
QDomNode node1 = attrs1.item( i );
QDomAttr attr1 = node1.toAttr();

if ( !element2.hasAttribute( attr1.name() ) )
{
qDebug( "Element2 has not attribute: %s, %s, %s", tag1.toAscii().data(), tag2.toAscii().data(), attr1.name().toAscii().data() );
return false;
}

if ( element2.attribute( attr1.name() ) != attr1.value() )
{
qDebug( "Element2 attribute has not the same value: %s, %s, %s", tag1.toAscii().data(), tag2.toAscii().data(), attr1.name().toAscii().data() );
return false;
}
}
}

if ( element1.hasChildNodes() != element2.hasChildNodes() )
{
qDebug( "Different childNodes: %s, %s", tag1.toAscii().data(), tag2.toAscii().data() );
return false;
}

if ( element1.hasChildNodes() )
{
QDomNodeList nodes1 = element1.childNodes();
QDomNodeList nodes2 = element2.childNodes();

if ( nodes1.size() != nodes2.size() )
{
qDebug( "Different childNodes size: %s, %s", tag1.toAscii().data(), tag2.toAscii().data() );
return false;
}

for ( int i = 0 ; i < nodes1.size() ; ++i )
{
QDomNode node1 = nodes1.at( i );
QDomNode node2 = nodes2.at( i );
if ( node1.isElement() && node2.isElement() )
{
QDomElement elt1 = node1.toElement();
QDomElement elt2 = node2.toElement();

if ( !compareElements( elt1, elt2 ) )
return false;
}
else if ( node1.isText() && node2.isText() )
{
QDomText txt1 = node1.toText();
QDomText txt2 = node2.toText();

if ( txt1.data() != txt2.data() )
{
qDebug( "Different text data: %s %s", tag1.toAscii().data(), txt1.data().toAscii().data() );
qDebug( "Different text data: %s %s", tag2.toAscii().data(), txt2.data().toAscii().data() );
return false;
}
}
}
}

if ( element1.text() != element2.text() )
{
qDebug( "Different text: %s %s", tag1.toAscii().data(), element1.text().toAscii().data() );
qDebug( "Different text: %s %s", tag2.toAscii().data(), element2.text().toAscii().data() );
return false;
}

return true;
}
static QDomElement comparableElement( const QString& xmlText )
{
QDomDocument doc;
if ( !doc.setContent( xmlText ) )
return QDomElement();
return doc.documentElement();
}


void TestQgsOgcUtils::testGeometryToGML()
{
QDomDocument doc;
QSharedPointer<QgsGeometry> geomPoint( QgsGeometry::fromPoint( QgsPoint( 111, 222 ) ) );
QSharedPointer<QgsGeometry> geomLine( QgsGeometry::fromWkt( "LINESTRING(111 222, 222 222)" ) );

// Elements to compare
QDomElement xmlElem;
QDomElement ogcElem;

// Test GML2
QDomElement elemInvalid = QgsOgcUtils::geometryToGML( 0, doc );
QVERIFY( elemInvalid.isNull() );
Expand All @@ -103,14 +217,18 @@ void TestQgsOgcUtils::testGeometryToGML()
QVERIFY( !elemPoint.isNull() );

doc.appendChild( elemPoint );
QCOMPARE( doc.toString( -1 ), QString( "<gml:Point><gml:coordinates cs=\",\" ts=\" \">111,222</gml:coordinates></gml:Point>" ) );
xmlElem = comparableElement( QString( "<gml:Point><gml:coordinates ts=\" \" cs=\",\">111,222</gml:coordinates></gml:Point>" ) );
ogcElem = comparableElement( doc.toString( -1 ) );
QVERIFY( compareElements( xmlElem, ogcElem ) );
doc.removeChild( elemPoint );

QDomElement elemLine = QgsOgcUtils::geometryToGML( geomLine.data(), doc );
QVERIFY( !elemLine.isNull() );

doc.appendChild( elemLine );
QCOMPARE( doc.toString( -1 ), QString( "<gml:LineString><gml:coordinates cs=\",\" ts=\" \">111,222 222,222</gml:coordinates></gml:LineString>" ) );
xmlElem = comparableElement( QString( "<gml:LineString><gml:coordinates ts=\" \" cs=\",\">111,222 222,222</gml:coordinates></gml:LineString>" ) );
ogcElem = comparableElement( doc.toString( -1 ) );
QVERIFY( compareElements( xmlElem, ogcElem ) );
doc.removeChild( elemLine );

// Test GML3
Expand All @@ -121,14 +239,18 @@ void TestQgsOgcUtils::testGeometryToGML()
QVERIFY( !elemPoint.isNull() );

doc.appendChild( elemPoint );
QCOMPARE( doc.toString( -1 ), QString( "<gml:Point><gml:pos srsDimension=\"2\">111 222</gml:pos></gml:Point>" ) );
xmlElem = comparableElement( QString( "<gml:Point><gml:pos srsDimension=\"2\">111 222</gml:pos></gml:Point>" ) );
ogcElem = comparableElement( doc.toString( -1 ) );
QVERIFY( compareElements( xmlElem, ogcElem ) );
doc.removeChild( elemPoint );

elemLine = QgsOgcUtils::geometryToGML( geomLine.data(), doc, "GML3" );
QVERIFY( !elemLine.isNull() );

doc.appendChild( elemLine );
QCOMPARE( doc.toString( -1 ), QString( "<gml:LineString><gml:posList srsDimension=\"2\">111 222 222 222</gml:posList></gml:LineString>" ) );
xmlElem = comparableElement( QString( "<gml:LineString><gml:posList srsDimension=\"2\">111 222 222 222</gml:posList></gml:LineString>" ) );
ogcElem = comparableElement( doc.toString( -1 ) );
QVERIFY( compareElements( xmlElem, ogcElem ) );
doc.removeChild( elemLine );
}

Expand Down Expand Up @@ -283,7 +405,10 @@ void TestQgsOgcUtils::testExpressionToOgcFilter()
qDebug( "EXPR: %s", exp.expression().toAscii().data() );
qDebug( "OGC : %s", doc.toString( -1 ).toAscii().data() );

QCOMPARE( xmlText, doc.toString( -1 ) );

QDomElement xmlElem = comparableElement( xmlText );
QDomElement ogcElem = comparableElement( doc.toString( -1 ) );
QVERIFY( compareElements( xmlElem, ogcElem ) );
}

void TestQgsOgcUtils::testExpressionToOgcFilter_data()
Expand Down Expand Up @@ -425,7 +550,10 @@ void TestQgsOgcUtils::testExpressionToOgcFilterWFS11()
qDebug( "SRSNAME: %s", srsName.toAscii().data() );
qDebug( "OGC : %s", doc.toString( -1 ).toAscii().data() );

QCOMPARE( xmlText, doc.toString( -1 ) );

QDomElement xmlElem = comparableElement( xmlText );
QDomElement ogcElem = comparableElement( doc.toString( -1 ) );
QVERIFY( compareElements( xmlElem, ogcElem ) );
}

void TestQgsOgcUtils::testExpressionToOgcFilterWFS11_data()
Expand Down Expand Up @@ -477,7 +605,9 @@ void TestQgsOgcUtils::testExpressionToOgcFilterWFS20()
qDebug( "SRSNAME: %s", srsName.toAscii().data() );
qDebug( "OGC : %s", doc.toString( -1 ).toAscii().data() );

QCOMPARE( xmlText, doc.toString( -1 ) );
QDomElement xmlElem = comparableElement( xmlText );
QDomElement ogcElem = comparableElement( doc.toString( -1 ) );
QVERIFY( compareElements( xmlElem, ogcElem ) );
}

void TestQgsOgcUtils::testExpressionToOgcFilterWFS20_data()
Expand Down

0 comments on commit 409a183

Please sign in to comment.