From 89b013d6935836d80bfcbab672aa94aea295867a Mon Sep 17 00:00:00 2001 From: Stephen Webb Date: Tue, 21 Nov 2023 09:56:17 +1100 Subject: [PATCH] Warn rather than fail the test when the locale 'en_US.UTF-8' is not available (#296) --- .../cpp/helpers/charsetdecodertestcase.cpp | 33 +++++++++---- .../cpp/helpers/charsetencodertestcase.cpp | 47 ++++++++++++------- 2 files changed, 53 insertions(+), 27 deletions(-) diff --git a/src/test/cpp/helpers/charsetdecodertestcase.cpp b/src/test/cpp/helpers/charsetdecodertestcase.cpp index ba7539db6..81497a7af 100644 --- a/src/test/cpp/helpers/charsetdecodertestcase.cpp +++ b/src/test/cpp/helpers/charsetdecodertestcase.cpp @@ -17,9 +17,11 @@ #include #include +#include #include "../logunit.h" #include "../insertwide.h" #include +#include using namespace log4cxx; using namespace log4cxx::helpers; @@ -122,16 +124,27 @@ LOGUNIT_CLASS(CharsetDecoderTestCase) const logchar* greet = utf8_greet; #endif - std::locale::global(std::locale("en_US.UTF-8")); - auto dec = CharsetDecoder::getDecoder(LOG4CXX_STR("locale")); - - ByteBuffer in(utf8_greet, sizeof (utf8_greet)); - LogString out; - log4cxx_status_t stat = dec->decode(in, out); - LOGUNIT_ASSERT_EQUAL(false, CharsetDecoder::isError(stat)); - stat = dec->decode(in, out); - LOGUNIT_ASSERT_EQUAL(false, CharsetDecoder::isError(stat)); - LOGUNIT_ASSERT(out == greet); + try + { + std::locale::global(std::locale("en_US.UTF-8")); + auto dec = CharsetDecoder::getDecoder(LOG4CXX_STR("locale")); + + ByteBuffer in(utf8_greet, sizeof (utf8_greet)); + LogString out; + log4cxx_status_t stat = dec->decode(in, out); + LOGUNIT_ASSERT_EQUAL(false, CharsetDecoder::isError(stat)); + stat = dec->decode(in, out); + LOGUNIT_ASSERT_EQUAL(false, CharsetDecoder::isError(stat)); + LOGUNIT_ASSERT(out == greet); + } + catch (std::runtime_error& ex) + { + LogString msg; + Transcoder::decode(ex.what(), msg); + msg.append(LOG4CXX_STR(": ")); + msg.append(LOG4CXX_STR("en_US.UTF-8")); + LogLog::warn(msg); + } } diff --git a/src/test/cpp/helpers/charsetencodertestcase.cpp b/src/test/cpp/helpers/charsetencodertestcase.cpp index b44a6e4cc..b4ca980ed 100644 --- a/src/test/cpp/helpers/charsetencodertestcase.cpp +++ b/src/test/cpp/helpers/charsetencodertestcase.cpp @@ -19,6 +19,8 @@ #include "../logunit.h" #include "../insertwide.h" #include +#include +#include #include #include #include @@ -200,28 +202,39 @@ LOGUNIT_CLASS(CharsetEncoderTestCase) #endif LogString greeting(greet); - std::locale::global(std::locale("en_US.UTF-8")); - auto enc = CharsetEncoder::getEncoder(LOG4CXX_STR("locale")); + try + { + std::locale::global(std::locale("en_US.UTF-8")); + auto enc = CharsetEncoder::getEncoder(LOG4CXX_STR("locale")); - char buf[BUFSIZE]; - ByteBuffer out(buf, BUFSIZE); - LogString::const_iterator iter = greeting.begin(); - log4cxx_status_t stat = enc->encode(greeting, iter, out); - LOGUNIT_ASSERT_EQUAL(false, CharsetEncoder::isError(stat)); - stat = enc->encode(greeting, iter, out); - LOGUNIT_ASSERT_EQUAL(false, CharsetEncoder::isError(stat)); + char buf[BUFSIZE]; + ByteBuffer out(buf, BUFSIZE); + LogString::const_iterator iter = greeting.begin(); + log4cxx_status_t stat = enc->encode(greeting, iter, out); + LOGUNIT_ASSERT_EQUAL(false, CharsetEncoder::isError(stat)); + stat = enc->encode(greeting, iter, out); + LOGUNIT_ASSERT_EQUAL(false, CharsetEncoder::isError(stat)); - out.flip(); - LOGUNIT_ASSERT_EQUAL((size_t) 13, out.limit()); + out.flip(); + LOGUNIT_ASSERT_EQUAL((size_t) 13, out.limit()); - for (size_t i = 0; i < out.limit(); i++) + for (size_t i = 0; i < out.limit(); i++) + { + unsigned expected = (unsigned)utf8_greet[i]; + unsigned actual = (unsigned)out.data()[i]; + LOGUNIT_ASSERT_EQUAL(expected, actual); + } + + LOGUNIT_ASSERT(iter == greeting.end()); + } + catch (std::runtime_error& ex) { - unsigned expected = (unsigned)utf8_greet[i]; - unsigned actual = (unsigned)out.data()[i]; - LOGUNIT_ASSERT_EQUAL(expected, actual); + LogString msg; + Transcoder::decode(ex.what(), msg); + msg.append(LOG4CXX_STR(": ")); + msg.append(LOG4CXX_STR("en_US.UTF-8")); + LogLog::warn(msg); } - - LOGUNIT_ASSERT(iter == greeting.end()); } #if APR_HAS_THREADS