|
74 | 74 | import static org.junit.jupiter.api.Assertions.assertFalse; |
75 | 75 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
76 | 76 | import static org.junit.jupiter.api.Assertions.assertNull; |
| 77 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
77 | 78 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| 79 | +import static org.mockito.ArgumentMatchers.any; |
78 | 80 | import static org.mockito.Mockito.mock; |
79 | 81 | import static org.mockito.Mockito.when; |
80 | 82 |
|
| 83 | +import java.io.IOException; |
| 84 | +import java.util.ArrayList; |
81 | 85 | import java.util.HashMap; |
82 | 86 | import java.util.Map; |
83 | 87 |
|
84 | 88 | import javax.servlet.ServletException; |
85 | 89 | import javax.servlet.http.HttpServletRequest; |
| 90 | +import javax.servlet.http.Part; |
86 | 91 |
|
| 92 | +import org.cip4.jdflib.util.ContainerUtil; |
87 | 93 | import org.junit.jupiter.api.Test; |
88 | 94 |
|
89 | 95 | class FileItemListTest extends JDFUtilityTestBase |
90 | 96 | { |
91 | 97 |
|
92 | 98 | @Test |
93 | | - void testString() throws ServletException |
| 99 | + void testString() throws Exception |
94 | 100 | { |
95 | 101 | assertNotNull(new FileItemList(getRequestMock(), 42).toString()); |
96 | 102 | } |
97 | 103 |
|
98 | 104 | @Test |
99 | | - void testGetBool() throws ServletException |
| 105 | + void testGetBool() throws Exception |
100 | 106 | { |
101 | 107 | assertFalse(new FileItemList(getRequestMock(), 42).getBoolField("a", false)); |
102 | 108 | assertTrue(new FileItemList(getRequestMock(), 42).getBoolField("a1", false)); |
103 | 109 | assertTrue(new FileItemList(getRequestMock(), 42).getBoolField("A1", false)); |
104 | 110 | } |
105 | 111 |
|
106 | 112 | @Test |
107 | | - void testGetInt() throws ServletException |
| 113 | + void testGetInt() throws Exception |
108 | 114 | { |
109 | 115 | assertEquals(0, new FileItemList(getRequestMock(), 42).getIntField("a", 0)); |
110 | 116 | assertEquals(1, new FileItemList(getRequestMock(), 42).getIntField("a2", 0)); |
111 | 117 | assertEquals(1, new FileItemList(getRequestMock(), 42).getIntField("A2", 0)); |
112 | 118 | } |
113 | 119 |
|
114 | 120 | @Test |
115 | | - void testGetField() throws ServletException |
| 121 | + void testGetField() throws Exception |
116 | 122 | { |
117 | 123 | assertEquals(null, new FileItemList(getRequestMock(), 42).getField("a")); |
118 | 124 | assertEquals("abc", new FileItemList(getRequestMock(), 42).getField("a3")); |
119 | 125 | assertEquals("abc", new FileItemList(getRequestMock(), 42).getField("A3")); |
120 | 126 | } |
121 | 127 |
|
122 | 128 | @Test |
123 | | - void testGetFile() throws ServletException |
| 129 | + void testGetFile() throws Exception |
124 | 130 | { |
125 | 131 | assertNull(new FileItemList(getRequestMock(), 42).getFile(0)); |
126 | 132 | assertNull(new FileItemList(getRequestMock(), 42).getFile("foo")); |
127 | 133 | } |
128 | 134 |
|
129 | 135 | @Test |
130 | | - void testGetFileStream() throws ServletException |
| 136 | + void testGetFileStream() throws Exception |
131 | 137 | { |
132 | 138 | assertNull(new FileItemList(getRequestMock(), 42).getFileInputStream(0)); |
133 | 139 | assertNull(new FileItemList(getRequestMock(), 42).getFileInputStream("foo")); |
134 | 140 | } |
135 | 141 |
|
136 | | - HttpServletRequest getRequestMock() |
| 142 | + @Test |
| 143 | + void testGetMem() throws Exception |
| 144 | + { |
| 145 | + assertNull(FileItemList.getMemoryFileItemList(getRequestMock(), 999).getFileInputStream(0)); |
| 146 | + assertNull(FileItemList.getFileItemList(getRequestMock(), 999).getFileInputStream(0)); |
| 147 | + } |
| 148 | + |
| 149 | + HttpServletRequest getRequestMock() throws IOException, ServletException |
137 | 150 | { |
138 | 151 | final HttpServletRequest mock = mock(HttpServletRequest.class); |
139 | 152 | final Map<String, String[]> pm = new HashMap<String, String[]>(); |
140 | 153 | pm.put("A1", new String[] { "true" }); |
141 | 154 | pm.put("A2", new String[] { "1" }); |
142 | 155 | pm.put("A3", new String[] { "abc" }); |
143 | 156 | when(mock.getParameterMap()).thenReturn(pm); |
| 157 | + |
| 158 | + final Part part1 = mock(Part.class); |
| 159 | + when(mock.getPart(any())).thenReturn(part1); |
| 160 | + when(mock.getParts()).thenReturn(ContainerUtil.add(new ArrayList<Part>(), part1)); |
144 | 161 | return mock; |
145 | 162 | } |
146 | 163 |
|
| 164 | + @Test |
| 165 | + void testGetFactory() throws Exception |
| 166 | + { |
| 167 | + final FileItemList l = new FileItemList(getRequestMock(), 42); |
| 168 | + assertThrows(IllegalArgumentException.class, () -> l.getFactory(true, 0)); |
| 169 | + l.getFactory(false, 0); |
| 170 | + l.getFactory(false, 99999); |
| 171 | + l.getFactory(true, 99999); |
| 172 | + } |
| 173 | + |
147 | 174 | } |
0 commit comments