mirror of
https://github.com/apache/struts.git
synced 2026-08-07 07:37:20 +00:00
WW-5336 Reduce cognitive complexity #makeSelectList
This commit is contained in:
@@ -174,42 +174,44 @@ public class StrutsUtil {
|
||||
return selectList;
|
||||
}
|
||||
|
||||
Collection selectedItems = null;
|
||||
|
||||
Object i = stack.findValue(selectedList);
|
||||
|
||||
if (i != null) {
|
||||
if (i.getClass().isArray()) {
|
||||
selectedItems = Arrays.asList((Object[]) i);
|
||||
} else if (i instanceof Collection) {
|
||||
selectedItems = (Collection) i;
|
||||
} else {
|
||||
selectedItems = singletonList(i);
|
||||
}
|
||||
}
|
||||
|
||||
Collection selectedItems = getSelectedItems(selectedList);
|
||||
for (Object element : items) {
|
||||
Object key;
|
||||
if (listKey == null || listKey.isEmpty()) {
|
||||
key = element;
|
||||
} else {
|
||||
key = findValue(listKey, element);
|
||||
}
|
||||
|
||||
Object value;
|
||||
if (listValue == null || listValue.isEmpty()) {
|
||||
value = element;
|
||||
} else {
|
||||
value = findValue(listValue, element);
|
||||
}
|
||||
boolean isSelected = value != null && selectedItems != null && selectedItems.contains(value);
|
||||
|
||||
Object key = computeKey(listKey, element);
|
||||
Object value = computeValue(listValue, element);
|
||||
boolean isSelected = value != null && selectedItems.contains(value);
|
||||
selectList.add(new ListEntry(key, value, isSelected));
|
||||
}
|
||||
|
||||
return selectList;
|
||||
}
|
||||
|
||||
private Collection getSelectedItems(String selectedListName) {
|
||||
Object i = stack.findValue(selectedListName);
|
||||
if (i == null) {
|
||||
return emptyList();
|
||||
}
|
||||
if (i.getClass().isArray()) {
|
||||
return Arrays.asList((Object[]) i);
|
||||
} else if (i instanceof Collection) {
|
||||
return (Collection) i;
|
||||
}
|
||||
return singletonList(i);
|
||||
}
|
||||
|
||||
private Object computeKey(String listKey, Object element) {
|
||||
if (listKey == null || listKey.isEmpty()) {
|
||||
return element;
|
||||
}
|
||||
return findValue(listKey, element);
|
||||
}
|
||||
|
||||
private Object computeValue(String listValue, Object element) {
|
||||
if (listValue == null || listValue.isEmpty()) {
|
||||
return element;
|
||||
}
|
||||
return findValue(listValue, element);
|
||||
}
|
||||
|
||||
public int toInt(long aLong) {
|
||||
return (int) aLong;
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ import org.springframework.mock.web.MockRequestDispatcher;
|
||||
import javax.servlet.RequestDispatcher;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -147,12 +147,9 @@ public class StrutsUtilTest extends StrutsInternalTestCase {
|
||||
}
|
||||
|
||||
|
||||
public void testMakeSelectListMethod() {
|
||||
public void testMakeSelectList() {
|
||||
String[] selectedList = new String[]{"Car", "Airplane", "Bus"};
|
||||
List<String> list = new ArrayList<>();
|
||||
list.add("Lorry");
|
||||
list.add("Car");
|
||||
list.add("Helicopter");
|
||||
List<String> list = Arrays.asList("Lorry", "Car", "Helicopter");
|
||||
|
||||
stack.getContext().put("mySelectedList", selectedList);
|
||||
stack.getContext().put("myList", list);
|
||||
@@ -171,6 +168,32 @@ public class StrutsUtilTest extends StrutsInternalTestCase {
|
||||
assertFalse(listMade.get(2).getIsSelected());
|
||||
}
|
||||
|
||||
public void testMakeSelectListCollection() {
|
||||
List<String> selectedList = Arrays.asList("Car", "Airplane", "Bus");
|
||||
List<String> list = Arrays.asList("Lorry", "Car", "Helicopter");
|
||||
|
||||
stack.getContext().put("mySelectedList", selectedList);
|
||||
stack.getContext().put("myList", list);
|
||||
|
||||
List<ListEntry> listMade = strutsUtil.makeSelectList("#mySelectedList", "#myList", null, null);
|
||||
|
||||
assertEquals(listMade.size(), 3);
|
||||
assertEquals(listMade.get(0).getKey(), "Lorry");
|
||||
assertEquals(listMade.get(0).getValue(), "Lorry");
|
||||
assertFalse(listMade.get(0).getIsSelected());
|
||||
assertEquals(listMade.get(1).getKey(), "Car");
|
||||
assertEquals(listMade.get(1).getValue(), "Car");
|
||||
assertTrue(listMade.get(1).getIsSelected());
|
||||
assertEquals(listMade.get(2).getKey(), "Helicopter");
|
||||
assertEquals(listMade.get(2).getValue(), "Helicopter");
|
||||
assertFalse(listMade.get(2).getIsSelected());
|
||||
}
|
||||
|
||||
public void testMakeSelectListNonExistent() {
|
||||
List<ListEntry> listMade = strutsUtil.makeSelectList("#mySelectedList", "#nonexistent", null, null);
|
||||
assertThat(listMade).isEmpty();
|
||||
}
|
||||
|
||||
public void testToInt() {
|
||||
assertEquals(strutsUtil.toInt(11L), 11);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user