View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14   */
15  
16  package org.fourthline.cling.mock;
17  
18  import org.fourthline.cling.UpnpService;
19  import org.fourthline.cling.UpnpServiceConfiguration;
20  import org.fourthline.cling.controlpoint.ControlPoint;
21  import org.fourthline.cling.controlpoint.ControlPointImpl;
22  import org.fourthline.cling.model.message.header.UpnpHeader;
23  import org.fourthline.cling.model.meta.LocalDevice;
24  import org.fourthline.cling.protocol.ProtocolFactory;
25  import org.fourthline.cling.protocol.ProtocolFactoryImpl;
26  import org.fourthline.cling.protocol.async.SendingNotificationAlive;
27  import org.fourthline.cling.protocol.async.SendingSearch;
28  import org.fourthline.cling.registry.Registry;
29  import org.fourthline.cling.registry.RegistryImpl;
30  import org.fourthline.cling.registry.RegistryMaintainer;
31  import org.fourthline.cling.transport.RouterException;
32  import org.fourthline.cling.transport.spi.NetworkAddressFactory;
33  
34  import javax.enterprise.inject.Alternative;
35  
36  /**
37   * Simplifies testing of core and non-core modules.
38   * <p>
39   * It uses the {@link org.fourthline.cling.mock.MockUpnpService.MockProtocolFactory}.
40   * </p>
41   *
42   * @author Christian Bauer
43   */
44  @Alternative
45  public class MockUpnpService implements UpnpService {
46  
47      protected final UpnpServiceConfiguration configuration;
48      protected final ControlPoint controlPoint;
49      protected final ProtocolFactory protocolFactory;
50      protected final Registry registry;
51      protected final MockRouter router;
52  
53      protected final NetworkAddressFactory networkAddressFactory;
54  
55      /**
56       * Single-thread of execution for the whole UPnP stack, no ALIVE messages or registry maintenance.
57       */
58      public MockUpnpService() {
59          this(false, new MockUpnpServiceConfiguration(false, false));
60      }
61  
62      /**
63       * No ALIVE messages.
64       */
65      public MockUpnpService(MockUpnpServiceConfiguration configuration) {
66          this(false, configuration);
67      }
68  
69      /**
70       * Single-thread of execution for the whole UPnP stack, except one background registry maintenance thread.
71       */
72      public MockUpnpService(final boolean sendsAlive, final boolean maintainsRegistry) {
73          this(sendsAlive, new MockUpnpServiceConfiguration(maintainsRegistry, false));
74      }
75  
76      public MockUpnpService(final boolean sendsAlive, final boolean maintainsRegistry, final boolean multiThreaded) {
77          this(sendsAlive, new MockUpnpServiceConfiguration(maintainsRegistry, multiThreaded));
78      }
79  
80      public MockUpnpService(final boolean sendsAlive, final MockUpnpServiceConfiguration configuration) {
81  
82          this.configuration = configuration;
83  
84          this.protocolFactory = createProtocolFactory(this, sendsAlive);
85  
86          this.registry = new RegistryImpl(this) {
87              @Override
88              protected RegistryMaintainer createRegistryMaintainer() {
89                  return configuration.isMaintainsRegistry() ? super.createRegistryMaintainer() : null;
90              }
91          };
92  
93          this.networkAddressFactory = this.configuration.createNetworkAddressFactory();
94  
95          this.router = createRouter();
96  
97          this.controlPoint = new ControlPointImpl(configuration, protocolFactory, registry);
98      }
99  
100     protected ProtocolFactory createProtocolFactory(UpnpService service, boolean sendsAlive) {
101         return new MockProtocolFactory(service, sendsAlive);
102     }
103 
104     protected MockRouter createRouter() {
105         return new MockRouter(getConfiguration(), getProtocolFactory());
106     }
107 
108     /**
109      * This factory customizes several protocols.
110      * <p>
111      * The {@link org.fourthline.cling.protocol.async.SendingNotificationAlive} protocol
112      * only sends messages if this feature is enabled when instantiating the factory.
113      * </p>
114      * <p>
115      * The {@link org.fourthline.cling.protocol.async.SendingSearch} protocol doesn't wait between
116      * sending search message bulks, this speeds up testing.
117      * </p>
118      */
119     public static class MockProtocolFactory extends ProtocolFactoryImpl {
120 
121         private boolean sendsAlive;
122 
123         public MockProtocolFactory(UpnpService upnpService, boolean sendsAlive) {
124             super(upnpService);
125             this.sendsAlive = sendsAlive;
126         }
127 
128         @Override
129         public SendingNotificationAlive createSendingNotificationAlive(LocalDevice localDevice) {
130             return new SendingNotificationAlive(getUpnpService(), localDevice) {
131                 @Override
132                 protected void execute() throws RouterException {
133                     if (sendsAlive) super.execute();
134                 }
135             };
136         }
137 
138         @Override
139         public SendingSearch createSendingSearch(UpnpHeader searchTarget, int mxSeconds) {
140             return new SendingSearch(getUpnpService(), searchTarget, mxSeconds) {
141                 @Override
142                 public int getBulkIntervalMilliseconds() {
143                     return 0; // Don't wait
144                 }
145             };
146         }
147     }
148 
149     public UpnpServiceConfiguration getConfiguration() {
150         return configuration;
151     }
152 
153     public ControlPoint getControlPoint() {
154         return controlPoint;
155     }
156 
157     public ProtocolFactory getProtocolFactory() {
158         return protocolFactory;
159     }
160 
161     public Registry getRegistry() {
162         return registry;
163     }
164 
165     public MockRouter getRouter() {
166         return router;
167     }
168 
169     public void shutdown() {
170         getRegistry().shutdown();
171         getConfiguration().shutdown();
172     }
173 }