1 /*--------------------------------------------------------------------------
\r
2 * Copyright 2008 Taro L. Saito
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
15 *--------------------------------------------------------------------------*/
\r
16 //--------------------------------------
\r
19 // OptionSchemaTest.java
\r
20 // Since: Oct 29, 2008 12:49:36 PM
\r
22 // $URL: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core/src/test/java/org/xerial/util/opt/OptionSchemaTest.java $
\r
24 //--------------------------------------
\r
25 package org.xerial.util.opt;
\r
27 import static org.junit.Assert.assertEquals;
\r
28 import static org.junit.Assert.assertNotNull;
\r
30 import java.io.IOException;
\r
31 import java.io.StringWriter;
\r
32 import java.util.List;
\r
34 import org.junit.After;
\r
35 import org.junit.Before;
\r
36 import org.junit.Test;
\r
37 import org.xerial.util.log.LogLevel;
\r
38 import org.xerial.util.log.Logger;
\r
40 public class OptionSchemaTest
\r
42 private static Logger _logger = Logger.getLogger(OptionSchemaTest.class);
\r
45 public void setUp() throws Exception
\r
49 public void tearDown() throws Exception
\r
52 @Usage(command = "> java -jar MyProg [option ..]", description = "sample program")
\r
55 @Option(symbol = "h", longName = "help", description = "display help message")
\r
56 boolean displayHelp;
\r
58 @Option(longName = "verbose", description = "output verbose messages")
\r
61 @Option(symbol = "l", longName = "loglevel", varName = "LOG_LEVEL", description = "set log level: ERROR, DEBUG, WARN")
\r
64 @Option(symbol = "o", varName = "FILE", description = "output file")
\r
67 @Argument(name = "file", index = 1, required = false)
\r
68 List<String> fileList;
\r
70 @Argument(index = 0)
\r
76 public void testPrintUsage() throws IOException
\r
78 OptionSchema schema = OptionSchema.newOptionSchema(MyOption.class);
\r
80 assertEquals(4, schema.getOptionItemList().size());
\r
81 assertEquals(2, schema.getArgumentItemList().size());
\r
82 assertNotNull(schema.getUsage());
\r
84 StringWriter out = new StringWriter();
\r
86 schema.printUsage(out);
\r
87 _logger.debug(out.toString());
\r
90 @Usage(command = "> java -jar MyProg [option ..]", description = "sample program", templatePath = "org/xerial/util/opt/help-message-alt.template")
\r
93 @Option(symbol = "h", longName = "help", description = "display help message")
\r
94 boolean displayHelp;
\r
96 @Option(longName = "verbose", description = "output verbose messages")
\r
99 @Option(symbol = "l", longName = "loglevel", varName = "LOG_LEVEL", description = "set log level: ERROR, DEBUG, WARN")
\r
102 @Option(symbol = "o", varName = "FILE", description = "output file")
\r
105 @Argument(name = "file", index = 1, required = false)
\r
106 List<String> fileList;
\r
108 @Argument(name = "sub_command", index = 0)
\r
113 public void alternativeTemplate() throws IOException
\r
115 OptionSchema schema = OptionSchema.newOptionSchema(MyOption2.class);
\r
117 assertEquals(4, schema.getOptionItemList().size());
\r
118 assertEquals(2, schema.getArgumentItemList().size());
\r
119 assertNotNull(schema.getUsage());
\r
121 StringWriter out = new StringWriter();
\r
123 schema.printUsage(out);
\r
124 _logger.debug(out.toString());
\r