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 tests.api.java.util; 19 20 import dalvik.annotation.TestTargetNew; 21 import dalvik.annotation.TestTargets; 22 import dalvik.annotation.TestLevel; 23 import dalvik.annotation.TestTargetClass; 24 25 import java.io.Serializable; 26 import java.util.Enumeration; 27 import java.util.PropertyPermission; 28 29 import org.apache.harmony.testframework.serialization.SerializationTest; 30 import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert; 31 32 @TestTargetClass(PropertyPermission.class) 33 public class PropertyPermissionTest extends junit.framework.TestCase { 34 35 static PropertyPermission javaPP = new PropertyPermission("java.*", "read"); 36 37 static PropertyPermission userPP = new PropertyPermission("user.name", 38 "read,write"); 39 40 /** 41 * @tests java.util.PropertyPermission#PropertyPermission(java.lang.String, 42 * java.lang.String) 43 */ 44 @TestTargetNew( 45 level = TestLevel.COMPLETE, 46 notes = "", 47 method = "PropertyPermission", 48 args = {java.lang.String.class, java.lang.String.class} 49 ) test_ConstructorLjava_lang_StringLjava_lang_String()50 public void test_ConstructorLjava_lang_StringLjava_lang_String() { 51 // Test for method java.util.PropertyPermission(java.lang.String, 52 // java.lang.String) 53 assertTrue("Used to test", true); 54 } 55 56 /** 57 * @tests java.util.PropertyPermission#equals(java.lang.Object) 58 */ 59 @TestTargetNew( 60 level = TestLevel.COMPLETE, 61 notes = "", 62 method = "equals", 63 args = {java.lang.Object.class} 64 ) test_equalsLjava_lang_Object()65 public void test_equalsLjava_lang_Object() { 66 // Test for method boolean 67 // java.util.PropertyPermission.equals(java.lang.Object) 68 PropertyPermission equalToJavaPP = new PropertyPermission("java.*", 69 "read"); 70 PropertyPermission notEqualToJavaPP = new PropertyPermission("java.*", 71 "read, write"); 72 PropertyPermission alsoNotEqualToJavaPP = new PropertyPermission( 73 "java.home", "read"); 74 75 assertTrue("Equal returned false for equal objects", javaPP 76 .equals(equalToJavaPP)); 77 assertTrue("Equal returned true for objects with different names", 78 !javaPP.equals(notEqualToJavaPP)); 79 assertTrue( 80 "Equal returned true for objects with different permissions", 81 !javaPP.equals(alsoNotEqualToJavaPP)); 82 } 83 84 /** 85 * @tests java.util.PropertyPermission#getActions() 86 */ 87 @TestTargetNew( 88 level = TestLevel.COMPLETE, 89 notes = "", 90 method = "getActions", 91 args = {} 92 ) test_getActions()93 public void test_getActions() { 94 // Test for method java.lang.String 95 // java.util.PropertyPermission.getActions() 96 assertEquals("getActions did not return proper action", "read", javaPP 97 .getActions()); 98 assertEquals("getActions did not return proper canonical representation of actions", 99 "read,write", userPP.getActions()); 100 } 101 102 /** 103 * @tests java.util.PropertyPermission#hashCode() 104 */ 105 @TestTargetNew( 106 level = TestLevel.COMPLETE, 107 notes = "", 108 method = "hashCode", 109 args = {} 110 ) test_hashCode()111 public void test_hashCode() { 112 // Test for method int java.util.PropertyPermission.hashCode() 113 assertTrue("javaPP returned wrong hashCode", 114 javaPP.hashCode() == javaPP.getName().hashCode()); 115 assertTrue("userPP returned wrong hashCode", 116 userPP.hashCode() == userPP.getName().hashCode()); 117 } 118 119 /** 120 * @tests java.util.PropertyPermission#implies(java.security.Permission) 121 */ 122 @TestTargetNew( 123 level = TestLevel.COMPLETE, 124 notes = "", 125 method = "implies", 126 args = {java.security.Permission.class} 127 ) test_impliesLjava_security_Permission()128 public void test_impliesLjava_security_Permission() { 129 // Test for method boolean 130 // java.util.PropertyPermission.implies(java.security.Permission) 131 PropertyPermission impliedByJavaPP = new PropertyPermission( 132 "java.home", "read"); 133 PropertyPermission notImpliedByJavaPP = new PropertyPermission( 134 "java.home", "read,write"); 135 PropertyPermission impliedByUserPP = new PropertyPermission( 136 "user.name", "read,write"); 137 PropertyPermission alsoImpliedByUserPP = new PropertyPermission( 138 "user.name", "write"); 139 assertTrue("Returned false for implied permission (subset of .*)", 140 javaPP.implies(impliedByJavaPP)); 141 assertTrue("Returned true for unimplied permission", !javaPP 142 .implies(notImpliedByJavaPP)); 143 assertTrue("Returned false for implied permission (equal)", userPP 144 .implies(impliedByUserPP)); 145 assertTrue("Returned false for implied permission (subset of actions)", 146 userPP.implies(alsoImpliedByUserPP)); 147 } 148 149 /** 150 * @tests java.util.PropertyPermission#newPermissionCollection() 151 */ 152 @TestTargetNew( 153 level = TestLevel.COMPLETE, 154 notes = "", 155 method = "newPermissionCollection", 156 args = {} 157 ) test_newPermissionCollection()158 public void test_newPermissionCollection() { 159 // Test for method java.security.PermissionCollection 160 // java.util.PropertyPermission.newPermissionCollection() 161 java.security.PermissionCollection pc = javaPP 162 .newPermissionCollection(); 163 pc.add(javaPP); 164 Enumeration elementEnum = pc.elements(); 165 assertTrue("Invalid PermissionCollection returned", elementEnum 166 .nextElement().equals(javaPP)); 167 } 168 169 /** 170 * @tests java.util.PropertyPermission#readObject(ObjectInputStream) 171 * @tests java.util.PropertyPermission#writeObject(ObjectOutputStream) 172 */ 173 @TestTargets({ 174 @TestTargetNew( 175 level = TestLevel.COMPLETE, 176 notes = "Verifies serialization/deserialization.", 177 method = "!SerializationSelf", 178 args = {} 179 ), 180 @TestTargetNew( 181 level = TestLevel.COMPLETE, 182 notes = "Verifies serialization/deserialization.", 183 method = "!SerializationGolden", 184 args = {} 185 ) 186 }) test_serialization()187 public void test_serialization() throws Exception{ 188 PropertyPermission pp = new PropertyPermission("test", "read"); 189 SerializationTest.verifySelf(pp, comparator); 190 SerializationTest.verifyGolden(this, pp, comparator); 191 } 192 193 /** 194 * Sets up the fixture, for example, open a network connection. This method 195 * is called before a test is executed. 196 */ setUp()197 protected void setUp() { 198 } 199 200 /** 201 * Tears down the fixture, for example, close a network connection. This 202 * method is called after a test is executed. 203 */ tearDown()204 protected void tearDown() { 205 } 206 207 private static final SerializableAssert comparator = new SerializableAssert() { 208 209 public void assertDeserialized(Serializable initial, Serializable deserialized) { 210 PropertyPermission initialPP = (PropertyPermission) initial; 211 PropertyPermission deseriaPP = (PropertyPermission) deserialized; 212 assertEquals("should be equal", initialPP, deseriaPP); 213 } 214 215 }; 216 } 217