View Javadoc
1   /*
2    * Copyright (c) 2013-2014, Dienst Landelijk Gebied - Ministerie van Economische Zaken
3    * 
4    * Gepubliceerd onder de BSD 2-clause licentie, 
5    * zie https://github.com/MinELenI/CBSviewer/blob/master/LICENSE.md voor de volledige licentie. 
6    */
7   package nl.mineleni.cbsviewer.util;
8   
9   import static org.junit.Assert.assertEquals;
10  import static org.junit.Assert.assertFalse;
11  import static org.junit.Assert.assertTrue;
12  
13  import java.util.List;
14  
15  import nl.mineleni.cbsviewer.util.xml.LayerDescriptor;
16  
17  import org.junit.After;
18  import org.junit.Before;
19  import org.junit.Test;
20  
21  import flexjson.JSONDeserializer;
22  
23  /**
24   * Test case voor {@link AvailableLayersBean}.
25   * 
26   * @author prinsmc
27   */
28  public class AvailableLayersBeanTest {
29  	private static final String ID1 = "vierkanten500m_oad2000";
30  	private static final String LAYERS1 = "omgevings_adres_dichtheid_2000";
31  	private static final String STYLES1 = "cbsvierkanten500m.oad2000";
32  	private static final String NAME1 = "Vierkant 500m - Omgevingsadressendichtheid 2000";
33  	private static final String URL1 = "http://geodata.nationaalgeoregister.nl/cbsvierkanten500m/wms";
34  
35  	/** test subject. */
36  	private AvailableLayersBean bean;
37  
38  	/**
39  	 * set up before each test.
40  	 * 
41  	 * @throws Exception
42  	 *             the exception
43  	 */
44  	@Before
45  	public void beforeTest() throws Exception {
46  		this.bean = new AvailableLayersBean();
47  	}
48  
49  	/**
50  	 * after each test.
51  	 * 
52  	 */
53  	@After
54  	public void afterTest() {
55  		this.bean = null;
56  	}
57  
58  	/**
59  	 * testcase voor {@link AvailableLayersBean#asJSON()}.
60  	 */
61  	@Test
62  	public void testAsJSON() {
63  		assertTrue(this.bean.asJSON().startsWith(
64  				"\n/* <![CDATA[ */ var _layers="));
65  	}
66  
67  	/**
68  	 * testcase voor {@link AvailableLayersBean#asJSON(boolean) }.
69  	 */
70  	@Test
71  	public void testAsJSONBoolean() {
72  		assertTrue(this.bean.asJSON(true).startsWith(
73  				"\n/* <![CDATA[ */ var _layers="));
74  		assertFalse(this.bean.asJSON(false).startsWith(
75  				"\n/* <![CDATA[ */ var _layers="));
76  		assertTrue(this.bean.asJSON(false).startsWith("["));
77  		// round trip tests
78  		List<LayerDescriptor> layers = new JSONDeserializer<List<LayerDescriptor>>()
79  				.use("values", LayerDescriptor.class).deserialize(
80  						this.bean.asJSON(false));
81  		assertFalse(layers.isEmpty());
82  		assertEquals(NAME1, layers.get(0).getName());
83  	}
84  
85  	/**
86  	 * testcase voor {@link AvailableLayersBean#getLayerByID(String)}.
87  	 */
88  	@Test
89  	public void testGetLayerByID() {
90  		assertEquals(ID1, this.bean.getLayerByID(ID1).getId());
91  		assertEquals(NAME1, this.bean.getLayerByID(ID1).getName());
92  	}
93  
94  	/**
95  	 * testcase voor
96  	 * {@link AvailableLayersBean#getLayerByLayers(String, String) }.
97  	 * 
98  	 * 
99  	 */
100 	@Test
101 	public void testGetLayerByLayersStringString() {
102 		assertEquals(ID1, this.bean.getLayerByLayers(LAYERS1, URL1).getId());
103 	}
104 
105 	/**
106 	 * testcase voor
107 	 * {@link AvailableLayersBean#getLayerByLayers(String,String, String) }.
108 	 */
109 	@Test
110 	public void testGetLayerByLayersStringStringString() {
111 		assertEquals(ID1, this.bean.getLayerByLayers(LAYERS1, URL1, STYLES1)
112 				.getId());
113 	}
114 
115 	/**
116 	 * testcase voor
117 	 * {@link nl.mineleni.cbsviewer.util.AvailableLayersBean#getLayerByName(String)}
118 	 * .
119 	 */
120 	@Test
121 	public void testGetLayerByName() {
122 		assertEquals(ID1, this.bean.getLayerByName(NAME1).getId());
123 		assertEquals(NAME1, this.bean.getLayerByName(NAME1).getName());
124 	}
125 
126 	/**
127 	 * testcase voor
128 	 * {@link nl.mineleni.cbsviewer.util.AvailableLayersBean#getLayers()}.
129 	 */
130 	@Test
131 	public void testGetLayers() {
132 		assertEquals(2, this.bean.getLayers().size());
133 		// test ook de ordering van de onderliggende arraylist
134 		assertEquals(ID1, this.bean.getLayers().get(0).getId());
135 	}
136 
137 	/**
138 	 * testcase voor
139 	 * {@link nl.mineleni.cbsviewer.util.AvailableLayersBean#getLayers()}.
140 	 */
141 	@Test(expected = UnsupportedOperationException.class)
142 	public void testGetLayersImmutable() {
143 		this.bean.getLayers().add(new LayerDescriptor());
144 	}
145 }