KickJava   Java API By Example, From Geeks To Geeks.

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


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 /** Default options that work with additonal arguments.
28  *
29  * @author Jaroslav Tulach
30  */

31 public class DefaultArgumentOptsTest extends TestCase {
32     private CommandLine l, wrong;
33     private AddArgsProc proc = new AddArgsProc();
34     private Option open;
35     private Option close;
36     private AddArgsProc closeProc = new AddArgsProc();
37
38     public DefaultArgumentOptsTest(String JavaDoc s) {
39         super(s);
40     }
41
42     protected void tearDown() throws Exception JavaDoc {
43
44         super.tearDown();
45     }
46
47     protected void setUpOk() throws Exception JavaDoc {
48         Provider.clearAll();
49         
50         
51         open = Option.additionalArguments((char)-1, "open");
52         Provider.add(proc, open);
53         close = Option.defaultArguments();
54         Provider.add(closeProc, close);
55         
56         l = CommandLine.getDefault();
57     }
58     
59     protected void setUpWrong() throws Exception JavaDoc {
60         setUpOk();
61         Option def = Option.defaultArguments();
62         Provider.add(closeProc, def);
63         
64         l = null;
65         wrong = CommandLine.getDefault();
66     }
67     
68     public void testOptionsPassedToOpen() throws Exception JavaDoc {
69         setUpOk();
70         
71         l.process(new String JavaDoc[] { "1", "--open", "2", "3" });
72         assertEquals("Processor found for long name", open, proc.option);
73         assertEquals("Three files provided", 3, proc.values.length);
74         assertEquals("first", "1", proc.values[0]);
75         assertEquals("second", "2", proc.values[1]);
76         assertEquals("third", "3", proc.values[2]);
77     }
78     public void testOptionsPassedToOpenWrong() throws Exception JavaDoc {
79         setUpWrong();
80         
81         wrong.process(new String JavaDoc[] { "1", "--open", "2", "3" });
82         assertEquals("Processor found for long name", open, proc.option);
83         assertEquals("Three files provided", 3, proc.values.length);
84         assertEquals("first", "1", proc.values[0]);
85         assertEquals("second", "2", proc.values[1]);
86         assertEquals("third", "3", proc.values[2]);
87     }
88     
89     public void testProcessingStopsAtDashDash() throws Exception JavaDoc {
90         setUpOk();
91         
92         l.process(new String JavaDoc[] { "1", "--", "--open", "2" });
93         assertEquals("Close Processor found for abbrevated name", close, closeProc.option);
94         assertNull("No open called", proc.option);
95         assertEquals("three options provided", 3, closeProc.values.length);
96         assertEquals("first", "1", closeProc.values[0]);
97         assertEquals("second", "--open", closeProc.values[1]);
98         assertEquals("third", "2", closeProc.values[2]);
99     }
100     public void testCannotHaveTwoDefaultOptions() throws Exception JavaDoc {
101         setUpWrong();
102         
103         try {
104             wrong.process(new String JavaDoc[] { "1", "--", "--open", "2" });
105             fail("Cannot have two default options");
106         } catch (CommandException ex) {
107             // ok
108
}
109         assertNull("No close", closeProc.option);
110         assertNull("No open", proc.option);
111     }
112     
113     static final class AddArgsProc implements Processor {
114         Option option;
115         String JavaDoc[] values;
116
117
118         public void process(Env env, Map JavaDoc<Option, String JavaDoc[]> values) throws CommandException {
119             assertNull("Not processed yet", option);
120             
121             assertEquals("One value", 1, values.size());
122             
123             option = values.keySet().iterator().next();
124             this.values = values.values().iterator().next();
125
126             assertNotNull("An option is provided", option);
127             assertNotNull("An array of args is provided", this.values);
128         }
129     }
130 }
131
Popular Tags