#define BOOST_TEST_MODULE execute_serializers #include #include #include #include #include #include #include #include #include #include #include #include #include #include "helpers.h" #include "fileStructure.h" namespace fs = boost::filesystem; BOOST_TEST_DONT_PRINT_LOG_VALUE ( TestModule::ClassMap::iterator ) class FileBased : public FileStructure { public: template void verifyByFile(const fs::path & infile, const boost::function & check = NULL) { const fs::path input = root / "initial" / infile; const fs::path tmpf = tmp / "byFile"; fs::create_directory(tmpf); const fs::path output = tmpf / infile; const fs::path outputJson = tmpf / fs::change_extension(infile, "json"); const fs::path outputXml = tmpf / fs::change_extension(infile, "xml"); BOOST_TEST_CHECKPOINT("Deserialize: " << input); T p = Slicer::DeserializeAny(input); if (check) { BOOST_TEST_CHECKPOINT("Check1: " << input); check(p); } BOOST_TEST_CHECKPOINT("Serialize " << input << " -> " << outputJson); Slicer::SerializeAny(p, outputJson); BOOST_TEST_CHECKPOINT("Serialize " << input << " -> " << outputXml); Slicer::SerializeAny(p, outputXml); if (check) { BOOST_TEST_CHECKPOINT("Check2: " << input); check(p); } BOOST_TEST_CHECKPOINT("Checksum: " << input << " === " << output); diff(input, output); } template void verifyByHelper(const fs::path & infile, const boost::function & in, const boost::function & out, const boost::function & ifree, const boost::function & check = NULL) { const fs::path input = root / "initial" / infile; const fs::path tmph = tmp / "byHandler"; fs::create_directory(tmph); const fs::path output = tmph / infile; BOOST_TEST_CHECKPOINT("Read: " << input); Internal docRead = in(input); BOOST_TEST_CHECKPOINT("Deserialize: " << input); T p = Slicer::DeserializeAny(docRead); ifree(docRead); if (check) { BOOST_TEST_CHECKPOINT("Check1: " << input); check(p); } BOOST_TEST_CHECKPOINT("Serialize: " << input); Internal docWrite; Slicer::SerializeAny(p, docWrite); if (check) { BOOST_TEST_CHECKPOINT("Check2: " << input); check(p); } BOOST_TEST_CHECKPOINT("Write: " << output); out(docWrite, output); ifree(docWrite); BOOST_TEST_CHECKPOINT("Checksum: " << input << " === " << output); diff(input, output); } }; void checkBuiltIns_valuesCorrect(const TestModule::BuiltInsPtr & bt) { BOOST_REQUIRE_EQUAL(bt->mbool, true); BOOST_REQUIRE_EQUAL(bt->mbyte, 4); BOOST_REQUIRE_EQUAL(bt->mshort, 40); BOOST_REQUIRE_EQUAL(bt->mint, 80); BOOST_REQUIRE_EQUAL(bt->mlong, 800); BOOST_REQUIRE_EQUAL(bt->mfloat, 3.125); BOOST_REQUIRE_EQUAL(bt->mdouble, 3.0625); BOOST_REQUIRE_EQUAL(bt->mstring, "Sample text"); } void checkInherits_types(const TestModule::InheritanceContPtr & i) { BOOST_REQUIRE(i->b); BOOST_REQUIRE(TestModule::D1Ptr::dynamicCast(i->b)); BOOST_REQUIRE_EQUAL(TestModule::D1Ptr::dynamicCast(i->b)->a, 1); BOOST_REQUIRE_EQUAL(TestModule::D1Ptr::dynamicCast(i->b)->b, 2); BOOST_REQUIRE_EQUAL(i->bs.size(), 3); BOOST_REQUIRE(i->bs[0]); BOOST_REQUIRE(TestModule::D2Ptr::dynamicCast(i->bs[0])); BOOST_REQUIRE_EQUAL(TestModule::D2Ptr::dynamicCast(i->bs[0])->a, 1); BOOST_REQUIRE_EQUAL(TestModule::D2Ptr::dynamicCast(i->bs[0])->c, 100); BOOST_REQUIRE(i->bs[1]); BOOST_REQUIRE(TestModule::D3Ptr::dynamicCast(i->bs[1])); BOOST_REQUIRE_EQUAL(TestModule::D3Ptr::dynamicCast(i->bs[1])->a, 2); BOOST_REQUIRE_EQUAL(TestModule::D3Ptr::dynamicCast(i->bs[1])->c, 100); BOOST_REQUIRE_EQUAL(TestModule::D3Ptr::dynamicCast(i->bs[1])->d, 200); BOOST_REQUIRE(i->bs[2]); BOOST_REQUIRE_EQUAL(i->bs[2]->a, 3); BOOST_REQUIRE(!TestModule::D1Ptr::dynamicCast(i->bs[2])); BOOST_REQUIRE(!TestModule::D2Ptr::dynamicCast(i->bs[2])); BOOST_REQUIRE(!TestModule::D3Ptr::dynamicCast(i->bs[2])); BOOST_REQUIRE_EQUAL(i->bm.size(), 3); BOOST_REQUIRE(TestModule::D1Ptr::dynamicCast(i->bm.find(10)->second)); BOOST_REQUIRE(TestModule::D3Ptr::dynamicCast(i->bm.find(12)->second)); BOOST_REQUIRE(!TestModule::D1Ptr::dynamicCast(i->bm.find(14)->second)); BOOST_REQUIRE(!TestModule::D2Ptr::dynamicCast(i->bm.find(14)->second)); BOOST_REQUIRE(!TestModule::D3Ptr::dynamicCast(i->bm.find(14)->second)); } void checkOptionals_notset(const TestModule::OptionalsPtr & opts) { BOOST_REQUIRE(!opts->optSimple); BOOST_REQUIRE(!opts->optStruct); BOOST_REQUIRE(!opts->optClass); BOOST_REQUIRE(!opts->optSeq); BOOST_REQUIRE(!opts->optDict); } void checkOptionals_areset(const TestModule::OptionalsPtr & opts) { BOOST_REQUIRE(opts->optSimple); BOOST_REQUIRE_EQUAL(opts->optSimple, 4); BOOST_REQUIRE(opts->optStruct); BOOST_REQUIRE_EQUAL(opts->optStruct->a, 1); BOOST_REQUIRE_EQUAL(opts->optStruct->b, 2); BOOST_REQUIRE(opts->optClass); BOOST_REQUIRE_EQUAL((*opts->optClass)->a, 1); BOOST_REQUIRE_EQUAL((*opts->optClass)->b, 2); BOOST_REQUIRE_EQUAL(opts->optSeq->size(), 2); BOOST_REQUIRE_EQUAL((*opts->optSeq)[0]->a, 3); BOOST_REQUIRE_EQUAL((*opts->optSeq)[0]->b, 4); BOOST_REQUIRE_EQUAL((*opts->optSeq)[1]->a, 5); BOOST_REQUIRE_EQUAL((*opts->optSeq)[1]->b, 6); BOOST_REQUIRE(opts->optDict); BOOST_REQUIRE_EQUAL(opts->optDict->size(), 2); BOOST_REQUIRE_EQUAL(opts->optDict->find(1), opts->optDict->end()); BOOST_REQUIRE(opts->optDict->find(10) != opts->optDict->end()); BOOST_REQUIRE_EQUAL(opts->optDict->find(10)->second->a, 11); BOOST_REQUIRE_EQUAL(opts->optDict->find(10)->second->b, 12); BOOST_REQUIRE(opts->optDict->find(13) != opts->optDict->end()); BOOST_REQUIRE_EQUAL(opts->optDict->find(13)->second->a, 14); BOOST_REQUIRE_EQUAL(opts->optDict->find(13)->second->b, 15); } void checkSeqOfClass(const TestModule::Classes & seqOfClass) { BOOST_REQUIRE_EQUAL(seqOfClass.size(), 2); BOOST_REQUIRE_EQUAL(seqOfClass[0]->a, 1); BOOST_REQUIRE_EQUAL(seqOfClass[0]->b, 2); BOOST_REQUIRE_EQUAL(seqOfClass[1]->a, 4); BOOST_REQUIRE_EQUAL(seqOfClass[1]->b, 5); } void checkStruct(const TestModule::StructType & st) { BOOST_REQUIRE_EQUAL(st.a, 10); BOOST_REQUIRE_EQUAL(st.b, 13); } template void checkAssertEq(const T & expected, const T & actual) { BOOST_REQUIRE_EQUAL(expected, actual); } void checkEntityRef(const TestModule2::EntityRef & er) { BOOST_REQUIRE_EQUAL(er.Id, 26); BOOST_REQUIRE_EQUAL(er.Name, "Hull City"); } void checkBare(const TestModule::BareContainers & bc) { BOOST_REQUIRE_EQUAL(bc.bareSeq.size(), 2); BOOST_REQUIRE_EQUAL(bc.bareSeq[0]->a, 1); BOOST_REQUIRE_EQUAL(bc.bareSeq[0]->b, 2); BOOST_REQUIRE_EQUAL(bc.bareSeq[1]->a, 3); BOOST_REQUIRE_EQUAL(bc.bareSeq[1]->b, 4); BOOST_REQUIRE_EQUAL(bc.bareMap.size(), 2); } void checkSomeEnums(const TestModule::SomeEnumsPtr & se) { BOOST_REQUIRE_EQUAL(se->one, TestModule::Ten); BOOST_REQUIRE_EQUAL(se->two, TestModule::FiftyFive); } void checkSomeNumbers(const TestModule::SomeNumbers & sn) { BOOST_REQUIRE_EQUAL(sn, TestModule::FiftyFive); } xmlpp::Document * readXml(const fs::path & path) { return new xmlpp::Document(xmlParseFile(path.string().c_str())); } void writeXml(xmlpp::Document * const & doc, const fs::path & path) { doc->write_to_file_formatted(path.string()); } void freeXml(xmlpp::Document * & doc) { delete doc; } json::Value readJson(const fs::path & path) { std::ifstream inFile(path.string()); std::stringstream buffer; buffer << inFile.rdbuf(); Glib::ustring doc(buffer.str()); Glib::ustring::const_iterator itr = doc.begin(); return json::parseValue(itr); } void writeJson(const json::Value & value, const fs::path & path) { std::ofstream outFile(path.string()); json::serializeValue(value, outFile, "utf-8"); } void freeJson(json::Value &) { } BOOST_FIXTURE_TEST_SUITE ( byFile, FileBased ); BOOST_AUTO_TEST_CASE( builtins_xml ) { verifyByFile("builtins.xml", checkBuiltIns_valuesCorrect); } BOOST_AUTO_TEST_CASE( structtype_xml ) { verifyByFile("struct.xml", checkStruct); } BOOST_AUTO_TEST_CASE( structtype_json ) { verifyByFile("struct2.json", checkStruct); } BOOST_AUTO_TEST_CASE( simplestring_xml ) { verifyByFile("string.xml", boost::bind(checkAssertEq, "test string", _1)); } BOOST_AUTO_TEST_CASE( simpleint_xml ) { verifyByFile("int.xml", boost::bind(checkAssertEq, 27, _1)); } BOOST_AUTO_TEST_CASE( simplestring_json ) { verifyByFile("string2.json", boost::bind(checkAssertEq, "test string", _1)); } BOOST_AUTO_TEST_CASE( simpleint_json ) { verifyByFile("int2.json", boost::bind(checkAssertEq, 27, _1)); } BOOST_AUTO_TEST_CASE( complexClass_xmlattrAndText ) { verifyByFile("entityref.xml", checkEntityRef); } BOOST_AUTO_TEST_CASE( sequenceOfClass_xml ) { verifyByFile("seqOfClass.xml", checkSeqOfClass); } BOOST_AUTO_TEST_CASE( sequenceOfClass_json ) { verifyByFile("seqOfClass2.json", checkSeqOfClass); } BOOST_AUTO_TEST_CASE( optionals_notset_xml ) { verifyByFile("optionals-notset.xml", checkOptionals_notset); } BOOST_AUTO_TEST_CASE( optionals_areset_xml ) { verifyByFile("optionals-areset.xml", checkOptionals_areset); } BOOST_AUTO_TEST_CASE( inherit_a_xml ) { verifyByFile("inherit-a.xml"); } BOOST_AUTO_TEST_CASE( inherit_b_xml ) { verifyByFile("inherit-b.xml", checkInherits_types); } BOOST_AUTO_TEST_CASE( conv_datetime_xml ) { verifyByFile("conv-datetime.xml"); } BOOST_AUTO_TEST_CASE( builtins2_json ) { verifyByFile("builtins2.json", checkBuiltIns_valuesCorrect); } BOOST_AUTO_TEST_CASE( optionals_areset2_json ) { verifyByFile("optionals-areset2.json", checkOptionals_areset); } BOOST_AUTO_TEST_CASE( inherit_c_json ) { verifyByFile("inherit-c.json", checkInherits_types); } BOOST_AUTO_TEST_CASE( inherit_d_json ) { verifyByFile("inherit-d.json"); } BOOST_AUTO_TEST_CASE( inherit_mapped_json ) { verifyByFile("inherit-mapped.json"); } BOOST_AUTO_TEST_CASE( xml_attribute_xml ) { verifyByFile("xmlattr.xml"); } BOOST_AUTO_TEST_CASE( xml_barecontainers_xml ) { verifyByFile("bare.xml", checkBare); } BOOST_AUTO_TEST_CASE( xml_classOfEnums_xml ) { verifyByFile("someenums.xml", checkSomeEnums); } BOOST_AUTO_TEST_CASE( xml_rootEnums_xml ) { verifyByFile("enum.xml", checkSomeNumbers); } BOOST_AUTO_TEST_CASE( json_rootEnums_json ) { verifyByFile("enum2.json", checkSomeNumbers); } BOOST_AUTO_TEST_CASE( invalid_enum ) { IceUtil::Handle> rootmp = new Slicer::ModelPartForRoot(); Slicer::DeserializerPtr jdeserializer = new Slicer::JsonFileDeserializer(root / "initial" / "invalidEnum.json"); BOOST_REQUIRE_THROW(jdeserializer->Deserialize(rootmp), Slicer::InvalidEnumerationValue); Slicer::DeserializerPtr xdeserializer = new Slicer::XmlFileDeserializer(root / "initial" / "invalidEnum.xml"); BOOST_REQUIRE_THROW(xdeserializer->Deserialize(rootmp), Slicer::InvalidEnumerationValue); } BOOST_AUTO_TEST_SUITE_END(); BOOST_FIXTURE_TEST_SUITE ( byHandler, FileBased ); BOOST_AUTO_TEST_CASE( optionals_areset2_json ) { verifyByHelper("optionals-areset2.json", readJson, writeJson, freeJson, checkOptionals_areset); } BOOST_AUTO_TEST_CASE( optionals_areset_xml ) { verifyByHelper("optionals-areset.xml", readXml, writeXml, freeXml, checkOptionals_areset); } BOOST_FIXTURE_TEST_SUITE ( compatWrapper, FileBased ); BOOST_AUTO_TEST_CASE( any ) { BOOST_TEST_CHECKPOINT("Create folders"); const fs::path tmpf = tmp / "compatWrapper"; fs::create_directory(tmpf); BOOST_TEST_CHECKPOINT("Figure out paths"); const boost::filesystem::path input = root / "initial" / "builtins.xml"; const boost::filesystem::path output = tmpf / "builtins.xml"; BOOST_TEST_CHECKPOINT("Deserialize with wrapper"); TestModule::BuiltInsPtr object = Slicer::Deserialize(input); BOOST_TEST_CHECKPOINT("Test object"); checkBuiltIns_valuesCorrect(object); BOOST_TEST_CHECKPOINT("Serialize with wrapper"); Slicer::Serialize(object, output); BOOST_TEST_CHECKPOINT("Checksum: " << input << " === " << output); diff(input, output); } BOOST_AUTO_TEST_SUITE_END(); BOOST_AUTO_TEST_SUITE_END();