001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.cli2.util; 018 019import java.util.Collections; 020import java.util.List; 021 022import junit.framework.TestCase; 023 024import org.apache.commons.cli2.CLITestCase; 025import org.apache.commons.cli2.Option; 026import org.apache.commons.cli2.option.CommandTest; 027import org.apache.commons.cli2.option.DefaultOptionTest; 028import org.apache.commons.cli2.option.GroupTest; 029import org.apache.commons.cli2.option.ParentTest; 030import org.apache.commons.cli2.option.SwitchTest; 031 032/** 033 * @author Rob Oxspring 034 */ 035public class ComparatorsTest extends TestCase { 036 public void testGroupFirst() { 037 final Option o1 = GroupTest.buildAntGroup(); 038 final Option o2 = ParentTest.buildLibParent(); 039 final List list = CLITestCase.list(o1, o2); 040 041 Collections.sort(list, Comparators.groupFirst()); 042 043 CLITestCase.assertListContentsEqual( 044 CLITestCase.list(o1, o2), 045 list); 046 } 047 048 public void testGroupLast() { 049 final Option o1 = GroupTest.buildAntGroup(); 050 final Option o2 = ParentTest.buildLibParent(); 051 final List list = CLITestCase.list(o1, o2); 052 053 Collections.sort(list, Comparators.groupLast()); 054 055 CLITestCase.assertListContentsEqual( 056 CLITestCase.list(o2, o1), 057 list); 058 } 059 060 public void testSwitchFirst() { 061 final Option o1 = SwitchTest.buildDisplaySwitch(); 062 final Option o2 = ParentTest.buildLibParent(); 063 final List list = CLITestCase.list(o1, o2); 064 065 Collections.sort(list, Comparators.switchFirst()); 066 067 CLITestCase.assertListContentsEqual( 068 CLITestCase.list(o1, o2), 069 list); 070 } 071 072 public void testSwitchLast() { 073 final Option o1 = SwitchTest.buildDisplaySwitch(); 074 final Option o2 = ParentTest.buildLibParent(); 075 //final Option o3 = new SwitchBuilder().withName("hidden").create(); 076 final List list = CLITestCase.list(o1, o2); 077 078 Collections.sort(list, Comparators.switchLast()); 079 080 CLITestCase.assertListContentsEqual( 081 CLITestCase.list(o2, o1), 082 list); 083 } 084 085 public void testCommandFirst() { 086 final Option o1 = CommandTest.buildCommitCommand(); 087 final Option o2 = ParentTest.buildLibParent(); 088 final List list = CLITestCase.list(o1, o2); 089 090 Collections.sort(list, Comparators.commandFirst()); 091 092 CLITestCase.assertListContentsEqual( 093 CLITestCase.list(o1, o2), 094 list); 095 } 096 097 public void testCommandLast() { 098 final Option o1 = CommandTest.buildCommitCommand(); 099 final Option o2 = ParentTest.buildLibParent(); 100 final List list = CLITestCase.list(o1, o2); 101 102 Collections.sort(list, Comparators.commandLast()); 103 104 CLITestCase.assertListContentsEqual( 105 CLITestCase.list(o2, o1), 106 list); 107 } 108 109 public void testDefaultOptionFirst() { 110 final Option o1 = DefaultOptionTest.buildHelpOption(); 111 final Option o2 = CommandTest.buildCommitCommand(); 112 final List list = CLITestCase.list(o1, o2); 113 114 Collections.sort(list, Comparators.defaultOptionFirst()); 115 116 CLITestCase.assertListContentsEqual( 117 CLITestCase.list(o1, o2), 118 list); 119 } 120 121 public void testDefaultOptionLast() { 122 final Option o1 = DefaultOptionTest.buildHelpOption(); 123 final Option o2 = CommandTest.buildCommitCommand(); 124 final List list = CLITestCase.list(o1, o2); 125 126 Collections.sort(list, Comparators.defaultOptionLast()); 127 128 CLITestCase.assertListContentsEqual( 129 CLITestCase.list(o2, o1), 130 list); 131 } 132 133 public void testNamedFirst() { 134 final Option o1 = DefaultOptionTest.buildHelpOption(); 135 final Option o2 = ParentTest.buildLibParent(); 136 final List list = CLITestCase.list(o1, o2); 137 138 Collections.sort(list, Comparators.namedFirst("--help")); 139 140 CLITestCase.assertListContentsEqual( 141 CLITestCase.list(o1, o2), 142 list); 143 } 144 145 public void testNamedLast() { 146 final Option o1 = DefaultOptionTest.buildHelpOption(); 147 final Option o2 = ParentTest.buildLibParent(); 148 final List list = CLITestCase.list(o1, o2); 149 150 Collections.sort(list, Comparators.namedLast("--help")); 151 152 CLITestCase.assertListContentsEqual( 153 CLITestCase.list(o2, o1), 154 list); 155 } 156 157 public void testPreferredNameFirst() { 158 final Option o1 = DefaultOptionTest.buildHelpOption(); 159 final Option o2 = ParentTest.buildLibParent(); 160 final List list = CLITestCase.list(o1, o2); 161 162 Collections.sort(list, Comparators.preferredNameFirst()); 163 164 CLITestCase.assertListContentsEqual( 165 CLITestCase.list(o1, o2), 166 list); 167 } 168 169 public void testPreferredNameLast() { 170 final Option o1 = DefaultOptionTest.buildHelpOption(); 171 final Option o2 = ParentTest.buildLibParent(); 172 final List list = CLITestCase.list(o1, o2); 173 174 Collections.sort(list, Comparators.preferredNameLast()); 175 176 CLITestCase.assertListContentsEqual( 177 CLITestCase.list(o2, o1), 178 list); 179 } 180 181 public void testRequiredFirst() { 182 final Option o1 = DefaultOptionTest.buildHelpOption(); 183 final Option o2 = DefaultOptionTest.buildXOption(); 184 final List list = CLITestCase.list(o1, o2); 185 186 Collections.sort(list, Comparators.requiredFirst()); 187 188 CLITestCase.assertListContentsEqual( 189 CLITestCase.list(o2, o1), 190 list); 191 } 192 193 public void testRequiredLast() { 194 final Option o1 = DefaultOptionTest.buildHelpOption(); 195 final Option o2 = DefaultOptionTest.buildXOption(); 196 final List list = CLITestCase.list(o1, o2); 197 198 Collections.sort(list, Comparators.requiredLast()); 199 200 CLITestCase.assertListContentsEqual( 201 CLITestCase.list(o1, o2), 202 list); 203 } 204 205 public void testChained() { 206 final Option o1 = CommandTest.buildCommitCommand(); 207 final Option o2 = SwitchTest.buildDisplaySwitch(); 208 final Option o3 = DefaultOptionTest.buildHelpOption(); 209 final List list = CLITestCase.list(o1, o2, o3); 210 211 Collections.sort( 212 list, 213 Comparators.chain( 214 Comparators.namedFirst("--help"), 215 Comparators.commandFirst())); 216 217 CLITestCase.assertListContentsEqual( 218 CLITestCase.list(o3, o1, o2), 219 list); 220 } 221}