View Javadoc
1   /*
2    * Copyright (C) 2019-2023 Dipl.-Inform. Kai Hofmann. All rights reserved!
3    */
4   package de.powerstat.validation.test;
5   
6   
7   import static org.junit.jupiter.api.Assertions.assertAll;
8   import static org.junit.jupiter.api.Assertions.assertEquals;
9   import static org.junit.jupiter.api.Assertions.assertThrows;
10  
11  import java.util.List;
12  
13  import org.junit.jupiter.api.Test;
14  
15  import de.powerstat.validation.ValidationUtils;
16  
17  
18  /**
19   * Validation utility tests.
20   */
21  final class ValidationUtilsTests
22   {
23    /* *
24     * Logger.
25     */
26    // private static final Logger LOGGER = LogManager.getLogger(ValidationUtilsTests.class);
27  
28    /**
29     * Illegal argument exception expected constant.
30     */
31    private static final String ILLEGAL_ARGUMENT_EXCEPTION = "Illegal argument exception expected"; //$NON-NLS-1$
32  
33    /**
34     * Url path not as expected constant.
35     */
36    private static final String URL_PATH_NOT_AS_EXPECTED = "Url path not as expected!"; //$NON-NLS-1$
37  
38    /**
39     * Wrong hostname constant.
40     */
41    private static final String WRONG_HOSTNAME = "Wrong hostname!"; //$NON-NLS-1$
42  
43    /**
44     * Wrong port constant.
45     */
46    private static final String WRONG_PORT = "Wrong port!"; //$NON-NLS-1$
47  
48    /**
49     * ValidationUtils path constant.
50     */
51    private static final String VALIDATION_UTILS = "/ValidationUtils"; //$NON-NLS-1$
52  
53    /**
54     * HTTP port.
55     */
56    private static final String HTTP = "80"; //$NON-NLS-1$
57  
58    /**
59     * Hostname.
60     */
61    private static final String WWW_POWERSTAT_DE = "www.powerstat.de"; //$NON-NLS-1$
62  
63    /**
64     * FE80.
65     */
66    private static final String FE80 = "fe:80::"; //$NON-NLS-1$
67  
68    /**
69     * Deprecated since version 3.0 constant.
70     */
71    private static final String DEPRECATED_SINCE_2_0 = "2.0"; //$NON-NLS-1$
72  
73  
74    /**
75     * Default constructor.
76     */
77    /* default */ ValidationUtilsTests()
78     {
79      super();
80     }
81  
82  
83    /**
84     * Test sanitizeUrlPath with valid url.
85     *
86     * @deprecated Use de.powerstat.validation.values.test.* instead.
87     */
88    @Deprecated(since = ValidationUtilsTests.DEPRECATED_SINCE_2_0, forRemoval = true)
89    @Test
90    /* default */ void testSanitizeUrlPathO()
91     {
92      final String url = ValidationUtilsTests.VALIDATION_UTILS;
93      final String resultUrl = ValidationUtils.sanitizeUrlPath(url);
94      assertEquals(url, resultUrl, ValidationUtilsTests.URL_PATH_NOT_AS_EXPECTED);
95     }
96  
97  
98    /**
99     * Test sanitizeUrlPath with valid url.
100    *
101    * @deprecated Use de.powerstat.validation.values.test.* instead.
102    */
103   @Deprecated(since = ValidationUtilsTests.DEPRECATED_SINCE_2_0, forRemoval = true)
104   @Test
105   /* default */ void testSanitizeUrlPathEmpty()
106    {
107     final String url = ""; //$NON-NLS-1$
108     final String resultUrl = ValidationUtils.sanitizeUrlPath(url);
109     assertEquals("/", resultUrl, ValidationUtilsTests.URL_PATH_NOT_AS_EXPECTED); //$NON-NLS-1$
110    }
111 
112 
113   /**
114    * Test sanitizeUrlPath with invalid url.
115    *
116    * @deprecated Use de.powerstat.validation.values.test.* instead.
117    */
118   @Deprecated(since = ValidationUtilsTests.DEPRECATED_SINCE_2_0, forRemoval = true)
119   @Test
120   /* default */ void testSanitizeUrlPathInvalid()
121    {
122     final String resultUrl = ValidationUtils.sanitizeUrlPath("ValidationUtils"); //$NON-NLS-1$
123     assertEquals(ValidationUtilsTests.VALIDATION_UTILS, resultUrl, ValidationUtilsTests.URL_PATH_NOT_AS_EXPECTED);
124    }
125 
126 
127   /**
128    * Split hostname port.
129    *
130    * @deprecated Use de.powerstat.validation.values.test.* instead.
131    */
132   @Deprecated(since = ValidationUtilsTests.DEPRECATED_SINCE_2_0, forRemoval = true)
133   @Test
134   /* default */ void testSplitHostnamePortOk1()
135    {
136     final List<String> hostnamePort = ValidationUtils.splitHostnamePort("www.powerstat.de:80"); //$NON-NLS-1$
137     assertAll(
138       () -> assertEquals(ValidationUtilsTests.WWW_POWERSTAT_DE, hostnamePort.get(0), ValidationUtilsTests.WRONG_HOSTNAME),
139       () -> assertEquals(ValidationUtilsTests.HTTP, hostnamePort.get(1), ValidationUtilsTests.WRONG_PORT)
140     );
141    }
142 
143 
144   /**
145    * Split hostname port.
146    *
147    * @deprecated Use de.powerstat.validation.values.test.* instead.
148    */
149   @Deprecated(since = ValidationUtilsTests.DEPRECATED_SINCE_2_0, forRemoval = true)
150   @Test
151   /* default */ void testSplitHostnamePortOk2()
152    {
153     final List<String> hostnamePort = ValidationUtils.splitHostnamePort("[fe:80::]:80"); //$NON-NLS-1$
154     assertAll(
155       () -> assertEquals(ValidationUtilsTests.FE80, hostnamePort.get(0), ValidationUtilsTests.WRONG_HOSTNAME),
156       () -> assertEquals(ValidationUtilsTests.HTTP, hostnamePort.get(1), ValidationUtilsTests.WRONG_PORT)
157     );
158    }
159 
160 
161   /**
162    * Split hostname port with error.
163    *
164    * @deprecated Use de.powerstat.validation.values.test.* instead.
165    */
166   @Deprecated(since = ValidationUtilsTests.DEPRECATED_SINCE_2_0, forRemoval = true)
167   @Test
168   /* default */ void testSplitHostnamePortWrong()
169    {
170     assertThrows(IllegalArgumentException.class, () ->
171      {
172       /* final List<String> hostnamePort = */ ValidationUtils.splitHostnamePort(ValidationUtilsTests.WWW_POWERSTAT_DE);
173      }, ValidationUtilsTests.ILLEGAL_ARGUMENT_EXCEPTION
174     );
175    }
176 
177  }