11package org .javawebstack .orm .test .querybuilding ;
22
33import org .javawebstack .orm .query .Query ;
4+ import org .javawebstack .orm .test .exception .SectionIndexOutOfBoundException ;
45import org .javawebstack .orm .test .shared .models .Datatype ;
56import org .javawebstack .orm .test .shared .verification .QueryVerification ;
67import org .junit .jupiter .api .Test ;
78
9+ import javax .xml .crypto .Data ;
10+
11+ import java .util .*;
12+ import java .util .stream .Collectors ;
13+
814import static org .javawebstack .orm .test .shared .setup .ModelSetup .setUpModel ;
15+ import static org .junit .jupiter .api .Assertions .assertTrue ;
16+ import static org .junit .jupiter .api .Assertions .fail ;
917
1018public class OrderByClauseTest {
1119
@@ -84,6 +92,43 @@ void testMultipleOrderByClausesOfMixedOrder() {
8492 .assertSectionContains ("ORDER BY" , "`primitive_integer` DESC" );
8593 }
8694
95+
96+ @ Test
97+ // This test is important because putting the order by statements in different order is relevant (they set priorities)
98+ void testMultipleOrderByClausesOfRandomOrderForCorrectOrder () throws SectionIndexOutOfBoundException {
99+ Query <Datatype > query = setUpModel (Datatype .class ).query ();
100+ ArrayList <String > columnNames = new ArrayList <>(Datatype .columnNames );
101+
102+ LinkedList <String > callOrder = new LinkedList <>();
103+
104+ Random r = new Random ();
105+ columnNames .stream ().unordered ().forEach ((singleColumn ) -> {
106+ query .order (singleColumn , r .nextBoolean ());
107+ callOrder .add (singleColumn );
108+ });
109+
110+ String queryString = new QueryVerification (query ).getSection ("ORDER BY" );
111+ int lastIndex = 0 ;
112+ int foundIndex = -1 ;
113+ for (String nextInCallOrder : callOrder ) {
114+ foundIndex = queryString .indexOf ("`" + nextInCallOrder + "`" );
115+ if (foundIndex < lastIndex ) {
116+ if (foundIndex == -1 )
117+ fail ("Not all columns occurred in the query string." );
118+ else
119+ fail ("The columns did not appear an the correct order." );
120+
121+ break ;
122+ }
123+
124+ lastIndex = foundIndex ;
125+ }
126+
127+ // If it came until here the test should count as passed.
128+ assertTrue (true );
129+
130+ }
131+
87132 @ Test
88133 void testMultipleOrderByClausesOfMixedOrderReversed () {
89134 Query <Datatype > query = setUpModel (Datatype .class ).query ()
@@ -94,5 +139,4 @@ void testMultipleOrderByClausesOfMixedOrderReversed() {
94139 .assertSectionContains ("ORDER BY" , "`primitive_integer` DESC" )
95140 .assertSectionContains ("ORDER BY" , "`wrapper_integer`" );
96141 }
97-
98142}
0 commit comments