Skip to content

Commit 12ab8b8

Browse files
committed
More Strings refactored to Paths
1 parent d442642 commit 12ab8b8

File tree

6 files changed

+60
-46
lines changed

6 files changed

+60
-46
lines changed

tmc-plugin/src/fi/helsinki/cs/tmc/model/PushEventListener.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.TimerTask;
1212
import java.util.logging.Level;
1313
import java.util.logging.Logger;
14+
1415
import org.cometd.bayeux.Channel;
1516
import org.cometd.bayeux.Message;
1617
import org.cometd.bayeux.client.ClientSession;

tmc-plugin/src/fi/helsinki/cs/tmc/spyware/eventsources/SourceSnapshotEventSource.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.io.Closeable;
2424
import java.io.File;
2525
import java.io.IOException;
26+
import java.nio.file.Path;
27+
import java.nio.file.Paths;
2628
import java.util.logging.Level;
2729
import java.util.logging.Logger;
2830
import javax.swing.SwingUtilities;
@@ -102,27 +104,27 @@ public void fileRenamed(FileRenameEvent fre) {
102104
public void fileAttributeChanged(FileAttributeEvent fae) {}
103105

104106
private void reactToChange(final ChangeType changeType, final FileObject fileObject) {
105-
String filePath = TmcFileUtils.tryGetPathRelativeToProject(fileObject);
107+
Path filePath = Paths.get(TmcFileUtils.tryGetPathRelativeToProject(fileObject));
106108
if (filePath == null) {
107109
return;
108110
}
109111
String metadata =
110112
JsonMaker.create()
111113
.add("cause", changeType.name().toLowerCase())
112-
.add("file", filePath)
114+
.add("file", filePath.toString())
113115
.toString();
114116
invokeSnapshotThreadViaEdt(fileObject, metadata);
115117
}
116118

117119
private void reactToRename(final ChangeType changeType, final FileRenameEvent renameEvent) {
118-
String filePath = TmcFileUtils.tryGetPathRelativeToProject(renameEvent.getFile());
120+
Path filePath = Paths.get(TmcFileUtils.tryGetPathRelativeToProject(renameEvent.getFile()));
119121
if (filePath == null) {
120122
return;
121123
}
122124
String metadata =
123125
JsonMaker.create()
124126
.add("cause", changeType.name().toLowerCase())
125-
.add("file", filePath)
127+
.add("file", filePath.toString())
126128
.add("previous_name", renameEvent.getName() + "." + renameEvent.getExt())
127129
.toString();
128130
invokeSnapshotThreadViaEdt(renameEvent.getFile(), metadata);

tmc-plugin/src/fi/helsinki/cs/tmc/spyware/eventsources/TextInsertEventSource.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import java.beans.PropertyChangeListener;
2626
import java.io.Closeable;
2727
import java.nio.charset.Charset;
28+
import java.nio.file.Path;
29+
import java.nio.file.Paths;
2830
import java.util.HashMap;
2931
import java.util.List;
3032
import java.util.Map;
@@ -168,10 +170,10 @@ private void sendEvent(Exercise ex, String eventType, String text) {
168170

169171
private String generatePatchDescription(
170172
FileObject fo, List<Patch> patches, boolean patchContainsFullDocument) {
171-
String filePath = TmcFileUtils.tryGetPathRelativeToProject(fo);
173+
Path filePath = Paths.get(TmcFileUtils.tryGetPathRelativeToProject(fo));
172174
if (filePath != null) {
173175
return JsonMaker.create()
174-
.add("file", filePath)
176+
.add("file", filePath.toString())
175177
.add("patches", PATCH_GENERATOR.patch_toText(patches))
176178
.add("full_document", patchContainsFullDocument)
177179
.toString();

tmc-plugin/src/fi/helsinki/cs/tmc/utilities/TmcFileUtils.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package fi.helsinki.cs.tmc.utilities;
22

3+
import java.nio.file.Path;
4+
import java.nio.file.Paths;
35
import org.netbeans.api.project.FileOwnerQuery;
46
import org.netbeans.api.project.Project;
57

@@ -13,20 +15,20 @@ public class TmcFileUtils {
1315
* Otherwise returns null.
1416
*/
1517
public static String tryGetPathRelativeToProject(FileObject fileObject) {
16-
String filePath = fileObject.getPath();
18+
Path filePath = Paths.get(fileObject.getPath());
1719

1820
try {
1921
Project p = FileOwnerQuery.getOwner(fileObject);
2022
String projectDirectory = p.getProjectDirectory().getPath();
21-
if (filePath.contains(projectDirectory)) {
23+
if (filePath.toString().contains(projectDirectory)) {
2224
filePath =
23-
filePath.substring(
24-
filePath.indexOf(projectDirectory) + projectDirectory.length());
25+
Paths.get(filePath.toString().substring(
26+
filePath.toString().indexOf(projectDirectory) + projectDirectory.length()));
2527
}
2628
} catch (Exception e) {
2729
return null;
2830
}
2931

30-
return filePath;
32+
return filePath.toString();
3133
}
3234
}

tmc-plugin/src/fi/helsinki/cs/tmc/utilities/zip/NbProjectUnzipper.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import java.io.IOException;
1313
import java.io.InputStream;
1414
import java.io.OutputStream;
15+
import java.nio.file.Path;
16+
import java.nio.file.Paths;
1517
import java.util.ArrayList;
1618
import java.util.HashSet;
1719
import java.util.List;
@@ -148,11 +150,11 @@ public Result unzipProject(byte[] data, File projectDir, boolean reallyWriteFile
148150
ZipEntry zent;
149151
while ((zent = zis.getNextEntry()) != null) {
150152
if (zent.getName().startsWith(projectDirInZip)) {
151-
String restOfPath = zent.getName().substring(projectDirInZip.length());
152-
restOfPath = trimSlashes(restOfPath);
153+
Path restOfPath = Paths.get(zent.getName().substring(projectDirInZip.length()));
154+
restOfPath = trimSlashes(restOfPath.toString());
153155

154-
String destFileRelativePath = trimSlashes(restOfPath.replace("/", File.separator));
155-
pathsInZip.add(destFileRelativePath);
156+
Path destFileRelativePath = trimSlashes(restOfPath.toString().replace("/", File.separator));
157+
pathsInZip.add(destFileRelativePath.toString());
156158
File destFile =
157159
new File(projectDir.toString() + File.separator + destFileRelativePath);
158160

@@ -167,17 +169,17 @@ public Result unzipProject(byte[] data, File projectDir, boolean reallyWriteFile
167169
if (destFile.exists()) {
168170
if (fileContentEquals(destFile, entryData)) {
169171
shouldWrite = false;
170-
result.unchangedFiles.add(destFileRelativePath);
171-
} else if (overwriting.mayOverwrite(destFileRelativePath)) {
172+
result.unchangedFiles.add(destFileRelativePath.toString());
173+
} else if (overwriting.mayOverwrite(destFileRelativePath.toString())) {
172174
shouldWrite = true;
173-
result.overwrittenFiles.add(destFileRelativePath);
175+
result.overwrittenFiles.add(destFileRelativePath.toString());
174176
} else {
175177
shouldWrite = false;
176-
result.skippedFiles.add(destFileRelativePath);
178+
result.skippedFiles.add(destFileRelativePath.toString());
177179
}
178180
} else {
179181
shouldWrite = true;
180-
result.newFiles.add(destFileRelativePath);
182+
result.newFiles.add(destFileRelativePath.toString());
181183
}
182184
if (shouldWrite && reallyWriteFiles) {
183185
FileUtils.forceMkdir(destFile.getParentFile());
@@ -204,40 +206,40 @@ private void deleteFilesNotInZip(
204206
boolean reallyWriteFiles)
205207
throws IOException {
206208
for (File file : curDir.listFiles()) {
207-
String relPath = file.getPath().substring(projectDir.getPath().length());
208-
relPath = trimSlashes(relPath);
209+
Path relPath = Paths.get(file.getPath().substring(projectDir.getPath().length()));
210+
relPath = trimSlashes(relPath.toString());
209211

210212
if (file.isDirectory()) {
211213
deleteFilesNotInZip(
212214
projectDir, file, result, pathsInZip, overwriting, reallyWriteFiles);
213215
}
214216

215-
if (!pathsInZip.contains(relPath)) {
216-
if (overwriting.mayDelete(relPath)) {
217+
if (!pathsInZip.contains(relPath.toString())) {
218+
if (overwriting.mayDelete(relPath.toString())) {
217219
if (file.isDirectory() && file.listFiles().length > 0) {
218220
// Won't delete directories if they still have contents
219-
result.skippedDeletingFiles.add(relPath);
221+
result.skippedDeletingFiles.add(relPath.toString());
220222
} else {
221223
if (reallyWriteFiles) {
222224
file.delete();
223225
}
224-
result.deletedFiles.add(relPath);
226+
result.deletedFiles.add(relPath.toString());
225227
}
226228
} else {
227-
result.skippedDeletingFiles.add(relPath);
229+
result.skippedDeletingFiles.add(relPath.toString());
228230
}
229231
}
230232
}
231233
}
232234

233-
private String trimSlashes(String s) {
235+
private Path trimSlashes(String s) {
234236
while (s.startsWith("/") || s.startsWith(File.separator)) {
235237
s = s.substring(1);
236238
}
237239
while (s.endsWith("/") || s.startsWith(File.separator)) {
238240
s = s.substring(0, s.length() - 1);
239241
}
240-
return s;
242+
return Paths.get(s);
241243
}
242244

243245
private String findProjectDirInZip(byte[] data) throws IOException {

tmc-plugin/test/qa-functional/src/fi/helsinki/cs/tmc/functionaltests/utils/FakeTmcServer.java

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,25 @@
22

33
import fi.helsinki.cs.tmc.model.ServerAccess;
44
import fi.helsinki.cs.tmc.testing.AdHocHttpServer;
5+
56
import java.io.UnsupportedEncodingException;
6-
import org.apache.http.entity.StringEntity;
77
import java.io.IOException;
88
import java.net.URI;
99
import java.net.URISyntaxException;
10+
import java.nio.file.Path;
11+
import java.nio.file.Paths;
1012
import java.util.HashMap;
1113
import java.util.List;
1214
import java.util.Map;
1315
import java.util.regex.Matcher;
1416
import java.util.regex.Pattern;
17+
1518
import org.apache.http.HttpException;
1619
import org.apache.http.HttpRequest;
1720
import org.apache.http.HttpResponse;
1821
import org.apache.http.NameValuePair;
1922
import org.apache.http.client.utils.URLEncodedUtils;
23+
import org.apache.http.entity.StringEntity;
2024
import org.apache.http.entity.ByteArrayEntity;
2125
import org.apache.http.protocol.HttpContext;
2226
import org.apache.http.protocol.HttpRequestHandler;
@@ -29,31 +33,31 @@ public class FakeTmcServer extends AdHocHttpServer {
2933
private String expectedPassword;
3034
private String coursesJson = "{}";
3135
private Map<String, String> courseDetails = new HashMap<String, String>();
32-
36+
3337
private HashMap<String, byte[]> zipFiles = new HashMap<String, byte[]>();
3438

3539
public FakeTmcServer() {
3640
setHandler(new Handler());
3741
}
38-
42+
3943
public synchronized FakeTmcServer expectUser(String username, String password) {
4044
this.expectedUsername = username;
4145
this.expectedPassword = password;
4246
return this;
4347
}
44-
48+
4549
public synchronized void respondWithCourses(String coursesJson) {
4650
this.coursesJson = coursesJson;
4751
}
4852

4953
public synchronized void respondWithCourseDetails(String id, String courseDetailsJson) {
5054
this.courseDetails.put(id, courseDetailsJson);
5155
}
52-
56+
5357
public synchronized void putZipFile(String path, byte[] data) {
5458
zipFiles.put(path, data);
5559
}
56-
60+
5761
public synchronized void clearResponses() {
5862
coursesJson = "{}";
5963
courseDetails.clear();
@@ -63,6 +67,7 @@ public synchronized void clearResponses() {
6367
private static final Pattern courseRegex = Pattern.compile("/courses/(.+)\\.json");
6468

6569
private class Handler implements HttpRequestHandler {
70+
6671
@Override
6772
public void handle(HttpRequest req, HttpResponse resp, HttpContext hc) throws HttpException, IOException {
6873
synchronized (FakeTmcServer.this) {
@@ -75,34 +80,34 @@ public void handle(HttpRequest req, HttpResponse resp, HttpContext hc) throws Ht
7580

7681
Map<String, String> params = parseQueryParameters(uri);
7782

78-
String path = uri.getPath();
79-
debug("Path: " + path);
83+
Path path = Paths.get(uri.getPath());
84+
debug("Path: " + path.toString());
8085

8186
if (path.startsWith("/courses.json")) {
8287
authenticate(params);
8388
debug("Responding with course list: " + coursesJson);
8489
respondWithJson(resp, coursesJson);
85-
} else if (courseRegex.matcher(path).matches()) {
86-
Matcher m = courseRegex.matcher(path);
90+
} else if (courseRegex.matcher(path.toString()).matches()) {
91+
Matcher m = courseRegex.matcher(path.toString());
8792
m.matches();
8893
String id = m.group(1);
8994

90-
System.out.println(courseDetails);
91-
System.out.println(id);
95+
System.out.println(courseDetails);
96+
System.out.println(id);
9297
String response = courseDetails.get(id);
9398
if (response != null) {
9499
authenticate(params);
95100
debug("Responding with course details: " + response);
96101
respondWithJson(resp, response);
97102
} else {
98-
debug("Unknown course path: " + path);
103+
debug("Unknown course path: " + path.toString());
99104
resp.setStatusCode(404);
100105
resp.setEntity(new StringEntity("Not Found"));
101106
}
102-
} else if (zipFiles.containsKey(path)) {
103-
respondWithBinary(resp, zipFiles.get(path), "application/zip");
107+
} else if (zipFiles.containsKey(path.toString())) {
108+
respondWithBinary(resp, zipFiles.get(path.toString()), "application/zip");
104109
} else {
105-
debug("Unknown path: " + path);
110+
debug("Unknown path: " + path.toString());
106111
resp.setStatusCode(404);
107112
resp.setEntity(new StringEntity("Not Found"));
108113
}
@@ -137,7 +142,7 @@ private void respondWithJson(HttpResponse resp, String json) {
137142
}
138143

139144
private void respondWithBinary(HttpResponse resp, byte[] data, String mimeType) {
140-
ByteArrayEntity entity = new ByteArrayEntity(data);
145+
ByteArrayEntity entity = new ByteArrayEntity(data);
141146
entity.setContentType(mimeType);
142147
resp.setEntity(entity);
143148
}

0 commit comments

Comments
 (0)