1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
// Copyright 2008, Google Inc. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// 3. Neither the name of Google Inc. nor the names of its contributors may be
// used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// This file contains the unit tests for the ObjectIdParserObserver class.
#include "kml/engine/object_id_parser_observer.h"
#include "boost/scoped_ptr.hpp"
#include "kml/dom/kml_funcs.h" // For kmldom::Parse()
#include "kml/dom/kml_factory.h"
#include "gtest/gtest.h"
namespace kmlengine {
class ObjectIdParserObserverTest : public testing::Test {
protected:
virtual void SetUp() {
object_id_parser_observer_.reset(
new ObjectIdParserObserver(&object_id_map_, false));
}
ObjectIdMap object_id_map_;
boost::scoped_ptr<ObjectIdParserObserver> object_id_parser_observer_;
};
// Verify the proper operation of NewElement() for an Object with an id.
TEST_F(ObjectIdParserObserverTest, TestBasicNewElementUsage) {
// Create an Object with an id.
const string kPlacemarkId("pm0");
kmldom::KmlFactory* factory = kmldom::KmlFactory::GetFactory();
kmldom::PlacemarkPtr placemark = factory->CreatePlacemark();
placemark->set_id(kPlacemarkId);
// Pass the Object to ObjectIdParserObserver::NewElement().
bool status = object_id_parser_observer_->NewElement(placemark);
ASSERT_EQ(status, true);
// Verify that there is only one entry in the map.
ASSERT_EQ(static_cast<size_t>(1), object_id_map_.size());
// Verify that the id maps to the Object.
kmldom::ObjectPtr object = object_id_map_[kPlacemarkId];
ASSERT_EQ(kPlacemarkId, object->get_id());
ASSERT_EQ(kmldom::Type_Placemark, object->Type());
}
// Verify that NewElement() properly permits a duplicate Object id in its
// default state.
TEST_F(ObjectIdParserObserverTest, TestNewElementPermitsDupeId) {
// By default strict parsing is not enabled.
// Create 2 Objects with the same id.
const string kId("some-id");
kmldom::KmlFactory* factory = kmldom::KmlFactory::GetFactory();
kmldom::PlacemarkPtr placemark = factory->CreatePlacemark();
placemark->set_id(kId);
kmldom::PointPtr point = factory->CreatePoint();
point->set_id(kId);
// Verify that the first use of NewElement() accepts the Object.
ASSERT_TRUE(object_id_parser_observer_->NewElement(placemark));
// Verify that the 2nd use of NewElement() also accepts the Object.
ASSERT_TRUE(object_id_parser_observer_->NewElement(point));
// Verify that the map has just the second object.
// TODO: multimap would allow proper storage and access of duplicate objects.
ASSERT_EQ(static_cast<size_t>(1), object_id_map_.size());
kmldom::ObjectPtr object = object_id_map_[kId];
ASSERT_EQ(kId, object->get_id());
ASSERT_EQ(kmldom::Type_Point, object->Type());
}
// Verify that NewElement() properly fails on a duplicate Object if
// strict parsing has been enabled.
TEST_F(ObjectIdParserObserverTest, TestNewElementFailsDupeId) {
// Enable strict parsing.
object_id_parser_observer_.reset(
new ObjectIdParserObserver(&object_id_map_, true));
// Create 2 Objects with the same id.
const string kId("some-id");
kmldom::KmlFactory* factory = kmldom::KmlFactory::GetFactory();
kmldom::PlacemarkPtr placemark = factory->CreatePlacemark();
placemark->set_id(kId);
kmldom::PointPtr point = factory->CreatePoint();
point->set_id(kId);
// Verify that the first use of NewElement() accepts the Object.
ASSERT_TRUE(object_id_parser_observer_->NewElement(placemark));
// Verify that the 2nd use of NewElement() rejects the Object.
ASSERT_FALSE(object_id_parser_observer_->NewElement(point));
// Verify that the map has just the first object.
ASSERT_EQ(static_cast<size_t>(1), object_id_map_.size());
kmldom::ObjectPtr object = object_id_map_[kId];
ASSERT_EQ(kId, object->get_id());
ASSERT_EQ(kmldom::Type_Placemark, object->Type());
}
// Verify that ObjectIdParserObserver does nothing with AddChild().
TEST_F(ObjectIdParserObserverTest, TestAddChild) {
kmldom::KmlFactory* factory = kmldom::KmlFactory::GetFactory();
kmldom::PlacemarkPtr placemark = factory->CreatePlacemark();
kmldom::PlacemarkPtr point = factory->CreatePlacemark();
// The default AddChild() simply always returns true.
ASSERT_TRUE(object_id_parser_observer_->AddChild(placemark, point));
ASSERT_TRUE(object_id_parser_observer_->AddChild(placemark, point));
ASSERT_TRUE(object_id_parser_observer_->AddChild(point, placemark));
ASSERT_TRUE(object_id_parser_observer_->AddChild(NULL, NULL));
}
// Verify that the destructor does not affect the map.
TEST_F(ObjectIdParserObserverTest, TestDestructor) {
// Use NewElement() to populate the map.
const string kPlacemarkId("pm0");
const string kPointId("pt0");
kmldom::KmlFactory* factory = kmldom::KmlFactory::GetFactory();
kmldom::PlacemarkPtr placemark = factory->CreatePlacemark();
placemark->set_id(kPlacemarkId);
kmldom::PointPtr point = factory->CreatePoint();
point->set_id(kPointId);
ASSERT_TRUE(object_id_parser_observer_->NewElement(placemark));
ASSERT_TRUE(object_id_parser_observer_->NewElement(point));
// Force delete of the underlying object_id_parser_observer
object_id_parser_observer_.reset();
// Verify that the object map has exactly the 2 expected mappings.
ASSERT_EQ(static_cast<size_t>(2), object_id_map_.size());
ASSERT_EQ(kmldom::Type_Placemark, object_id_map_[kPlacemarkId]->Type());
ASSERT_EQ(kmldom::Type_Point, object_id_map_[kPointId]->Type());
}
// Verify that NewElement ignores a non-Object with id attribute.
TEST_F(ObjectIdParserObserverTest, TestNonObjectWithId) {
// The Parse() function in kmldom preserves id of a non object as an
// unknown attribute.
const string kId("kml-is-not-an-object");
kmldom::ElementPtr root = kmldom::Parse("<kml id=\"" + kId + "\"/>", NULL);
// Verify that the parse succeeded and created a non-Object.
ASSERT_EQ(kmldom::Type_kml, root->Type());
ASSERT_FALSE(root->IsA(kmldom::Type_Object));
// TODO: use the unknown attribute API to verify the id.
ASSERT_TRUE(object_id_parser_observer_->NewElement(root));
// Verify that the object map has no mappings.
ASSERT_TRUE(object_id_map_.empty());
// Verify that a map with an Object with this id is not effected by
// a call to NewElement() with a non-Object which happens to have an
// id attribute with this same value.
kmldom::KmlFactory* factory = kmldom::KmlFactory::GetFactory();
kmldom::PlacemarkPtr placemark = factory->CreatePlacemark();
placemark->set_id(kId);
ASSERT_TRUE(object_id_parser_observer_->NewElement(placemark));
// Verify that a non-Object Element with an id does not return false.
ASSERT_TRUE(object_id_parser_observer_->NewElement(root));
// Verify that only a proper Object is in the map.
ASSERT_EQ(static_cast<size_t>(1), object_id_map_.size());
ASSERT_EQ(kmldom::Type_Placemark, object_id_map_[kId]->Type());
ASSERT_EQ(kId, AsObject(object_id_map_[kId])->get_id());
}
} // end namespace kmlengine
|