File: entity_mapper_test.cc

package info (click to toggle)
libkml 1.3.0-13
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,660 kB
  • sloc: cpp: 48,088; python: 2,008; xml: 1,806; ansic: 1,766; php: 223; java: 195; ruby: 109; perl: 108; sh: 42; makefile: 17
file content (297 lines) | stat: -rw-r--r-- 10,048 bytes parent folder | download | duplicates (8)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// 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 EntityMapper class.

#include "kml/engine/entity_mapper.h"
#include "boost/scoped_ptr.hpp"
#include "gtest/gtest.h"
#include "kml/base/file.h"

// The following define is a convenience for testing inside Google.
#ifdef GOOGLE_INTERNAL
#include "kml/base/google_internal_test.h"
#endif

#ifndef DATADIR
#error *** DATADIR must be defined! ***
#endif

using kmlbase::File;
using kmldom::DocumentPtr;
using kmldom::FeaturePtr;
using kmldom::KmlFactory;
using kmldom::KmlPtr;
using kmldom::PlacemarkPtr;

namespace kmlengine {

class EntityMapperTest : public testing::Test {
 protected:
  KmlFilePtr kml_file_;
};

// A hunk of KML that uses all possible entities.
const static string kEntityKml(
  "<Document>\n"
  "  <Schema name=\"TrailHeadType\" id=\"TrailHeadTypeId\">\n"
  "    <SimpleField type=\"string\" name=\"TrailHeadName\">\n"
  "      <displayName><![CDATA[<b>Trail Head Name</b>]]></displayName>\n"
  "    </SimpleField>\n"
  "    <SimpleField type=\"double\" name=\"TrailLength\">\n"
  "      <displayName><![CDATA[<i>The length in miles</i>]]></displayName>\n"
  "    </SimpleField>\n"
  "    <SimpleField type=\"int\" name=\"ElevationGain\">\n"
  "      <displayName><![CDATA[<i>change in altitude</i>]]></displayName>\n"
  "    </SimpleField>\n"
  "  </Schema>\n"
  "  <Placemark id=\"foo\" targetId=\"bar\">\n"
  "    <name>__NAME__</name>\n"
  "    <address>__ADDRESS__</address>\n"
  "    <Snippet>__SNIPPET__</Snippet>\n"
  "    <description>__DESCRIPTION__</description>\n"
  "    <ExtendedData>\n"
  "      <Data name=\"holeNumber\">\n"
  "        <value>1</value>\n"
  "      </Data>\n"
  "      <Data name=\"holeYardage\">\n"
  "        <value>234</value>\n"
  "      </Data>\n"
  "      <SchemaData schemaUrl=\"#TrailHeadTypeId\">\n"
  "        <SimpleData name=\"TrailHeadName\">Mount Everest</SimpleData>\n"
  "        <SimpleData name=\"TrailLength\">347.45</SimpleData>\n"
  "        <SimpleData name=\"ElevationGain\">10000</SimpleData>\n"
  "      </SchemaData>\n"
  "    </ExtendedData>\n"
  "  </Placemark>\n"
  "</Document>"
);

// A table mapping all entities contained in the above KML document with their
// expected replacement strings.
const static struct {
  string entity;
  string replacement;
} kEntityMap[] = {
  {"id", "foo"},
  {"targetId", "bar"},
  {"name", "__NAME__"},
  {"address", "__ADDRESS__"},
  {"Snippet", "__SNIPPET__"},
  {"description", "__DESCRIPTION__"},
  {"TrailHeadType/ElevationGain", "10000"},
  {"TrailHeadType/ElevationGain/displayName", "<i>change in altitude</i>"},
  {"TrailHeadType/TrailHeadName", "Mount Everest"},
  {"TrailHeadType/TrailHeadName/displayName", "<b>Trail Head Name</b>"},
  {"TrailHeadType/TrailLength", "347.45"},
  {"TrailHeadType/TrailLength/displayName", "<i>The length in miles</i>"},
  {"holeNumber", "1"},
  {"holeYardage", "234"},
};

TEST_F(EntityMapperTest, TestGetEntityFields) {
  string errs;
  kml_file_ = KmlFile::CreateFromParse(kEntityKml, NULL);
  ASSERT_TRUE(kml_file_);
  ASSERT_TRUE(errs.empty());

  DocumentPtr doc = kmldom::AsDocument(kml_file_->get_root());
  PlacemarkPtr p = kmldom::AsPlacemark(doc->get_feature_array_at(0));

  kmlbase::StringMap entity_map;
  EntityMapper entity_mapper(kml_file_, &entity_map);
  entity_mapper.GetEntityFields(p);

  // Verify that the correct number of entities were extracted from the KML.
  const size_t kSizeEntityMap = sizeof(kEntityMap)/sizeof(kEntityMap[0]);
  ASSERT_EQ(kSizeEntityMap, entity_map.size());

  // Verify that the entity map was populated as expected.
  for (size_t i = 0; i < kSizeEntityMap; ++i) {
    // The entity exists within the map.
    ASSERT_TRUE(entity_map.find(kEntityMap[i].entity) != entity_map.end());
    // The entity has the expected replacement text.
    ASSERT_EQ(kEntityMap[i].replacement,
                         entity_map[kEntityMap[i].entity]);
  }
}

// A table mapping entities to their expected replacements.
const static struct {
  string raw_text;
  string expanded_text;
} kReplacments[] = {
  {
    "abcdef",
    "abcdef",
  },
  {
    "abc$[]def",
    "abc$[]def",
  },
  {
    "abc$[noSuchEntity]def",
    "abc$[noSuchEntity]def",
  },
  {
    "$[name]$[description]",
    "__NAME____DESCRIPTION__",
  },
  {
    "xxx$[name]xxx$[description]xxx",
    "xxx__NAME__xxx__DESCRIPTION__xxx",
  },
  {
    "  $[name]  $[description]  ",
    "  __NAME__  __DESCRIPTION__  ",
  },
  {
    "  $[name]$[name]  $[description] $[description] $[name]",
    "  __NAME____NAME__  __DESCRIPTION__ __DESCRIPTION__ __NAME__",
  },
};

TEST_F(EntityMapperTest, TestCreateExpandedEntities) {
  kml_file_ = KmlFile::CreateFromParse(kEntityKml, NULL);
  DocumentPtr doc = kmldom::AsDocument(kml_file_->get_root());
  PlacemarkPtr p = kmldom::AsPlacemark(doc->get_feature_array_at(0));

  kmlbase::StringMap entity_map;
  EntityMapper entity_mapper(kml_file_, &entity_map);
  entity_mapper.GetEntityFields(p);

  // Verify that CreateExpandedEntities handles various kinds of entity
  // references, spacing, multiple references.
  for (size_t i = 0; i < sizeof(kReplacments)/sizeof(kReplacments[0]); ++i) {
    ASSERT_EQ(
        kReplacments[i].expanded_text,
        CreateExpandedEntities(kReplacments[i].raw_text, entity_map));
  }
}

TEST_F(EntityMapperTest, TestAltMarkupData) {
  const string kDataKml = (
    "<Placemark>"
    "<ExtendedData>"
    "<Data name=\"data1\">"
    "<displayName>1st display name</displayName>"
    "<value>1st</value>"
    "</Data>"
    "<Data name=\"data2\">"
    "<displayName>2nd display name</displayName>"
    "<value>2nd</value>"
    "</Data>"
    "<!-- The below are legal, but are in Google Earth they are overridden by"
    "the Feature's name and description for the balloon text replacements. -->"
    "<Data name=\"name\"><value>data name</value></Data>"
    "<Data name=\"description\"><value>data description</value></Data>"
    "</ExtendedData>"
    "</Placemark>"
  );
  const struct {
    const char* key;
    const char* value;
  } kKeyValues[] = {
    {
      "1st display name",
      "1st",
    },
    {
      "2nd display name",
      "2nd",
    },
    {
      "name",
      "data name",
    },
    {
      "description",
      "data description",
    },
  };

  kml_file_ = KmlFile::CreateFromParse(kDataKml, NULL);
  ASSERT_TRUE(kml_file_);
  PlacemarkPtr p = kmldom::AsPlacemark(kml_file_->get_root());
  ASSERT_TRUE(p);
  kmlbase::StringMap entity_map;
  kmlbase::StringPairVector alt_markup_map;
  EntityMapper entity_mapper(kml_file_, &entity_map, &alt_markup_map);
  entity_mapper.GetEntityFields(p);
  ASSERT_EQ(static_cast<size_t>(6), entity_map.size());
  ASSERT_EQ(static_cast<size_t>(4), alt_markup_map.size());
  for (int i = 0; i < 4; i++) {
    ASSERT_EQ(kKeyValues[i].key, alt_markup_map[i].first);
    ASSERT_EQ(kKeyValues[i].value, alt_markup_map[i].second);
  }
  // If GetEntityFields is called multiple times, the StringMap is overwritten
  // and the StringPairVector is appended to.
  entity_mapper.GetEntityFields(p);
  ASSERT_EQ(static_cast<size_t>(6), entity_map.size());
  ASSERT_EQ(static_cast<size_t>(8), alt_markup_map.size());
  entity_mapper.GetEntityFields(p);
  ASSERT_EQ(static_cast<size_t>(6), entity_map.size());
  ASSERT_EQ(static_cast<size_t>(12), alt_markup_map.size());
}

TEST_F(EntityMapperTest, TestAltMarkupSchemaData) {
  const struct {
    const char* key;
    const char* value;
  } kSchemaDataMappings[] = {
    {
      "s_name:simple field display name 1",
      "one",
    },
    {
      "s_name:sfield2",
      "2",
    },
  };

  const string kSchemaDataKml = string(DATADIR) + "/kml/schemadata.kml";
  string data;
  string errors;
  ASSERT_TRUE(File::ReadFileToString(kSchemaDataKml, &data));
  kml_file_ = KmlFile::CreateFromParse(data, &errors);
  ASSERT_FALSE(data.empty());
  ASSERT_TRUE(errors.empty());
  ASSERT_TRUE(kml_file_);
  KmlPtr kml = kmldom::AsKml(kml_file_->get_root());
  DocumentPtr doc = kmldom::AsDocument(kml->get_feature());
  PlacemarkPtr p = kmldom::AsPlacemark(doc->get_feature_array_at(0));
  ASSERT_TRUE(p);
  kmlbase::StringMap entity_map;
  kmlbase::StringPairVector alt_markup_map;
  EntityMapper entity_mapper(kml_file_, &entity_map, &alt_markup_map);
  entity_mapper.GetEntityFields(p);
  ASSERT_EQ(static_cast<size_t>(2), alt_markup_map.size());
  for (int i = 0; i < 2; i++) {
    ASSERT_EQ(kSchemaDataMappings[i].key, alt_markup_map[i].first);
    ASSERT_EQ(kSchemaDataMappings[i].value, alt_markup_map[i].second);
  }
}

}  // end namespace kmlengine