2 #include "ace/Get_Opt.h"
3 #include "tao/ZIOP/ZIOP.h"
4 #include "tao/Compression/zlib/ZlibCompressor_Factory.h"
5 #include "tao/Compression/bzip2/Bzip2Compressor_Factory.h"
6 #include "TestCompressor/TestCompressor_Factory.h"
9 static const ACE_TCHAR
*ior
= ACE_TEXT("file://") DEFAULT_IOR_FILENAME
;
10 static ::Compression::CompressionManager_var compression_manager
= 0;
11 CORBA::ULong big_msg_size
= 40000;
13 int start_tests (Test::Hello_ptr hello
, CORBA::ORB_ptr orb
);
16 parse_args (int argc
, ACE_TCHAR
*argv
[])
18 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("k:t:s:"));
21 while ((c
= get_opts ()) != -1)
25 ior
= get_opts
.opt_arg ();
28 big_msg_size
= ACE_OS::strtoul (get_opts
.opt_arg (),0,10);
31 test
= ACE_OS::atoi (get_opts
.opt_arg ());
36 ACE_ERROR_RETURN ((LM_ERROR
,
43 // Indicates successful parsing of the command line
48 register_factories (CORBA::ORB_ptr orb
)
50 CORBA::Object_var compression_manager_obj
=
51 orb
->resolve_initial_references("CompressionManager");
53 compression_manager
= ::Compression::CompressionManager::_narrow (
54 compression_manager_obj
.in ());
56 if (CORBA::is_nil(compression_manager
.in ()))
57 ACE_ERROR_RETURN ((LM_ERROR
,
58 " (%P|%t) Panic: nil compression manager\n"),
60 ::Compression::CompressorFactory_ptr compressor_factory
;
61 ::Compression::CompressorFactory_var compr_fact
;
63 //register Zlib compressor
64 ACE_NEW_RETURN (compressor_factory
, TAO::Zlib_CompressorFactory (), 1);
65 compr_fact
= compressor_factory
;
66 compression_manager
->register_factory(compr_fact
.in ());
68 // register bzip2 compressor
69 ACE_NEW_RETURN (compressor_factory
, TAO::Bzip2_CompressorFactory (), 1);
70 compr_fact
= compressor_factory
;
71 compression_manager
->register_factory(compr_fact
.in ());
73 // register test compressor
74 ACE_NEW_RETURN (compressor_factory
, TAO::Test_CompressorFactory (), 1);
75 compr_fact
= compressor_factory
;
76 compression_manager
->register_factory(compr_fact
.in ());
81 create_compressor_id_level_list_policy (CORBA::ORB_ptr orb
, bool add_zlib_for_test_1
)
83 ::Compression::CompressorIdLevelList compressor_id_list
;
88 if (add_zlib_for_test_1
)
89 compressor_id_list
.length(2);
91 compressor_id_list
.length(1);
92 compressor_id_list
[0].compressor_id
= ::Compression::COMPRESSORID_LZO
;
93 compressor_id_list
[0].compression_level
= CLIENT_COMPRESSION_LEVEL
;
94 if (add_zlib_for_test_1
)
96 compressor_id_list
[1].compressor_id
= ::Compression::COMPRESSORID_ZLIB
;
97 compressor_id_list
[1].compression_level
= CLIENT_COMPRESSION_LEVEL
;
101 compressor_id_list
.length(1);
102 compressor_id_list
[0].compressor_id
= COMPRESSORID_FOR_TESTING
;
103 compressor_id_list
[0].compression_level
= CLIENT_COMPRESSION_LEVEL
;
108 compressor_id_list
.length(2);
109 compressor_id_list
[0].compressor_id
= ::Compression::COMPRESSORID_ZLIB
;
110 compressor_id_list
[0].compression_level
= CLIENT_COMPRESSION_LEVEL
;
111 compressor_id_list
[1].compressor_id
= ::Compression::COMPRESSORID_BZIP2
;
112 compressor_id_list
[1].compression_level
= CLIENT_COMPRESSION_LEVEL
;
115 CORBA::Any compressor_id_any
;
116 compressor_id_any
<<= compressor_id_list
;
118 return orb
->create_policy (ZIOP::COMPRESSOR_ID_LEVEL_LIST_POLICY_ID
, compressor_id_any
);
122 create_low_value_policy (CORBA::ORB_ptr orb
)
124 // Setting policy for minimum amount of bytes that needs to be
125 // compressed. If a message is smaller than this, it doesn't get
127 // make sure everything gets compressed.
128 CORBA::ULong compression_low_value
= 10;
130 compression_low_value
= 5000000;
132 CORBA::Any low_value_any
;
133 low_value_any
<<= compression_low_value
;
135 return orb
->create_policy (ZIOP::COMPRESSION_LOW_VALUE_POLICY_ID
, low_value_any
);
139 create_compression_enabled_policy (CORBA::ORB_ptr orb
)
141 // Setting policy whether compression is used.
142 CORBA::Boolean compression_enabling
= true;
143 CORBA::Any compression_enabling_any
;
144 compression_enabling_any
<<= CORBA::Any::from_boolean(compression_enabling
);
146 return orb
->create_policy (ZIOP::COMPRESSION_ENABLING_POLICY_ID
, compression_enabling_any
);
150 create_min_ratio_policy (CORBA::ORB_ptr orb
)
152 CORBA::Any min_compression_ratio_any
;
153 Compression::CompressionRatio min_compression_ratio
= 0.50;
154 min_compression_ratio_any
<<= min_compression_ratio
;
156 return orb
->create_policy (ZIOP::COMPRESSION_MIN_RATIO_POLICY_ID
, min_compression_ratio_any
);
160 create_policies (CORBA::ORB_ptr orb
, bool add_zlib_compressor
)
162 CORBA::PolicyList
policies(4);
165 policies
[0] = create_compressor_id_level_list_policy (orb
, add_zlib_compressor
);
166 policies
[1] = create_low_value_policy (orb
);
167 policies
[2] = create_compression_enabled_policy (orb
);
168 policies
[3] = create_min_ratio_policy (orb
);
170 CORBA::Object_var tmp
= orb
->string_to_object(ior
);
171 CORBA::Object_var tmp2
= tmp
->_set_policy_overrides (policies
, CORBA::ADD_OVERRIDE
);
172 Test::Hello_var hello
= Test::Hello::_narrow(tmp2
.in ());
173 return hello
._retn ();
177 prepare_tests (CORBA::ORB_ptr orb
, bool create_factories
=true)
179 #if defined TAO_HAS_ZIOP && TAO_HAS_ZIOP == 1
180 if (create_factories
)
182 register_factories(orb
);
185 return create_policies (orb
, !create_factories
);
187 ACE_UNUSED_ARG (create_factories
);
188 CORBA::Object_var tmp
= orb
->string_to_object(ior
);
189 Test::Hello_var hello
= Test::Hello::_narrow(tmp
.in ());
190 return hello
._retn ();
195 check_results (CORBA::ORB_ptr orb
)
197 #if defined TAO_HAS_ZIOP && TAO_HAS_ZIOP == 1
203 // should throw an exception
204 ::Compression::Compressor_var
compressor (
205 compression_manager
->get_compressor (
206 ::Compression::COMPRESSORID_LZO
,
207 LEAST_COMPRESSION_LEVEL
));
208 ACE_ERROR_RETURN ((LM_ERROR
,
209 ACE_TEXT ("ERROR : check_results, ")
210 ACE_TEXT ("no exception thrown when applying for ")
211 ACE_TEXT ("LZO Compressor\n")),
214 catch (const ::Compression::UnknownCompressorId
&)
216 ACE_DEBUG ((LM_DEBUG
,
217 ACE_TEXT ("check_results, expected exception caught, ")
218 ACE_TEXT ("(unknown factory)\n")));
219 Test::Hello_var hello
= prepare_tests (orb
, false);
221 return start_tests (hello
.in (), orb
);
227 ::Compression::Compressor_var
compressor (
228 compression_manager
->get_compressor (
229 ::Compression::COMPRESSORID_ZLIB
,
230 LEAST_COMPRESSION_LEVEL
));
231 if (!CORBA::is_nil (compressor
))
233 if (compressor
->compressed_bytes () == 0)
234 ACE_ERROR_RETURN ((LM_ERROR
,
235 ACE_TEXT ("ERROR : check_results, no compression used ")
236 ACE_TEXT ("during test 1a\n")),
243 ACE_ERROR_RETURN ((LM_ERROR
,
244 ACE_TEXT ("ERROR : check_results, zlib compressor not found ")
245 ACE_TEXT ("during test 1a\n")),
255 // low value policy test. No compression should be used.
256 ::Compression::Compressor_var
compressor (
257 compression_manager
->get_compressor (
258 ::Compression::COMPRESSORID_ZLIB
,
259 LEAST_COMPRESSION_LEVEL
));
260 if (!CORBA::is_nil (compressor
))
262 if (compressor
->compressed_bytes () != 0)
263 ACE_ERROR_RETURN ((LM_ERROR
,
264 ACE_TEXT ("ERROR : check_results, compression used ")
265 ACE_TEXT ("during test %d\n"), test
),
272 ACE_ERROR_RETURN ((LM_ERROR
,
273 ACE_TEXT ("ERROR : check_results, zlib compressor not found ")
274 ACE_TEXT ("during test %d\n"), test
),
280 ACE_ERROR_RETURN ((LM_ERROR
,
281 ACE_TEXT ("ERROR : check_results, unknown test ID\n")),
284 ACE_ERROR_RETURN ((LM_ERROR
,
285 ACE_TEXT ("ERROR : check_results, unexpected\n")),
288 ACE_UNUSED_ARG (orb
);
294 run_string_test (Test::Hello_ptr hello
)
297 ACE_TEXT("run_string_test, start\n")));
302 ACE_TEXT("*** NOTE TestCompressor is EXPECTED to throw IDL:omg.org/Compression/CompressionException ***\n")));
305 CORBA::String_var the_string
= hello
->get_string ("This is a test string"
306 "This is a test string"
307 "This is a test string"
308 "This is a test string"
309 "This is a test string"
310 "This is a test string"
311 "This is a test string"
312 "This is a test string"
313 "This is a test string");
314 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) - string returned <%C>\n",
316 if (ACE_OS::strstr (the_string
.in (), "Hello there") == 0)
318 ACE_ERROR_RETURN ((LM_ERROR
,
319 ACE_TEXT ("ERROR : run_string_test, unexpected string received\n")),
327 run_big_reply_test (Test::Hello_ptr hello
)
330 ACE_TEXT("start get_big_reply\n")));
335 ACE_TEXT("*** NOTE TestCompressor is EXPECTED to throw IDL:omg.org/Compression/CompressionException ***\n")));
338 //Prepare to send a large number of bytes. Should be compressed
339 Test::Octet_Seq_var dummy
= hello
->get_big_reply (big_msg_size
);
340 if (dummy
->length () > 0)
342 ACE_DEBUG ((LM_DEBUG
,
343 ACE_TEXT("get_big_reply, received = %d bytes\n"),
348 ACE_ERROR_RETURN ((LM_ERROR
,
349 ACE_TEXT ("ERROR : get_big_reply, ")
350 ACE_TEXT ("error receiving client side blob\n")),
357 run_big_request_test (Test::Hello_ptr hello
)
359 Test::Octet_Seq
send_msg(big_msg_size
);
360 send_msg
.length (big_msg_size
);
362 for (CORBA::ULong i
= 0; i
< big_msg_size
; ++i
)
364 send_msg
[i
]= static_cast<CORBA::Octet
> (i
& 0xff);
368 ACE_TEXT("run_big_request_test, send = %d bytes\n"), big_msg_size
));
373 ACE_TEXT("*** NOTE TestCompressor is EXPECTED to throw IDL:omg.org/Compression/CompressionException ***\n")));
376 hello
->big_request(send_msg
);
381 start_tests (Test::Hello_ptr hello
, CORBA::ORB_ptr orb
)
386 result
+= run_string_test (hello
);
387 result
+= run_big_request_test (hello
);
389 result
+= run_big_reply_test (hello
);
391 result
+= check_results (orb
);
396 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
401 CORBA::ORB_var orb
= CORBA::ORB_init (argc
, argv
);
403 if (parse_args (argc
, argv
) != 0)
406 Test::Hello_var hello
= prepare_tests (orb
.in ());
408 if (CORBA::is_nil (hello
.in ()))
410 ACE_ERROR_RETURN ((LM_DEBUG
,
411 "ERROR : Nil Test::Hello reference <%C>\n",
418 result
+= start_tests(hello
.in (), orb
.in ());
420 catch (const CORBA::Exception
& ex
)
422 ex
._tao_print_exception ("Exception caught:");
429 ACE_TEXT("*** NOTE TestCompressor is EXPECTED to throw IDL:omg.org/Compression/CompressionException ***\n")));
436 catch (const CORBA::Exception
& ex
)
438 ex
._tao_print_exception ("Exception caught:");