78fb9ea68f8684fba1512444c39951e552ae17ee
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.core.tests / src / org / eclipse / linuxtools / lttng2 / core / tests / session / SessionConfigGeneratorTest.java
1 /**********************************************************************
2 * Copyright (c) 2014 École Polytechnique de Montréal
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 * Contributors:
10 * Guilliano Molaire - Initial API and implementation
11 *********************************************************************/
12 package org.eclipse.linuxtools.lttng2.core.tests.session;
13
14 import static org.junit.Assert.assertFalse;
15 import static org.junit.Assert.assertTrue;
16 import static org.junit.Assert.fail;
17
18 import java.io.File;
19 import java.util.HashSet;
20 import java.util.Set;
21
22 import org.eclipse.core.runtime.IPath;
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.linuxtools.internal.lttng2.core.Activator;
25 import org.eclipse.linuxtools.internal.lttng2.core.control.model.IDomainInfo;
26 import org.eclipse.linuxtools.internal.lttng2.core.control.model.ISessionInfo;
27 import org.eclipse.linuxtools.internal.lttng2.core.control.model.TraceSessionState;
28 import org.eclipse.linuxtools.internal.lttng2.core.control.model.impl.BufferType;
29 import org.eclipse.linuxtools.internal.lttng2.core.control.model.impl.SessionInfo;
30 import org.eclipse.linuxtools.lttng2.core.session.SessionConfigGenerator;
31 import org.eclipse.linuxtools.lttng2.core.session.SessionConfigStrings;
32 import org.eclipse.linuxtools.lttng2.core.tests.control.model.impl.ModelImplFactory;
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36
37 /**
38 * This class contains tests for the class {@link SessionConfigGenerator}.
39 */
40 public class SessionConfigGeneratorTest {
41
42 // ------------------------------------------------------------------------
43 // Test data
44 // ------------------------------------------------------------------------
45
46 /** Session files for validation */
47 private static final File VALID_SESSION_FILE = new File("../org.eclipse.linuxtools.lttng2.core.tests/test_session_config_files/test_valid.lttng");
48 private static final File INVALID_SESSION_FILE = new File("../org.eclipse.linuxtools.lttng2.core.tests/test_session_config_files/test_invalid.lttng");
49
50 private static final String SESSION_FILENAME = "test_session." + SessionConfigStrings.SESSION_CONFIG_FILE_EXTENSION;
51 private static final IPath SESSION_FILE_PATH = Activator.getDefault().getStateLocation().addTrailingSeparator().append(SESSION_FILENAME);
52 private static final String TRACE_SESSION_PATH = "/home/user/folder";
53
54 private static final String SESSION_NAME_1 = "session1";
55 private static final String SESSION_NAME_2 = "session2";
56 private static final String SESSION_NAME_3 = "session3";
57
58 /** Session informations for generation tests */
59 private ISessionInfo fValidSessionInfo = null;
60 private ISessionInfo fValidSessionSnapshotInfo = null;
61 private ISessionInfo fInvalidSessionInfo = null;
62
63 // ------------------------------------------------------------------------
64 // Housekeeping
65 // ------------------------------------------------------------------------
66 /**
67 * Perform pre-test initialization.
68 */
69 @Before
70 public void setUp() {
71 /* A valid domain with shared buffer type */
72 ModelImplFactory factory = new ModelImplFactory();
73 IDomainInfo domain = factory.getDomainInfo1();
74 domain.setBufferType(BufferType.BUFFER_SHARED);
75
76 /* The valid sessions */
77 fValidSessionInfo = new SessionInfo(SESSION_NAME_1);
78 fValidSessionInfo.setSessionPath(TRACE_SESSION_PATH);
79 fValidSessionInfo.setSessionState(TraceSessionState.ACTIVE);
80 fValidSessionInfo.addDomain(domain);
81
82 fValidSessionSnapshotInfo = new SessionInfo(SESSION_NAME_2);
83 fValidSessionSnapshotInfo.setSessionPath(TRACE_SESSION_PATH);
84 fValidSessionSnapshotInfo.setSessionState(TraceSessionState.ACTIVE);
85 fValidSessionSnapshotInfo.addDomain(domain);
86 fValidSessionSnapshotInfo.setSnapshotInfo(factory.getSnapshotInfo1());
87
88 /* The invalid session contains an event with an invalid type */
89 fInvalidSessionInfo = factory.getSessionInfo2();
90 fInvalidSessionInfo.setName(SESSION_NAME_3);
91 }
92
93 /**
94 * Delete the session file created
95 */
96 @After
97 public void tearUp() {
98 /* Tear up the file created if it exists */
99 File sessionConfigurationFile = SESSION_FILE_PATH.toFile();
100 if (sessionConfigurationFile.exists()) {
101 sessionConfigurationFile.delete();
102 }
103
104 }
105
106 // ------------------------------------------------------------------------
107 // Tests
108 // ------------------------------------------------------------------------
109
110 /**
111 * Test method for {@link SessionConfigGenerator#sessionValidate(File)}
112 */
113 @Test
114 public void testSessionValidate() {
115 File testSessionFile = VALID_SESSION_FILE;
116 if ((testSessionFile == null) || !testSessionFile.exists()) {
117 fail("Session test file does not exist");
118 }
119 IStatus status = SessionConfigGenerator.sessionValidate(testSessionFile);
120 if (!status.isOK()) {
121 fail(status.getMessage());
122 }
123
124 testSessionFile = INVALID_SESSION_FILE;
125 if ((testSessionFile == null) || !testSessionFile.exists()) {
126 fail("Session test file does not exist");
127 }
128 assertFalse(SessionConfigGenerator.sessionValidate(testSessionFile).isOK());
129 }
130
131 /**
132 * Test method for
133 * {@link SessionConfigGenerator#generateSessionConfig(Set, IPath)}
134 */
135 @Test
136 public void testGenerateSessionConfig() {
137 /* Should fail since it's empty */
138 final Set<ISessionInfo> sessions = new HashSet<>();
139 assertFalse(SessionConfigGenerator.generateSessionConfig(sessions, SESSION_FILE_PATH).isOK());
140
141 /* Add a valid session and validate */
142 sessions.add(fValidSessionInfo);
143 assertTrue(SessionConfigGenerator.generateSessionConfig(sessions, SESSION_FILE_PATH).isOK());
144 assertTrue(SessionConfigGenerator.sessionValidate(SESSION_FILE_PATH.toFile()).isOK());
145
146 /* Add a valid snapshot session and validate */
147 sessions.add(fValidSessionSnapshotInfo);
148 assertTrue(SessionConfigGenerator.generateSessionConfig(sessions, SESSION_FILE_PATH).isOK());
149 assertTrue(SessionConfigGenerator.sessionValidate(SESSION_FILE_PATH.toFile()).isOK());
150
151 /* Add an invalid session */
152 sessions.add(fInvalidSessionInfo);
153 assertFalse(SessionConfigGenerator.generateSessionConfig(sessions, SESSION_FILE_PATH).isOK());
154 }
155 }
This page took 0.037799 seconds and 4 git commands to generate.