aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorroubert@google.com <roubert@google.com@38ededc0-08b8-5190-f2ac-b31f878777ad>2014-05-30 17:28:16 +0000
committerroubert@google.com <roubert@google.com@38ededc0-08b8-5190-f2ac-b31f878777ad>2014-05-30 17:28:16 +0000
commit17471f7b69a94f92480650b06721e698204be3c0 (patch)
tree5e8a24375f70181d3bcfe87e11a8e566dafa4619
parentb61f18aac9eba56eff3bae712371924567f89d15 (diff)
downloadsrc-17471f7b69a94f92480650b06721e698204be3c0.tar.gz
Break implementation out of the public OndemandSupplier interface.
The current situation, where OndemandSupplier has a nested class which is a RuleHierarchy, is an artefact from the time when OndemandSupplier was an internal interface (r244). It does no longer make any sense. This change makes Supplier::RuleHierarchy a simple struct and moves all the implementation details into an internal class OndemandSupplyTask. R=rouslan@chromium.org BUG= Review URL: https://codereview.appspot.com/101960046 git-svn-id: http://libaddressinput.googlecode.com/svn/trunk@260 38ededc0-08b8-5190-f2ac-b31f878777ad
-rw-r--r--cpp/include/libaddressinput/ondemand_supplier.h32
-rw-r--r--cpp/include/libaddressinput/supplier.h8
-rw-r--r--cpp/libaddressinput.gypi3
-rw-r--r--cpp/src/ondemand_supplier.cc118
-rw-r--r--cpp/src/ondemand_supply_task.cc141
-rw-r--r--cpp/src/ondemand_supply_task.h72
-rw-r--r--cpp/src/preload_supplier.cc4
-rw-r--r--cpp/src/validation_task.cc28
-rw-r--r--cpp/test/ondemand_supply_task_test.cc (renamed from cpp/test/ondemand_supplier_test.cc)50
-rw-r--r--cpp/test/supplier_test.cc2
-rw-r--r--cpp/test/validation_task_test.cc2
11 files changed, 266 insertions, 194 deletions
diff --git a/cpp/include/libaddressinput/ondemand_supplier.h b/cpp/include/libaddressinput/ondemand_supplier.h
index af0eb9d..0fe33f8 100644
--- a/cpp/include/libaddressinput/ondemand_supplier.h
+++ b/cpp/include/libaddressinput/ondemand_supplier.h
@@ -21,7 +21,6 @@
#include <libaddressinput/util/scoped_ptr.h>
#include <map>
-#include <set>
#include <string>
namespace i18n {
@@ -60,37 +59,6 @@ class OndemandSupplier : public Supplier {
// Loads the metadata needed for |lookup_key|, then calls |supplied|.
virtual void Supply(const LookupKey& lookup_key, const Callback& supplied);
- // A RuleHierarchy object encapsulates the set of Rule objects corresponding
- // to a LookupKey, together with methods for retrieving and parsing data as
- // necessary from a Retriever object.
- class RuleHierarchy : public Supplier::RuleHierarchy {
- public:
- RuleHierarchy(const LookupKey& lookup_key,
- std::map<std::string, const Rule*>* rules,
- const Callback& supplied);
- virtual ~RuleHierarchy();
-
- // Adds lookup key string |key| to the queue of data to be retrieved.
- void Queue(const std::string& key);
-
- // Retrieves and parses data for all queued keys, then calls |supplied_|.
- void Retrieve(const Retriever& retriever);
-
- private:
- void Load(bool success, const std::string& key, const std::string& data);
- void Loaded();
-
- std::set<std::string> pending_;
- const LookupKey& lookup_key_;
- std::map<std::string, const Rule*>* const rule_cache_;
- const Callback& supplied_;
- const scoped_ptr<const i18n::addressinput::Callback< // Retriever::Callback
- std::string, std::string> > retrieved_;
- bool success_;
-
- DISALLOW_COPY_AND_ASSIGN(RuleHierarchy);
- };
-
private:
const scoped_ptr<const Retriever> retriever_;
std::map<std::string, const Rule*> rule_cache_;
diff --git a/cpp/include/libaddressinput/supplier.h b/cpp/include/libaddressinput/supplier.h
index 0b4bd90..93dce86 100644
--- a/cpp/include/libaddressinput/supplier.h
+++ b/cpp/include/libaddressinput/supplier.h
@@ -42,11 +42,9 @@ class Supplier {
// A RuleHierarchy object encapsulates the hierarchical list of Rule objects
// that corresponds to a particular LookupKey.
- class RuleHierarchy {
- public:
- RuleHierarchy() : rule_() {}
- virtual ~RuleHierarchy() {}
- const Rule* rule_[4]; // Cf. LookupKey::kHierarchy.
+ struct RuleHierarchy {
+ RuleHierarchy() : rule() {}
+ const Rule* rule[4]; // Cf. LookupKey::kHierarchy.
};
};
diff --git a/cpp/libaddressinput.gypi b/cpp/libaddressinput.gypi
index f5e668c..b268b8c 100644
--- a/cpp/libaddressinput.gypi
+++ b/cpp/libaddressinput.gypi
@@ -29,6 +29,7 @@
'src/lookup_key_util.cc',
'src/null_storage.cc',
'src/ondemand_supplier.cc',
+ 'src/ondemand_supply_task.cc',
'src/post_box_matchers.cc',
'src/preload_supplier.cc',
'src/region_data.cc',
@@ -66,7 +67,7 @@
'test/lookup_key_test.cc',
'test/lookup_key_util_test.cc',
'test/null_storage_test.cc',
- 'test/ondemand_supplier_test.cc',
+ 'test/ondemand_supply_task_test.cc',
'test/post_box_matchers_test.cc',
'test/preload_supplier_test.cc',
'test/region_data_builder_test.cc',
diff --git a/cpp/src/ondemand_supplier.cc b/cpp/src/ondemand_supplier.cc
index f125387..9228570 100644
--- a/cpp/src/ondemand_supplier.cc
+++ b/cpp/src/ondemand_supplier.cc
@@ -15,20 +15,14 @@
#include <libaddressinput/ondemand_supplier.h>
#include <algorithm>
-#include <cassert>
#include <cstddef>
#include <map>
-#include <set>
#include <string>
-#include <utility>
-#include <libaddressinput/address_field.h>
-#include <libaddressinput/callback.h>
-#include <libaddressinput/supplier.h>
-#include <libaddressinput/util/basictypes.h>
#include <libaddressinput/util/scoped_ptr.h>
#include "lookup_key.h"
+#include "ondemand_supply_task.h"
#include "region_data_constants.h"
#include "retriever.h"
#include "rule.h"
@@ -51,8 +45,8 @@ OndemandSupplier::~OndemandSupplier() {
void OndemandSupplier::Supply(const LookupKey& lookup_key,
const Callback& supplied) {
- RuleHierarchy* hierarchy =
- new RuleHierarchy(lookup_key, &rule_cache_, supplied);
+ OndemandSupplyTask* task =
+ new OndemandSupplyTask(lookup_key, &rule_cache_, supplied);
if (RegionDataConstants::IsSupported(lookup_key.GetRegionCode())) {
size_t max_depth = std::min(
@@ -64,114 +58,14 @@ void OndemandSupplier::Supply(const LookupKey& lookup_key,
std::map<std::string, const Rule*>::const_iterator it =
rule_cache_.find(key);
if (it != rule_cache_.end()) {
- hierarchy->rule_[depth] = it->second;
+ task->hierarchy_.rule[depth] = it->second;
} else {
- hierarchy->Queue(key); // If not in the cache, it needs to be loaded.
+ task->Queue(key); // If not in the cache, it needs to be loaded.
}
}
}
- hierarchy->Retrieve(*retriever_);
-}
-
-OndemandSupplier::RuleHierarchy::RuleHierarchy(
- const LookupKey& lookup_key,
- std::map<std::string, const Rule*>* rules,
- const Callback& supplied)
- : lookup_key_(lookup_key),
- rule_cache_(rules),
- supplied_(supplied),
- retrieved_(BuildCallback(this, &OndemandSupplier::RuleHierarchy::Load)),
- success_(true) {
- assert(rule_cache_ != NULL);
- assert(retrieved_ != NULL);
-}
-
-OndemandSupplier::RuleHierarchy::~RuleHierarchy() {
-}
-
-void OndemandSupplier::RuleHierarchy::Queue(const std::string& key) {
- assert(pending_.find(key) == pending_.end());
- pending_.insert(key);
-}
-
-void OndemandSupplier::RuleHierarchy::Retrieve(const Retriever& retriever) {
- if (pending_.empty()) {
- Loaded();
- } else {
- // When the final pending rule has been retrieved, the retrieved_ callback,
- // implemented by Load(), will finish by calling Loaded(), which will finish
- // by delete'ing this RuleHierarchy object. So after the final call to
- // retriever.Retrieve() no attributes of this object can be accessed (as the
- // object then no longer will exist, if the final callback has finished by
- // then), and the condition statement of the loop must therefore not use the
- // otherwise obvious it != pending_.end() but instead test a local variable
- // that isn't affected by the object being deleted.
- bool done = false;
- for (std::set<std::string>::const_iterator
- it = pending_.begin(); !done; ) {
- const std::string& key = *it++;
- done = it == pending_.end();
- retriever.Retrieve(key, *retrieved_);
- }
- }
-}
-
-void OndemandSupplier::RuleHierarchy::Load(bool success,
- const std::string& key,
- const std::string& data) {
- // Sanity check: This key should be present in the set of pending requests.
- size_t status = pending_.erase(key);
- assert(status == 1); // There will always be one item erased from the set.
- (void)status; // Prevent unused variable if assert() is optimized away.
-
- size_t depth = std::count(key.begin(), key.end(), '/') - 1;
- assert(depth < arraysize(LookupKey::kHierarchy));
- AddressField field = LookupKey::kHierarchy[depth];
-
- if (success) {
- // The address metadata server will return the empty JSON "{}" when it
- // successfully performed a lookup, but didn't find any data for that key.
- if (data != "{}") {
- Rule* rule = new Rule;
- if (field == COUNTRY) {
- // All rules on the COUNTRY level inherit from the default rule.
- rule->CopyFrom(Rule::GetDefault());
- }
- if (rule->ParseSerializedRule(data)) {
- // Try inserting the Rule object into the rule_cache_ map, or else find
- // the already existing Rule object with the same ID already in the map.
- // It is possible that a key was queued even though the corresponding
- // Rule object is already in the cache, as the data server is free to do
- // advanced normalization and aliasing so that the ID of the data
- // returned is different from the key requested. (It would be possible
- // to cache such aliases, to increase performance in the case where a
- // certain alias is requested repeatedly, but such a cache would then
- // have to be kept to some limited size to not grow indefinitely with
- // every possible permutation of a name recognized by the data server.)
- std::pair<std::map<std::string, const Rule*>::iterator, bool> result =
- rule_cache_->insert(std::make_pair(rule->GetId(), rule));
- if (!result.second) { // There was already an entry with this ID.
- delete rule;
- }
- rule_[depth] = result.first->second; // Pointer to object in the map.
- } else {
- delete rule;
- success_ = false;
- }
- }
- } else {
- success_ = false;
- }
-
- if (pending_.empty()) {
- Loaded();
- }
-}
-
-void OndemandSupplier::RuleHierarchy::Loaded() {
- supplied_(success_, lookup_key_, *this);
- delete this;
+ task->Retrieve(*retriever_);
}
} // namespace addressinput
diff --git a/cpp/src/ondemand_supply_task.cc b/cpp/src/ondemand_supply_task.cc
new file mode 100644
index 0000000..ab92746
--- /dev/null
+++ b/cpp/src/ondemand_supply_task.cc
@@ -0,0 +1,141 @@
+// Copyright (C) 2014 Google Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "ondemand_supply_task.h"
+
+#include <algorithm>
+#include <cassert>
+#include <cstddef>
+#include <map>
+#include <set>
+#include <string>
+#include <utility>
+
+#include <libaddressinput/address_field.h>
+#include <libaddressinput/callback.h>
+#include <libaddressinput/supplier.h>
+#include <libaddressinput/util/basictypes.h>
+#include <libaddressinput/util/scoped_ptr.h>
+
+#include "lookup_key.h"
+#include "retriever.h"
+#include "rule.h"
+
+namespace i18n {
+namespace addressinput {
+
+OndemandSupplyTask::OndemandSupplyTask(
+ const LookupKey& lookup_key,
+ std::map<std::string, const Rule*>* rules,
+ const Supplier::Callback& supplied)
+ : hierarchy_(),
+ pending_(),
+ lookup_key_(lookup_key),
+ rule_cache_(rules),
+ supplied_(supplied),
+ retrieved_(BuildCallback(this, &OndemandSupplyTask::Load)),
+ success_(true) {
+ assert(rule_cache_ != NULL);
+ assert(retrieved_ != NULL);
+}
+
+OndemandSupplyTask::~OndemandSupplyTask() {
+}
+
+void OndemandSupplyTask::Queue(const std::string& key) {
+ assert(pending_.find(key) == pending_.end());
+ pending_.insert(key);
+}
+
+void OndemandSupplyTask::Retrieve(const Retriever& retriever) {
+ if (pending_.empty()) {
+ Loaded();
+ } else {
+ // When the final pending rule has been retrieved, the retrieved_ callback,
+ // implemented by Load(), will finish by calling Loaded(), which will finish
+ // by delete'ing this RuleHierarchy object. So after the final call to
+ // retriever.Retrieve() no attributes of this object can be accessed (as the
+ // object then no longer will exist, if the final callback has finished by
+ // then), and the condition statement of the loop must therefore not use the
+ // otherwise obvious it != pending_.end() but instead test a local variable
+ // that isn't affected by the object being deleted.
+ bool done = false;
+ for (std::set<std::string>::const_iterator it = pending_.begin(); !done; ) {
+ const std::string& key = *it++;
+ done = it == pending_.end();
+ retriever.Retrieve(key, *retrieved_);
+ }
+ }
+}
+
+void OndemandSupplyTask::Load(bool success,
+ const std::string& key,
+ const std::string& data) {
+ // Sanity check: This key should be present in the set of pending requests.
+ size_t status = pending_.erase(key);
+ assert(status == 1); // There will always be one item erased from the set.
+ (void)status; // Prevent unused variable if assert() is optimized away.
+
+ size_t depth = std::count(key.begin(), key.end(), '/') - 1;
+ assert(depth < arraysize(LookupKey::kHierarchy));
+ AddressField field = LookupKey::kHierarchy[depth];
+
+ if (success) {
+ // The address metadata server will return the empty JSON "{}" when it
+ // successfully performed a lookup, but didn't find any data for that key.
+ if (data != "{}") {
+ Rule* rule = new Rule;
+ if (field == COUNTRY) {
+ // All rules on the COUNTRY level inherit from the default rule.
+ rule->CopyFrom(Rule::GetDefault());
+ }
+ if (rule->ParseSerializedRule(data)) {
+ // Try inserting the Rule object into the rule_cache_ map, or else find
+ // the already existing Rule object with the same ID already in the map.
+ // It is possible that a key was queued even though the corresponding
+ // Rule object is already in the cache, as the data server is free to do
+ // advanced normalization and aliasing so that the ID of the data
+ // returned is different from the key requested. (It would be possible
+ // to cache such aliases, to increase performance in the case where a
+ // certain alias is requested repeatedly, but such a cache would then
+ // have to be kept to some limited size to not grow indefinitely with
+ // every possible permutation of a name recognized by the data server.)
+ std::pair<std::map<std::string, const Rule*>::iterator, bool> result =
+ rule_cache_->insert(std::make_pair(rule->GetId(), rule));
+ if (!result.second) { // There was already an entry with this ID.
+ delete rule;
+ }
+ // Pointer to object in the map.
+ hierarchy_.rule[depth] = result.first->second;
+ } else {
+ delete rule;
+ success_ = false;
+ }
+ }
+ } else {
+ success_ = false;
+ }
+
+ if (pending_.empty()) {
+ Loaded();
+ }
+}
+
+void OndemandSupplyTask::Loaded() {
+ supplied_(success_, lookup_key_, hierarchy_);
+ delete this;
+}
+
+} // namespace addressinput
+} // namespace i18n
diff --git a/cpp/src/ondemand_supply_task.h b/cpp/src/ondemand_supply_task.h
new file mode 100644
index 0000000..61e3567
--- /dev/null
+++ b/cpp/src/ondemand_supply_task.h
@@ -0,0 +1,72 @@
+// Copyright (C) 2014 Google Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef I18N_ADDRESSINPUT_ONDEMAND_SUPPLY_TASK_H_
+#define I18N_ADDRESSINPUT_ONDEMAND_SUPPLY_TASK_H_
+
+#include <libaddressinput/supplier.h>
+#include <libaddressinput/util/basictypes.h>
+#include <libaddressinput/util/scoped_ptr.h>
+
+#include <map>
+#include <set>
+#include <string>
+
+#include "retriever.h"
+
+namespace i18n {
+namespace addressinput {
+
+class LookupKey;
+class Retriever;
+class Rule;
+
+// An OndemandSupplyTask object encapsulates the information necessary to
+// retrieve the set of Rule objects corresponding to a LookupKey and call a
+// callback when that has been done. Calling the Retrieve() method will load
+// required metadata, then call the callback and delete the OndemandSupplyTask
+// object itself.
+class OndemandSupplyTask {
+ public:
+ OndemandSupplyTask(const LookupKey& lookup_key,
+ std::map<std::string, const Rule*>* rules,
+ const Supplier::Callback& supplied);
+ ~OndemandSupplyTask();
+
+ // Adds lookup key string |key| to the queue of data to be retrieved.
+ void Queue(const std::string& key);
+
+ // Retrieves and parses data for all queued keys, then calls |supplied_|.
+ void Retrieve(const Retriever& retriever);
+
+ Supplier::RuleHierarchy hierarchy_;
+
+ private:
+ void Load(bool success, const std::string& key, const std::string& data);
+ void Loaded();
+
+ std::set<std::string> pending_;
+ const LookupKey& lookup_key_;
+ std::map<std::string, const Rule*>* const rule_cache_;
+ const Supplier::Callback& supplied_;
+ const scoped_ptr<const Retriever::Callback> retrieved_;
+ bool success_;
+
+ DISALLOW_COPY_AND_ASSIGN(OndemandSupplyTask);
+};
+
+} // namespace addressinput
+} // namespace i18n
+
+#endif // I18N_ADDRESSINPUT_ONDEMAND_SUPPLY_TASK_H_
diff --git a/cpp/src/preload_supplier.cc b/cpp/src/preload_supplier.cc
index be438a2..5219e9a 100644
--- a/cpp/src/preload_supplier.cc
+++ b/cpp/src/preload_supplier.cc
@@ -170,7 +170,7 @@ const Rule* PreloadSupplier::GetRule(const LookupKey& lookup_key) const {
if (!GetRuleHierarchy(lookup_key, &hierarchy)) {
return NULL;
}
- return hierarchy.rule_[lookup_key.GetDepth()];
+ return hierarchy.rule[lookup_key.GetDepth()];
}
void PreloadSupplier::LoadRules(const std::string& region_code,
@@ -219,7 +219,7 @@ bool PreloadSupplier::GetRuleHierarchy(const LookupKey& lookup_key,
if (it == rule_cache_.end()) {
return depth > 0; // No data on COUNTRY level is failure.
}
- hierarchy->rule_[depth] = it->second;
+ hierarchy->rule[depth] = it->second;
}
}
diff --git a/cpp/src/validation_task.cc b/cpp/src/validation_task.cc
index 7299472..567b5aa 100644
--- a/cpp/src/validation_task.cc
+++ b/cpp/src/validation_task.cc
@@ -75,7 +75,7 @@ void ValidationTask::Validate(bool success,
if (success) {
if (address_.IsFieldEmpty(COUNTRY)) {
ReportProblemMaybe(COUNTRY, MISSING_REQUIRED_FIELD);
- } else if (hierarchy.rule_[0] == NULL) {
+ } else if (hierarchy.rule[0] == NULL) {
ReportProblemMaybe(COUNTRY, UNKNOWN_VALUE);
} else {
CheckUnexpectedField(hierarchy);
@@ -94,8 +94,8 @@ void ValidationTask::Validate(bool success,
// metadata and the current value of that field is not empty.
void ValidationTask::CheckUnexpectedField(
const Supplier::RuleHierarchy& hierarchy) const {
- assert(hierarchy.rule_[0] != NULL);
- const Rule& country_rule = *hierarchy.rule_[0];
+ assert(hierarchy.rule[0] != NULL);
+ const Rule& country_rule = *hierarchy.rule[0];
static const AddressField kFields[] = {
// COUNTRY is never unexpected.
@@ -120,8 +120,8 @@ void ValidationTask::CheckUnexpectedField(
// metadata and the current value of that field is empty.
void ValidationTask::CheckMissingRequiredField(
const Supplier::RuleHierarchy& hierarchy) const {
- assert(hierarchy.rule_[0] != NULL);
- const Rule& country_rule = *hierarchy.rule_[0];
+ assert(hierarchy.rule[0] != NULL);
+ const Rule& country_rule = *hierarchy.rule[0];
for (std::vector<AddressField>::const_iterator
it = country_rule.GetRequired().begin();
@@ -144,9 +144,9 @@ void ValidationTask::CheckUnknownValue(
for (size_t depth = 1; depth < arraysize(LookupKey::kHierarchy); ++depth) {
AddressField field = LookupKey::kHierarchy[depth];
if (!(address_.IsFieldEmpty(field) ||
- hierarchy.rule_[depth - 1] == NULL ||
- hierarchy.rule_[depth - 1]->GetSubKeys().empty() ||
- hierarchy.rule_[depth] != NULL)) {
+ hierarchy.rule[depth - 1] == NULL ||
+ hierarchy.rule[depth - 1]->GetSubKeys().empty() ||
+ hierarchy.rule[depth] != NULL)) {
ReportProblemMaybe(field, UNKNOWN_VALUE);
}
}
@@ -154,8 +154,8 @@ void ValidationTask::CheckUnknownValue(
void ValidationTask::CheckPostalCodeFormatAndValue(
const Supplier::RuleHierarchy& hierarchy) const {
- assert(hierarchy.rule_[0] != NULL);
- const Rule& country_rule = *hierarchy.rule_[0];
+ assert(hierarchy.rule[0] != NULL);
+ const Rule& country_rule = *hierarchy.rule[0];
if (!(ShouldReport(POSTAL_CODE, INVALID_FORMAT) ||
ShouldReport(POSTAL_CODE, MISMATCHING_VALUE))) {
@@ -184,10 +184,10 @@ void ValidationTask::CheckPostalCodeFormatAndValue(
for (size_t depth = arraysize(LookupKey::kHierarchy) - 1;
depth > 0; --depth) {
- if (hierarchy.rule_[depth] != NULL) {
+ if (hierarchy.rule[depth] != NULL) {
// Validate sub-region specific postal code format. A sub-region specifies
// the regular expression for a prefix of the postal code.
- const RE2ptr* prefix_ptr = hierarchy.rule_[depth]->GetPostalCodeMatcher();
+ const RE2ptr* prefix_ptr = hierarchy.rule[depth]->GetPostalCodeMatcher();
if (prefix_ptr != NULL) {
if (!RE2::PartialMatch(address_.postal_code, *prefix_ptr->ptr)) {
ReportProblem(POSTAL_CODE, MISMATCHING_VALUE);
@@ -200,8 +200,8 @@ void ValidationTask::CheckPostalCodeFormatAndValue(
void ValidationTask::CheckUsesPoBox(
const Supplier::RuleHierarchy& hierarchy) const {
- assert(hierarchy.rule_[0] != NULL);
- const Rule& country_rule = *hierarchy.rule_[0];
+ assert(hierarchy.rule[0] != NULL);
+ const Rule& country_rule = *hierarchy.rule[0];
if (allow_postal_ ||
!ShouldReport(STREET_ADDRESS, USES_P_O_BOX) ||
diff --git a/cpp/test/ondemand_supplier_test.cc b/cpp/test/ondemand_supply_task_test.cc
index 691f5b8..fcb5c38 100644
--- a/cpp/test/ondemand_supplier_test.cc
+++ b/cpp/test/ondemand_supply_task_test.cc
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-#include <libaddressinput/ondemand_supplier.h>
+#include "ondemand_supply_task.h"
#include <libaddressinput/callback.h>
#include <libaddressinput/downloader.h>
@@ -39,13 +39,13 @@ using i18n::addressinput::BuildCallback;
using i18n::addressinput::Downloader;
using i18n::addressinput::LookupKey;
using i18n::addressinput::NullStorage;
-using i18n::addressinput::OndemandSupplier;
+using i18n::addressinput::OndemandSupplyTask;
using i18n::addressinput::Retriever;
using i18n::addressinput::Rule;
using i18n::addressinput::scoped_ptr;
using i18n::addressinput::Supplier;
-class RuleHierarchyTest : public testing::Test {
+class OndemandSupplyTaskTest : public testing::Test {
protected:
class MockDownloader : public Downloader {
public:
@@ -78,22 +78,20 @@ class RuleHierarchyTest : public testing::Test {
const scoped_ptr<const Supplier::Callback> supplied_;
protected:
- RuleHierarchyTest()
+ OndemandSupplyTaskTest()
: downloader_(new MockDownloader),
rule_cache_(),
retriever_(
new Retriever(
MockDownloader::kMockDataUrl, downloader_, new NullStorage)),
- supplied_(BuildCallback(this, &RuleHierarchyTest::Supplied)),
+ supplied_(BuildCallback(this, &OndemandSupplyTaskTest::Supplied)),
success_(true),
lookup_key_(),
rule_(),
called_(false),
- hierarchy_(
- new OndemandSupplier::RuleHierarchy(
- lookup_key_, &rule_cache_, *supplied_)) {}
+ task_(new OndemandSupplyTask(lookup_key_, &rule_cache_, *supplied_)) {}
- virtual ~RuleHierarchyTest() {
+ virtual ~OndemandSupplyTaskTest() {
for (std::map<std::string, const Rule*>::const_iterator
it = rule_cache_.begin(); it != rule_cache_.end(); ++it) {
delete it->second;
@@ -101,11 +99,11 @@ class RuleHierarchyTest : public testing::Test {
}
void Queue(const std::string& key) {
- hierarchy_->Queue(key);
+ task_->Queue(key);
}
void Retrieve() {
- hierarchy_->Retrieve(*retriever_);
+ task_->Retrieve(*retriever_);
}
bool success_; // Expected status from MockDownloader.
@@ -119,22 +117,22 @@ class RuleHierarchyTest : public testing::Test {
const Supplier::RuleHierarchy& hierarchy) {
ASSERT_EQ(success_, success);
ASSERT_EQ(&lookup_key_, &lookup_key);
- ASSERT_EQ(hierarchy_, &hierarchy);
- std::memcpy(rule_, hierarchy.rule_, sizeof rule_);
+ ASSERT_EQ(&task_->hierarchy_, &hierarchy);
+ std::memcpy(rule_, hierarchy.rule, sizeof rule_);
called_ = true;
}
- OndemandSupplier::RuleHierarchy* const hierarchy_;
+ OndemandSupplyTask* const task_;
- DISALLOW_COPY_AND_ASSIGN(RuleHierarchyTest);
+ DISALLOW_COPY_AND_ASSIGN(OndemandSupplyTaskTest);
};
-const char RuleHierarchyTest::MockDownloader::kMockDataUrl[] = "mock:///";
+const char OndemandSupplyTaskTest::MockDownloader::kMockDataUrl[] = "mock:///";
-const size_t RuleHierarchyTest::MockDownloader::kMockDataUrlLength =
- sizeof RuleHierarchyTest::MockDownloader::kMockDataUrl - 1;
+const size_t OndemandSupplyTaskTest::MockDownloader::kMockDataUrlLength =
+ sizeof OndemandSupplyTaskTest::MockDownloader::kMockDataUrl - 1;
-TEST_F(RuleHierarchyTest, Empty) {
+TEST_F(OndemandSupplyTaskTest, Empty) {
ASSERT_NO_FATAL_FAILURE(Retrieve());
ASSERT_TRUE(called_);
EXPECT_TRUE(rule_[0] == NULL);
@@ -143,7 +141,7 @@ TEST_F(RuleHierarchyTest, Empty) {
EXPECT_TRUE(rule_[3] == NULL);
}
-TEST_F(RuleHierarchyTest, Invalid) {
+TEST_F(OndemandSupplyTaskTest, Invalid) {
Queue("data/XA");
success_ = false;
@@ -152,7 +150,7 @@ TEST_F(RuleHierarchyTest, Invalid) {
ASSERT_TRUE(called_);
}
-TEST_F(RuleHierarchyTest, Valid) {
+TEST_F(OndemandSupplyTaskTest, Valid) {
downloader_->data_.insert(std::make_pair("data/XA", "{\"id\":\"data/XA\"}"));
Queue("data/XA");
@@ -172,7 +170,7 @@ TEST_F(RuleHierarchyTest, Valid) {
EXPECT_TRUE(rule_[0]->GetPostalCodeMatcher() == NULL);
}
-TEST_F(RuleHierarchyTest, ValidHierarchy) {
+TEST_F(OndemandSupplyTaskTest, ValidHierarchy) {
downloader_->data_.insert(
std::make_pair("data/XA", "{\"id\":\"data/XA\"}"));
downloader_->data_.insert(
@@ -212,7 +210,7 @@ TEST_F(RuleHierarchyTest, ValidHierarchy) {
EXPECT_TRUE(rule_[3]->GetRequired().empty());
}
-TEST_F(RuleHierarchyTest, InvalidJson1) {
+TEST_F(OndemandSupplyTaskTest, InvalidJson1) {
downloader_->data_.insert(std::make_pair("data/XA", ":"));
success_ = false;
@@ -223,7 +221,7 @@ TEST_F(RuleHierarchyTest, InvalidJson1) {
ASSERT_TRUE(called_);
}
-TEST_F(RuleHierarchyTest, InvalidJson2) {
+TEST_F(OndemandSupplyTaskTest, InvalidJson2) {
downloader_->data_.insert(std::make_pair("data/XA", "{\"id\":\"data/XA\"}"));
downloader_->data_.insert(std::make_pair("data/XA/aa", ":"));
@@ -236,7 +234,7 @@ TEST_F(RuleHierarchyTest, InvalidJson2) {
ASSERT_TRUE(called_);
}
-TEST_F(RuleHierarchyTest, EmptyJsonJustMeansServerKnowsNothingAboutKey) {
+TEST_F(OndemandSupplyTaskTest, EmptyJsonJustMeansServerKnowsNothingAboutKey) {
downloader_->data_.insert(std::make_pair("data/XA", "{\"id\":\"data/XA\"}"));
downloader_->data_.insert(std::make_pair("data/XA/aa", "{}"));
@@ -253,7 +251,7 @@ TEST_F(RuleHierarchyTest, EmptyJsonJustMeansServerKnowsNothingAboutKey) {
EXPECT_EQ("data/XA", rule_[0]->GetId());
}
-TEST_F(RuleHierarchyTest, IfCountryFailsAllFails) {
+TEST_F(OndemandSupplyTaskTest, IfCountryFailsAllFails) {
downloader_->data_.insert(
std::make_pair("data/XA/aa", "{\"id\":\"data/XA/aa\"}"));
diff --git a/cpp/test/supplier_test.cc b/cpp/test/supplier_test.cc
index 94d0431..22a8f7b 100644
--- a/cpp/test/supplier_test.cc
+++ b/cpp/test/supplier_test.cc
@@ -152,7 +152,7 @@ class SupplierTest : public testing::TestWithParam<SupplierWrapper* (*)()> {
const Supplier::RuleHierarchy& hierarchy) {
ASSERT_TRUE(success);
ASSERT_EQ(&lookup_key_, &lookup_key);
- std::memcpy(rule_, hierarchy.rule_, sizeof rule_);
+ std::memcpy(rule_, hierarchy.rule, sizeof rule_);
called_ = true;
}
diff --git a/cpp/test/validation_task_test.cc b/cpp/test/validation_task_test.cc
index e0ae009..94ae621 100644
--- a/cpp/test/validation_task_test.cc
+++ b/cpp/test/validation_task_test.cc
@@ -67,7 +67,7 @@ class ValidationTaskTest : public testing::Test {
for (size_t i = 0; i < arraysize(json_) && json_[i] != NULL; ++i) {
ASSERT_TRUE(rule[i].ParseSerializedRule(json_[i]));
- hierarchy->rule_[i] = &rule[i];
+ hierarchy->rule[i] = &rule[i];
}
(*task->supplied_)(success_, *task->lookup_key_, *hierarchy);