~ubuntu-branches/ubuntu/vivid/eclipse-linuxtools/vivid-proposed

« back to all changes in this revision

Viewing changes to oprofile/org.eclipse.linuxtools.oprofile.core.tests/src/org/eclipse/linuxtools/oprofile/core/tests/TestCheckEventsPreParse.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam, Jakub Adam, tony mancill
  • Date: 2014-10-11 11:44:05 UTC
  • mfrom: (1.2.4)
  • Revision ID: package-import@ubuntu.com-20141011114405-yazjvxfzzhmi5sgj
Tags: 3.1.0-1
[ Jakub Adam ]
* New upstream release (Closes: #761524).
* Refreshed d/patches.
* Don't build removed feature org.eclipse.linuxtools.tools.launch
  - merged into org.eclipse.linuxtools.profiling.
* Use javac target 1.7.
* Build new feature org.eclipse.linuxtools.dataviewers.feature
  - required by Valgrind integration.
* Build-depend on eclipse-remote-services-api and eclipse-cdt-autotools.
* Bump Standards-Version to 3.9.6.
* Override incompatible-java-bytecode-format - linuxtools needs Java 7.
* Remove unused codeless-jar override.

[ tony mancill ]
* Tweak short package description to make lintian happy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
47
47
 */
48
48
public class TestCheckEventsPreParse {
49
49
 
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";
54
 
 
55
 
        // the values are checked for validity in the order they
56
 
        // appear here (ctr, event, umask)
57
 
        private String ctr;
58
 
        private String umask;
59
 
        private CheckEventAdapter cea;
60
 
 
61
 
        /**
62
 
         * Set the counter, existing event and its default unit mask.
63
 
         */
64
 
        @Before
65
 
        public void setUp (){
66
 
                String devOprofileAbsFilePath = null;
67
 
                Path devOprofilePath = new Path("resources/dev/oprofile/");
68
 
                URL devOprofileURL = FileLocator.find(FrameworkUtil.getBundle(this.getClass()), devOprofilePath , null);
69
 
                try {
70
 
                        devOprofileAbsFilePath = FileLocator.toFileURL(devOprofileURL).getFile();
71
 
                } catch (IOException e) {
72
 
                        fail("Failed to convert the resource file's path.");
73
 
                }
74
 
                InfoAdapter.setOprofileDir(devOprofileAbsFilePath);
75
 
 
76
 
                File devFile = new File(InfoAdapter.DEV_OPROFILE + "0");
77
 
                if (devFile.exists()){
78
 
                        ctr = "0";
79
 
                }
80
 
                File cpuFile = new File(InfoAdapter.CPUTYPE);
81
 
 
82
 
                BufferedReader bi = null;
83
 
                BufferedReader eventReader = null;
84
 
                try {
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);
89
 
 
90
 
                        eventReader = new BufferedReader(new FileReader(opArchEvents));
91
 
                        String line;
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);
97
 
 
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);
102
 
 
103
 
                                        BufferedReader unitMaskReader = null;
104
 
                                        try {
105
 
                                                unitMaskReader = new BufferedReader(new FileReader(
106
 
                                                                opArchUnitMasks));
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(
115
 
                                                                                unitMaskDef, 16));
116
 
                                                                break;
117
 
                                                        }
118
 
                                                }
119
 
                                        } finally {
120
 
                                                if (unitMaskReader != null) {
121
 
                                                        unitMaskReader.close();
122
 
                                                }
123
 
                                        }
124
 
                                        break;
125
 
                                }
126
 
                        }
127
 
                } catch (FileNotFoundException e) {
128
 
                } catch (IOException e) {
129
 
                } finally {
130
 
                        if (bi != null) {
131
 
                                try {
132
 
                                        bi.close();
133
 
                                } catch (IOException e) {
134
 
                                }
135
 
                        }
136
 
                        if (eventReader != null) {
137
 
                                try {
138
 
                                        eventReader.close();
139
 
                                } catch (IOException e) {
140
 
                                }
141
 
                        }
142
 
                }
143
 
        }
144
 
 
145
 
        @Test
146
 
        public void testBadCounter () {
147
 
                ctr = "999";
148
 
                assertValidity(REL_PATH_TO_CHECKEVENT_BAD_COUNTER);
149
 
        }
150
 
        @Test
151
 
        public void testBadUnitMask (){
152
 
                umask = "999";
153
 
                assertValidity(REL_PATH_TO_CHECKEVENT_BAD_UMASK);
154
 
        }
155
 
        @Test @Ignore
156
 
        public void testOk (){
157
 
                assertValidity(REL_PATH_TO_CHECKEVENT_OK);
158
 
        }
159
 
 
160
 
        public void assertValidity (String path){
161
 
                IFileStore fileStore = null;
162
 
                String infoAbsFilePath = null;
163
 
 
164
 
                Path infoFilePath = new Path(REL_PATH_TO_INFO_PRE_PARSE_RAW);
165
 
                URL infoFileURL = FileLocator.find(FrameworkUtil.getBundle(this.getClass()), infoFilePath, null);
166
 
                try {
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.");
171
 
                }
172
 
 
173
 
                InfoAdapter ia = new InfoAdapter(fileStore);
174
 
                ia.process();
175
 
 
176
 
                cea = new CheckEventAdapter(ctr, "CPU_CLK_UNHALTED", umask);
177
 
                cea.process();
178
 
                Document actualDocument = cea.getDocument();
179
 
                Element actualRoot = (Element) actualDocument.getElementsByTagName(CheckEventAdapter.CHECK_EVENTS).item(0);
180
 
 
181
 
                Path filePath = new Path(path);
182
 
                URL fileURL = FileLocator.find(FrameworkUtil.getBundle(this.getClass()), filePath, null);
183
 
                Element expectedRoot = null;
184
 
                try {
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);
193
 
 
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.");
202
 
                }
203
 
 
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());
207
 
        }
 
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";
 
54
 
 
55
    // the values are checked for validity in the order they
 
56
    // appear here (ctr, event, umask)
 
57
    private String ctr;
 
58
    private String umask;
 
59
    private CheckEventAdapter cea;
 
60
 
 
61
    /**
 
62
     * Set the counter, existing event and its default unit mask.
 
63
     */
 
64
    @Before
 
65
    public void setUp (){
 
66
        String devOprofileAbsFilePath = null;
 
67
        Path devOprofilePath = new Path("resources/dev/oprofile/");
 
68
        URL devOprofileURL = FileLocator.find(FrameworkUtil.getBundle(this.getClass()), devOprofilePath , null);
 
69
        try {
 
70
            devOprofileAbsFilePath = FileLocator.toFileURL(devOprofileURL).getFile();
 
71
        } catch (IOException e) {
 
72
            fail("Failed to convert the resource file's path.");
 
73
        }
 
74
        InfoAdapter.setOprofileDir(devOprofileAbsFilePath);
 
75
 
 
76
        File devFile = new File(InfoAdapter.DEV_OPROFILE + "0");
 
77
        if (devFile.exists()){
 
78
            ctr = "0";
 
79
        }
 
80
        File cpuFile = new File(InfoAdapter.CPUTYPE);
 
81
 
 
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);
 
88
 
 
89
            try (BufferedReader eventReader = new BufferedReader(
 
90
                    new FileReader(opArchEvents))) {
 
91
                String line;
 
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);
 
97
 
 
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);
 
102
 
 
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(
 
111
                                            "0x", "");
 
112
                                    umask = String.valueOf(Integer.parseInt(
 
113
                                            unitMaskDef, 16));
 
114
                                    break;
 
115
                                }
 
116
                            }
 
117
                        }
 
118
                        break;
 
119
                    }
 
120
                }
 
121
            }
 
122
        } catch (IOException e) {
 
123
        }
 
124
    }
 
125
 
 
126
    @Test
 
127
    public void testBadCounter () {
 
128
        ctr = "999";
 
129
        assertValidity(REL_PATH_TO_CHECKEVENT_BAD_COUNTER);
 
130
    }
 
131
    @Test
 
132
    public void testBadUnitMask (){
 
133
        umask = "999";
 
134
        assertValidity(REL_PATH_TO_CHECKEVENT_BAD_UMASK);
 
135
    }
 
136
    @Test @Ignore
 
137
    public void testOk (){
 
138
        assertValidity(REL_PATH_TO_CHECKEVENT_OK);
 
139
    }
 
140
 
 
141
    public void assertValidity (String path){
 
142
        IFileStore fileStore = null;
 
143
        String infoAbsFilePath = null;
 
144
 
 
145
        Path infoFilePath = new Path(REL_PATH_TO_INFO_PRE_PARSE_RAW);
 
146
        URL infoFileURL = FileLocator.find(FrameworkUtil.getBundle(this.getClass()), infoFilePath, null);
 
147
        try {
 
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.");
 
152
        }
 
153
 
 
154
        InfoAdapter ia = new InfoAdapter(fileStore);
 
155
        ia.process();
 
156
 
 
157
        cea = new CheckEventAdapter(ctr, "CPU_CLK_UNHALTED", umask);
 
158
        cea.process();
 
159
        Document actualDocument = cea.getDocument();
 
160
        Element actualRoot = (Element) actualDocument.getElementsByTagName(CheckEventAdapter.CHECK_EVENTS).item(0);
 
161
 
 
162
        Path filePath = new Path(path);
 
163
        URL fileURL = FileLocator.find(FrameworkUtil.getBundle(this.getClass()), filePath, null);
 
164
        Element expectedRoot = null;
 
165
        try {
 
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);
 
174
 
 
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.");
 
183
        }
 
184
 
 
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());
 
188
    }
208
189
}