48
48
public class TestCheckEventsPreParse {
50
private static final String REL_PATH_TO_CHECKEVENT_BAD_COUNTER = "resources/test_check-event_invalid_counter.xml";
51
private static final String REL_PATH_TO_CHECKEVENT_BAD_UMASK = "resources/test_check-event_invalid_umask.xml";
52
private static final String REL_PATH_TO_CHECKEVENT_OK = "resources/test_check-event_ok.xml";
53
private static final String REL_PATH_TO_INFO_PRE_PARSE_RAW = "resources/test_info_pre_parse_raw.xml";
55
// the values are checked for validity in the order they
56
// appear here (ctr, event, umask)
59
private CheckEventAdapter cea;
62
* Set the counter, existing event and its default unit mask.
66
String devOprofileAbsFilePath = null;
67
Path devOprofilePath = new Path("resources/dev/oprofile/");
68
URL devOprofileURL = FileLocator.find(FrameworkUtil.getBundle(this.getClass()), devOprofilePath , null);
70
devOprofileAbsFilePath = FileLocator.toFileURL(devOprofileURL).getFile();
71
} catch (IOException e) {
72
fail("Failed to convert the resource file's path.");
74
InfoAdapter.setOprofileDir(devOprofileAbsFilePath);
76
File devFile = new File(InfoAdapter.DEV_OPROFILE + "0");
77
if (devFile.exists()){
80
File cpuFile = new File(InfoAdapter.CPUTYPE);
82
BufferedReader bi = null;
83
BufferedReader eventReader = null;
85
bi = new BufferedReader(new FileReader(cpuFile));
86
String cpuType = bi.readLine();
87
File opArchEvents = new File(InfoAdapter.OP_SHARE + cpuType + "/" + InfoAdapter.EVENTS);
88
File opArchUnitMasks = new File(InfoAdapter.OP_SHARE + cpuType + "/" + InfoAdapter.UNIT_MASKS);
90
eventReader = new BufferedReader(new FileReader(opArchEvents));
92
while ((line = eventReader.readLine()) != null){
93
// find the first event and use it
94
if (line.contains("name:")){
95
int start = line.indexOf("name:") + 5;
96
int end = line.indexOf(" ", start);
98
// get the string that references the unit mask type
99
start = line.indexOf("um:") + 3;
100
end = line.indexOf(" ", start);
101
String um = line.substring(start, end);
103
BufferedReader unitMaskReader = null;
105
unitMaskReader = new BufferedReader(new FileReader(
107
while ((line = unitMaskReader.readLine()) != null) {
108
if (line.contains("name:" + um + " ")) {
109
start = line.indexOf("default:") + 8;
110
String unitMaskDef = line.substring(start);
111
// convert from hex. to dec.
112
unitMaskDef = unitMaskDef
113
.replaceFirst("0x", "");
114
umask = String.valueOf(Integer.parseInt(
120
if (unitMaskReader != null) {
121
unitMaskReader.close();
127
} catch (FileNotFoundException e) {
128
} catch (IOException e) {
133
} catch (IOException e) {
136
if (eventReader != null) {
139
} catch (IOException e) {
146
public void testBadCounter () {
148
assertValidity(REL_PATH_TO_CHECKEVENT_BAD_COUNTER);
151
public void testBadUnitMask (){
153
assertValidity(REL_PATH_TO_CHECKEVENT_BAD_UMASK);
156
public void testOk (){
157
assertValidity(REL_PATH_TO_CHECKEVENT_OK);
160
public void assertValidity (String path){
161
IFileStore fileStore = null;
162
String infoAbsFilePath = null;
164
Path infoFilePath = new Path(REL_PATH_TO_INFO_PRE_PARSE_RAW);
165
URL infoFileURL = FileLocator.find(FrameworkUtil.getBundle(this.getClass()), infoFilePath, null);
167
infoAbsFilePath = FileLocator.toFileURL(infoFileURL).getFile();
168
fileStore = EFS.getLocalFileSystem().getStore(new Path(infoAbsFilePath));
169
} catch (IOException e) {
170
fail("Failed to convert the resource file's path.");
173
InfoAdapter ia = new InfoAdapter(fileStore);
176
cea = new CheckEventAdapter(ctr, "CPU_CLK_UNHALTED", umask);
178
Document actualDocument = cea.getDocument();
179
Element actualRoot = (Element) actualDocument.getElementsByTagName(CheckEventAdapter.CHECK_EVENTS).item(0);
181
Path filePath = new Path(path);
182
URL fileURL = FileLocator.find(FrameworkUtil.getBundle(this.getClass()), filePath, null);
183
Element expectedRoot = null;
185
String absFilePath = FileLocator.toFileURL(fileURL).getFile();
186
File file = new File (absFilePath);
187
FileInputStream inp = new FileInputStream(file);
188
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
189
DocumentBuilder builder;
190
builder = factory.newDocumentBuilder();
191
Document expectedDocument = builder.parse(inp);
192
expectedRoot = (Element) expectedDocument.getElementsByTagName(CheckEventAdapter.CHECK_EVENTS).item(0);
194
} catch (FileNotFoundException e) {
195
fail("File was not found.");
196
} catch (IOException e) {
197
fail("Failed to convert the resource file's path.");
198
} catch (SAXException e) {
199
fail("Failed to parse the XML.");
200
} catch (ParserConfigurationException e) {
201
fail("Failed to create a document builder.");
204
Element expectedResultTag = (Element) expectedRoot.getElementsByTagName(CheckEventAdapter.RESULT).item(0);
205
Element actualResultTag = (Element) actualRoot.getElementsByTagName(CheckEventAdapter.RESULT).item(0);
206
assertEquals(expectedResultTag.getTextContent(), actualResultTag.getTextContent());
50
private static final String REL_PATH_TO_CHECKEVENT_BAD_COUNTER = "resources/test_check-event_invalid_counter.xml";
51
private static final String REL_PATH_TO_CHECKEVENT_BAD_UMASK = "resources/test_check-event_invalid_umask.xml";
52
private static final String REL_PATH_TO_CHECKEVENT_OK = "resources/test_check-event_ok.xml";
53
private static final String REL_PATH_TO_INFO_PRE_PARSE_RAW = "resources/test_info_pre_parse_raw.xml";
55
// the values are checked for validity in the order they
56
// appear here (ctr, event, umask)
59
private CheckEventAdapter cea;
62
* Set the counter, existing event and its default unit mask.
66
String devOprofileAbsFilePath = null;
67
Path devOprofilePath = new Path("resources/dev/oprofile/");
68
URL devOprofileURL = FileLocator.find(FrameworkUtil.getBundle(this.getClass()), devOprofilePath , null);
70
devOprofileAbsFilePath = FileLocator.toFileURL(devOprofileURL).getFile();
71
} catch (IOException e) {
72
fail("Failed to convert the resource file's path.");
74
InfoAdapter.setOprofileDir(devOprofileAbsFilePath);
76
File devFile = new File(InfoAdapter.DEV_OPROFILE + "0");
77
if (devFile.exists()){
80
File cpuFile = new File(InfoAdapter.CPUTYPE);
82
try (BufferedReader bi = new BufferedReader(new FileReader(cpuFile))) {
83
String cpuType = bi.readLine();
84
File opArchEvents = new File(InfoAdapter.OP_SHARE + cpuType + "/"
85
+ InfoAdapter.EVENTS);
86
File opArchUnitMasks = new File(InfoAdapter.OP_SHARE + cpuType
87
+ "/" + InfoAdapter.UNIT_MASKS);
89
try (BufferedReader eventReader = new BufferedReader(
90
new FileReader(opArchEvents))) {
92
while ((line = eventReader.readLine()) != null) {
93
// find the first event and use it
94
if (line.contains("name:")) {
95
int start = line.indexOf("name:") + 5;
96
int end = line.indexOf(" ", start);
98
// get the string that references the unit mask type
99
start = line.indexOf("um:") + 3;
100
end = line.indexOf(" ", start);
101
String um = line.substring(start, end);
103
try (BufferedReader unitMaskReader = new BufferedReader(
104
new FileReader(opArchUnitMasks))) {
105
while ((line = unitMaskReader.readLine()) != null) {
106
if (line.contains("name:" + um + " ")) {
107
start = line.indexOf("default:") + 8;
108
String unitMaskDef = line.substring(start);
109
// convert from hex. to dec.
110
unitMaskDef = unitMaskDef.replaceFirst(
112
umask = String.valueOf(Integer.parseInt(
122
} catch (IOException e) {
127
public void testBadCounter () {
129
assertValidity(REL_PATH_TO_CHECKEVENT_BAD_COUNTER);
132
public void testBadUnitMask (){
134
assertValidity(REL_PATH_TO_CHECKEVENT_BAD_UMASK);
137
public void testOk (){
138
assertValidity(REL_PATH_TO_CHECKEVENT_OK);
141
public void assertValidity (String path){
142
IFileStore fileStore = null;
143
String infoAbsFilePath = null;
145
Path infoFilePath = new Path(REL_PATH_TO_INFO_PRE_PARSE_RAW);
146
URL infoFileURL = FileLocator.find(FrameworkUtil.getBundle(this.getClass()), infoFilePath, null);
148
infoAbsFilePath = FileLocator.toFileURL(infoFileURL).getFile();
149
fileStore = EFS.getLocalFileSystem().getStore(new Path(infoAbsFilePath));
150
} catch (IOException e) {
151
fail("Failed to convert the resource file's path.");
154
InfoAdapter ia = new InfoAdapter(fileStore);
157
cea = new CheckEventAdapter(ctr, "CPU_CLK_UNHALTED", umask);
159
Document actualDocument = cea.getDocument();
160
Element actualRoot = (Element) actualDocument.getElementsByTagName(CheckEventAdapter.CHECK_EVENTS).item(0);
162
Path filePath = new Path(path);
163
URL fileURL = FileLocator.find(FrameworkUtil.getBundle(this.getClass()), filePath, null);
164
Element expectedRoot = null;
166
String absFilePath = FileLocator.toFileURL(fileURL).getFile();
167
File file = new File (absFilePath);
168
FileInputStream inp = new FileInputStream(file);
169
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
170
DocumentBuilder builder;
171
builder = factory.newDocumentBuilder();
172
Document expectedDocument = builder.parse(inp);
173
expectedRoot = (Element) expectedDocument.getElementsByTagName(CheckEventAdapter.CHECK_EVENTS).item(0);
175
} catch (FileNotFoundException e) {
176
fail("File was not found.");
177
} catch (IOException e) {
178
fail("Failed to convert the resource file's path.");
179
} catch (SAXException e) {
180
fail("Failed to parse the XML.");
181
} catch (ParserConfigurationException e) {
182
fail("Failed to create a document builder.");
185
Element expectedResultTag = (Element) expectedRoot.getElementsByTagName(CheckEventAdapter.RESULT).item(0);
186
Element actualResultTag = (Element) actualRoot.getElementsByTagName(CheckEventAdapter.RESULT).item(0);
187
assertEquals(expectedResultTag.getTextContent(), actualResultTag.getTextContent());