1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.harmony.tests.java.lang.reflect; 19 20 import java.lang.reflect.Modifier; 21 22 public class ModifierTest extends junit.framework.TestCase { 23 24 private static final int ALL_FLAGS = 0x7FF; 25 26 /** 27 * java.lang.reflect.Modifier#Modifier() 28 */ 29 public void test_Constructor() { 30 // Test for method java.lang.reflect.Modifier() 31 new Modifier(); 32 } 33 34 /** 35 * java.lang.reflect.Modifier#isAbstract(int) 36 */ 37 public void test_isAbstractI() { 38 // Test for method boolean java.lang.reflect.Modifier.isAbstract(int) 39 assertTrue("ABSTRACT returned false", Modifier.isAbstract(ALL_FLAGS)); 40 assertTrue("ABSTRACT returned false", Modifier 41 .isAbstract(Modifier.ABSTRACT)); 42 assertTrue("Non-ABSTRACT returned true", !Modifier 43 .isAbstract(Modifier.TRANSIENT)); 44 } 45 46 /** 47 * java.lang.reflect.Modifier#isFinal(int) 48 */ 49 public void test_isFinalI() { 50 // Test for method boolean java.lang.reflect.Modifier.isFinal(int) 51 assertTrue("FINAL returned false", Modifier.isFinal(ALL_FLAGS)); 52 assertTrue("FINAL returned false", Modifier.isFinal(Modifier.FINAL)); 53 assertTrue("Non-FINAL returned true", !Modifier 54 .isFinal(Modifier.TRANSIENT)); 55 } 56 57 /** 58 * java.lang.reflect.Modifier#isInterface(int) 59 */ 60 public void test_isInterfaceI() { 61 // Test for method boolean java.lang.reflect.Modifier.isInterface(int) 62 assertTrue("INTERFACE returned false", Modifier.isInterface(ALL_FLAGS)); 63 assertTrue("INTERFACE returned false", Modifier 64 .isInterface(Modifier.INTERFACE)); 65 assertTrue("Non-INTERFACE returned true", !Modifier 66 .isInterface(Modifier.TRANSIENT)); 67 } 68 69 /** 70 * java.lang.reflect.Modifier#isNative(int) 71 */ 72 public void test_isNativeI() { 73 // Test for method boolean java.lang.reflect.Modifier.isNative(int) 74 assertTrue("NATIVE returned false", Modifier.isNative(ALL_FLAGS)); 75 assertTrue("NATIVE returned false", Modifier.isNative(Modifier.NATIVE)); 76 assertTrue("Non-NATIVE returned true", !Modifier 77 .isNative(Modifier.TRANSIENT)); 78 } 79 80 /** 81 * java.lang.reflect.Modifier#isPrivate(int) 82 */ 83 public void test_isPrivateI() { 84 // Test for method boolean java.lang.reflect.Modifier.isPrivate(int) 85 assertTrue("PRIVATE returned false", Modifier.isPrivate(ALL_FLAGS)); 86 assertTrue("PRIVATE returned false", Modifier 87 .isPrivate(Modifier.PRIVATE)); 88 assertTrue("Non-PRIVATE returned true", !Modifier 89 .isPrivate(Modifier.TRANSIENT)); 90 } 91 92 /** 93 * java.lang.reflect.Modifier#isProtected(int) 94 */ 95 public void test_isProtectedI() { 96 // Test for method boolean java.lang.reflect.Modifier.isProtected(int) 97 assertTrue("PROTECTED returned false", Modifier.isProtected(ALL_FLAGS)); 98 assertTrue("PROTECTED returned false", Modifier 99 .isProtected(Modifier.PROTECTED)); 100 assertTrue("Non-PROTECTED returned true", !Modifier 101 .isProtected(Modifier.TRANSIENT)); 102 } 103 104 /** 105 * java.lang.reflect.Modifier#isPublic(int) 106 */ 107 public void test_isPublicI() { 108 // Test for method boolean java.lang.reflect.Modifier.isPublic(int) 109 assertTrue("PUBLIC returned false", Modifier.isPublic(ALL_FLAGS)); 110 assertTrue("PUBLIC returned false", Modifier.isPublic(Modifier.PUBLIC)); 111 assertTrue("Non-PUBLIC returned true", !Modifier 112 .isPublic(Modifier.TRANSIENT)); 113 } 114 115 /** 116 * java.lang.reflect.Modifier#isStatic(int) 117 */ 118 public void test_isStaticI() { 119 // Test for method boolean java.lang.reflect.Modifier.isStatic(int) 120 assertTrue("STATIC returned false", Modifier.isStatic(ALL_FLAGS)); 121 assertTrue("STATIC returned false", Modifier.isStatic(Modifier.STATIC)); 122 assertTrue("Non-STATIC returned true", !Modifier 123 .isStatic(Modifier.TRANSIENT)); 124 } 125 126 /** 127 * java.lang.reflect.Modifier#isStrict(int) 128 */ 129 public void test_isStrictI() { 130 // Test for method boolean java.lang.reflect.Modifier.isStrict(int) 131 assertTrue("STRICT returned false", Modifier.isStrict(Modifier.STRICT)); 132 assertTrue("Non-STRICT returned true", !Modifier 133 .isStrict(Modifier.TRANSIENT)); 134 } 135 136 /** 137 * java.lang.reflect.Modifier#isSynchronized(int) 138 */ 139 public void test_isSynchronizedI() { 140 // Test for method boolean 141 // java.lang.reflect.Modifier.isSynchronized(int) 142 assertTrue("Synchronized returned false", Modifier 143 .isSynchronized(ALL_FLAGS)); 144 assertTrue("Non-Synchronized returned true", !Modifier 145 .isSynchronized(Modifier.VOLATILE)); 146 } 147 148 /** 149 * java.lang.reflect.Modifier#isTransient(int) 150 */ 151 public void test_isTransientI() { 152 // Test for method boolean java.lang.reflect.Modifier.isTransient(int) 153 assertTrue("Transient returned false", Modifier.isTransient(ALL_FLAGS)); 154 assertTrue("Transient returned false", Modifier 155 .isTransient(Modifier.TRANSIENT)); 156 assertTrue("Non-Transient returned true", !Modifier 157 .isTransient(Modifier.VOLATILE)); 158 } 159 160 /** 161 * java.lang.reflect.Modifier#isVolatile(int) 162 */ 163 public void test_isVolatileI() { 164 // Test for method boolean java.lang.reflect.Modifier.isVolatile(int) 165 assertTrue("Volatile returned false", Modifier.isVolatile(ALL_FLAGS)); 166 assertTrue("Volatile returned false", Modifier 167 .isVolatile(Modifier.VOLATILE)); 168 assertTrue("Non-Volatile returned true", !Modifier 169 .isVolatile(Modifier.TRANSIENT)); 170 } 171 172 /** 173 * java.lang.reflect.Modifier#toString(int) 174 */ 175 public void test_toStringI() { 176 // Test for method java.lang.String 177 // java.lang.reflect.Modifier.toString(int) 178 assertTrue("Returned incorrect string value: " 179 + Modifier.toString(java.lang.reflect.Modifier.PUBLIC 180 + java.lang.reflect.Modifier.ABSTRACT), Modifier 181 .toString( 182 java.lang.reflect.Modifier.PUBLIC 183 + java.lang.reflect.Modifier.ABSTRACT).equals( 184 "public abstract")); 185 186 int i = 0xFFF; 187 String modification = "public protected private abstract static final transient " 188 + "volatile synchronized native strictfp interface"; 189 assertTrue("Returned incorrect string value", Modifier.toString(i) 190 .equals(modification)); 191 } 192 193 public void test_Constants_Value() { 194 assertEquals(1024, Modifier.ABSTRACT); 195 assertEquals(16, Modifier.FINAL); 196 assertEquals(512, Modifier.INTERFACE); 197 assertEquals(256, Modifier.NATIVE); 198 assertEquals(2, Modifier.PRIVATE); 199 assertEquals(4, Modifier.PROTECTED); 200 assertEquals(1, Modifier.PUBLIC); 201 assertEquals(8, Modifier.STATIC); 202 assertEquals(2048, Modifier.STRICT); 203 assertEquals(32, Modifier.SYNCHRONIZED); 204 assertEquals(128, Modifier.TRANSIENT); 205 assertEquals(64, Modifier.VOLATILE); 206 } 207 208 abstract class AbstractClazz { 209 } 210 211 final class FinalClazz { 212 } 213 214 static class StaticClazz { 215 } 216 217 interface InterfaceClazz { 218 } 219 220 public class PublicClazz { 221 } 222 223 protected class ProtectedClazz { 224 } 225 226 private class PrivateClazz { 227 } 228 229 public abstract class PublicAbstractClazz { 230 } 231 232 protected abstract class ProtectedAbstractClazz { 233 } 234 235 private abstract class PrivateAbstractClazz { 236 } 237 238 public final class PublicFinalClazz { 239 } 240 241 protected final class ProtectedFinalClazz { 242 } 243 244 private final class PrivateFinalClazz { 245 } 246 247 public static class PublicStaticClazz { 248 } 249 250 protected static class ProtectedStaticClazz { 251 } 252 253 private static class PrivateStaticClazz { 254 } 255 256 public interface PublicInterface { 257 } 258 259 protected interface ProtectedInterface { 260 } 261 262 private interface PrivateInterface { 263 } 264 265 static abstract class StaticAbstractClazz { 266 } 267 268 public static abstract class PublicStaticAbstractClazz { 269 } 270 271 protected static abstract class ProtectedStaticAbstractClazz { 272 } 273 274 private static abstract class PrivateStaticAbstractClazz { 275 } 276 277 static final class StaticFinalClazz { 278 } 279 280 public static final class PublicStaticFinalClazz { 281 } 282 283 protected static final class ProtectedStaticFinalClazz { 284 } 285 286 private static final class PrivateStaticFinalClazz { 287 } 288 289 static interface StaticInterface { 290 } 291 292 public static interface PublicStaticInterface { 293 } 294 295 protected static interface ProtectedStaticInterface { 296 } 297 298 private static interface PrivateStaticInterface { 299 } 300 301 static abstract interface StaticAbstractInterface { 302 } 303 304 public static abstract interface PublicStaticAbstractInterface { 305 } 306 307 protected static abstract interface ProtectedStaticAbstractInterface { 308 } 309 310 private static abstract interface PrivateStaticAbstractInterface { 311 } 312 313 public void test_Class_Modifier() { 314 assertEquals(Modifier.ABSTRACT, AbstractClazz.class.getModifiers()); 315 assertEquals(Modifier.FINAL, FinalClazz.class.getModifiers()); 316 assertEquals(Modifier.STATIC, StaticClazz.class.getModifiers()); 317 assertEquals(Modifier.INTERFACE + Modifier.STATIC + Modifier.ABSTRACT, 318 InterfaceClazz.class.getModifiers()); 319 320 assertEquals(Modifier.PUBLIC, PublicClazz.class.getModifiers()); 321 assertEquals(Modifier.PROTECTED, ProtectedClazz.class.getModifiers()); 322 assertEquals(Modifier.PRIVATE, PrivateClazz.class.getModifiers()); 323 324 assertEquals(Modifier.PUBLIC + Modifier.ABSTRACT, 325 PublicAbstractClazz.class.getModifiers()); 326 assertEquals(Modifier.PROTECTED + Modifier.ABSTRACT, 327 ProtectedAbstractClazz.class.getModifiers()); 328 assertEquals(Modifier.PRIVATE + Modifier.ABSTRACT, 329 PrivateAbstractClazz.class.getModifiers()); 330 331 assertEquals(Modifier.PUBLIC + Modifier.FINAL, PublicFinalClazz.class 332 .getModifiers()); 333 assertEquals(Modifier.PROTECTED + Modifier.FINAL, 334 ProtectedFinalClazz.class.getModifiers()); 335 assertEquals(Modifier.PRIVATE + Modifier.FINAL, PrivateFinalClazz.class 336 .getModifiers()); 337 338 assertEquals(Modifier.PUBLIC + Modifier.STATIC, PublicStaticClazz.class 339 .getModifiers()); 340 assertEquals(Modifier.PROTECTED + Modifier.STATIC, 341 ProtectedStaticClazz.class.getModifiers()); 342 assertEquals(Modifier.PRIVATE + Modifier.STATIC, 343 PrivateStaticClazz.class.getModifiers()); 344 345 assertEquals(Modifier.PUBLIC + Modifier.INTERFACE + Modifier.STATIC 346 + Modifier.ABSTRACT, PublicInterface.class.getModifiers()); 347 assertEquals(Modifier.STATIC + Modifier.FINAL, StaticFinalClazz.class 348 .getModifiers()); 349 assertEquals(Modifier.PRIVATE + Modifier.INTERFACE + Modifier.STATIC 350 + Modifier.ABSTRACT, PrivateInterface.class.getModifiers()); 351 352 assertEquals(Modifier.STATIC + Modifier.ABSTRACT, 353 StaticAbstractClazz.class.getModifiers()); 354 assertEquals(Modifier.PUBLIC + Modifier.STATIC + Modifier.ABSTRACT, 355 PublicStaticAbstractClazz.class.getModifiers()); 356 assertEquals(Modifier.PROTECTED + Modifier.STATIC + Modifier.ABSTRACT, 357 ProtectedStaticAbstractClazz.class.getModifiers()); 358 assertEquals(Modifier.PRIVATE + Modifier.STATIC + Modifier.ABSTRACT, 359 PrivateStaticAbstractClazz.class.getModifiers()); 360 361 assertEquals(Modifier.STATIC + Modifier.FINAL, StaticFinalClazz.class 362 .getModifiers()); 363 assertEquals(Modifier.PUBLIC + Modifier.STATIC + Modifier.FINAL, 364 PublicStaticFinalClazz.class.getModifiers()); 365 assertEquals(Modifier.PROTECTED + Modifier.STATIC + Modifier.FINAL, 366 ProtectedStaticFinalClazz.class.getModifiers()); 367 assertEquals(Modifier.PRIVATE + Modifier.STATIC + Modifier.FINAL, 368 PrivateStaticFinalClazz.class.getModifiers()); 369 370 assertEquals(Modifier.INTERFACE + Modifier.STATIC + Modifier.ABSTRACT, 371 StaticInterface.class.getModifiers()); 372 assertEquals(Modifier.PUBLIC + Modifier.INTERFACE + Modifier.STATIC 373 + Modifier.ABSTRACT, PublicStaticInterface.class.getModifiers()); 374 assertEquals(Modifier.PROTECTED + Modifier.INTERFACE + Modifier.STATIC 375 + Modifier.ABSTRACT, ProtectedStaticInterface.class 376 .getModifiers()); 377 assertEquals(Modifier.PRIVATE + Modifier.INTERFACE + Modifier.STATIC 378 + Modifier.ABSTRACT, PrivateStaticInterface.class 379 .getModifiers()); 380 381 assertEquals(Modifier.INTERFACE + Modifier.STATIC + Modifier.ABSTRACT, 382 StaticAbstractInterface.class.getModifiers()); 383 assertEquals(Modifier.PUBLIC + Modifier.INTERFACE + Modifier.STATIC 384 + Modifier.ABSTRACT, PublicStaticAbstractInterface.class 385 .getModifiers()); 386 assertEquals(Modifier.PROTECTED + Modifier.INTERFACE + Modifier.STATIC 387 + Modifier.ABSTRACT, ProtectedStaticAbstractInterface.class 388 .getModifiers()); 389 assertEquals(Modifier.PRIVATE + Modifier.INTERFACE + Modifier.STATIC 390 + Modifier.ABSTRACT, PrivateStaticAbstractInterface.class 391 .getModifiers()); 392 } 393 394 static abstract class MethodClass { 395 396 public abstract void publicAbstractMethod(); 397 398 public static void publicStaticMethod() { 399 } 400 401 public final void publicFinalMethod() { 402 } 403 404 public static final void publicStaticFinalMethod() { 405 } 406 } 407 408 public void test_Method_Modifier() throws Exception { 409 assertEquals(Modifier.PUBLIC + Modifier.ABSTRACT, MethodClass.class 410 .getMethod("publicAbstractMethod", new Class[0]).getModifiers()); 411 assertEquals(Modifier.PUBLIC + Modifier.STATIC, MethodClass.class 412 .getMethod("publicStaticMethod", new Class[0]).getModifiers()); 413 414 assertEquals(Modifier.PUBLIC + Modifier.FINAL, MethodClass.class 415 .getMethod("publicFinalMethod", new Class[0]).getModifiers()); 416 417 assertEquals(Modifier.PUBLIC + Modifier.STATIC + Modifier.FINAL, 418 MethodClass.class.getMethod("publicStaticFinalMethod", 419 new Class[0]).getModifiers()); 420 } 421 422 /** 423 * Sets up the fixture, for example, open a network connection. This method 424 * is called before a test is executed. 425 */ 426 protected void setUp() { 427 } 428 429 /** 430 * Tears down the fixture, for example, close a network connection. This 431 * method is called after a test is executed. 432 */ 433 protected void tearDown() { 434 } 435 } 436