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 *********************************************************************/
12 package org
.eclipse
.linuxtools
.lttng2
.core
.tests
.session
;
14 import static org
.junit
.Assert
.assertFalse
;
15 import static org
.junit
.Assert
.assertTrue
;
16 import static org
.junit
.Assert
.fail
;
19 import java
.util
.HashSet
;
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
;
38 * This class contains tests for the class {@link SessionConfigGenerator}.
40 public class SessionConfigGeneratorTest
{
42 // ------------------------------------------------------------------------
44 // ------------------------------------------------------------------------
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");
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";
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";
58 /** Session informations for generation tests */
59 private ISessionInfo fValidSessionInfo
= null;
60 private ISessionInfo fValidSessionSnapshotInfo
= null;
61 private ISessionInfo fInvalidSessionInfo
= null;
63 // ------------------------------------------------------------------------
65 // ------------------------------------------------------------------------
67 * Perform pre-test initialization.
71 /* A valid domain with shared buffer type */
72 ModelImplFactory factory
= new ModelImplFactory();
73 IDomainInfo domain
= factory
.getDomainInfo1();
74 domain
.setBufferType(BufferType
.BUFFER_SHARED
);
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
);
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());
88 /* The invalid session contains an event with an invalid type */
89 fInvalidSessionInfo
= factory
.getSessionInfo2();
90 fInvalidSessionInfo
.setName(SESSION_NAME_3
);
94 * Delete the session file created
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();
106 // ------------------------------------------------------------------------
108 // ------------------------------------------------------------------------
111 * Test method for {@link SessionConfigGenerator#sessionValidate(File)}
114 public void testSessionValidate() {
115 File testSessionFile
= VALID_SESSION_FILE
;
116 if ((testSessionFile
== null) || !testSessionFile
.exists()) {
117 fail("Session test file does not exist");
119 IStatus status
= SessionConfigGenerator
.sessionValidate(testSessionFile
);
120 if (!status
.isOK()) {
121 fail(status
.getMessage());
124 testSessionFile
= INVALID_SESSION_FILE
;
125 if ((testSessionFile
== null) || !testSessionFile
.exists()) {
126 fail("Session test file does not exist");
128 assertFalse(SessionConfigGenerator
.sessionValidate(testSessionFile
).isOK());
133 * {@link SessionConfigGenerator#generateSessionConfig(Set, IPath)}
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());
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());
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());
151 /* Add an invalid session */
152 sessions
.add(fInvalidSessionInfo
);
153 assertFalse(SessionConfigGenerator
.generateSessionConfig(sessions
, SESSION_FILE_PATH
).isOK());