tmf : Add test suite for the pattern segment builder
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.analysis.xml.core.tests / src / org / eclipse / tracecompass / tmf / analysis / xml / core / tests / stateprovider / TmfXmlPatternSegmentBuilderTest.java
1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 ******************************************************************************/
9 package org.eclipse.tracecompass.tmf.analysis.xml.core.tests.stateprovider;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.fail;
14
15 import java.io.File;
16
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.eclipse.tracecompass.tmf.analysis.xml.core.model.ITmfXmlModelFactory;
20 import org.eclipse.tracecompass.tmf.analysis.xml.core.model.TmfXmlPatternSegmentBuilder;
21 import org.eclipse.tracecompass.tmf.analysis.xml.core.model.readwrite.TmfXmlReadWriteModelFactory;
22 import org.eclipse.tracecompass.tmf.analysis.xml.core.module.XmlUtils;
23 import org.eclipse.tracecompass.tmf.analysis.xml.core.segment.TmfXmlPatternSegment;
24 import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
25 import org.eclipse.tracecompass.tmf.analysis.xml.core.tests.common.TmfXmlTestFiles;
26 import org.eclipse.tracecompass.tmf.analysis.xml.core.tests.module.XmlUtilsTest;
27 import org.eclipse.tracecompass.tmf.analysis.xml.core.tests.stubs.PatternSegmentFactoryStub;
28 import org.eclipse.tracecompass.tmf.analysis.xml.core.tests.stubs.StateSystemContainerStub;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.w3c.dom.Element;
32 import org.w3c.dom.Node;
33 import org.w3c.dom.NodeList;
34
35 /**
36 * Test suite for the XML pattern segment builder. The builder should create the
37 * corresponding pattern segment :
38 *
39 * <p>
40 * -name : "test"
41 * </p>
42 * <p>
43 * -content :
44 * </p>
45 * <p>
46 * &nbsp;&nbsp;&nbsp;&nbsp;-field1 : 5
47 * </p>
48 * <p>
49 * &nbsp;&nbsp;&nbsp;&nbsp;-field2 : "test"
50 * </p>
51 * <p>
52 * &nbsp;&nbsp;&nbsp;&nbsp;-field3 : 1
53 * </p>
54 *
55 * @author Jean-Christian Kouame
56 */
57 public class TmfXmlPatternSegmentBuilderTest {
58
59 private static final @NonNull String ANALYSIS_ID = "xml test pattern segment";
60 private final @NonNull StateSystemContainerStub fContainer = new StateSystemContainerStub();
61 private final @NonNull ITmfXmlModelFactory fModelFactory = TmfXmlReadWriteModelFactory.getInstance();
62 private File fTestXmlFile;
63
64 /**
65 * test the {@link XmlUtils#xmlValidate(File)} method
66 */
67 @Before
68 public void testXmlValidate() {
69 fTestXmlFile = TmfXmlTestFiles.VALID_PATTERN_SEGMENT.getFile();
70 if ((fTestXmlFile == null) || !fTestXmlFile.exists()) {
71 fail("XML pattern test file does not exist");
72 }
73 IStatus status = XmlUtils.xmlValidate(fTestXmlFile);
74 if (!status.isOK()) {
75 fail(status.getMessage());
76 }
77 }
78
79 /**
80 * Create a pattern segment builder that will generate a pattern segment.
81 * This method test the data of the pattern segment created.
82 */
83 @Test
84 public void testBuilder() {
85 Element doc = XmlUtils.getElementInFile(fTestXmlFile.getPath(), TmfXmlStrings.PATTERN, ANALYSIS_ID);
86 NodeList patternSegments = doc.getElementsByTagName(TmfXmlStrings.SEGMENT);
87 assertEquals("Number of pattern segments", 2, patternSegments.getLength());
88
89 final Node item2 = patternSegments.item(1);
90 assertNotNull("pattern segment 2", item2);
91 // create a pattern segment builder using the second pattern segment description in the XML pattern file
92 TmfXmlPatternSegmentBuilder builder = new TmfXmlPatternSegmentBuilder(fModelFactory, (Element) item2, fContainer);
93 assertNotNull("builder", builder);
94
95 //Create a pattern segment and test its content
96 TmfXmlPatternSegment segment = builder.generatePatternSegment(PatternSegmentFactoryStub.TEST_2_END_EVENT,
97 PatternSegmentFactoryStub.TEST_2_START_EVENT.getTimestamp(),
98 PatternSegmentFactoryStub.TEST_2_END_EVENT.getTimestamp());
99 XmlUtilsTest.testPatternSegmentData(PatternSegmentFactoryStub.TEST_2, segment);
100 }
101 }
This page took 0.038164 seconds and 5 git commands to generate.