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 6, 2016
1 parent 72f93c9 commit 736b2da
Showing 1 changed file with 140 additions and 32 deletions.
172 changes: 140 additions & 32 deletions tests/src/core/testqgsogcutils.cpp
Expand Up @@ -93,12 +93,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 @@ -107,14 +221,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 @@ -125,14 +243,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 @@ -287,7 +409,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 @@ -449,7 +574,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 All @@ -473,14 +601,6 @@ void TestQgsOgcUtils::testExpressionToOgcFilterWFS11_data()
"</ogc:Filter>" );
}

static QString normalizeXML( const QString& xmlText )
{
QDomDocument doc;
if ( !doc.setContent( xmlText, true ) )
return QString();
return doc.toString( -1 );
}

void TestQgsOgcUtils::testExpressionToOgcFilterWFS20()
{
QFETCH( QString, exprText );
Expand All @@ -506,15 +626,9 @@ void TestQgsOgcUtils::testExpressionToOgcFilterWFS20()
qDebug( "SRSNAME: %s", srsName.toAscii().data() );
qDebug( "OGC : %s", doc.toString( -1 ).toAscii().data() );

QString normalizedExpected( normalizeXML( xmlText ) );
QString normalizedGot( normalizeXML( doc.toString( -1 ) ) );

if ( normalizedExpected != normalizedGot )
{
qDebug( "Normalized expected: %s", normalizedExpected.toAscii().data() );
qDebug( "Normalized got: %s", normalizedGot.toAscii().data() );
}
QCOMPARE( normalizedExpected, normalizedGot );
QDomElement xmlElem = comparableElement( xmlText );
QDomElement ogcElem = comparableElement( doc.toString( -1 ) );
QVERIFY( compareElements( xmlElem, ogcElem ) );
}

void TestQgsOgcUtils::testExpressionToOgcFilterWFS20_data()
Expand Down Expand Up @@ -614,15 +728,9 @@ void TestQgsOgcUtils::testSQLStatementToOgcFilter()
filterVersion == QgsOgcUtils::FILTER_FES_2_0 ? "FES 2.0" : "unknown" );
qDebug( "OGC : %s", doc.toString( -1 ).toAscii().data() );

QString normalizedExpected( normalizeXML( xmlText ) );
QString normalizedGot( normalizeXML( doc.toString( -1 ) ) );

if ( normalizedExpected != normalizedGot )
{
qDebug( "Normalized expected: %s", normalizedExpected.toAscii().data() );
qDebug( "Normalized got: %s", normalizedGot.toAscii().data() );
}
QCOMPARE( normalizedExpected, normalizedGot );
QDomElement xmlElem = comparableElement( xmlText );
QDomElement ogcElem = comparableElement( doc.toString( -1 ) );
QVERIFY( compareElements( xmlElem, ogcElem ) );
}

void TestQgsOgcUtils::testSQLStatementToOgcFilter_data()
Expand Down

0 comments on commit 736b2da

Please sign in to comment.