Added
Link Here
|
1 |
// This file is part of the program FRYSK. |
2 |
// |
3 |
// Copyright 2006, Red Hat Inc. |
4 |
// |
5 |
// FRYSK is free software; you can redistribute it and/or modify it |
6 |
// under the terms of the GNU General Public License as published by |
7 |
// the Free Software Foundation; version 2 of the License. |
8 |
// |
9 |
// FRYSK is distributed in the hope that it will be useful, but |
10 |
// WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 |
// General Public License for more details. |
13 |
// |
14 |
// You should have received a copy of the GNU General Public License |
15 |
// along with FRYSK; if not, write to the Free Software Foundation, |
16 |
// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
17 |
// |
18 |
// In addition, as a special exception, Red Hat, Inc. gives You the |
19 |
// additional right to link the code of FRYSK with code not covered |
20 |
// under the GNU General Public License ("Non-GPL Code") and to |
21 |
// distribute linked combinations including the two, subject to the |
22 |
// limitations in this paragraph. Non-GPL Code permitted under this |
23 |
// exception must only link to the code of FRYSK through those well |
24 |
// defined interfaces identified in the file named EXCEPTION found in |
25 |
// the source code files (the "Approved Interfaces"). The files of |
26 |
// Non-GPL Code may instantiate templates or use macros or inline |
27 |
// functions from the Approved Interfaces without causing the |
28 |
// resulting work to be covered by the GNU General Public |
29 |
// License. Only Red Hat, Inc. may make changes or additions to the |
30 |
// list of Approved Interfaces. You must obey the GNU General Public |
31 |
// License in all respects for all of the FRYSK code and other code |
32 |
// used in conjunction with FRYSK except the Non-GPL Code covered by |
33 |
// this exception. If you modify this file, you may extend this |
34 |
// exception to your version of the file, but you are not obligated to |
35 |
// do so. If you do not wish to provide this exception without |
36 |
// modification, you must delete this exception statement from your |
37 |
// version and license this file solely under the GPL without |
38 |
// exception. |
39 |
|
40 |
|
41 |
package frysk.proc; |
42 |
|
43 |
import java.util.Observable; |
44 |
import java.util.Observer; |
45 |
import java.util.logging.Level; |
46 |
|
47 |
import frysk.event.RequestStopEvent; |
48 |
|
49 |
public class TestGetProc |
50 |
extends TestLib |
51 |
{ |
52 |
class ProcCounter |
53 |
implements Observer |
54 |
{ |
55 |
|
56 |
int count = 0; |
57 |
|
58 |
public void update (Observable o, Object arg) |
59 |
{ |
60 |
count++; |
61 |
} |
62 |
|
63 |
public int getCount () |
64 |
{ |
65 |
return count; |
66 |
} |
67 |
} |
68 |
|
69 |
class MyFinder |
70 |
implements Host.FindProc |
71 |
{ |
72 |
ProcId expectedId; |
73 |
|
74 |
boolean shouldParent; |
75 |
public MyFinder (ProcId pid, boolean shouldHaveParent) |
76 |
{ |
77 |
expectedId = pid; |
78 |
shouldParent = shouldHaveParent; |
79 |
} |
80 |
|
81 |
public void procFound (ProcId procId) |
82 |
{ |
83 |
Proc proc = Manager.host.getProc(procId); |
84 |
logger.log(Level.FINE, "proc: {0} proc parent: {1} \n", |
85 |
new Object[] { proc, proc.getParent() }); |
86 |
assertEquals(expectedId, procId); |
87 |
if (!shouldParent) |
88 |
assertNull("Should not have parent", proc.getParent()); |
89 |
else |
90 |
assertNotNull("Should have parent", proc.getParent()); |
91 |
|
92 |
Manager.eventLoop.add(new RequestStopEvent(Manager.eventLoop)); |
93 |
} |
94 |
|
95 |
public void procNotFound (ProcId procId, Exception e) |
96 |
{ |
97 |
logger.log(Level.FINE, "{0} procId\n", procId); |
98 |
fail("Could not find process with ID" + procId.id); |
99 |
} |
100 |
} |
101 |
|
102 |
private void doGetProc (AckProcess ackProc, int expectedCount, boolean shouldHaveParent) |
103 |
{ |
104 |
logger.log(Level.FINE, "doGetProc ackProc {0}\n", ackProc); |
105 |
ProcCounter o = new ProcCounter(); |
106 |
Manager.host.observableProcAddedXXX.addObserver(o); |
107 |
|
108 |
/* |
109 |
* This finds out how many processes are associated with the frysk process. |
110 |
* For example: init->gnome terminal->bash->frysk. |
111 |
*/ |
112 |
Manager.host.getSelf(); |
113 |
int preFind = o.getCount(); |
114 |
|
115 |
/* |
116 |
* Find out how many processes are associated with the test process. Should |
117 |
* be just the one. |
118 |
*/ |
119 |
Host.FindProc finder = new MyFinder(new ProcId(ackProc.getPid()), shouldHaveParent); |
120 |
Manager.host.requestGetProc(new ProcId(ackProc.getPid()), finder); |
121 |
assertRunUntilStop("testGetProc"); |
122 |
|
123 |
int postFind = o.getCount(); |
124 |
|
125 |
assertEquals(expectedCount, postFind - preFind); |
126 |
logger.log(Level.FINE, "doGetProc exiting"); |
127 |
} |
128 |
|
129 |
public void testGetAndRefreshFailed () |
130 |
{ |
131 |
|
132 |
Host.FindProc finder = new Host.FindProc() |
133 |
{ |
134 |
public void procFound (ProcId procId) |
135 |
{ |
136 |
logger.log(Level.FINE, "{0} procId\n", procId); |
137 |
fail("Found proc 0, should have failed."); |
138 |
} |
139 |
|
140 |
public void procNotFound (ProcId procId, Exception e) |
141 |
{ |
142 |
logger.log(Level.FINE, "{0} procId\n", procId); |
143 |
Manager.eventLoop.add(new RequestStopEvent(Manager.eventLoop)); |
144 |
|
145 |
} |
146 |
}; |
147 |
|
148 |
Manager.host.requestGetProc(new ProcId(0), finder); |
149 |
assertRunUntilStop("testFindFailed"); |
150 |
|
151 |
} |
152 |
|
153 |
public void testGetProcDetached () |
154 |
{ |
155 |
AckProcess ackProc = new DetachedAckProcess(); |
156 |
doGetProc(ackProc, 1, false); |
157 |
} |
158 |
|
159 |
public void testGetProcAttached () |
160 |
{ |
161 |
AckProcess ackProc = new AttachedAckProcess(); |
162 |
|
163 |
// expect no additional processes to be added to the procPool. |
164 |
doGetProc(ackProc, 0, true); |
165 |
} |
166 |
|
167 |
public void testGetProcAckDaemon () |
168 |
{ |
169 |
AckProcess ackProc = new AckDaemonProcess(); |
170 |
doGetProc(ackProc, 1, false); |
171 |
} |
172 |
|
173 |
} |