OSDN Git Service

imported from subversion repository
[xerial/xerial-core.git] / src / test / java / org / xerial / util / opt / OptionSchemaTest.java
1 /*--------------------------------------------------------------------------\r
2  *  Copyright 2008 Taro L. Saito\r
3  *\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
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\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
17 // XerialJ\r
18 //\r
19 // OptionSchemaTest.java\r
20 // Since: Oct 29, 2008 12:49:36 PM\r
21 //\r
22 // $URL: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core/src/test/java/org/xerial/util/opt/OptionSchemaTest.java $\r
23 // $Author: leo $\r
24 //--------------------------------------\r
25 package org.xerial.util.opt;\r
26 \r
27 import static org.junit.Assert.assertEquals;\r
28 import static org.junit.Assert.assertNotNull;\r
29 \r
30 import java.io.IOException;\r
31 import java.io.StringWriter;\r
32 import java.util.List;\r
33 \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
39 \r
40 public class OptionSchemaTest\r
41 {\r
42     private static Logger _logger = Logger.getLogger(OptionSchemaTest.class);\r
43 \r
44     @Before\r
45     public void setUp() throws Exception\r
46     {}\r
47 \r
48     @After\r
49     public void tearDown() throws Exception\r
50     {}\r
51 \r
52     @Usage(command = "> java -jar MyProg [option ..]", description = "sample program")\r
53     class MyOption\r
54     {\r
55         @Option(symbol = "h", longName = "help", description = "display help message")\r
56         boolean      displayHelp;\r
57 \r
58         @Option(longName = "verbose", description = "output verbose messages")\r
59         boolean      verbose;\r
60 \r
61         @Option(symbol = "l", longName = "loglevel", varName = "LOG_LEVEL", description = "set log level: ERROR, DEBUG, WARN")\r
62         LogLevel     logLevel;\r
63 \r
64         @Option(symbol = "o", varName = "FILE", description = "output file")\r
65         String       outputFile;\r
66 \r
67         @Argument(name = "file", index = 1, required = false)\r
68         List<String> fileList;\r
69 \r
70         @Argument(index = 0)\r
71         String       subCommand;\r
72 \r
73     }\r
74 \r
75     @Test\r
76     public void testPrintUsage() throws IOException\r
77     {\r
78         OptionSchema schema = OptionSchema.newOptionSchema(MyOption.class);\r
79 \r
80         assertEquals(4, schema.getOptionItemList().size());\r
81         assertEquals(2, schema.getArgumentItemList().size());\r
82         assertNotNull(schema.getUsage());\r
83 \r
84         StringWriter out = new StringWriter();\r
85         out.append("\n");\r
86         schema.printUsage(out);\r
87         _logger.debug(out.toString());\r
88     }\r
89 \r
90     @Usage(command = "> java -jar MyProg [option ..]", description = "sample program", templatePath = "org/xerial/util/opt/help-message-alt.template")\r
91     class MyOption2\r
92     {\r
93         @Option(symbol = "h", longName = "help", description = "display help message")\r
94         boolean      displayHelp;\r
95 \r
96         @Option(longName = "verbose", description = "output verbose messages")\r
97         boolean      verbose;\r
98 \r
99         @Option(symbol = "l", longName = "loglevel", varName = "LOG_LEVEL", description = "set log level: ERROR, DEBUG, WARN")\r
100         LogLevel     logLevel;\r
101 \r
102         @Option(symbol = "o", varName = "FILE", description = "output file")\r
103         String       outputFile;\r
104 \r
105         @Argument(name = "file", index = 1, required = false)\r
106         List<String> fileList;\r
107 \r
108         @Argument(name = "sub_command", index = 0)\r
109         String       subCommand;\r
110     }\r
111 \r
112     @Test\r
113     public void alternativeTemplate() throws IOException\r
114     {\r
115         OptionSchema schema = OptionSchema.newOptionSchema(MyOption2.class);\r
116 \r
117         assertEquals(4, schema.getOptionItemList().size());\r
118         assertEquals(2, schema.getArgumentItemList().size());\r
119         assertNotNull(schema.getUsage());\r
120 \r
121         StringWriter out = new StringWriter();\r
122         out.append("\n");\r
123         schema.printUsage(out);\r
124         _logger.debug(out.toString());\r
125     }\r
126 \r
127 }\r