IpResolvingTransportFactoryTest.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 "../IpResolvingTransportFactory.h"
00025 #include "generated/IpResolvingTransportFactoryDriver.h"
00026 #include "generated/NameResolverMock.h"
00027 #include "generated/TransportFactoryMock.h"
00028 #include "generated/TransportFactoryStatusMock.h"
00029 #include "generated/TransportMock.h"
00030 #include "Imports.cpp"
00031 
00032 namespace Protocols {
00033 namespace Generics {
00034 namespace Testing {
00035 
00037 
00039 class IpResolvingTransportFactoryTest : public CppUnit::TestFixture
00040 {
00041     CPPUNIT_TEST_SUITE (IpResolvingTransportFactoryTest);
00042     CPPUNIT_TEST (testNoResolutionNecessaryJustDelegateToTheOtherFactory);
00043     CPPUNIT_TEST (testStartNameResolutionWhenUriIsNotIpAddressButNoNamesFound);
00044     CPPUNIT_TEST (testCreateTransportWithNameResolutionOtherFactoryFails);
00045     CPPUNIT_TEST (testCreateTransportWithNameResolutionOtherFactorySucceeds);
00046     CPPUNIT_TEST_SUITE_END();
00047 
00048     auto_ptr <NameResolverMock>                     nameResolver;
00049     auto_ptr <TransportFactoryStatusMock>           status;
00050     auto_ptr <TransportMock>                        transport;
00051     auto_ptr <TransportFactoryMock>                 otherFactory;
00052     auto_ptr <IpResolvingTransportFactory>          factoryReal;
00053     auto_ptr <IpResolvingTransportFactoryDriver>    factory;
00054 
00055     const Uri                                       notIpAddressUri;
00056     const QList <QHostAddress>                      noAddresses;
00057     const QList <QHostAddress>                      singleAddress;
00058 
00059 public:
00060     IpResolvingTransportFactoryTest()
00061      :  notIpAddressUri (Uri::fromUnencoded ("tcp://www.calitko.org")),
00062         noAddresses(),
00063         singleAddress (QList <QHostAddress>() += QHostAddress ("123.1.2.3"))
00064     {
00065     }
00066 
00067     void setUp()
00068     {
00069         nameResolver.reset (new NameResolverMock);
00070         status.reset (new TransportFactoryStatusMock);
00071         transport.reset (new TransportMock);
00072         otherFactory.reset (new TransportFactoryMock);
00073         factoryReal.reset (new IpResolvingTransportFactory (*otherFactory,
00074                                                             *nameResolver));
00075         factory.reset (new IpResolvingTransportFactoryDriver (*factoryReal));
00076     }
00077 
00078     void tearDown()
00079     {
00080         factory.reset();
00081         factoryReal.reset();
00082         otherFactory.reset();
00083         transport.reset();
00084         status.reset();
00085         nameResolver.reset();
00086     }
00087 
00088     void testNoResolutionNecessaryJustDelegateToTheOtherFactory()
00089     {
00090         Uri ipAddressUri = Uri::fromUnencoded ("tcp://127.0.0.1:22");
00091         call (factory->createTransport (ipAddressUri, *status))
00092             .willCall (otherFactory->createTransport (ipAddressUri, *status))
00093             .returns()
00094         .returns();
00095     }
00096 
00097     void refCreateTransportStartsNameResolution (const Uri &uri)
00098     {
00099         QByteArray hostName = uri.host();
00100         call (factory->createTransport (uri, *status))
00101             .willCall (nameResolver->resolveName (hostName, *factory))
00102             .returns()
00103         .returns();
00104     }
00105 
00106     void refNameResolutionFails (const Uri &uri)
00107     {
00108         QByteArray hostName = uri.host();
00109         nameResolver->calls (factory->nameResolverResolvedName (hostName,
00110                                                                 noAddresses))
00111             .willCall (status->transportFactoryFailed (uri))
00112             .returns()
00113         .returns();
00114     }
00115 
00116     // \todo make a static member in the class under test?
00117     Uri createResolvedUri (const Uri &uri,
00118                             const QList <QHostAddress> &addresses)
00119     {
00120         Uri resolvedUri = uri;
00121         // \todo Implement Uri::setHost() and Uri::setPort() and Uri::setUserInfo()
00122         resolvedUri.setAuthority (uri.userInfo(),
00123                                   addresses.first().toString().toLatin1(),
00124                                   uri.port());
00125         return resolvedUri;
00126     }
00127 
00128     void refNameResolved (const Uri &uri, const QList <QHostAddress> &addresses)
00129     {
00130         QByteArray hostName = uri.host();
00131         Uri resolvedUri = createResolvedUri (uri, addresses);
00132 
00133         nameResolver->calls (factory->nameResolverResolvedName (hostName,
00134                                                                 addresses))
00135             .willCall (otherFactory->createTransport (resolvedUri, *factory))
00136             .returns()
00137         .returns();
00138     }
00139 
00140     void refOtherFactoryFails (const Uri &uri,
00141                                const QList <QHostAddress> &addresses)
00142     {
00143         Uri resolvedUri = createResolvedUri (uri, addresses);
00144         otherFactory->calls (factory->transportFactoryFailed (resolvedUri))
00145             .willCall (status->transportFactoryFailed (uri))
00146             .returns()
00147         .returns();
00148     }
00149 
00150     void refOtherFactorySucceeds (const Uri &uri,
00151                                   const QList <QHostAddress> &addresses)
00152     {
00153         Uri resolvedUri = createResolvedUri (uri, addresses);
00154 
00155         otherFactory->calls (factory->transportFactorySucceeded (resolvedUri,
00156                                                                  *transport))
00157             .willCall (status->transportFactorySucceeded (uri, *transport))
00158             .returns()
00159         .returns();
00160     }
00161 
00162     void refDestroyTransport()
00163     {
00164         call (factory->destroyTransport (*transport))
00165             .willCall (otherFactory->destroyTransport (*transport))
00166             .returns()
00167         .returns();
00168     }
00169 
00170     void testStartNameResolutionWhenUriIsNotIpAddressButNoNamesFound()
00171     {
00172         refCreateTransportStartsNameResolution (notIpAddressUri);
00173         refNameResolutionFails (notIpAddressUri);
00174     }
00175 
00176     void testCreateTransportWithNameResolutionOtherFactoryFails()
00177     {
00178         refCreateTransportStartsNameResolution (notIpAddressUri);
00179         refNameResolved (notIpAddressUri, singleAddress);
00180         refOtherFactoryFails (notIpAddressUri, singleAddress);
00181     }
00182 
00183     void testCreateTransportWithNameResolutionOtherFactorySucceeds()
00184     {
00185         refCreateTransportStartsNameResolution (notIpAddressUri);
00186         refNameResolved (notIpAddressUri, singleAddress);
00187         refOtherFactorySucceeds (notIpAddressUri, singleAddress);
00188         refDestroyTransport();
00189     }
00190 };
00191 
00192 CPPUNIT_TEST_SUITE_REGISTRATION (IpResolvingTransportFactoryTest);
00193 
00194 } // namespace Testing
00195 } // namespace Generics
00196 } // namespace Protocols