|
1 | 1 | package com.ly.doc.util; |
2 | 2 |
|
3 | 3 | import com.ly.doc.constants.DocGlobalConstants; |
| 4 | +import com.ly.doc.constants.DocLanguage; |
| 5 | +import com.ly.doc.constants.DocTags; |
4 | 6 | import com.ly.doc.enums.IEnum; |
5 | 7 | import com.ly.doc.enums.OrderEnum; |
6 | 8 | import com.ly.doc.utils.DocUtil; |
7 | | -import com.ly.doc.constants.DocLanguage; |
| 9 | +import com.thoughtworks.qdox.model.DocletTag; |
8 | 10 | import org.junit.jupiter.api.Test; |
| 11 | +import org.mockito.Mockito; |
9 | 12 |
|
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.Collections; |
10 | 15 | import java.util.HashMap; |
11 | 16 | import java.util.List; |
12 | 17 | import java.util.Map; |
13 | 18 |
|
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 21 | +import static org.mockito.Mockito.when; |
| 22 | + |
14 | 23 | /** |
15 | 24 | * @author yu 2018/12/10. |
16 | 25 | */ |
17 | 26 | public class DocUtilTest { |
18 | 27 |
|
| 28 | + /** |
| 29 | + * Assuming DocTags.PARAM is "param" |
| 30 | + */ |
| 31 | + private static final String TAG_NAME_PARAM = DocTags.PARAM; |
| 32 | + |
| 33 | + /** |
| 34 | + * Assuming DocTags.EXTENSION is "extension" |
| 35 | + */ |
| 36 | + private static final String TAG_NAME_EXTENSION = DocTags.EXTENSION; |
| 37 | + |
| 38 | + /** |
| 39 | + * Assuming DocGlobalConstants.NO_COMMENTS_FOUND |
| 40 | + */ |
| 41 | + private static final String NO_COMMENTS_FOUND = DocGlobalConstants.NO_COMMENTS_FOUND; |
| 42 | + |
19 | 43 | @Test |
20 | 44 | public void test() { |
21 | 45 | System.setProperty(DocGlobalConstants.DOC_LANGUAGE, DocLanguage.CHINESE.getCode()); |
@@ -80,4 +104,127 @@ public void testReplaceGenericParameter() { |
80 | 104 | System.out.println(result); // Output: com.Test<List<Use |
81 | 105 | } |
82 | 106 |
|
| 107 | + // Helper method to create a mock DocletTag |
| 108 | + private DocletTag createMockTag(String value) { |
| 109 | + DocletTag mockTag = Mockito.mock(DocletTag.class); |
| 110 | + when(mockTag.getValue()).thenReturn(value); |
| 111 | + return mockTag; |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void testGetCommentsByTag_ParamWithNewlines() { |
| 116 | + // Scenario 1: Normal case - param name and description on same line |
| 117 | + DocletTag tag1 = createMockTag("id user identifier"); |
| 118 | + // Scenario 2: Description starts on a new line with indentation |
| 119 | + DocletTag tag2 = createMockTag("name\n user's full name"); |
| 120 | + // Scenario 3: Description has multiple newlines and complex formatting |
| 121 | + DocletTag tag3 = createMockTag( |
| 122 | + "age user's age in years\n Default is 25.\n NOTE: Must be > 0."); |
| 123 | + // Scenario 4: Only parameter name, no description |
| 124 | + DocletTag tag4 = createMockTag("flag"); |
| 125 | + // Scenario 5: Parameter name with leading/trailing whitespace in the raw value |
| 126 | + // part that's not the name itself |
| 127 | + DocletTag tag5 = createMockTag(" email user's email address "); |
| 128 | + // Scenario 6: Description starts immediately after parameter name WITH A SPACE |
| 129 | + // (Standard Javadoc format) |
| 130 | + DocletTag tag6 = createMockTag("status Active user status flag"); |
| 131 | + // Scenario 7: Description contains leading whitespace after the parameter name |
| 132 | + DocletTag tag7 = createMockTag("count Number of items to retrieve"); |
| 133 | + |
| 134 | + List<DocletTag> tags = Arrays.asList(tag1, tag2, tag3, tag4, tag5, tag6, tag7); |
| 135 | + |
| 136 | + Map<String, String> result = DocUtil.getCommentsByTag(tags, TAG_NAME_PARAM); |
| 137 | + |
| 138 | + // Verify results |
| 139 | + assertEquals("user identifier", result.get("id"), "Scenario 1: Description should be 'user identifier'"); |
| 140 | + assertEquals("user's full name", result.get("name"), |
| 141 | + "Scenario 2: Description should be 'user's full name', preserving content after newline."); |
| 142 | + assertEquals("user's age in years\n Default is 25.\n NOTE: Must be > 0.", |
| 143 | + result.get("age"), "Scenario 3: Description should preserve newlines and formatting."); |
| 144 | + assertEquals(NO_COMMENTS_FOUND, result.get("flag"), "Scenario 4: Should return NO_COMMENTS_FOUND for 'flag'"); |
| 145 | + assertEquals("user's email address", result.get("email"), |
| 146 | + "Scenario 5: Name should be 'email', description should be 'user's email address'"); |
| 147 | + assertEquals("Active user status flag", result.get("status"), |
| 148 | + "Scenario 6: Description should be 'Active user status flag'"); |
| 149 | + assertEquals("Number of items to retrieve", result.get("count"), |
| 150 | + "Scenario 7: Description should be 'Number of items to retrieve'"); |
| 151 | + |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + public void testGetCommentsByTag_ExtensionWithNewlines() { |
| 156 | + // Similar scenarios for @extension |
| 157 | + DocletTag tag1 = createMockTag("extId extension identifier"); |
| 158 | + DocletTag tag2 = createMockTag("extName\n extension name"); |
| 159 | + DocletTag tag3 = createMockTag("extConfig extension configuration details\n with multiple lines."); |
| 160 | + DocletTag tag4 = createMockTag("extFlag"); |
| 161 | + |
| 162 | + List<DocletTag> tags = Arrays.asList(tag1, tag2, tag3, tag4); |
| 163 | + |
| 164 | + Map<String, String> result = DocUtil.getCommentsByTag(tags, TAG_NAME_EXTENSION); |
| 165 | + |
| 166 | + // Verify results |
| 167 | + assertEquals("extension identifier", result.get("extId")); |
| 168 | + assertEquals("extension name", result.get("extName")); |
| 169 | + assertEquals("extension configuration details\n with multiple lines.", result.get("extConfig")); |
| 170 | + assertEquals(NO_COMMENTS_FOUND, result.get("extFlag")); |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + public void testGetCommentsByTag_ParamWithCarriageReturn() { |
| 175 | + // Scenario: Description has Windows-style CRLF (\r\n) |
| 176 | + String valueWithCRLF = "token\r\n Authentication token,\r\n required for access."; |
| 177 | + DocletTag tag = createMockTag(valueWithCRLF); |
| 178 | + |
| 179 | + List<DocletTag> tags = Collections.singletonList(tag); |
| 180 | + Map<String, String> result = DocUtil.getCommentsByTag(tags, TAG_NAME_PARAM); |
| 181 | + |
| 182 | + assertEquals("Authentication token,\r\n required for access.", result.get("token"), |
| 183 | + "Should handle CRLF correctly, preserving the newline and content."); |
| 184 | + } |
| 185 | + |
| 186 | + @Test |
| 187 | + public void testGetCommentsByTag_ParamOnlyName() { |
| 188 | + // Scenario: Only parameter name exists, no space/description |
| 189 | + DocletTag tag = createMockTag("justParamName"); |
| 190 | + List<DocletTag> tags = Collections.singletonList(tag); |
| 191 | + Map<String, String> result = DocUtil.getCommentsByTag(tags, TAG_NAME_PARAM); |
| 192 | + |
| 193 | + assertEquals(NO_COMMENTS_FOUND, result.get("justParamName"), |
| 194 | + "Should return NO_COMMENTS_FOUND for 'justParamName'"); |
| 195 | + } |
| 196 | + |
| 197 | + @Test |
| 198 | + public void testGetCommentsByTag_ParamEmptyValue() { |
| 199 | + // Scenario: Empty value string |
| 200 | + DocletTag tag = createMockTag(""); |
| 201 | + List<DocletTag> tags = Collections.singletonList(tag); |
| 202 | + Map<String, String> result = DocUtil.getCommentsByTag(tags, TAG_NAME_PARAM); |
| 203 | + |
| 204 | + // An empty string after trim results in [""], so parts[0] is "", and parts.length |
| 205 | + // is 1. |
| 206 | + // The logic assigns pName = parts[0] (which is ""), and pValue remains the |
| 207 | + // default NO_COMMENTS_FOUND. |
| 208 | + // The map.put(pName.trim(), pValue) effectively does map.put("", |
| 209 | + // NO_COMMENTS_FOUND). |
| 210 | + assertEquals(NO_COMMENTS_FOUND, result.get(""), // 修正 NO_COMMENTS_FOUND 的值 |
| 211 | + "Should handle empty value, key should be empty string."); |
| 212 | + assertTrue(result.containsKey(""), "Map should contain an entry with an empty string key."); |
| 213 | + } |
| 214 | + |
| 215 | + @Test |
| 216 | + public void testGetCommentsByTag_ParamWhitespaceOnlyValue() { |
| 217 | + // Scenario: Value is only whitespace |
| 218 | + DocletTag tag = createMockTag(" \t \n "); |
| 219 | + List<DocletTag> tags = Collections.singletonList(tag); |
| 220 | + Map<String, String> result = DocUtil.getCommentsByTag(tags, TAG_NAME_PARAM); |
| 221 | + |
| 222 | + // Whitespace-only string after trim becomes "", same as empty value scenario. |
| 223 | + // After trim and split, pName should be "" and pValue should be |
| 224 | + // NO_COMMENTS_FOUND. |
| 225 | + assertEquals(NO_COMMENTS_FOUND, result.get(""), |
| 226 | + "Should handle whitespace-only value, key should be empty string."); |
| 227 | + assertTrue(result.containsKey(""), "Map should contain an entry with an empty string key."); |
| 228 | + } |
| 229 | + |
83 | 230 | } |
0 commit comments