Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0fc8a4c093949d453a66f4faf646a7d617d8d018 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package org.eclipse.linuxtools.oprofile.core.tests;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import junit.framework.TestCase;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.linuxtools.internal.oprofile.core.opxml.info.InfoAdapter;
import org.osgi.framework.FrameworkUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * Test cases for checking validity of the info parsed from oprofile that is
 * modified to mimic the XML format expected by the SAX parser.
 * The oprofile module must be loaded and the driver interface must be
 * available. ie. run opcontrol --init
 */
public class TestInfoPreParse extends TestCase {
	
	private static final String REL_PATH_TO_INFO_PRE_PARSE_RAW = "resources/test_info_pre_parse_raw.xml";
	private static final String REL_PATH_TO_INFO_PRE_PARSE_EXEPECTED = "resources/test_info_pre_parse_expected.xml";
	Element [] rootList;
	ArrayList<ArrayList<String>> valueList;
	
	public TestInfoPreParse() {
		super("test info pre parsers");
	}
	
	@Override
	protected void setUp() {
		File file = null;
		String absFilePath = null;
		
		Path filePath = new Path(REL_PATH_TO_INFO_PRE_PARSE_RAW);
		URL fileURL = FileLocator.find(FrameworkUtil.getBundle(this.getClass()), filePath, null);
		try {
			absFilePath = FileLocator.toFileURL(fileURL).getFile();
			file = new File (absFilePath);
		} catch (IOException e) {
			fail("Failed to convert the resource file's path.");
		}
		InfoAdapter ia = new InfoAdapter(file);
		ia.process();
		Document actualDocument = ia.getDocument();
		Element actualRoot = (Element) actualDocument.getElementsByTagName(InfoAdapter.INFO).item(0);
		
		filePath = new Path(REL_PATH_TO_INFO_PRE_PARSE_EXEPECTED);
		fileURL = FileLocator.find(FrameworkUtil.getBundle(this.getClass()), filePath, null);
		Element expectedRoot = null;
		try {
			absFilePath = FileLocator.toFileURL(fileURL).getFile();
			file = new File (absFilePath);
			FileInputStream inp = new FileInputStream(file);
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder;
			builder = factory.newDocumentBuilder();
			Document expectedDocument = builder.parse(inp);
			expectedRoot = (Element) expectedDocument.getElementsByTagName(InfoAdapter.INFO).item(0);
			
		} catch (FileNotFoundException e) {
			fail("File was not found.");
		} catch (IOException e) {
			fail("Failed to convert the resource file's path.");
		} catch (SAXException e) {
			fail("Failed to parse the XML.");
		} catch (ParserConfigurationException e) {
			fail("Failed to create a document builder.");
		}
		
		rootList = new Element [] {expectedRoot, actualRoot};
		valueList = new ArrayList<ArrayList<String>> ();
		
		for (int i = 0; i < rootList.length; i++){
			valueList.add(new ArrayList<String>());
		}
	}
	
	public void testBasicConfig (){
		final String [] tags = new String [] {InfoAdapter.NUM_COUNTERS, InfoAdapter.TIMER_MODE};
		final String [] defTags = new String [] {InfoAdapter.SAMPLE_DIR, InfoAdapter.LOCK_FILE, InfoAdapter.LOG_FILE, InfoAdapter.DUMP_STATUS};

		
		// compare num-counters and timer-mode
		for (int i = 0; i < rootList.length; i++){
			for (int j = 0; j < tags.length; j++){
				Element elm = (Element) rootList[i].getElementsByTagName(tags[j]).item(0);
				valueList.get(i).add(elm.getTextContent());
			}
		}
		assertSameValues(valueList);
		clearValues(valueList);
		
		// compare defaults
		for (int i = 0; i < rootList.length; i++){
			Element defTag = (Element) rootList[i].getElementsByTagName(InfoAdapter.DEFAULTS).item(0);
			for (int j = 0; j < defTags.length; j++){
				Element elm = (Element) defTag.getElementsByTagName(defTags[j]).item(0);
				valueList.get(i).add(elm.getTextContent());
			}
		}
		assertSameValues(valueList);
		clearValues(valueList);
	}
	
	public void testEventData (){
		final String [] eventTags = new String [] {InfoAdapter.NAME, InfoAdapter.DESCRIPTION, InfoAdapter.MINIMUM};
		final String [] unitMaskTags = new String [] {InfoAdapter.TYPE, InfoAdapter.DEFAULT};
		final String [] maskTags = new String [] {InfoAdapter.VALUE};
		
		// compare the event data
		for (int i = 0; i < rootList.length; i++){
			Element eventListTag = (Element) rootList[i].getElementsByTagName(InfoAdapter.EVENT_LIST).item(0);
			NodeList eventTagList = eventListTag.getElementsByTagName(InfoAdapter.EVENT);
			for (int j = 0; j < eventTagList.getLength(); j++){
				//name description value minimum
				Element event = (Element) eventTagList.item(j);
				for (int k = 0; k < eventTags.length; k++){
					Element elm = (Element) (event.getElementsByTagName(eventTags[k]).item(0));
					valueList.get(i).add(elm.getTextContent());
				}
				
				//type default
				Element unitMaskTag = (Element) event.getElementsByTagName(InfoAdapter.UNITMASK).item(0);
				for (int k = 0; k < unitMaskTags.length; k++){
					Element elem = (Element) unitMaskTag.getElementsByTagName(unitMaskTags[k]).item(0);
					valueList.get(i).add(elem.getTextContent());
				}
				
				// value description
				// description is omitted (whitespace differences cause failure)
				NodeList maskTagList = unitMaskTag.getElementsByTagName(InfoAdapter.MASK);
				for (int k = 0; k < maskTagList.getLength(); k++){
					Element mask = (Element) maskTagList.item(k);
					for (int n = 0; n < maskTags.length; n++){
						Element maskVal = (Element) mask.getElementsByTagName(maskTags[n]).item(0);
						valueList.get(i).add(maskVal.getTextContent());
					}
				}
			}
		}
		assertSameValues(valueList);
		clearValues(valueList);
	}

	private void clearValues(ArrayList<ArrayList<String>> valueList) {
		for (int i = 0; i < valueList.size(); i++){
			valueList.get(i).clear();
		}
	}

	private void assertSameValues(ArrayList<ArrayList<String>> valueList) {
		for (int i = 0; i < valueList.get(0).size(); i++){
			System.out.println(valueList.get(0).get(i) +" "+ valueList.get(1).get(i));
			assertEquals(valueList.get(0).get(i), valueList.get(1).get(i));
		}
	}
}

Back to the top