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.test.ssdp;
17  
18  import org.fourthline.cling.mock.MockUpnpService;
19  import org.fourthline.cling.mock.MockUpnpServiceConfiguration;
20  import org.fourthline.cling.model.ExpirationDetails;
21  import org.fourthline.cling.model.meta.RemoteDevice;
22  import org.fourthline.cling.model.resource.Resource;
23  import org.fourthline.cling.test.data.SampleData;
24  import org.testng.annotations.Test;
25  
26  import java.net.URI;
27  import java.util.List;
28  
29  import static org.testng.Assert.assertEquals;
30  
31  public class RegistryExpirationTest {
32  
33      @Test
34      public void addAndExpire() throws Exception {
35  
36          MockUpnpService upnpService = new MockUpnpService(false, true);
37  
38          RemoteDevice rd = SampleData.createRemoteDevice(
39                  SampleData.createRemoteDeviceIdentity(1)
40          );
41          upnpService.getRegistry().addDevice(rd);
42          
43          assertEquals(upnpService.getRegistry().getRemoteDevices().size(), 1);
44  
45          Thread.sleep(3000);
46  
47          assertEquals(upnpService.getRegistry().getRemoteDevices().size(), 0);
48  
49          upnpService.shutdown();
50      }
51  
52      @Test
53      public void overrideAgeThenAddAndExpire() throws Exception {
54  
55          MockUpnpService upnpService = new MockUpnpService(
56              new MockUpnpServiceConfiguration(true) {
57  
58                  @Override
59                  public Integer getRemoteDeviceMaxAgeSeconds() {
60                      return 0;
61                  }
62              }
63          );
64  
65          RemoteDevice rd = SampleData.createRemoteDevice(
66                  SampleData.createRemoteDeviceIdentity(1)
67          );
68          upnpService.getRegistry().addDevice(rd);
69  
70          assertEquals(upnpService.getRegistry().getRemoteDevices().size(), 1);
71  
72          Thread.sleep(3000);
73  
74          // Still registered!
75          assertEquals(upnpService.getRegistry().getRemoteDevices().size(), 1);
76  
77          // Update should not change the expiration time
78          upnpService.getRegistry().update(rd.getIdentity());
79  
80          Thread.sleep(3000);
81  
82          // Still registered!
83          assertEquals(upnpService.getRegistry().getRemoteDevices().size(), 1);
84  
85          upnpService.shutdown();
86      }
87  
88      @Test
89      public void addAndUpdateAndExpire() throws Exception {
90  
91          MockUpnpService upnpService = new MockUpnpService(false, true);
92  
93          RemoteDevice rd = SampleData.createRemoteDevice(
94                  SampleData.createRemoteDeviceIdentity(2)
95          );
96  
97          // Add it to registry
98          upnpService.getRegistry().addDevice(rd);
99          Thread.sleep(1000);
100         assertEquals(upnpService.getRegistry().getRemoteDevices().size(), 1);
101 
102         // Update it in registry
103         upnpService.getRegistry().addDevice(rd);
104         Thread.sleep(1000);
105         assertEquals(upnpService.getRegistry().getRemoteDevices().size(), 1);
106 
107         // Update again
108         upnpService.getRegistry().update(rd.getIdentity());
109         Thread.sleep(1000);
110         assertEquals(upnpService.getRegistry().getRemoteDevices().size(), 1);
111 
112         // Wait for expiration
113         Thread.sleep(3000);
114         assertEquals(upnpService.getRegistry().getRemoteDevices().size(), 0);
115 
116 
117         upnpService.shutdown();
118     }
119 
120     @Test
121     public void addResourceAndExpire() throws Exception {
122 
123         MockUpnpService upnpService = new MockUpnpService(false, true);
124 
125         Resource resource = new Resource(URI.create("/this/is/a/test"), "foo");
126         upnpService.getRegistry().addResource(resource, 2);
127 
128         assertEquals(upnpService.getRegistry().getResources().size(), 1);
129 
130         Thread.sleep(4000);
131 
132         assertEquals(upnpService.getRegistry().getResources().size(), 0);
133 
134         upnpService.shutdown();
135     }
136 
137     @Test
138     public void addResourceAndMaintain() throws Exception {
139 
140         MockUpnpService upnpService = new MockUpnpService(false, true);
141 
142         final TestRunnable testRunnable = new TestRunnable();
143 
144         Resource resource = new Resource<String>(URI.create("/this/is/a/test"), "foo") {
145             @Override
146             public void maintain(List<Runnable> pendingExecutions, ExpirationDetails expirationDetails) {
147                 if (expirationDetails.getSecondsUntilExpiration() == 1) {
148                     pendingExecutions.add(testRunnable);
149                 }
150             }
151         };
152         upnpService.getRegistry().addResource(resource, 2);
153 
154         assertEquals(upnpService.getRegistry().getResources().size(), 1);
155 
156         Thread.sleep(2000);
157 
158         assertEquals(testRunnable.wasExecuted, true);
159 
160         upnpService.shutdown();
161     }
162 
163     protected class TestRunnable implements Runnable {
164         boolean wasExecuted = false;
165 
166         public void run() {
167             wasExecuted = true;
168         }
169     }
170 
171 }