Commit | Line | Data |
---|---|---|
500d5c41 GM |
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 | *********************************************************************/ | |
8e8c0226 AM |
12 | |
13 | package org.eclipse.linuxtools.lttng2.control.core.tests.session; | |
500d5c41 GM |
14 | |
15 | import static org.junit.Assert.assertFalse; | |
16 | import static org.junit.Assert.assertTrue; | |
17 | import static org.junit.Assert.fail; | |
18 | ||
19 | import java.io.File; | |
20 | import java.util.HashSet; | |
21 | import java.util.Set; | |
22 | ||
23 | import org.eclipse.core.runtime.IPath; | |
24 | import org.eclipse.core.runtime.IStatus; | |
8e8c0226 AM |
25 | import org.eclipse.linuxtools.internal.lttng2.control.core.Activator; |
26 | import org.eclipse.linuxtools.internal.lttng2.control.core.model.IDomainInfo; | |
27 | import org.eclipse.linuxtools.internal.lttng2.control.core.model.ISessionInfo; | |
28 | import org.eclipse.linuxtools.internal.lttng2.control.core.model.TraceSessionState; | |
29 | import org.eclipse.linuxtools.internal.lttng2.control.core.model.impl.BufferType; | |
30 | import org.eclipse.linuxtools.internal.lttng2.control.core.model.impl.SessionInfo; | |
31 | import org.eclipse.linuxtools.lttng2.control.core.session.SessionConfigGenerator; | |
32 | import org.eclipse.linuxtools.lttng2.control.core.session.SessionConfigStrings; | |
33 | import org.eclipse.linuxtools.lttng2.control.core.tests.model.impl.ModelImplFactory; | |
500d5c41 GM |
34 | import org.junit.After; |
35 | import org.junit.Before; | |
36 | import org.junit.Test; | |
37 | ||
38 | /** | |
39 | * This class contains tests for the class {@link SessionConfigGenerator}. | |
40 | */ | |
41 | public class SessionConfigGeneratorTest { | |
42 | ||
43 | // ------------------------------------------------------------------------ | |
44 | // Test data | |
45 | // ------------------------------------------------------------------------ | |
46 | ||
47 | /** Session files for validation */ | |
86703386 AM |
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"); | |
500d5c41 GM |
50 | |
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"; | |
54 | ||
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"; | |
58 | ||
59 | /** Session informations for generation tests */ | |
60 | private ISessionInfo fValidSessionInfo = null; | |
61 | private ISessionInfo fValidSessionSnapshotInfo = null; | |
62 | private ISessionInfo fInvalidSessionInfo = null; | |
63 | ||
64 | // ------------------------------------------------------------------------ | |
65 | // Housekeeping | |
66 | // ------------------------------------------------------------------------ | |
67 | /** | |
68 | * Perform pre-test initialization. | |
69 | */ | |
70 | @Before | |
71 | public void setUp() { | |
72 | /* A valid domain with shared buffer type */ | |
73 | ModelImplFactory factory = new ModelImplFactory(); | |
74 | IDomainInfo domain = factory.getDomainInfo1(); | |
75 | domain.setBufferType(BufferType.BUFFER_SHARED); | |
76 | ||
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); | |
82 | ||
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()); | |
88 | ||
89 | /* The invalid session contains an event with an invalid type */ | |
90 | fInvalidSessionInfo = factory.getSessionInfo2(); | |
91 | fInvalidSessionInfo.setName(SESSION_NAME_3); | |
92 | } | |
93 | ||
94 | /** | |
95 | * Delete the session file created | |
96 | */ | |
97 | @After | |
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(); | |
103 | } | |
104 | ||
105 | } | |
106 | ||
107 | // ------------------------------------------------------------------------ | |
108 | // Tests | |
109 | // ------------------------------------------------------------------------ | |
110 | ||
111 | /** | |
112 | * Test method for {@link SessionConfigGenerator#sessionValidate(File)} | |
113 | */ | |
114 | @Test | |
115 | public void testSessionValidate() { | |
116 | File testSessionFile = VALID_SESSION_FILE; | |
117 | if ((testSessionFile == null) || !testSessionFile.exists()) { | |
118 | fail("Session test file does not exist"); | |
119 | } | |
120 | IStatus status = SessionConfigGenerator.sessionValidate(testSessionFile); | |
121 | if (!status.isOK()) { | |
122 | fail(status.getMessage()); | |
123 | } | |
124 | ||
125 | testSessionFile = INVALID_SESSION_FILE; | |
126 | if ((testSessionFile == null) || !testSessionFile.exists()) { | |
127 | fail("Session test file does not exist"); | |
128 | } | |
129 | assertFalse(SessionConfigGenerator.sessionValidate(testSessionFile).isOK()); | |
130 | } | |
131 | ||
132 | /** | |
133 | * Test method for | |
134 | * {@link SessionConfigGenerator#generateSessionConfig(Set, IPath)} | |
135 | */ | |
136 | @Test | |
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()); | |
141 | ||
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()); | |
146 | ||
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()); | |
151 | ||
152 | /* Add an invalid session */ | |
153 | sessions.add(fInvalidSessionInfo); | |
154 | assertFalse(SessionConfigGenerator.generateSessionConfig(sessions, SESSION_FILE_PATH).isOK()); | |
155 | } | |
156 | } |