KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > javax > management > PermissionTestCase


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package test.javax.management;
10
11 import java.security.Permission JavaDoc;
12 import java.security.PermissionCollection JavaDoc;
13
14 import test.MX4JTestCase;
15
16 /**
17  * @version $Revision: 1.3 $
18  */

19 public class PermissionTestCase extends MX4JTestCase
20 {
21    public PermissionTestCase(String JavaDoc s)
22    {
23       super(s);
24    }
25
26    protected void shouldBeEqual(Permission JavaDoc p1, Permission JavaDoc p2)
27    {
28       if (!p1.equals(p2)) fail("Permission " + p1 + " should be equal to Permission " + p2);
29       if (p1.hashCode() != p2.hashCode()) fail("Permission " + p1 + " should have hashCode equal to Permission " + p2);
30    }
31
32    protected void shouldImply(Permission JavaDoc p1, Permission JavaDoc p2)
33    {
34       if (p1.equals(p2))
35       {
36          // Test identity
37
if (!imply(p1, p2)) fail("Permission " + p1 + " should imply Permission " + p2);
38          if (!imply(p2, p1)) fail("Permission " + p2 + " should imply Permission " + p1);
39       }
40       else
41       {
42          // Test antisymmetry
43
if (!imply(p1, p2)) fail("Permission " + p1 + " should imply Permission " + p2);
44          if (imply(p2, p1)) fail("Permission " + p2 + " should not imply Permission " + p1);
45       }
46    }
47
48    protected void shouldNotImply(Permission JavaDoc p1, Permission JavaDoc p2)
49    {
50       if (p1.equals(p2)) fail("Permissions cannot be equal");
51       if (imply(p1, p2)) fail("Permission " + p1 + " should not imply Permission " + p2);
52       if (imply(p2, p1)) fail("Permission " + p2 + " should not imply Permission " + p1);
53    }
54
55    protected boolean imply(Permission JavaDoc p1, Permission JavaDoc p2)
56    {
57       PermissionCollection JavaDoc pc = p1.newPermissionCollection();
58       if (pc == null)
59       {
60          // No PermissionCollection provided, go directly to the Permission
61
return p1.implies(p2);
62       }
63       else
64       {
65          return pc.implies(p2);
66       }
67    }
68 }
69
Popular Tags