1 /**********************************************************************
2 * Copyright (c) 2014 École Polytechnique de Montréal
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
10 * Guilliano Molaire - Initial API and implementation
11 *********************************************************************/
13 package org
.eclipse
.tracecompass
.lttng2
.control
.core
.tests
.session
;
15 import static org
.junit
.Assert
.assertFalse
;
16 import static org
.junit
.Assert
.assertTrue
;
17 import static org
.junit
.Assert
.fail
;
20 import java
.util
.HashSet
;
23 import org
.eclipse
.core
.runtime
.IPath
;
24 import org
.eclipse
.core
.runtime
.IStatus
;
25 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.core
.Activator
;
26 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.core
.model
.IDomainInfo
;
27 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.core
.model
.ISessionInfo
;
28 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.core
.model
.TraceSessionState
;
29 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.core
.model
.impl
.BufferType
;
30 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.core
.model
.impl
.SessionInfo
;
31 import org
.eclipse
.tracecompass
.lttng2
.control
.core
.session
.SessionConfigGenerator
;
32 import org
.eclipse
.tracecompass
.lttng2
.control
.core
.session
.SessionConfigStrings
;
33 import org
.eclipse
.tracecompass
.lttng2
.control
.core
.tests
.model
.impl
.ModelImplFactory
;
34 import org
.junit
.After
;
35 import org
.junit
.Before
;
36 import org
.junit
.Test
;
39 * This class contains tests for the class {@link SessionConfigGenerator}.
41 public class SessionConfigGeneratorTest
{
43 // ------------------------------------------------------------------------
45 // ------------------------------------------------------------------------
47 /** Session files for validation */
48 private static final File VALID_SESSION_FILE
= new File("../org.eclipse.tracecompass.lttng2.control.core.tests/test_session_config_files/test_valid.lttng");
49 private static final File INVALID_SESSION_FILE
= new File("../org.eclipse.tracecompass.lttng2.control.core.tests/test_session_config_files/test_invalid.lttng");
51 private static final String SESSION_FILENAME
= "test_session." + SessionConfigStrings
.SESSION_CONFIG_FILE_EXTENSION
;
52 private static final IPath SESSION_FILE_PATH
= Activator
.getDefault().getStateLocation().addTrailingSeparator().append(SESSION_FILENAME
);
53 private static final String TRACE_SESSION_PATH
= "/home/user/folder";
55 private static final String SESSION_NAME_1
= "session1";
56 private static final String SESSION_NAME_2
= "session2";
57 private static final String SESSION_NAME_3
= "session3";
59 /** Session informations for generation tests */
60 private ISessionInfo fValidSessionInfo
= null;
61 private ISessionInfo fValidSessionSnapshotInfo
= null;
62 private ISessionInfo fInvalidSessionInfo
= null;
64 // ------------------------------------------------------------------------
66 // ------------------------------------------------------------------------
68 * Perform pre-test initialization.
72 /* A valid domain with shared buffer type */
73 ModelImplFactory factory
= new ModelImplFactory();
74 IDomainInfo domain
= factory
.getDomainInfo1();
75 domain
.setBufferType(BufferType
.BUFFER_SHARED
);
77 /* The valid sessions */
78 fValidSessionInfo
= new SessionInfo(SESSION_NAME_1
);
79 fValidSessionInfo
.setSessionPath(TRACE_SESSION_PATH
);
80 fValidSessionInfo
.setSessionState(TraceSessionState
.ACTIVE
);
81 fValidSessionInfo
.addDomain(domain
);
83 fValidSessionSnapshotInfo
= new SessionInfo(SESSION_NAME_2
);
84 fValidSessionSnapshotInfo
.setSessionPath(TRACE_SESSION_PATH
);
85 fValidSessionSnapshotInfo
.setSessionState(TraceSessionState
.ACTIVE
);
86 fValidSessionSnapshotInfo
.addDomain(domain
);
87 fValidSessionSnapshotInfo
.setSnapshotInfo(factory
.getSnapshotInfo1());
89 /* The invalid session contains an event with an invalid type */
90 fInvalidSessionInfo
= factory
.getSessionInfo2();
91 fInvalidSessionInfo
.setName(SESSION_NAME_3
);
95 * Delete the session file created
98 public void tearUp() {
99 /* Tear up the file created if it exists */
100 File sessionConfigurationFile
= SESSION_FILE_PATH
.toFile();
101 if (sessionConfigurationFile
.exists()) {
102 sessionConfigurationFile
.delete();
107 // ------------------------------------------------------------------------
109 // ------------------------------------------------------------------------
112 * Test method for {@link SessionConfigGenerator#sessionValidate(File)}
115 public void testSessionValidate() {
116 File testSessionFile
= VALID_SESSION_FILE
;
117 if ((testSessionFile
== null) || !testSessionFile
.exists()) {
118 fail("Session test file does not exist");
120 IStatus status
= SessionConfigGenerator
.sessionValidate(testSessionFile
);
121 if (!status
.isOK()) {
122 fail(status
.getMessage());
125 testSessionFile
= INVALID_SESSION_FILE
;
126 if ((testSessionFile
== null) || !testSessionFile
.exists()) {
127 fail("Session test file does not exist");
129 assertFalse(SessionConfigGenerator
.sessionValidate(testSessionFile
).isOK());
134 * {@link SessionConfigGenerator#generateSessionConfig(Set, IPath)}
137 public void testGenerateSessionConfig() {
138 /* Should fail since it's empty */
139 final Set
<ISessionInfo
> sessions
= new HashSet
<>();
140 assertFalse(SessionConfigGenerator
.generateSessionConfig(sessions
, SESSION_FILE_PATH
).isOK());
142 /* Add a valid session and validate */
143 sessions
.add(fValidSessionInfo
);
144 assertTrue(SessionConfigGenerator
.generateSessionConfig(sessions
, SESSION_FILE_PATH
).isOK());
145 assertTrue(SessionConfigGenerator
.sessionValidate(SESSION_FILE_PATH
.toFile()).isOK());
147 /* Add a valid snapshot session and validate */
148 sessions
.add(fValidSessionSnapshotInfo
);
149 assertTrue(SessionConfigGenerator
.generateSessionConfig(sessions
, SESSION_FILE_PATH
).isOK());
150 assertTrue(SessionConfigGenerator
.sessionValidate(SESSION_FILE_PATH
.toFile()).isOK());
152 /* Add an invalid session */
153 sessions
.add(fInvalidSessionInfo
);
154 assertFalse(SessionConfigGenerator
.generateSessionConfig(sessions
, SESSION_FILE_PATH
).isOK());