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 example.localservice;
17  
18  import org.fourthline.cling.binding.LocalServiceBinder;
19  import org.fourthline.cling.binding.annotations.AnnotationLocalServiceBinder;
20  import org.fourthline.cling.model.DefaultServiceManager;
21  import org.fourthline.cling.model.meta.DeviceDetails;
22  import org.fourthline.cling.model.meta.LocalDevice;
23  import org.fourthline.cling.model.meta.LocalService;
24  import org.fourthline.cling.model.types.Datatype;
25  import org.fourthline.cling.model.types.DeviceType;
26  import org.fourthline.cling.test.data.SampleData;
27  import org.testng.annotations.DataProvider;
28  import org.testng.annotations.Test;
29  
30  import static org.testng.Assert.assertEquals;
31  
32  /**
33   * Exclusive list of string values
34   * <p>
35   * If you have a static list of legal string values, set it directly on the annotation
36   * of your state variable's field:
37   * </p>
38   * <a class="citation" href="javacode://example.localservice.MyServiceWithAllowedValues" style="include: VAR"/>
39   * <p>
40   * Alternatively, if your allowed values have to be determined dynamically when
41   * your service is being bound, you can implement a class with the
42   * <code>org.fourthline.cling.binding.AllowedValueProvider</code> interface:
43   * </p>
44   * <a class="citation" href="javacode://example.localservice.MyServiceWithAllowedValueProvider" style="include: PROVIDER"/>
45   * <p>
46   * Then, instead of specifying a static list of string values in your state variable declaration,
47   * name the provider class:
48   * </p>
49   * <a class="citation" id="MyServiceWithAllowedValueProvider-VAR" href="javacode://example.localservice.MyServiceWithAllowedValueProvider" style="include: VAR"/>
50   * <p>
51   * Note that this provider will only be queried when your annotations are being processed,
52   * once when your service is bound in Cling.
53   * </p>
54   */
55  public class AllowedValueTest {
56  
57      public LocalDevice createTestDevice(Class serviceClass) throws Exception {
58  
59          LocalServiceBinder binder = new AnnotationLocalServiceBinder();
60          LocalService svc = binder.read(serviceClass);
61          svc.setManager(new DefaultServiceManager(svc, serviceClass));
62  
63          return new LocalDevice(
64              SampleData.createLocalDeviceIdentity(),
65              new DeviceType("mydomain", "CustomDevice", 1),
66              new DeviceDetails("A Custom Device"),
67              svc
68          );
69      }
70  
71      @DataProvider(name = "devices")
72      public Object[][] getDevices() {
73          try {
74              return new LocalDevice[][]{
75                  {createTestDevice(MyServiceWithAllowedValues.class)},
76                  {createTestDevice(MyServiceWithAllowedValueProvider.class)},
77              };
78          } catch (Exception ex) {
79              ex.printStackTrace(System.err);
80              // Damn testng swallows exceptions in provider/factory methods
81              throw new RuntimeException(ex);
82          }
83      }
84  
85      @Test(dataProvider = "devices")
86      public void validateBinding(LocalDevice device) {
87          LocalService svc = device.getServices()[0];
88          assertEquals(svc.getStateVariables().length, 1);
89          assertEquals(svc.getStateVariables()[0].getTypeDetails().getDatatype().getBuiltin(), Datatype.Builtin.STRING);
90          assertEquals(svc.getStateVariables()[0].getTypeDetails().getAllowedValues().length, 3);
91          assertEquals(svc.getStateVariables()[0].getTypeDetails().getAllowedValues()[0], "Foo");
92          assertEquals(svc.getStateVariables()[0].getTypeDetails().getAllowedValues()[1], "Bar");
93          assertEquals(svc.getStateVariables()[0].getTypeDetails().getAllowedValues()[2], "Baz");
94      }
95  
96  }