More tests update
[ACE_TAO.git] / TAO / tests / Compression / bzip2server.cpp
bloba9e938e92835510e4c50c9864386e697de67c041
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/bzip2/Bzip2Compressor_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&)
19 succeed = true;
21 catch (const CORBA::Exception&)
25 if (!succeed)
27 ACE_ERROR ((LM_ERROR,
28 "(%t) ERROR, get invalid compression factory failed\n"));
31 return succeed;
34 bool
35 test_duplicate_compression_factory (
36 Compression::CompressionManager_ptr cm,
37 Compression::CompressorFactory_ptr cf)
39 bool succeed = false;
40 try
42 // Register duplicate
43 cm->register_factory (cf);
45 catch (const Compression::FactoryAlreadyRegistered&)
47 succeed = true;
49 catch (const CORBA::Exception&)
53 if (!succeed)
55 ACE_ERROR ((LM_ERROR,
56 "(%t) ERROR, register duplicate factory failed\n"));
59 return succeed;
62 bool
63 test_register_nil_compression_factory (
64 Compression::CompressionManager_ptr cm)
66 bool succeed = false;
67 try
69 // Register nil factory
70 cm->register_factory (Compression::CompressorFactory::_nil());
72 catch (const CORBA::BAD_PARAM& ex)
74 if ((ex.minor() & 0xFFFU) == 44)
76 succeed = true;
79 catch (const CORBA::Exception&)
83 if (!succeed)
85 ACE_ERROR ((LM_ERROR,
86 "(%t) ERROR, register nill factory failed\n"));
89 return succeed;
92 bool
93 test_compression (CORBA::ULong nelements,
94 Compression::CompressionManager_ptr cm)
96 bool succeed = false;
98 CORBA::OctetSeq mytest;
99 mytest.length (nelements);
100 for (CORBA::ULong j = 0; j != nelements; ++j)
102 mytest[j] = 'a';
105 Compression::Compressor_var compressor = cm->get_compressor (
106 ::Compression::COMPRESSORID_BZIP2, 6);
108 CORBA::OctetSeq myout;
109 myout.length ((CORBA::ULong)(mytest.length() * 1.1));
111 compressor->compress (mytest, myout);
113 CORBA::OctetSeq decompress;
114 decompress.length (nelements);
116 compressor->decompress (myout, decompress);
118 if (decompress != mytest)
120 ACE_ERROR ((LM_ERROR, "Error, decompress not working\n"));
122 else
124 succeed = true;
125 ACE_DEBUG ((LM_DEBUG, "Compression worked with bzip2, original "
126 "size %d, compressed size %d\n",
127 mytest.length(), myout.length ()));
129 return succeed;
133 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
135 int retval = 0;
138 CORBA::ORB_var orb =
139 CORBA::ORB_init (argc, argv);
141 CORBA::Object_var compression_manager =
142 orb->resolve_initial_references("CompressionManager");
144 Compression::CompressionManager_var manager =
145 Compression::CompressionManager::_narrow (compression_manager.in ());
147 if (CORBA::is_nil(manager.in ()))
148 ACE_ERROR_RETURN ((LM_ERROR,
149 " (%P|%t) Panic: nil compression manager\n"),
152 Compression::CompressorFactory_ptr compressor_factory;
154 ACE_NEW_RETURN (compressor_factory, TAO::Bzip2_CompressorFactory (), 1);
156 Compression::CompressorFactory_var compr_fact = compressor_factory;
157 manager->register_factory(compr_fact.in ());
159 if (!test_duplicate_compression_factory (manager.in (), compr_fact.in ()))
160 retval = 1;
162 if (!test_register_nil_compression_factory (manager.in ()))
163 retval = 1;
165 if (!test_compression (1024, manager.in ()))
166 retval = 1;
168 if (!test_compression (5, manager.in ()))
169 retval = 1;
171 if (!test_invalid_compression_factory (manager.in ()))
172 retval = 1;
174 orb->destroy ();
176 catch (const CORBA::Exception& ex)
178 ex._tao_print_exception ("Exception caught:");
179 retval = 1;
182 return retval;