You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@etch.apache.org by ve...@apache.org on 2012/06/01 14:26:35 UTC

svn commit: r1345115 - in /incubator/etch/trunk/binding-cpp/runtime: include/serialization/EtchSetSerializer.h src/main/CMakeLists.txt src/main/serialization/EtchSetSerializer.cpp src/test/CMakeLists.txt src/test/serialization/EtchSetSerializerTest.cpp

Author: veithm
Date: Fri Jun  1 12:26:35 2012
New Revision: 1345115

URL: http://svn.apache.org/viewvc?rev=1345115&view=rev
Log:
ETCH-184 Implementation of EtchSetSerializer

EtchSetSerializer has been implemented

Change-Id: Icf22a13854e120f7149fbeec14bbde654559584a

Added:
    incubator/etch/trunk/binding-cpp/runtime/include/serialization/EtchSetSerializer.h
    incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchSetSerializer.cpp
    incubator/etch/trunk/binding-cpp/runtime/src/test/serialization/EtchSetSerializerTest.cpp
Modified:
    incubator/etch/trunk/binding-cpp/runtime/src/main/CMakeLists.txt
    incubator/etch/trunk/binding-cpp/runtime/src/test/CMakeLists.txt

Added: incubator/etch/trunk/binding-cpp/runtime/include/serialization/EtchSetSerializer.h
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/include/serialization/EtchSetSerializer.h?rev=1345115&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/include/serialization/EtchSetSerializer.h (added)
+++ incubator/etch/trunk/binding-cpp/runtime/include/serialization/EtchSetSerializer.h Fri Jun  1 12:26:35 2012
@@ -0,0 +1,66 @@
+/* $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you 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 __ETCHSETSERIALIZER_H__
+#define __ETCHSETSERIALIZER_H__
+
+#include "common/EtchString.h"
+#include "common/EtchHashSet.h"
+#include "common/EtchNativeArray.h"
+#include "serialization/EtchImportExportHelper.h"
+#include "serialization/EtchStructValue.h"
+#include "serialization/EtchField.h"
+#include "serialization/EtchType.h"
+#include "serialization/EtchClass2TypeMap.h"
+#include "serialization/EtchValidatorObject.h"
+
+
+class EtchSetSerializer : public EtchImportExportHelper {
+public:
+
+  /**
+   * Constructor
+   */
+  EtchSetSerializer(EtchType* type, EtchField* field);
+
+  /**
+   * Destructor
+   */
+  virtual ~EtchSetSerializer();
+
+  virtual status_t exportValue(EtchValueFactory* vf, capu::SmartPointer<EtchObject> value, EtchStructValue*& result);
+
+  virtual status_t importValue(EtchStructValue* value, capu::SmartPointer<EtchObject> &result);
+
+  /**
+   * Defines custom fields in the value factory so that the importer can find them.
+   * @param type
+   * @param collection
+   */
+  static status_t Init(EtchType* type, EtchClass2TypeMap* class2type);
+
+private:
+
+  EtchField mField;
+  EtchType* mType;
+  const static EtchString FIELD_NAME;
+};
+
+
+#endif /* ETCHSETSERIALIZER_H */
+

Modified: incubator/etch/trunk/binding-cpp/runtime/src/main/CMakeLists.txt
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/main/CMakeLists.txt?rev=1345115&r1=1345114&r2=1345115&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/main/CMakeLists.txt (original)
+++ incubator/etch/trunk/binding-cpp/runtime/src/main/CMakeLists.txt Fri Jun  1 12:26:35 2012
@@ -91,6 +91,7 @@ SET(MAIN_INCLUDES
     ${PROJECT_SOURCE_DIR}/include/serialization/EtchRuntimeExceptionSerializer.h
     ${PROJECT_SOURCE_DIR}/include/serialization/EtchAuthenticationExceptionSerializer.h
     ${PROJECT_SOURCE_DIR}/include/serialization/EtchHashTableSerializer.h
+    ${PROJECT_SOURCE_DIR}/include/serialization/EtchSetSerializer.h
     ${PROJECT_SOURCE_DIR}/include/util/EtchUtil.h
     )
 
@@ -142,6 +143,7 @@ SET(MAIN_SOURCES
     serialization/EtchRuntimeExceptionSerializer.cpp
     serialization/EtchAuthenticationExceptionSerializer.cpp
     serialization/EtchHashTableSerializer.cpp
+    serialization/EtchSetSerializer.cpp
     util/EtchUtil.cpp
     )
 

Added: incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchSetSerializer.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchSetSerializer.cpp?rev=1345115&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchSetSerializer.cpp (added)
+++ incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchSetSerializer.cpp Fri Jun  1 12:26:35 2012
@@ -0,0 +1,102 @@
+/* $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you 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 "serialization/EtchSetSerializer.h"
+
+const EtchString EtchSetSerializer::FIELD_NAME("keys");
+
+EtchSetSerializer::EtchSetSerializer(EtchType* type, EtchField* field)
+: mField(*field), mType(type) {
+
+}
+
+EtchSetSerializer::~EtchSetSerializer() {
+
+}
+
+status_t EtchSetSerializer::exportValue(EtchValueFactory* vf, capu::SmartPointer<EtchObject> value, EtchStructValue*& result) {
+  if ((value.get() == NULL) || (vf == NULL))
+    return ETCH_EINVAL;
+  if (value->getObjectType() != EtchHashSet<capu::SmartPointer<EtchObject> >::TYPE())
+    return ETCH_EINVAL;
+
+  capu::SmartPointer<EtchHashSet<capu::SmartPointer<EtchObject> > > set = capu::smartpointer_cast<EtchHashSet<capu::SmartPointer<EtchObject> > > (value);
+
+  EtchHashSet<capu::SmartPointer<EtchObject> >::Iterator it = set->begin();
+  capu::SmartPointer<EtchNativeArray<capu::SmartPointer<EtchObject> > > array = new EtchNativeArray<capu::SmartPointer<EtchObject> > (1, set->count());
+  capu::int32_t i = 0;
+  capu::SmartPointer<EtchObject> ptr;
+
+  while (it.hasNext()) {
+    it.next(&ptr);
+    array->set(i, ptr);
+    i++;
+  }
+
+  result = new EtchStructValue(mType, vf);
+  if (result->put(mField, array) != ETCH_OK)
+    return ETCH_ERROR;
+
+  return ETCH_OK;
+}
+
+status_t EtchSetSerializer::importValue(EtchStructValue* value, capu::SmartPointer<EtchObject> &result) {
+  if (value == NULL) {
+    return ETCH_EINVAL;
+  } else if (!value->isType(mType)) {
+    return ETCH_EINVAL;
+  }
+  capu::SmartPointer<EtchObject> tmp;
+  if (value->get(mField, &tmp) != ETCH_OK)
+    return ETCH_ERROR;
+
+  EtchHashSet<capu::SmartPointer<EtchObject> >* set = new EtchHashSet<capu::SmartPointer<EtchObject> >();
+
+  capu::SmartPointer<EtchNativeArray<capu::SmartPointer<EtchObject> > > array = capu::smartpointer_cast<EtchNativeArray<capu::SmartPointer<EtchObject> > > (tmp);
+
+  capu::SmartPointer<EtchObject> ptr;
+  for (capu::int32_t i = 0; i < array->getLength(); i++) {
+    array->get(i, &ptr);
+    status_t res = set->put(ptr);
+    if (res != ETCH_OK)
+      return res;
+  }
+  result = (capu::SmartPointer<EtchObject>) set;
+  return ETCH_OK;
+}
+
+status_t EtchSetSerializer::Init(EtchType* type, EtchClass2TypeMap* class2type) {
+  status_t result;
+  EtchField field_ptr;
+  result = type->getField(FIELD_NAME, &field_ptr);
+  if (result != ETCH_OK)
+    return result;
+  class2type->put(EtchNativeArray<capu::SmartPointer<EtchObject> >::TYPE(), type);
+  type->setComponentType(EtchHashSet<capu::SmartPointer<EtchObject> >::TYPE());
+  //set the import export helper
+  type->setImportExportHelper(new EtchSetSerializer(type, &field_ptr));
+  capu::SmartPointer<EtchValidator> tmp;
+  result = EtchValidatorObject::Get(1, tmp);
+  if (result != ETCH_OK)
+    return result;
+  result = type->putValidator(field_ptr, tmp);
+  if (result != ETCH_OK)
+    return result;
+  type->lock();
+  return ETCH_OK;
+}

Modified: incubator/etch/trunk/binding-cpp/runtime/src/test/CMakeLists.txt
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/test/CMakeLists.txt?rev=1345115&r1=1345114&r2=1345115&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/test/CMakeLists.txt (original)
+++ incubator/etch/trunk/binding-cpp/runtime/src/test/CMakeLists.txt Fri Jun  1 12:26:35 2012
@@ -72,6 +72,7 @@ add_executable (etch-cpp-test
     serialization/EtchRuntimeExceptionSerializerTest.cpp
     serialization/EtchAuthenticationExceptionSerializerTest.cpp
     serialization/EtchHashTableSerializerTest.cpp
+    serialization/EtchSetSerializerTest.cpp
     util/EtchUtilTest.cpp
     ${GTEST}/src/gtest-all.cc
     ${GMOCK}/src/gmock-all.cc

Added: incubator/etch/trunk/binding-cpp/runtime/src/test/serialization/EtchSetSerializerTest.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/test/serialization/EtchSetSerializerTest.cpp?rev=1345115&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/test/serialization/EtchSetSerializerTest.cpp (added)
+++ incubator/etch/trunk/binding-cpp/runtime/src/test/serialization/EtchSetSerializerTest.cpp Fri Jun  1 12:26:35 2012
@@ -0,0 +1,177 @@
+/* $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you 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 "gtest/gtest.h"
+#include "gmock/gmock.h"
+#include "serialization/EtchValueFactory.h"
+#include "serialization/EtchSetSerializer.h"
+#include "common/EtchNativeArray.h"
+
+class MockValueFactory5 : public virtual EtchValueFactory {
+public:
+
+  ~MockValueFactory5() {
+
+  }
+  MOCK_METHOD2(getType, status_t(capu::uint32_t id, EtchType*& result));
+
+  MOCK_METHOD2(getType, status_t(EtchString& name, EtchType*& result));
+
+  MOCK_METHOD1(addType, status_t(EtchType* type));
+
+  MOCK_METHOD0(lockDynamicTypes, status_t());
+
+  MOCK_METHOD0(unlockDynamicTypes, status_t());
+
+  MOCK_METHOD2(getMessageId, status_t(EtchMessage* msg, capu::int64_t &result));
+
+  MOCK_METHOD2(setMessageId, status_t(EtchMessage* msg, capu::int64_t msgid));
+
+  MOCK_METHOD0(get_mf__messageId, EtchField());
+
+  MOCK_METHOD2(getInReplyTo, status_t(EtchMessage* msg, capu::int64_t &result));
+
+  MOCK_METHOD2(setInReplyTo, status_t(EtchMessage* msg, capu::int64_t msgid));
+
+  MOCK_METHOD0(get_mf__inReplyTo, EtchField());
+
+  MOCK_METHOD2(importCustomValue, status_t(EtchStructValue* _struct, capu::SmartPointer<EtchObject> & result));
+
+  MOCK_METHOD2(exportCustomValue, status_t(capu::SmartPointer<EtchObject> value, EtchStructValue*& result));
+
+  MOCK_METHOD0(get_mt__exception, EtchType*());
+
+  MOCK_METHOD2(getCustomStructType, status_t(capu::int32_t c, EtchType *& type));
+
+  EtchLevel getLevel() {
+    return LEVEL_FULL;
+  }
+
+  MOCK_METHOD1(setLevel, EtchLevel(EtchLevel level));
+
+};
+
+TEST(EtchSetSerializerTest, initTest) {
+  EtchClass2TypeMap* c2type = new EtchClass2TypeMap();
+  EtchString typeName("type1");
+  EtchType* type = new EtchType(10, typeName);
+  EtchType* result;
+  EXPECT_TRUE(EtchSetSerializer::Init(type, c2type) == ETCH_OK);
+  c2type->get(EtchNativeArray<capu::SmartPointer<EtchObject> >::TYPE(), &result);
+
+  //check the added type to class to type matching
+  EXPECT_TRUE(type == result);
+  //check the initialized component type
+  EXPECT_TRUE(type->getComponentType() == EtchHashSet<capu::SmartPointer<EtchObject> >::TYPE());
+
+  //to check the initialized validator
+  capu::SmartPointer<EtchValidator> validator;
+  EtchField field;
+  typeName = "keys";
+  type->getField(typeName, &field);
+  type->getValidator(field, validator);
+  capu::SmartPointer<EtchValidator> val;
+  EtchValidatorObject::Get(1, val);
+  EXPECT_TRUE(validator == val);
+  delete type;
+  delete c2type;
+}
+
+TEST(EtchSetSerializerTest, exportTest) {
+  EtchClass2TypeMap* c2type = new EtchClass2TypeMap();
+  EtchString typeName("type1");
+  EtchValueFactory* factory = new MockValueFactory5();
+  EtchType* type = new EtchType(10, typeName);
+  capu::SmartPointer<EtchObject> object = new EtchHashSet<capu::SmartPointer<EtchObject> > ();
+  capu::SmartPointer<EtchObject> object2 = new EtchInt32();
+  capu::SmartPointer<EtchObject> object3;
+  EtchStructValue* result;
+  //initialize the serializer
+  EXPECT_TRUE(EtchSetSerializer::Init(type, c2type) == ETCH_OK);
+  EtchImportExportHelper* test = type->getImportExportHelper();
+  //check with invalid values
+  EXPECT_TRUE(test->exportValue(NULL, NULL, result) == ETCH_EINVAL);
+  EXPECT_TRUE(test->exportValue(NULL, object, result) == ETCH_EINVAL);
+  EXPECT_TRUE(test->exportValue(factory, NULL, result) == ETCH_EINVAL);
+
+  capu::SmartPointer<EtchHashSet<capu::SmartPointer<EtchObject> > > set = capu::smartpointer_cast<EtchHashSet<capu::SmartPointer<EtchObject> > > (object);
+
+  set->put(new EtchInt32(90));
+  //export values
+  EXPECT_TRUE(test->exportValue(factory, object, result) == ETCH_OK);
+
+  //check it has been correctly serialized or not
+  EtchString str("keys");
+  EtchField field;
+
+  //get the fields
+  EXPECT_TRUE(type->getField(str, &field) == ETCH_OK);
+  //use the field to get the serialized value
+  EXPECT_TRUE(result->get(field, &object3) == ETCH_OK);
+  capu::SmartPointer<EtchNativeArray<capu::SmartPointer<EtchObject> > > array = capu::smartpointer_cast<EtchNativeArray<capu::SmartPointer<EtchObject> > > (object3);
+  //check the assigned type
+  EXPECT_TRUE(type->getComponentType() == EtchHashSet<capu::SmartPointer<EtchObject> >::TYPE());
+  //get the serialized value
+  capu::SmartPointer<EtchObject> object4;
+  array->get(0, &object4);
+  EtchInt32 *serialized = (EtchInt32 *) object4.get();
+  //check the serialized value
+  EXPECT_TRUE(serialized->get() == 90);
+
+  delete c2type;
+  delete factory;
+  delete type;
+  delete result;
+}
+
+TEST(EtchSetSerializerTest, importTest) {
+  EtchClass2TypeMap* c2type = new EtchClass2TypeMap();
+  EtchString typeName("type1");
+  EtchValueFactory* factory = new MockValueFactory5();
+  EtchType* type = new EtchType(10, typeName);
+  capu::SmartPointer<EtchObject> object = new EtchHashSet<capu::SmartPointer<EtchObject> > ();
+  capu::SmartPointer<EtchObject> object2;
+  EtchStructValue* structValue;
+  //initialize the serializer
+  EXPECT_TRUE(EtchSetSerializer::Init(type, c2type) == ETCH_OK);
+  //get the serializer
+  EtchImportExportHelper* test = type->getImportExportHelper();
+  //check with invalid values
+  capu::SmartPointer<EtchHashSet<capu::SmartPointer<EtchObject> > > set = capu::smartpointer_cast<EtchHashSet<capu::SmartPointer<EtchObject> > > (object);
+
+  set->put(new EtchInt32(90));
+  //export values
+  EXPECT_TRUE(test->exportValue(factory, object, structValue) == ETCH_OK);
+
+  //UPPER PART IS USED FOR GENERATING EXAMPLE INPUT VALUE FOR IMPORT VALUE
+  capu::SmartPointer<EtchObject> result;
+  //IMPORT TEST
+  EXPECT_TRUE(test->importValue(NULL, result) == ETCH_EINVAL);
+  EXPECT_TRUE(test->importValue(structValue, result) == ETCH_OK);
+
+  capu::SmartPointer<EtchHashSet<capu::SmartPointer<EtchObject> > > importedSet = capu::smartpointer_cast<EtchHashSet<capu::SmartPointer<EtchObject> > > (result);
+  EtchHashSet<capu::SmartPointer<EtchObject> >::Iterator it = importedSet->begin();
+  //get the imported value
+  it.next(&object2);
+  //check the imported value
+  EXPECT_TRUE(((EtchInt32*) object2.get())->get() == 90);
+  delete c2type;
+  delete factory;
+  delete type;
+  delete structValue;
+}