SearchConditionDo implements Serializable { public void setNewSortField(String field) { if (sortList.isEmpty()) { SortDo sort = new SortDo(field, true); sortList.add(sort); } else { SortDo first = sortList.get(0); if (first.getField().equals(field)) { first.setAsc(!first.isAsc()); } else { SortDo same = null; for (SortDo history : sortList) { if (history.getField().equals(field)) { same = history; } } sortList.remove(same); if (sortList.size() >= getSortHistory()) { sortList.remove(sortList.size() - 1); } SortDo newFirst = new SortDo(field, true); sortList.add(0, newFirst); } } }  void init(); void buildQuery(String entityName); void setNewSortField(String field); Object addParam(String key, Object value); PaginationDo getPagination(); void setPagination(PaginationDo pagination); Map<String, Object> getParams(); void setParams(Map<String, Object> params); int getSortHistory(); void setSortHistory(int sortHistory); String getCountQuery(); void setCountQuery(String countQuery); String getSearchQuery(); void setSearchQuery(String searchQuery); Map<String, Object> getQueryParams(); void setQueryParams(Map<String, Object> queryParams);  }
@Test public void testOrderBy() { SearchConditionDo condition = new SearchConditionDo(); assertThat("初期状態", condition.buildOrderBy("entity"), is("")); condition.setNewSortField("field1"); assertThat("フィールド1追加", condition.buildOrderBy("entity"), is(" ORDER BY entity.field1 ASC")); condition.setNewSortField("field1"); assertThat("フィールド1逆転", condition.buildOrderBy("entity"), is(" ORDER BY entity.field1 DESC")); condition.setNewSortField("field2"); assertThat("フィールド2追加", condition.buildOrderBy("entity"), is(" ORDER BY entity.field2 ASC, entity.field1 DESC")); condition.setNewSortField("field2"); assertThat("フィールド2逆転", condition.buildOrderBy("entity"), is(" ORDER BY entity.field2 DESC, entity.field1 DESC")); condition.setNewSortField("field1"); assertThat("フィールド1追加", condition.buildOrderBy("entity"), is(" ORDER BY entity.field1 ASC, entity.field2 DESC")); condition.setNewSortField("field3"); condition.setNewSortField("field4"); assertThat("フィールド1追加", condition.buildOrderBy("entity"), is(" ORDER BY entity.field4 ASC, entity.field3 ASC, entity.field1 ASC")); }