-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
FileTestHelper.java
192 lines (172 loc) · 7.13 KB
/
FileTestHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.server.session;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.eclipse.jetty.toolchain.test.FS;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDir;
import org.eclipse.jetty.util.ClassLoadingObjectInputStream;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* FileTestHelper
*/
public class FileTestHelper
{
static int __workers = 0;
public static File getFile(WorkDir workDir, String sessionId) throws IOException
{
try (Stream<Path> s = Files.list(workDir.getPath()))
{
return s
.filter((path) -> path.getFileName().toString().contains(sessionId))
.findFirst()
.map(Path::toFile)
.orElse(null);
}
}
public static void assertSessionExists(WorkDir workDir, String sessionId, boolean exists) throws IOException
{
try (Stream<Path> s = Files.list(workDir.getPath()))
{
Optional<Path> sessionPath = s
.filter((path) -> path.getFileName().toString().contains(sessionId))
.findFirst();
if (exists)
assertTrue(sessionPath.isPresent());
else
assertFalse(sessionPath.isPresent());
}
}
public static void assertFileExists(WorkDir workDir, String filename, boolean exists)
{
Path path = workDir.getPath().resolve(filename);
if (exists)
assertTrue(Files.exists(path), "File should exist: " + path);
else
assertFalse(Files.exists(path), "File should NOT exist: " + path);
}
public static void createFile(WorkDir workDir, String filename)
throws IOException
{
Path path = workDir.getPath().resolve(filename);
Files.deleteIfExists(path);
FS.touch(path);
}
public static void createFile(WorkDir workDir, String id, String contextPath, String vhost,
String lastNode, long created, long accessed,
long lastAccessed, long maxIdle, long expiry,
long cookieSet, Map<String, Object> attributes)
throws Exception
{
String filename = "" + expiry + "_" + contextPath + "_" + vhost + "_" + id;
Path path = workDir.getPath().resolve(filename);
try (OutputStream fos = Files.newOutputStream(path);
DataOutputStream out = new DataOutputStream(fos))
{
out.writeUTF(id);
out.writeUTF(contextPath);
out.writeUTF(vhost);
out.writeUTF(lastNode);
out.writeLong(created);
out.writeLong(accessed);
out.writeLong(lastAccessed);
out.writeLong(cookieSet);
out.writeLong(expiry);
out.writeLong(maxIdle);
if (attributes != null)
{
SessionData tmp = new SessionData(id, contextPath, vhost, created, accessed, lastAccessed, maxIdle);
ObjectOutputStream oos = new ObjectOutputStream(out);
SessionData.serializeAttributes(tmp, oos);
}
}
}
public static boolean checkSessionPersisted(WorkDir workDir, SessionData data)
throws Exception
{
String filename = "" + data.getExpiry() + "_" + data.getContextPath() + "_" + data.getVhost() + "_" + data.getId();
Path file = workDir.getPath().resolve(filename);
assertTrue(Files.exists(file));
try (InputStream in = Files.newInputStream(file);
DataInputStream di = new DataInputStream(in))
{
String id = di.readUTF();
String contextPath = di.readUTF();
String vhost = di.readUTF();
String lastNode = di.readUTF();
long created = di.readLong();
long accessed = di.readLong();
long lastAccessed = di.readLong();
long cookieSet = di.readLong();
long expiry = di.readLong();
long maxIdle = di.readLong();
assertEquals(data.getId(), id);
assertEquals(data.getContextPath(), contextPath);
assertEquals(data.getVhost(), vhost);
assertEquals(data.getLastNode(), lastNode);
assertEquals(data.getCreated(), created);
assertEquals(data.getAccessed(), accessed);
assertEquals(data.getLastAccessed(), lastAccessed);
assertEquals(data.getCookieSet(), cookieSet);
assertEquals(data.getExpiry(), expiry);
assertEquals(data.getMaxInactiveMs(), maxIdle);
SessionData tmp = new SessionData(id, contextPath, vhost, created, accessed, lastAccessed, maxIdle);
ClassLoadingObjectInputStream ois = new ClassLoadingObjectInputStream(di);
SessionData.deserializeAttributes(tmp, ois);
//same number of attributes
assertEquals(data.getAllAttributes().size(), tmp.getAllAttributes().size());
//same keys
assertEquals(tmp.getAllAttributes().keySet(), data.getKeys());
//same values
for (String name : data.getKeys())
{
assertEquals(tmp.getAttribute(name), data.getAttribute(name));
}
}
return true;
}
public static void deleteFile(WorkDir workDir, String sessionId) throws IOException
{
// Collect
try (Stream<Path> s = Files.list(workDir.getPath()))
{
s.filter((path) -> path.getFileName().toString().contains(sessionId))
.collect(Collectors.toList()) // Delete outside of list stream
.forEach(FS::deleteFile);
}
}
public static FileSessionDataStoreFactory newSessionDataStoreFactory(WorkDir workDir)
{
FileSessionDataStoreFactory storeFactory = new FileSessionDataStoreFactory();
storeFactory.setStoreDir(workDir.getPath().toFile());
return storeFactory;
}
}