KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > sendopts > AdditionalArgumentOptsTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.api.sendopts;
20
21 import java.util.Map JavaDoc;
22 import junit.framework.TestCase;
23 import org.netbeans.modules.sendopts.OptionImpl;
24 import org.netbeans.spi.sendopts.Env;
25 import org.netbeans.spi.sendopts.Option;
26
27 /** Options that work with additonal arguments.
28  *
29  * @author Jaroslav Tulach
30  */

31 public class AdditionalArgumentOptsTest extends TestCase {
32     private CommandLine l;
33     private AddArgsProc proc = new AddArgsProc();
34     private Option open;
35     private Option close;
36     private AddArgsProc closeProc = new AddArgsProc();
37
38     public AdditionalArgumentOptsTest(String JavaDoc s) {
39         super(s);
40     }
41
42     protected void tearDown() throws Exception JavaDoc {
43
44         super.tearDown();
45     }
46
47     protected void setUp() throws Exception JavaDoc {
48         Provider.clearAll();
49         open = Option.additionalArguments((char)-1, "open");
50         Provider.add(proc, open);
51         close = Option.additionalArguments('C', "close");
52         Provider.add(closeProc, close);
53         
54         l = CommandLine.getDefault();
55     }
56
57     
58     public void testCanHypenBeAdditionalArg() throws Exception JavaDoc {
59         l.process(new String JavaDoc[] { "--open", "-", "+", "*", "/" });
60
61         assertNotNull("Processor called", proc.option);
62         assertNotNull("args provided", proc.values);
63         assertEquals("Four ", 4, proc.values.length);
64         assertEquals("-", proc.values[0]);
65         assertEquals("+", proc.values[1]);
66         assertEquals("*", proc.values[2]);
67         assertEquals("/", proc.values[3]);
68     }
69     
70     public void testOpenCannotHaveAnArgument() throws Exception JavaDoc {
71         try {
72             l.process(new String JavaDoc[] { "--open=ahoj", "1", "2", "3" });
73             fail("Open cannot have an argument");
74         } catch (CommandException ex) {
75             // ok
76
}
77         assertNull("Processor not called", proc.option);
78         assertNull("No args provided", proc.values);
79     }
80     
81     public void testOptionsPassedToOpen() throws Exception JavaDoc {
82         l.process(new String JavaDoc[] { "1", "--open", "2", "3" });
83         assertEquals("Processor found for long name", open, proc.option);
84         assertEquals("Three files provided", 3, proc.values.length);
85         assertEquals("first", "1", proc.values[0]);
86         assertEquals("second", "2", proc.values[1]);
87         assertEquals("third", "3", proc.values[2]);
88
89
90         proc.option = null;
91         proc.values = null;
92         l.process(new String JavaDoc[] { "1", "--open", "2", "3" });
93         assertEquals("Processor found for long name", open, proc.option);
94         assertEquals("Three files provided", 3, proc.values.length);
95         assertEquals("first", "1", proc.values[0]);
96         assertEquals("second", "2", proc.values[1]);
97         assertEquals("third", "3", proc.values[2]);
98     }
99     public void testCannotHaveTwoAdditionalOptionUsedAtOnce() throws Exception JavaDoc {
100         try {
101             l.process(new String JavaDoc[] { "--open", "ahoj", "--close" });
102             fail("open & close cannot be used at once");
103         } catch (CommandException ex) {
104             // ok
105
}
106             
107         assertNull("No processor called1", proc.option);
108         assertNull("No processor called2", closeProc.option);
109         assertNull("Value is unset", proc.values);
110     }
111     
112     public void testProcessingStopsAtDashDash() throws Exception JavaDoc {
113         l.process(new String JavaDoc[] { "--ope", "1", "--", "--close", "2" });
114         assertEquals("Processor found for abbrevated name", open, proc.option);
115         assertNull("No close called", closeProc.option);
116         assertEquals("three options provided", 3, proc.values.length);
117         assertEquals("first", "1", proc.values[0]);
118         assertEquals("second", "--close", proc.values[1]);
119         assertEquals("third", "2", proc.values[2]);
120     }
121     public void testShortOptionWorksAsWell() throws Exception JavaDoc {
122         l.process(new String JavaDoc[] { "-C", "1", "--", "--open", "2" });
123         assertEquals("Close Processor found for abbrevated name", close, closeProc.option);
124         assertNull("No open called", proc.option);
125         assertEquals("three options provided", 3, closeProc.values.length);
126         assertEquals("first", "1", closeProc.values[0]);
127         assertEquals("second", "--open", closeProc.values[1]);
128         assertEquals("third", "2", closeProc.values[2]);
129     }
130     
131     static final class AddArgsProc implements Processor {
132         Option option;
133         String JavaDoc[] values;
134
135         public void process(Option o, Env env, String JavaDoc[] args) throws CommandException {
136         }
137
138         public void process(Env env, Map JavaDoc<Option, String JavaDoc[]> values) throws CommandException {
139             assertNull("Not processed yet", option);
140             
141             assertEquals("One value", 1, values.size());
142             
143             option = values.keySet().iterator().next();
144             this.values = values.values().iterator().next();
145
146             assertNotNull("An option is provided", option);
147             assertNotNull("An array of args is provided", this.values);
148         }
149     }
150 }
151
Popular Tags