[mapguide-commits] r7371 - branches/2.5/MgDev/Server/src/UnitTesting

svn_mapguide at osgeo.org svn_mapguide at osgeo.org
Tue Feb 19 16:09:40 PST 2013


Author: jng
Date: 2013-02-19 16:09:39 -0800 (Tue, 19 Feb 2013)
New Revision: 7371

Modified:
   branches/2.5/MgDev/Server/src/UnitTesting/TestFeatureService.cpp
   branches/2.5/MgDev/Server/src/UnitTesting/TestFeatureService.h
Log:
This was originally a test case for #2218, but I realised it was creating a *server* impl of MgFeatureService and not a proxy one that would exercise the affected code path. So I've re-purposed the test case to exercise UpdateFeatures in general (for inserts)

Modified: branches/2.5/MgDev/Server/src/UnitTesting/TestFeatureService.cpp
===================================================================
--- branches/2.5/MgDev/Server/src/UnitTesting/TestFeatureService.cpp	2013-02-20 00:08:16 UTC (rev 7370)
+++ branches/2.5/MgDev/Server/src/UnitTesting/TestFeatureService.cpp	2013-02-20 00:09:39 UTC (rev 7371)
@@ -270,6 +270,9 @@
         Ptr<MgResourceIdentifier> fsres9 = new MgResourceIdentifier(L"Library://UnitTests/Data/GetIdentityPropertiesTest.FeatureSource");
         if (pService->ResourceExists(fsres9)) pService->DeleteResource(fsres9);
 
+        Ptr<MgResourceIdentifier> fsres10 = new MgResourceIdentifier(L"Library://UnitTests/Data/2218.FeatureSource");
+        if (pService->ResourceExists(fsres10)) pService->DeleteResource(fsres10);
+
         #ifdef _DEBUG
         ACE_DEBUG((LM_INFO, ACE_TEXT("TestFeatureService::TestEnd()\n")));
         MgFdoConnectionManager* pFdoConnectionManager = MgFdoConnectionManager::GetInstance();
@@ -2366,7 +2369,7 @@
 
 void TestFeatureService::TestCase_SavePoint()
 {
-     try
+    try
     {
         MgServiceManager* serviceManager = MgServiceManager::GetInstance();
         if(serviceManager == 0)
@@ -2465,6 +2468,145 @@
 ///----------------------------------------------------------------------------
 /// Test Case Description:
 ///
+/// This test case exercises the UpdateFeatures API for insertion
+///----------------------------------------------------------------------------
+void TestFeatureService::TestCase_UpdateFeaturesInsert()
+{
+    try
+    {
+        MgServiceManager* serviceManager = MgServiceManager::GetInstance();
+        if(serviceManager == 0)
+        {
+            throw new MgNullReferenceException(L"TestFeatureService.TestCase_SavePoint", __LINE__, __WFILE__, NULL, L"", NULL);
+        }
+
+        Ptr<MgFeatureService> pService = dynamic_cast<MgFeatureService*>(serviceManager->RequestService(MgServiceType::FeatureService));
+        if (pService == 0)
+        {
+            throw new MgServiceNotAvailableException(L"TestFeatureService.TestCase_SavePoint", __LINE__, __WFILE__, NULL, L"", NULL);
+        }
+
+        Ptr<MgResourceIdentifier> featureSource = new MgResourceIdentifier(L"Library://UnitTests/Data/2218.FeatureSource");
+
+        //Create our test data store (SQLite)
+        STRING scName = L"Default";
+        STRING srsWkt = L"LOCAL_CS[\"Non-Earth (Meter)\",LOCAL_DATUM[\"Local Datum\",0],UNIT[\"Meter\", 1],AXIS[\"X\",EAST],AXIS[\"Y\",NORTH]]";
+        
+        //Setup schema
+        Ptr<MgFeatureSchema> schema = new MgFeatureSchema(L"Default", L"Test schema for TestCase_2218");
+        Ptr<MgClassDefinition> klass = new MgClassDefinition();
+        klass->SetName(L"TestClass");
+        Ptr<MgPropertyDefinitionCollection> clsProps = klass->GetProperties();
+        Ptr<MgPropertyDefinitionCollection> idProps = klass->GetIdentityProperties();
+        
+        //ID
+        Ptr<MgDataPropertyDefinition> idProperty = new MgDataPropertyDefinition(L"ID");
+        idProperty->SetDataType(MgPropertyType::Int32);
+        idProperty->SetAutoGeneration(false);
+        idProperty->SetNullable(false);
+        clsProps->Add(idProperty);
+        idProps->Add(idProperty);
+        //Name
+        Ptr<MgDataPropertyDefinition> nameProperty = new MgDataPropertyDefinition(L"Name");
+        nameProperty->SetDataType(MgPropertyType::String);
+        nameProperty->SetLength(255);
+        nameProperty->SetNullable(true);
+        clsProps->Add(nameProperty);
+        //Geom
+        Ptr<MgGeometricPropertyDefinition> geomProperty = new MgGeometricPropertyDefinition(L"Geom");
+        geomProperty->SetSpatialContextAssociation(scName);
+        clsProps->Add(geomProperty);
+
+        klass->SetDefaultGeometryPropertyName(L"Geom");
+
+        Ptr<MgClassDefinitionCollection> classes = schema->GetClasses();
+        classes->Add(klass);
+
+        //Create the feature source
+        Ptr<MgFileFeatureSourceParams> fileParams = new MgFileFeatureSourceParams(L"OSGeo.SQLite", scName, srsWkt, schema);
+        pService->CreateFeatureSource(featureSource, fileParams);
+
+        //Set up insert command
+        Ptr<MgFeatureCommandCollection> commands = new MgFeatureCommandCollection();
+        Ptr<MgPropertyCollection> propVals = new MgPropertyCollection();
+        
+        Ptr<MgInt32Property> idVal = new MgInt32Property(L"ID", 1);
+        Ptr<MgStringProperty> nameVal = new MgStringProperty(L"Name", L"Foo");
+        
+        Ptr<MgWktReaderWriter> wktRw = new MgWktReaderWriter();
+        Ptr<MgAgfReaderWriter> agfRw = new MgAgfReaderWriter();
+        Ptr<MgGeometry> geom = wktRw->Read(L"POINT (1 2)");
+        Ptr<MgByteReader> agf = agfRw->Write(geom);
+        Ptr<MgGeometryProperty> geomVal = new MgGeometryProperty(L"Geom", agf);
+
+        propVals->Add(idVal);
+        propVals->Add(nameVal);
+        propVals->Add(geomVal);
+
+        Ptr<MgInsertFeatures> insert = new MgInsertFeatures(L"Default:TestClass", propVals);
+        commands->Add(insert);
+
+        //Execute insert. Should be fine
+        Ptr<MgPropertyCollection> result = pService->UpdateFeatures(featureSource, commands, true);
+        CPPUNIT_ASSERT(result->GetCount() == 1);
+        INT32 i = 0;
+        Ptr<MgProperty> resItem1 = result->GetItem(i);
+        CPPUNIT_ASSERT(resItem1->GetPropertyType() == MgPropertyType::Feature);
+        Ptr<MgFeatureReader> rdr = ((MgFeatureProperty*)resItem1.p)->GetValue();
+        rdr->Close();
+
+        //Change name, retain same id
+        nameVal->SetValue(L"Bar");
+
+        //Execute again, expect MgFdoException due to constraint violation
+        CPPUNIT_ASSERT_THROW_MG(result = pService->UpdateFeatures(featureSource, commands, true), MgFdoException*);
+        
+        //Expect one inserted result
+        Ptr<MgFeatureQueryOptions> query = new MgFeatureQueryOptions();
+        Ptr<MgFeatureReader> qryReader = pService->SelectFeatures(featureSource, L"Default:TestClass", query);
+        INT32 count = 0;
+        while (qryReader->ReadNext()) { count++; }
+        qryReader->Close();
+        CPPUNIT_ASSERT(1 == count);
+
+        //Execute again, useTransaction = false. Should not throw exception, but log
+        //the error as a MgStringProperty
+        result = pService->UpdateFeatures(featureSource, commands, false);
+        CPPUNIT_ASSERT(result->GetCount() == 1);
+        resItem1 = result->GetItem(i);
+        CPPUNIT_ASSERT(resItem1->GetPropertyType() == MgPropertyType::String); //Errors are of type String
+
+        //Use new id
+        idVal->SetValue(2);
+
+        //Should be fine now
+        result = pService->UpdateFeatures(featureSource, commands, true);
+        CPPUNIT_ASSERT(result->GetCount() == 1);
+        resItem1 = result->GetItem(i);
+        CPPUNIT_ASSERT(resItem1->GetPropertyType() == MgPropertyType::Feature);
+        rdr = ((MgFeatureProperty*)resItem1.p)->GetValue();
+        rdr->Close();
+    }
+    catch(MgException* e)
+    {
+        STRING message = e->GetDetails(TEST_LOCALE);
+        SAFE_RELEASE(e);
+        CPPUNIT_FAIL(MG_WCHAR_TO_CHAR(message.c_str()));
+    }
+    catch(FdoException* e)
+    {
+        FDO_SAFE_RELEASE(e);
+        CPPUNIT_FAIL("FdoException occurred");
+    }
+    catch(...)
+    {
+        throw;
+    }
+}
+
+///----------------------------------------------------------------------------
+/// Test Case Description:
+///
 /// This test case exercises the FDO join optimization
 ///----------------------------------------------------------------------------
 void TestFeatureService::TestCase_JoinFdoFeatures()

Modified: branches/2.5/MgDev/Server/src/UnitTesting/TestFeatureService.h
===================================================================
--- branches/2.5/MgDev/Server/src/UnitTesting/TestFeatureService.h	2013-02-20 00:08:16 UTC (rev 7370)
+++ branches/2.5/MgDev/Server/src/UnitTesting/TestFeatureService.h	2013-02-20 00:09:39 UTC (rev 7371)
@@ -39,9 +39,8 @@
     CPPUNIT_TEST(TestCase_ApplySchema);
     CPPUNIT_TEST(TestCase_SelectFeatures);
     CPPUNIT_TEST(TestCase_SelectAggregate);
-//    CPPUNIT_TEST(TestCase_UpdateFeatures);
+    CPPUNIT_TEST(TestCase_UpdateFeaturesInsert);
     CPPUNIT_TEST(TestCase_UpdateFeaturesPartialFailure);
-//  TODO write test case when know how to make command collection
     CPPUNIT_TEST(TestCase_ExecuteSqlQuery);
     CPPUNIT_TEST(TestCase_ExecuteSqlNonQuery);
     CPPUNIT_TEST(TestCase_GetSpatialContexts);
@@ -62,7 +61,6 @@
     CPPUNIT_TEST(TestCase_SavePoint);
     CPPUNIT_TEST(TestCase_JoinFdoFeatures);
     CPPUNIT_TEST(TestCase_BenchmarkSqliteJoin);
-    
     CPPUNIT_TEST(TestCase_BenchmarkSqliteAggregateJoin);
 
     CPPUNIT_TEST(TestEnd); // This must be the very last unit test
@@ -89,7 +87,7 @@
     void TestCase_ApplySchema();
     void TestCase_SelectFeatures();
     void TestCase_SelectAggregate();
-//    void TestCase_UpdateFeatures();
+    void TestCase_UpdateFeaturesInsert();
     void TestCase_UpdateFeaturesPartialFailure();
     void TestCase_ExecuteSqlQuery();
     void TestCase_ExecuteSqlNonQuery();



More information about the mapguide-commits mailing list