| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
package org.apache.felix.gogo.runtime; |
| 20 |
|
| 21 |
import junit.framework.TestCase; |
| 22 |
|
| 23 |
import org.apache.felix.service.command.CommandSession; |
| 24 |
import org.apache.felix.service.command.Descriptor; |
| 25 |
import org.apache.felix.service.command.Parameter; |
| 26 |
|
| 27 |
public class TestCoercion extends TestCase |
| 28 |
{ |
| 29 |
public boolean fBool(boolean t) |
| 30 |
{ |
| 31 |
return t; |
| 32 |
} |
| 33 |
|
| 34 |
public double fDouble(double x) |
| 35 |
{ |
| 36 |
return x; |
| 37 |
} |
| 38 |
|
| 39 |
public int fInt(int x) |
| 40 |
{ |
| 41 |
return x; |
| 42 |
} |
| 43 |
|
| 44 |
public long fLong(long x) |
| 45 |
{ |
| 46 |
return x; |
| 47 |
} |
| 48 |
|
| 49 |
// the presence of this method checks that it is not invoked instead of fInt(int) |
| 50 |
public String fInt(Object[] x) |
| 51 |
{ |
| 52 |
return "array"; |
| 53 |
} |
| 54 |
|
| 55 |
public String fString(String s) |
| 56 |
{ |
| 57 |
return s; |
| 58 |
} |
| 59 |
|
| 60 |
public void testSimpleTypes() throws Exception |
| 61 |
{ |
| 62 |
Context c = new Context(); |
| 63 |
c.addCommand("fBool", this); |
| 64 |
c.addCommand("fDouble", this); |
| 65 |
c.addCommand("fInt", this); |
| 66 |
c.addCommand("fLong", this); |
| 67 |
c.addCommand("fString", this); |
| 68 |
|
| 69 |
assertEquals("fBool true", true, c.execute("fBool true")); |
| 70 |
assertEquals("fBool 'false'", false, c.execute("fBool 'false'")); |
| 71 |
|
| 72 |
assertEquals("fDouble 11", 11.0, c.execute("fDouble 11")); |
| 73 |
assertEquals("fDouble '11'", 11.0, c.execute("fDouble '11'")); |
| 74 |
|
| 75 |
assertEquals("fInt 22", 22, c.execute("fInt 22")); |
| 76 |
assertEquals("fInt '23'", 23, c.execute("fInt '23'")); |
| 77 |
assertEquals("fInt 1 2", "array", c.execute("fInt 1 2")); |
| 78 |
|
| 79 |
assertEquals("fLong 33", 33L, c.execute("fLong 33")); |
| 80 |
assertEquals("fLong '34'", 34L, c.execute("fLong '34'")); |
| 81 |
|
| 82 |
assertEquals("fString wibble", "wibble", c.execute("fString wibble")); |
| 83 |
assertEquals("fString 'wibble'", "wibble", c.execute("fString 'wibble'")); |
| 84 |
|
| 85 |
try |
| 86 |
{ |
| 87 |
Object r = c.execute("fString "); |
| 88 |
fail("too few args: expected IllegalArgumentException, got: " + r); |
| 89 |
} |
| 90 |
catch (IllegalArgumentException e) |
| 91 |
{ |
| 92 |
} |
| 93 |
|
| 94 |
try |
| 95 |
{ |
| 96 |
Object r = c.execute("fString a b"); |
| 97 |
fail("too many args: expected IllegalArgumentException, got: " + r); |
| 98 |
} |
| 99 |
catch (IllegalArgumentException e) |
| 100 |
{ |
| 101 |
} |
| 102 |
|
| 103 |
try |
| 104 |
{ |
| 105 |
Object r = c.execute("fLong string"); |
| 106 |
fail("wrong arg type: expected IllegalArgumentException, got: " + r); |
| 107 |
} |
| 108 |
catch (IllegalArgumentException e) |
| 109 |
{ |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
public String bundles(Long id) |
| 114 |
{ |
| 115 |
return "long"; |
| 116 |
} |
| 117 |
|
| 118 |
public String bundles(String loc) |
| 119 |
{ |
| 120 |
return "string"; |
| 121 |
} |
| 122 |
|
| 123 |
public void testBestCoercion() throws Exception |
| 124 |
{ |
| 125 |
Context c = new Context(); |
| 126 |
c.addCommand("bundles", this); |
| 127 |
|
| 128 |
assertEquals("bundles myloc", "string", c.execute("bundles myloc")); |
| 129 |
assertEquals("bundles 1", "long", c.execute("bundles 1")); |
| 130 |
assertEquals("bundles '1'", "string", c.execute("bundles '1'")); |
| 131 |
} |
| 132 |
|
| 133 |
@Descriptor("list all installed bundles") |
| 134 |
public String p0( |
| 135 |
@Descriptor("show location") @Parameter(names = { "-l", "--location" }, presentValue = "true", absentValue = "false") boolean showLoc, |
| 136 |
@Descriptor("show symbolic name") @Parameter(names = { "-s", "--symbolicname" }, presentValue = "true", absentValue = "false") boolean showSymbolic) |
| 137 |
{ |
| 138 |
return showLoc + ":" + showSymbolic; |
| 139 |
} |
| 140 |
|
| 141 |
// function with session and parameter |
| 142 |
public boolean p01( |
| 143 |
CommandSession session, |
| 144 |
@Parameter(names = { "-f", "--flag" }, presentValue = "true", absentValue = "false") boolean flag) |
| 145 |
{ |
| 146 |
assertNotNull("session must not be null", session); |
| 147 |
return flag; |
| 148 |
} |
| 149 |
|
| 150 |
public void testParameter0() throws Exception |
| 151 |
{ |
| 152 |
Context c = new Context(); |
| 153 |
c.addCommand("p0", this); |
| 154 |
c.addCommand("p01", this); |
| 155 |
|
| 156 |
assertEquals("p0", "false:false", c.execute("p0")); |
| 157 |
assertEquals("p0 -l", "true:false", c.execute("p0 -l")); |
| 158 |
assertEquals("p0 --location", "true:false", c.execute("p0 --location")); |
| 159 |
assertEquals("p0 -l -s", "true:true", c.execute("p0 -l -s")); |
| 160 |
assertEquals("p0 -s -l", "true:true", c.execute("p0 -s -l")); |
| 161 |
try |
| 162 |
{ |
| 163 |
Object r = c.execute("p0 wibble"); |
| 164 |
fail("too many args: expected IllegalArgumentException, got: " + r); |
| 165 |
} |
| 166 |
catch (IllegalArgumentException e) |
| 167 |
{ |
| 168 |
} |
| 169 |
assertEquals("p01 -f", true, c.execute("p01 -f")); |
| 170 |
} |
| 171 |
|
| 172 |
public String p1( |
| 173 |
@Parameter(names = { "-p", "--parameter" }, absentValue = "absent") String parm1) |
| 174 |
{ |
| 175 |
return parm1; |
| 176 |
} |
| 177 |
|
| 178 |
public void testParameter1() throws Exception |
| 179 |
{ |
| 180 |
Context c = new Context(); |
| 181 |
c.addCommand("p1", this); |
| 182 |
|
| 183 |
assertEquals("no parameter", "absent", c.execute("p1")); |
| 184 |
|
| 185 |
// FELIX-2894 |
| 186 |
assertEquals("correct parameter", "wibble", c.execute("p1 -p wibble")); |
| 187 |
|
| 188 |
try |
| 189 |
{ |
| 190 |
Object r = c.execute("p1 -p"); |
| 191 |
fail("missing parameter: expected IllegalArgumentException, got: " + r); |
| 192 |
} |
| 193 |
catch (IllegalArgumentException e) |
| 194 |
{ |
| 195 |
} |
| 196 |
|
| 197 |
try |
| 198 |
{ |
| 199 |
Object r = c.execute("p1 -X"); |
| 200 |
fail("wrong parameter: expected IllegalArgumentException, got: " + r); |
| 201 |
} |
| 202 |
catch (IllegalArgumentException e) |
| 203 |
{ |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
} |