View Javadoc
1   /*
2     Licensed to the Apache Software Foundation (ASF) under one or more
3     contributor license agreements.  See the NOTICE file distributed with
4     this work for additional information regarding copyright ownership.
5     The ASF licenses this file to You under the Apache License, Version 2.0
6     (the "License"); you may not use this file except in compliance with
7     the License.  You may obtain a copy of the License at
8   
9         http://www.apache.org/licenses/LICENSE-2.0
10  
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16   */
17  
18  package org.apache.commons.cli;
19  
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.io.PrintWriter;
24  import java.io.StringWriter;
25  import java.util.Arrays;
26  import java.util.List;
27  
28  import org.junit.jupiter.api.Test;
29  
30  public class SolrCreateToolTest {
31  
32      public List<Option> getOptions() {
33          // @formatter:off
34          return Arrays.asList(
35              SolrCliTest.OPTION_ZKHOST,
36              SolrCliTest.OPTION_SOLRURL,
37              SolrCliTest.OPTION_ZKHOST_DEPRECATED,
38              SolrCliTest.OPTION_SOLRURL,
39              Option.builder("c")
40                  .longOpt("name")
41                  .argName("NAME")
42                  .hasArg()
43                  .required(true)
44                  .desc("Name of collection or core to create.")
45                  .build(),
46              Option.builder("s")
47                  .longOpt("shards")
48                  .argName("#")
49                  .hasArg()
50                  .required(false)
51                  .desc("Number of shards; default is 1.")
52                  .build(),
53              Option.builder("rf")
54                  .longOpt("replication-factor")
55                  .argName("#")
56                  .hasArg()
57                  .required(false)
58                  .desc("Number of copies of each document across the collection (replicas per shard); default is 1.")
59                  .build(),
60              Option.builder("d")
61                  .longOpt("confdir")
62                  .argName("NAME")
63                  .hasArg()
64                  .required(false)
65                  .desc("Configuration directory to copy when creating the new collection; default is "
66                          + SolrCliTest.DEFAULT_CONFIG_SET
67                          + '.')
68                  .build(),
69              Option.builder("n")
70                  .longOpt("confname")
71                  .argName("NAME")
72                  .hasArg()
73                  .required(false)
74                  .desc("Configuration name; default is the collection name.")
75                  .build(),
76              SolrCliTest.OPTION_CREDENTIALS);
77        // @formatter:on
78      }
79  
80      private String printHelp(final HelpFormatter formatter) {
81          final Options options = new Options();
82          getOptions().forEach(options::addOption);
83          final String cmdLineSyntax = getClass().getName();
84          final StringWriter out = new StringWriter();
85          final PrintWriter pw = new PrintWriter(out);
86          formatter.printHelp(pw, formatter.getWidth(), cmdLineSyntax, null, options, formatter.getLeftPadding(), formatter.getDescPadding(), null, false);
87          pw.flush();
88          final String actual = out.toString();
89          assertTrue(actual.contains("-z,--zk-host <HOST>              Zookeeper connection string; unnecessary"));
90          return actual;
91      }
92  
93      @Test
94      public void testHelpFormatter() {
95          final HelpFormatter formatter = new HelpFormatter();
96          final String actual = printHelp(formatter);
97          assertFalse(actual.contains("Deprecated"));
98      }
99  
100     @Test
101     public void testHelpFormatterDeprecated() {
102         final HelpFormatter formatter = HelpFormatter.builder().setShowDeprecated(true).get();
103         final String actual = printHelp(formatter);
104         assertTrue(actual.contains("-zkHost,--zkHost <HOST>          [Deprecated] Zookeeper connection"));
105     }
106 }