1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-05 08:46:02 +02:00

core.tests: Ensure zipFile is closed.

Try-with-resources to ensure everything is properly closed.

Change-Id: I5c0d286b926a7ff86d48f943347929de03de5f0e
Signed-off-by: Alexander Kurtakov <akurtako@redhat.com>
This commit is contained in:
Alexander Kurtakov 2015-12-02 22:58:45 +02:00
parent 375b162ea2
commit e636b977a9

View file

@ -44,8 +44,7 @@ public class ProjectCreator extends TestCase {
private static final IProgressMonitor monitor = new NullProgressMonitor(); private static final IProgressMonitor monitor = new NullProgressMonitor();
public static IProject createProject(IPath zipPath, String projectName) throws Exception { public static IProject createProject(IPath zipPath, String projectName) throws Exception {
ZipFile zipFile = new ZipFile(CTestPlugin.getDefault().getFileInPlugin(zipPath)); try (ZipFile zipFile = new ZipFile(CTestPlugin.getDefault().getFileInPlugin(zipPath))) {
IWorkspace workspace = ResourcesPlugin.getWorkspace(); IWorkspace workspace = ResourcesPlugin.getWorkspace();
IWorkspaceRoot root = workspace.getRoot(); IWorkspaceRoot root = workspace.getRoot();
IPath rootPath = root.getLocation(); IPath rootPath = root.getLocation();
@ -53,17 +52,17 @@ public class ProjectCreator extends TestCase {
Enumeration entries = zipFile.entries(); Enumeration entries = zipFile.entries();
while (entries.hasMoreElements()) { while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry)entries.nextElement(); ZipEntry entry = (ZipEntry) entries.nextElement();
if (!entry.isDirectory()) { if (!entry.isDirectory()) {
IPath entryPath = rootPath.append(entry.getName()); IPath entryPath = rootPath.append(entry.getName());
IPath entryDir = entryPath.removeLastSegments(1); IPath entryDir = entryPath.removeLastSegments(1);
entryDir.toFile().mkdirs(); entryDir.toFile().mkdirs();
InputStream in = zipFile.getInputStream(entry); try (InputStream in = zipFile.getInputStream(entry);
OutputStream out = new FileOutputStream(entryPath.toFile()); OutputStream out = new FileOutputStream(entryPath.toFile())) {
for (int n = in.read(buffer); n >= 0; n = in.read(buffer)) for (int n = in.read(buffer); n >= 0; n = in.read(buffer)) {
out.write(buffer, 0, n); out.write(buffer, 0, n);
in.close(); }
out.close(); }
// Is this the .project file? // Is this the .project file?
if (".project".equals(entryPath.lastSegment())) { if (".project".equals(entryPath.lastSegment())) {
@ -80,6 +79,7 @@ public class ProjectCreator extends TestCase {
return project; return project;
} }
}
public static IProject createCManagedProject(String projectName) throws Exception { public static IProject createCManagedProject(String projectName) throws Exception {
return createProject(new Path("resources/zips/CManaged.zip"), projectName); return createProject(new Path("resources/zips/CManaged.zip"), projectName);