Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Compression / zlibserver.cpp
blob02f9add34efcf04f66ae0156c445ff039efc6dfc
1 #include "ace/Get_Opt.h"
2 #include "ace/OS_NS_stdio.h"
3 #include "tao/ORB.h"
4 #include "tao/Compression/Compression.h"
5 #include "tao/Compression/zlib/ZlibCompressor_Factory.h"
7 bool
8 test_invalid_compression_factory (Compression::CompressionManager_ptr cm)
10 bool succeed = false;
11 try
13 // Get an invalid compression factory
14 Compression::CompressorFactory_var factory =
15 cm->get_factory (100);
17 catch (const Compression::UnknownCompressorId& ex)
19 ACE_UNUSED_ARG (ex);
20 succeed = true;
22 catch (const CORBA::Exception&)
26 if (!succeed)
28 ACE_ERROR ((LM_ERROR,
29 "(%t) ERROR, get invalid compression factory failed\n"));
32 return succeed;
36 bool
37 test_duplicate_compression_factory (
38 Compression::CompressionManager_ptr cm,
39 Compression::CompressorFactory_ptr cf)
41 bool succeed = false;
42 try
44 // Register duplicate
45 cm->register_factory (cf);
47 catch (const Compression::FactoryAlreadyRegistered&)
49 succeed = true;
51 catch (const CORBA::Exception&)
55 if (!succeed)
57 ACE_ERROR ((LM_ERROR,
58 "(%t) ERROR, register duplicate factory failed\n"));
61 return succeed;
64 bool
65 test_register_nil_compression_factory (
66 Compression::CompressionManager_ptr cm)
68 bool succeed = false;
69 try
71 // Register nil factory
72 cm->register_factory (Compression::CompressorFactory::_nil());
74 catch (const CORBA::BAD_PARAM& ex)
76 if ((ex.minor() & 0xFFFU) == 44)
78 succeed = true;
81 catch (const CORBA::Exception&)
85 if (!succeed)
87 ACE_ERROR ((LM_ERROR,
88 "(%t) ERROR, register nill factory failed\n"));
91 return succeed;
94 bool
95 test_compression (CORBA::ULong nelements,
96 Compression::CompressionManager_ptr cm)
98 bool succeed = false;
100 CORBA::OctetSeq mytest;
101 mytest.length (nelements);
102 for (CORBA::ULong j = 0; j != nelements; ++j)
104 mytest[j] = 'a';
107 Compression::Compressor_var compressor = cm->get_compressor (::Compression::COMPRESSORID_ZLIB, 6);
109 CORBA::OctetSeq myout;
110 myout.length ((CORBA::ULong)(mytest.length() * 1.1));
112 compressor->compress (mytest, myout);
114 CORBA::OctetSeq decompress;
115 decompress.length (nelements);
117 compressor->decompress (myout, decompress);
119 if (decompress != mytest)
121 ACE_ERROR ((LM_ERROR, "Error, decompress not working\n"));
124 else
126 succeed = true;
127 ACE_DEBUG ((LM_DEBUG, "Compression worked with zlib, original "
128 "size %d, compressed size %d\n",
129 mytest.length(), myout.length ()));
131 return succeed;
135 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
137 int retval = 0;
140 CORBA::ORB_var orb =
141 CORBA::ORB_init (argc, argv);
143 CORBA::Object_var compression_manager =
144 orb->resolve_initial_references("CompressionManager");
146 Compression::CompressionManager_var manager =
147 Compression::CompressionManager::_narrow (compression_manager.in ());
149 if (CORBA::is_nil(manager.in ()))
150 ACE_ERROR_RETURN ((LM_ERROR,
151 " (%P|%t) Panic: nil compression manager\n"),
154 Compression::CompressorFactory_ptr compressor_factory;
156 ACE_NEW_RETURN (compressor_factory, TAO::Zlib_CompressorFactory (), 1);
158 Compression::CompressorFactory_var compr_fact = compressor_factory;
159 manager->register_factory(compr_fact.in ());
161 if (!test_duplicate_compression_factory (manager.in (), compr_fact.in ()))
162 retval = 1;
164 if (!test_register_nil_compression_factory (manager.in ()))
165 retval = 1;
167 if (!test_compression (1024, manager.in ()))
168 retval = 1;
170 if (!test_compression (5, manager.in ()))
171 retval = 1;
174 if (!test_invalid_compression_factory (manager.in ()))
175 retval = 1;
177 orb->destroy ();
179 catch (const CORBA::Exception& ex)
181 ex._tao_print_exception ("Exception caught:");
182 retval = 1;
185 return retval;