View Javadoc
1   /*
2    * Copyright (C) 2022-2023 Dipl.-Inform. Kai Hofmann. All rights reserved!
3    */
4   package de.powerstat.validation.values.test;
5   
6   import static org.junit.jupiter.api.Assertions.assertAll;
7   import static org.junit.jupiter.api.Assertions.assertEquals;
8   import static org.junit.jupiter.api.Assertions.assertFalse;
9   import static org.junit.jupiter.api.Assertions.assertNotEquals;
10  import static org.junit.jupiter.api.Assertions.assertNotNull;
11  import static org.junit.jupiter.api.Assertions.assertThrows;
12  import static org.junit.jupiter.api.Assertions.assertTrue;
13  
14  import org.junit.jupiter.api.Test;
15  
16  import de.powerstat.validation.values.AddressWithWGS84Position;
17  import de.powerstat.validation.values.BFPONumber;
18  import de.powerstat.validation.values.Block;
19  import de.powerstat.validation.values.BuildingName;
20  import de.powerstat.validation.values.BuildingNr;
21  import de.powerstat.validation.values.City;
22  import de.powerstat.validation.values.Country;
23  import de.powerstat.validation.values.Department;
24  import de.powerstat.validation.values.District;
25  import de.powerstat.validation.values.Lines;
26  import de.powerstat.validation.values.Neighbourhood;
27  import de.powerstat.validation.values.PoBoxNumber;
28  import de.powerstat.validation.values.PostalCode;
29  import de.powerstat.validation.values.Province;
30  import de.powerstat.validation.values.Street;
31  import de.powerstat.validation.values.SubBuilding;
32  import de.powerstat.validation.values.WGS84Position;
33  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
34  
35  /**
36   * AddressWithWGS84Position tests.
37   */
38  @SuppressFBWarnings({"EC_NULL_ARG", "PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS"})
39  final class AddressWithWGS84PositionTests
40   {
41    /**
42     * Line break unix.
43     */
44    private static final String LINEBREAK_UNIX = "\n"; //$NON-NLS-1$
45  
46    /**
47     * Line break windows.
48     */
49    private static final String LINEBREAK_WIN = "\r\n"; //$NON-NLS-1$
50  
51    /**
52     * DE germany.
53     */
54    private static final String DE = "DE"; //$NON-NLS-1$
55  
56    /**
57     * Postal code.
58     */
59    private static final String POSTALCODE28307 = "28307"; //$NON-NLS-1$
60  
61    /**
62     * Postal code.
63     */
64    private static final String POSTALCODE28359 = "28359"; //$NON-NLS-1$
65  
66    /**
67     * City name.
68     */
69    private static final String BREMEN = "Bremen"; //$NON-NLS-1$
70  
71    /**
72     * Street name.
73     */
74    private static final String ARBERGER_HEERSTR = "Arberger Heerstr."; //$NON-NLS-1$
75  
76    /**
77     * Street name.
78     */
79    private static final String BIBLIOTHEKSTRASSE = "Bibliothekstraße"; //$NON-NLS-1$
80  
81    /**
82     * Building nr 92.
83     */
84    private static final String BUILDINGNR92 = "92"; //$NON-NLS-1$
85  
86    /**
87     * Building nr 1.
88     */
89    private static final String BUIDINGNR1 = "1"; //$NON-NLS-1$
90  
91    /**
92     * Test constant.
93     */
94    private static final String TEST = "Test"; //$NON-NLS-1$
95  
96    /**
97     * Constructor constant.
98     */
99    private static final String CONSTRUCTOR = "constructor"; //$NON-NLS-1$
100 
101   /**
102    * Address with wgs84 position is null constant.
103    */
104   private static final String ADDRESS_WITH_WGS84_POSITION_IS_NULL = "AddressWithWGS84Position is null"; //$NON-NLS-1$
105 
106   /**
107    * Wrong country constant.
108    */
109   private static final String WRONG_COUNTRY = "Wrong country"; //$NON-NLS-1$
110 
111   /**
112    * Wrong postal code constant.
113    */
114   private static final String WRONG_POSTAL_CODE = "Wrong postal code"; //$NON-NLS-1$
115 
116   /**
117    * Wrong city constant.
118    */
119   private static final String WRONG_CITY = "Wrong city"; //$NON-NLS-1$
120 
121   /**
122    * Illegal argument exception constant.
123    */
124   private static final String ILLEGAL_ARGUMENT_EXCEPTION = "Illegal argument exception"; //$NON-NLS-1$
125 
126 
127   /**
128    * Default constructor.
129    */
130   /* default */ AddressWithWGS84PositionTests()
131    {
132     super();
133    }
134 
135 
136   /**
137    * Constructor/factory test.
138    */
139   @Test
140   /* default */ void testConstructor1()
141    {
142     final AddressWithWGS84Position address = AddressWithWGS84Position.of(Country.of(AddressWithWGS84PositionTests.DE), PostalCode.of(AddressWithWGS84PositionTests.POSTALCODE28307), City.of(AddressWithWGS84PositionTests.BREMEN), null, null, Street.of(AddressWithWGS84PositionTests.ARBERGER_HEERSTR), BuildingNr.of(AddressWithWGS84PositionTests.BUILDINGNR92), null, null, null, null, null, null, null, null, WGS84Position.of(0.0, 0.0, 0.0));
143     assertAll(CONSTRUCTOR,
144       () -> assertNotNull(address, ADDRESS_WITH_WGS84_POSITION_IS_NULL)
145     );
146    }
147 
148 
149   /**
150    * Constructor/Factory test.
151    */
152   @Test
153   /* default */ void testConstructor2()
154    {
155     // Country, PostalCode, City, Province, District, Street, BuildingNr, BuildingName, SubBuilding, PoBoxNumber, Department, Neighbourhood, Block, BFPONumber, Lines, WGS84Position
156     final AddressWithWGS84Position address = AddressWithWGS84Position.of("DE,28307,Bremen,,,,,,,12345,,,,,,0.0 0.0 0.0");
157     assertAll(CONSTRUCTOR,
158       () -> assertNotNull(address, ADDRESS_WITH_WGS84_POSITION_IS_NULL),
159       () -> assertEquals(AddressWithWGS84PositionTests.DE, address.getCountry().stringValue(), WRONG_COUNTRY),
160       () -> assertEquals(AddressWithWGS84PositionTests.POSTALCODE28307, address.getPostalCode().stringValue(), WRONG_POSTAL_CODE),
161       () -> assertEquals(AddressWithWGS84PositionTests.BREMEN, address.getCity().stringValue(), WRONG_CITY),
162       () -> assertEquals(12345, address.getPoBoxNumber().longValue(), "Wrong pobox number")
163       // address.getPosition().getLatitude()
164       // address.getPosition().getLongitude()
165       // address.getPosition().getAltitude()
166     );
167    }
168 
169 
170   /**
171    * Constructor/factory test.
172    */
173   @Test
174   /* default */ void testConstructor3()
175    {
176     assertThrows(IllegalArgumentException.class, () ->
177      {
178       /* final AddressWithWGS84Position address = */ AddressWithWGS84Position.of("");
179      }, ILLEGAL_ARGUMENT_EXCEPTION
180     );
181    }
182 
183 
184   /**
185    * Constructor/factory test.
186    */
187   @Test
188   /* default */ void testConstructor4()
189    {
190     assertThrows(IllegalArgumentException.class, () ->
191      {
192       /* final AddressWithWGS84Position address = */ AddressWithWGS84Position.of("DE,28307,Bremen,,,,,,,12345,,,,,,0.0 0.0 0.0,1");
193      }, ILLEGAL_ARGUMENT_EXCEPTION
194     );
195    }
196 
197 
198   /**
199    * Constructor/factory test.
200    */
201   @Test
202   /* default */ void testConstructor5()
203    {
204     assertThrows(NullPointerException.class, () ->
205      {
206       /* final AddressWithWGS84Position address = */ AddressWithWGS84Position.of("DE,28307,Bremen,,,,,,,12345");
207      }, "Null pointer exception" //$NON-NLS-1$
208     );
209    }
210 
211 
212   /**
213    * Constructor/Factory test.
214    */
215   @Test
216   /* default */ void testConstructor6()
217    {
218     // Country, PostalCode, City, Province, District, Street, BuildingNr, BuildingName, SubBuilding, PoBoxNumber, Department, Neighbourhood, Block, BFPONumber, Lines, WGS84Position
219     final AddressWithWGS84Position address = AddressWithWGS84Position.of("DE,28307,Bremen,Bremen,9,,,Home,SubB,,Software Quality,Friendly,Block,1,Lines,0.0 0.0 0.0");
220     assertAll(CONSTRUCTOR,
221       () -> assertNotNull(address, ADDRESS_WITH_WGS84_POSITION_IS_NULL),
222       () -> assertEquals(AddressWithWGS84PositionTests.DE, address.getCountry().stringValue(), WRONG_COUNTRY),
223       () -> assertEquals(AddressWithWGS84PositionTests.POSTALCODE28307, address.getPostalCode().stringValue(), WRONG_POSTAL_CODE),
224       () -> assertEquals(AddressWithWGS84PositionTests.BREMEN, address.getCity().stringValue(), WRONG_CITY)
225       // Province
226       // District
227       // BuildingName
228       // SubBuilding
229       // Department
230       // Neighbourhood
231       // Block
232       // BFPONumber
233       // Lines
234     );
235    }
236 
237 
238   /**
239    * Get cposition test.
240    */
241   @Test
242   /* default */ void testGetPosition()
243    {
244     final WGS84Position pos = WGS84Position.of(0.0, 0.0, 0.0);
245     final AddressWithWGS84Position address = AddressWithWGS84Position.of(Country.of(AddressWithWGS84PositionTests.DE), PostalCode.of(AddressWithWGS84PositionTests.POSTALCODE28307), City.of(AddressWithWGS84PositionTests.BREMEN), null, null, Street.of(AddressWithWGS84PositionTests.ARBERGER_HEERSTR), BuildingNr.of(AddressWithWGS84PositionTests.BUILDINGNR92), null, null, null, null, null, null, null, null, pos);
246     final WGS84Position position = address.getPosition();
247     assertEquals(pos, position, "Wrong position"); //$NON-NLS-1$
248    }
249 
250 
251   /**
252    * Test string value.
253    */
254   @Test
255   /* default */ void testStringValue()
256    {
257     final WGS84Position pos = WGS84Position.of(0.0, 0.0, 0.0);
258     final AddressWithWGS84Position address = AddressWithWGS84Position.of(Country.of(AddressWithWGS84PositionTests.DE), PostalCode.of(AddressWithWGS84PositionTests.POSTALCODE28307), City.of(AddressWithWGS84PositionTests.BREMEN), null, null, null, null, null, null, PoBoxNumber.of(12345), null, null, null, null, null, pos);
259     assertEquals("\nPostfach 12345\n28307 Bremen\n\n0.0 0.0 0.0", address.stringValue().replace(AddressWithWGS84PositionTests.LINEBREAK_WIN, AddressWithWGS84PositionTests.LINEBREAK_UNIX).replace('\r', '\n'), "AddressWithWGS84Position not as expected");
260    }
261 
262 
263   /**
264    * Test hash code.
265    */
266   @Test
267   /* default */ void testHashCode()
268    {
269     final AddressWithWGS84Position address1 = AddressWithWGS84Position.of(Country.of(AddressWithWGS84PositionTests.DE), PostalCode.of(AddressWithWGS84PositionTests.POSTALCODE28307), City.of(AddressWithWGS84PositionTests.BREMEN), null, null, Street.of(AddressWithWGS84PositionTests.ARBERGER_HEERSTR), BuildingNr.of(AddressWithWGS84PositionTests.BUILDINGNR92), null, null, null, null, null, null, null, null, WGS84Position.of(0.0, 0.0, 0.0));
270     final AddressWithWGS84Position address2 = AddressWithWGS84Position.of(Country.of(AddressWithWGS84PositionTests.DE), PostalCode.of(AddressWithWGS84PositionTests.POSTALCODE28307), City.of(AddressWithWGS84PositionTests.BREMEN), null, null, Street.of(AddressWithWGS84PositionTests.ARBERGER_HEERSTR), BuildingNr.of(AddressWithWGS84PositionTests.BUILDINGNR92), null, null, null, null, null, null, null, null, WGS84Position.of(0.0, 0.0, 0.0));
271     final AddressWithWGS84Position address3 = AddressWithWGS84Position.of(Country.of(AddressWithWGS84PositionTests.DE), PostalCode.of(AddressWithWGS84PositionTests.POSTALCODE28359), City.of(AddressWithWGS84PositionTests.BREMEN), null, null, Street.of(AddressWithWGS84PositionTests.BIBLIOTHEKSTRASSE), BuildingNr.of(AddressWithWGS84PositionTests.BUIDINGNR1), null, null, null, null, null, null, null, null, WGS84Position.of(0.0, 0.0, 0.0));
272     assertAll("testHashCode", //$NON-NLS-1$
273       () -> assertEquals(address1.hashCode(), address2.hashCode(), "hashCodes are not equal"), //$NON-NLS-1$
274       () -> assertNotEquals(address1.hashCode(), address3.hashCode(), "hashCodes are equal") //$NON-NLS-1$
275     );
276    }
277 
278 
279   /**
280    * Test equals.
281    */
282   @Test
283   @SuppressWarnings("java:S5785")
284   /* default */ void testEquals()
285    {
286     final AddressWithWGS84Position address1 = AddressWithWGS84Position.of(Country.of(AddressWithWGS84PositionTests.DE), PostalCode.of(AddressWithWGS84PositionTests.POSTALCODE28307), City.of(AddressWithWGS84PositionTests.BREMEN), null, null, Street.of(AddressWithWGS84PositionTests.ARBERGER_HEERSTR), BuildingNr.of(AddressWithWGS84PositionTests.BUILDINGNR92), null, null, null, null, null, null, null, null, WGS84Position.of(0.0, 0.0, 0.0));
287     final AddressWithWGS84Position address2 = AddressWithWGS84Position.of(Country.of(AddressWithWGS84PositionTests.DE), PostalCode.of(AddressWithWGS84PositionTests.POSTALCODE28307), City.of(AddressWithWGS84PositionTests.BREMEN), null, null, Street.of(AddressWithWGS84PositionTests.ARBERGER_HEERSTR), BuildingNr.of(AddressWithWGS84PositionTests.BUILDINGNR92), null, null, null, null, null, null, null, null, WGS84Position.of(0.0, 0.0, 0.0));
288     final AddressWithWGS84Position address3 = AddressWithWGS84Position.of(Country.of(AddressWithWGS84PositionTests.DE), PostalCode.of(AddressWithWGS84PositionTests.POSTALCODE28359), City.of(AddressWithWGS84PositionTests.BREMEN), null, null, Street.of(AddressWithWGS84PositionTests.BIBLIOTHEKSTRASSE), BuildingNr.of(AddressWithWGS84PositionTests.BUIDINGNR1), null, null, null, null, null, null, null, null, WGS84Position.of(0.0, 0.0, 0.0));
289     final AddressWithWGS84Position address4 = AddressWithWGS84Position.of(Country.of(AddressWithWGS84PositionTests.DE), PostalCode.of(AddressWithWGS84PositionTests.POSTALCODE28307), City.of(AddressWithWGS84PositionTests.BREMEN), null, null, Street.of(AddressWithWGS84PositionTests.ARBERGER_HEERSTR), BuildingNr.of(AddressWithWGS84PositionTests.BUILDINGNR92), null, null, null, null, null, null, null, null, WGS84Position.of(0.0, 0.0, 0.0));
290     assertAll("testEquals", //$NON-NLS-1$
291       () -> assertTrue(address1.equals(address1), "address11 is not equal"), //$NON-NLS-1$
292       () -> assertTrue(address1.equals(address2), "address12 are not equal"), //$NON-NLS-1$
293       () -> assertTrue(address2.equals(address1), "address21 are not equal"), //$NON-NLS-1$
294       () -> assertTrue(address2.equals(address4), "address24 are not equal"), //$NON-NLS-1$
295       () -> assertTrue(address1.equals(address4), "address14 are not equal"), //$NON-NLS-1$
296       () -> assertFalse(address1.equals(address3), "address13 are equal"), //$NON-NLS-1$
297       () -> assertFalse(address3.equals(address1), "address31 are equal"), //$NON-NLS-1$
298       () -> assertFalse(address1.equals(null), "address10 is equal") //$NON-NLS-1$
299     );
300    }
301 
302 
303   /**
304    * Test not equals.
305    */
306   @Test
307   @SuppressWarnings("java:S5785")
308   /* default */ void testNotEquals()
309    {
310     final AddressWithWGS84Position address1 = AddressWithWGS84Position.of(Country.of(AddressWithWGS84PositionTests.DE), PostalCode.of(AddressWithWGS84PositionTests.POSTALCODE28307), City.of(AddressWithWGS84PositionTests.BREMEN), null, null, Street.of(AddressWithWGS84PositionTests.ARBERGER_HEERSTR), BuildingNr.of(AddressWithWGS84PositionTests.BUILDINGNR92), null, null, null, null, null, null, null, null, WGS84Position.of(0.0, 0.0, 0.0));
311     final AddressWithWGS84Position address2 = AddressWithWGS84Position.of(Country.of(AddressWithWGS84PositionTests.DE), PostalCode.of(AddressWithWGS84PositionTests.POSTALCODE28307), City.of(AddressWithWGS84PositionTests.BREMEN), null, null, Street.of(AddressWithWGS84PositionTests.ARBERGER_HEERSTR), BuildingNr.of(AddressWithWGS84PositionTests.BUILDINGNR92), null, null, null, null, null, null, null, null, WGS84Position.of(1.0, 1.0, 0.0));
312     assertAll("testNotEquals", //$NON-NLS-1$
313       () -> assertFalse(address1.equals(new Object()), "address1o is equal"), //$NON-NLS-1$
314       () -> assertFalse(address1.equals(address2), "address12 is equal") //$NON-NLS-1$
315     );
316    }
317 
318 
319   /**
320    * Test toString.
321    */
322   @Test
323   /* default */ void testToString()
324    {
325     final AddressWithWGS84Position address = AddressWithWGS84Position.of(Country.of(AddressWithWGS84PositionTests.DE), PostalCode.of(AddressWithWGS84PositionTests.POSTALCODE28307), City.of(AddressWithWGS84PositionTests.BREMEN), Province.of(AddressWithWGS84PositionTests.TEST), District.of("9"), Street.of(AddressWithWGS84PositionTests.ARBERGER_HEERSTR), BuildingNr.of(AddressWithWGS84PositionTests.BUILDINGNR92), BuildingName.of("TestBuilding"), SubBuilding.of("SubB"), PoBoxNumber.of(4711), Department.of(AddressWithWGS84PositionTests.TEST), Neighbourhood.of("neighbour"), Block.of("blk"), BFPONumber.of(1), Lines.of(AddressWithWGS84PositionTests.TEST), WGS84Position.of(0, 0, 0)); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
326     assertEquals("AddressWithWGS84Position[position=WGS84Position[latitude=0.0, longitude=0.0, altitude=0.0], Address[country=DE, postalCode=28307, city=Bremen, province=Test, district=9, street=Arberger Heerstr., buildingNr=92, buildingName=TestBuilding, subBuilding=SubB, poBoxNumber=4711, department=Test, neighbourhood=neighbour, block=blk, bFPONumber=1, lines=Test]]", address.toString(), "toString not equal"); //$NON-NLS-1$ //$NON-NLS-2$
327    }
328 
329  }