00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00018
00020
00021 #ifndef NET_INSTAWEB_UTIL_CACHE_TEST_BASE_H_
00022 #define NET_INSTAWEB_UTIL_CACHE_TEST_BASE_H_
00023
00024 #include "net/instaweb/util/public/cache_interface.h"
00025
00026 #include "net/instaweb/util/public/basictypes.h"
00027 #include "net/instaweb/util/public/gtest.h"
00028 #include "net/instaweb/util/public/shared_string.h"
00029 #include "net/instaweb/util/public/string.h"
00030
00031 namespace net_instaweb {
00032
00033 class CacheTestBase : public testing::Test {
00034 public:
00037 class Callback : public CacheInterface::Callback {
00038 public:
00039 Callback() { Reset(); }
00040 virtual ~Callback() {}
00041 Callback* Reset() {
00042 called_ = false;
00043 validate_called_ = false;
00044 state_ = CacheInterface::kNotFound;
00045 SharedString empty;
00046 *value() = empty;
00047 invalid_value_ = NULL;
00048 return this;
00049 }
00050
00051 virtual bool ValidateCandidate(const GoogleString& key,
00052 CacheInterface::KeyState state) {
00053 validate_called_ = true;
00054 if ((invalid_value_ != NULL) && (*value()->get() == invalid_value_)) {
00055 return false;
00056 }
00057 return true;
00058 }
00059
00060 virtual void Done(CacheInterface::KeyState state) {
00061 EXPECT_TRUE(validate_called_);
00062 called_ = true;
00063 state_ = state;
00064 }
00065
00066 void set_invalid_value(const char* v) { invalid_value_ = v; }
00067
00068 bool called_;
00069 bool validate_called_;
00070 CacheInterface::KeyState state_;
00071
00072 private:
00073 const char* invalid_value_;
00074
00075 DISALLOW_COPY_AND_ASSIGN(Callback);
00076 };
00077
00078 CacheTestBase() : invalid_value_(NULL) {
00079 }
00080
00081 void CheckGet(const char* key, const GoogleString& expected_value) {
00082 CheckGet(Cache(), key, expected_value);
00083 }
00084
00085 void CheckGet(CacheInterface* cache, const char* key,
00086 const GoogleString& expected_value) {
00087 cache->Get(key, ResetCallback());
00088 ASSERT_TRUE(callback_.called_);
00089 ASSERT_EQ(CacheInterface::kAvailable, callback_.state_)
00090 << "For key: " << key;
00091 EXPECT_EQ(expected_value, **callback_.value()) << "For key: " << key;
00092 SanityCheck();
00093 }
00094
00095 void CheckPut(const char* key, const char* value) {
00096 CheckPut(Cache(), key, value);
00097 }
00098
00099 void CheckPut(CacheInterface* cache, const char* key, const char* value) {
00100 SharedString put_buffer(value);
00101 cache->Put(key, &put_buffer);
00102 SanityCheck();
00103 }
00104
00105 void CheckNotFound(const char* key) {
00106 CheckNotFound(Cache(), key);
00107 }
00108
00109 void CheckNotFound(CacheInterface* cache, const char* key) {
00110 cache->Get(key, ResetCallback());
00111 ASSERT_TRUE(callback_.called_);
00112 EXPECT_NE(CacheInterface::kAvailable, callback_.state_)
00113 << "For key: " << key;
00114 SanityCheck();
00115 }
00116
00117 protected:
00118 virtual CacheInterface* Cache() = 0;
00119 virtual void SanityCheck() {
00120 }
00121
00124 const Callback& callback() const { return callback_; }
00125
00128 Callback* ResetCallback() {
00129 callback_.Reset();
00130 callback_.set_invalid_value(invalid_value_);
00131 return &callback_;
00132 }
00133
00134 void set_invalid_value(const char* v) { invalid_value_ = v; }
00135
00136 private:
00137 const char* invalid_value_;
00138 Callback callback_;
00139 };
00140
00141 }
00142
00143 #endif ///< NET_INSTAWEB_UTIL_CACHE_TEST_BASE_H_