ClientHttpSessionFactoryTest.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2006-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 */
00022 
00023 #include "Qt.h"
00024 #include "../ClientHttpSessionFactory.h"
00025 #include "../CompositeSingleHostClientHttpSession.h"
00026 #include "generated/ClientHttpSessionFactoryDriver.h"
00027 #include "generated/ClientHttpSessionFactoryMock.h" // \todo for ClientHttpSessionFactoryStatusMock
00028 #include "generated/ClientHttpSessionStatusMock.h"
00029 #include "Imports.cpp"
00030 
00031 namespace Protocols {
00032 namespace Http {
00033 namespace Testing {
00034 
00035 // \todo can we refactor that away?
00036 class SessionCreatedChecker
00037 {
00038     Uri uri_;
00039 
00040 public:
00041     static ClientHttpSession *checkedHttpSession;
00042 
00043     SessionCreatedChecker (const Uri &uri)
00044     : uri_ (uri) {}
00045 
00046     typedef void (Function) (const Uri &, ClientHttpSession *);
00047 
00048     bool check (const Uri &uri, ClientHttpSession *httpSession)
00049     {
00050         checkedHttpSession = httpSession;
00051         CPPUNIT_ASSERT (uri_ == uri);
00052         CPPUNIT_ASSERT (httpSession != 0);
00053         CPPUNIT_ASSERT (typeid (CompositeSingleHostClientHttpSession) ==
00054                             typeid (*httpSession));
00055         return true;
00056     }
00057 };
00058 
00059 using Generics::TransportStatus; // \todo remove after refactoring the hacks
00060 class AnyPointer
00061 {
00062 public:
00063     typedef void (Function) (TransportStatus *);
00064 
00065     bool check (TransportStatus *)
00066     {
00067         return true;
00068     }
00069 };
00070 
00071 ClientHttpSession * SessionCreatedChecker::checkedHttpSession = 0;
00072 
00074 
00076 class ClientHttpSessionFactoryTest : public CppUnit::TestFixture
00077 {
00078     CPPUNIT_TEST_SUITE (ClientHttpSessionFactoryTest);
00079     CPPUNIT_TEST (testConvertHttpUriOnlyWithSchemeAndHostToTcpUri);
00080     CPPUNIT_TEST (testConvertHttpUriOnlyWithAllFieldsToTcpUri);
00081     CPPUNIT_TEST (testConvertHttpUriWithoutSchemeReturnsNullUri);
00082     CPPUNIT_TEST (testCreateClientHttpSessionFromTransportThenDestroyThem);
00083     CPPUNIT_TEST (testCreateClientHttpSessionForUriSucceedsAndDestroyThem);
00084     CPPUNIT_TEST (testCreateClientHttpSessionForUriFails);
00085     CPPUNIT_TEST (testCreateClientHttpSessionForUriFailsSynchronously);
00086     CPPUNIT_TEST_SUITE_END();
00087 
00088     auto_ptr <TransportFactoryMock>                 transportFactory;
00089     auto_ptr <TransportMock>                        transport;
00090     auto_ptr <ClientHttpSessionStatusMock>          httpSessionStatus;
00091     auto_ptr <ClientHttpSessionFactoryStatusMock>   factoryStatus;
00092     auto_ptr <ClientHttpSessionFactory>             factoryReal;
00093     auto_ptr <ClientHttpSessionFactoryDriver>       factory;
00094 
00095     const Uri                                       uri;
00096     const Uri                                       uriForTransport;
00097     const Uri                                       uriAllFields;
00098     const Uri                                       uriAllFieldsForTransport;
00099     const Uri                                       badUri;
00100     const Uri                                       badUriForTransport;
00101 
00102 public:
00103     ClientHttpSessionFactoryTest()
00104      :  uri (Uri::fromUnencoded ("http://www.calitko.org")),
00105         uriForTransport (
00106             Uri::fromUnencoded ("tcp://www.calitko.org:80?rb=8192&wb=8192")),
00107         uriAllFields (
00108             Uri::fromUnencoded ("http://u:p@www.calitko.org:45/p?aha=&a#f")),
00109         uriAllFieldsForTransport (
00110             Uri::fromUnencoded ("tcp://www.calitko.org:45?rb=8192&wb=8192")),
00111         badUri (Uri::fromUnencoded ("//www.calitko.org")),
00112         badUriForTransport()
00113     {
00114     }
00115 
00116     void setUp()
00117     {
00118         transportFactory.reset (new TransportFactoryMock);
00119         transport.reset (new TransportMock);
00120         httpSessionStatus.reset (new ClientHttpSessionStatusMock);
00121         factoryStatus.reset (new ClientHttpSessionFactoryStatusMock);
00122         factoryReal.reset (new ClientHttpSessionFactory
00123                                 (*transportFactory));
00124         factory.reset (new ClientHttpSessionFactoryDriver (*factoryReal));
00125 
00126         // In all tests transport->setTransportStatus() should be called at
00127         // least once:
00128         // \todo That looks ugly... Once we implement custom checkers for
00129         // for return values and constrains for parameters, we could integrate
00130         // this check back in the individual test but now we just save us
00131         // some unnecessary duplication of ugly code:
00132         ExpectationPair <ExpectedFunction <void (TransportStatus *)> >
00133             expectation = transport->setTransportStatus (checker (AnyPointer()));
00134         expectation.list.add (expectation.function);
00135     }
00136 
00137     void tearDown()
00138     {
00139         factory.reset();
00140         factoryReal.reset();
00141         factoryStatus.reset();
00142         httpSessionStatus.reset();
00143         transport.reset();
00144         transportFactory.reset();
00145     }
00146 
00148     void scenarioMakeTransportUri (const Uri &httpUri,
00149                                    const Uri &expectedTransportUri)
00150     {
00151         Uri transportUri = ClientHttpSessionFactory::makeTransportUri (httpUri);
00152         CPPUNIT_ASSERT (expectedTransportUri == transportUri);
00153     }
00154 
00156     void testConvertHttpUriOnlyWithSchemeAndHostToTcpUri()
00157     {
00158         scenarioMakeTransportUri (uri, uriForTransport);
00159     }
00160 
00162     void testConvertHttpUriOnlyWithAllFieldsToTcpUri()
00163     {
00164         scenarioMakeTransportUri (uriAllFields, uriAllFieldsForTransport);
00165     }
00166 
00168     void testConvertHttpUriWithoutSchemeReturnsNullUri()
00169     {
00170         scenarioMakeTransportUri (badUri, badUriForTransport);
00171     }
00172 
00174     ClientHttpSession * refCreateSessionFromTransport()
00175     {
00176         ClientHttpSession *httpSession =
00177             factoryReal->createSession (*transport, *httpSessionStatus);
00178         CPPUNIT_ASSERT (httpSession != 0);
00179         CPPUNIT_ASSERT (typeid (CompositeSingleHostClientHttpSession) ==
00180                             typeid (*httpSession));
00181         return httpSession;
00182     }
00183 
00185     void refCreateSessionFromUri (const Uri &uri)
00186     {
00187         Uri transportUri = ClientHttpSessionFactory::makeTransportUri (uri);
00188         call (factory->createSession (uri, *httpSessionStatus, *factoryStatus))
00189             .willCall (transportFactory->createTransport (transportUri,
00190                                                           *factory))
00191             .returns()
00192         .returns();
00193     }
00194 
00196     void refSessionCreatedFromUriAsynchronously (const Uri &uri)
00197     {
00198         Uri transportUri = ClientHttpSessionFactory::makeTransportUri (uri);
00199         transportFactory->calls (factory->transportFactorySucceeded
00200                                         (transportUri, *transport))
00201             .willCall (factoryStatus->clientHttpSessionFactorySucceeded
00202                                     (checker (SessionCreatedChecker (uri))))
00203             .returns()
00204         .returns();
00205     }
00206 
00208     void refSessionNotCreatedFromUriAsynchronously (const Uri &uri)
00209     {
00210         Uri transportUri = ClientHttpSessionFactory::makeTransportUri (uri);
00211         transportFactory->calls (factory->transportFactoryFailed (transportUri))
00212             .willCall (factoryStatus->clientHttpSessionFactoryFailed (uri))
00213             .returns()
00214         .returns();
00215     }
00216 
00218     void refDestroySession (ClientHttpSession *httpSession)
00219     {
00220         call (factory->destroySession (httpSession))
00221             .willCall (transportFactory->destroyTransport (*transport))
00222             .returns()
00223         .returns();
00224     }
00225 
00227     void testCreateClientHttpSessionFromTransportThenDestroyThem()
00228     {
00229         ClientHttpSession *httpSession = refCreateSessionFromTransport();
00230         refDestroySession (httpSession);
00231     }
00232 
00234     void testCreateClientHttpSessionForUriSucceedsAndDestroyThem()
00235     {
00236         refCreateSessionFromUri (uri);
00237         refSessionCreatedFromUriAsynchronously (uri);
00238         refDestroySession (SessionCreatedChecker::checkedHttpSession);
00239     }
00240 
00242     void testCreateClientHttpSessionForUriFails()
00243     {
00244         refCreateSessionFromUri (uri);
00245         refSessionNotCreatedFromUriAsynchronously (uri);
00246     }
00247 
00249     void testCreateClientHttpSessionForUriFailsSynchronously()
00250     {
00251         call (factory->createSession (badUri, *httpSessionStatus,
00252                                       *factoryStatus))
00253             .willCall (factoryStatus->clientHttpSessionFactoryFailed (badUri))
00254             .returns()
00255         .returns();
00256     }
00257 };
00258 
00259 CPPUNIT_TEST_SUITE_REGISTRATION (ClientHttpSessionFactoryTest);
00260 
00261 } // namespace Testing
00262 } // namespace Http
00263 } // namespace Protocols