From 1cfdae8be57c0c00663eb6c84abdbbf68735c7b0 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Tue, 10 Jul 2012 20:18:08 +0200 Subject: [PATCH] Reorder task shouldn't leave corrupted files behind --- .../intellij/util/io/zip/ReorderJarsMain.java | 57 +++++++++-------- .../intellij/util/io/zip/ReorderJarsTest.java | 64 +++++++++++++------ 2 files changed, 76 insertions(+), 45 deletions(-) diff --git a/platform/util/src/com/intellij/util/io/zip/ReorderJarsMain.java b/platform/util/src/com/intellij/util/io/zip/ReorderJarsMain.java index 7b5782d86268..e4a05c01b613 100644 --- a/platform/util/src/com/intellij/util/io/zip/ReorderJarsMain.java +++ b/platform/util/src/com/intellij/util/io/zip/ReorderJarsMain.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2012 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,11 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -/* - * User: anna - * Date: 23-Apr-2009 - */ package com.intellij.util.io.zip; import com.intellij.openapi.util.io.FileUtil; @@ -30,25 +25,26 @@ import java.io.File; import java.io.IOException; import java.util.*; +/** + * @author anna + * @since 23-Apr-2009 + */ +@SuppressWarnings("CallToPrintStackTrace") public class ReorderJarsMain { - private ReorderJarsMain() { - } + private ReorderJarsMain() { } public static void main(String[] args) { - final String orderTxtPath = args[0]; - final String jarsPath = args[1]; - final String destinationHomePath = args[2]; - final String libPath = args.length > 3 ? args[3] : null; - try { - final Map> toReorder = getOrder(new File(orderTxtPath)); + final String orderTxtPath = args[0]; + final String jarsPath = args[1]; + final String destinationPath = args[2]; + final String libPath = args.length > 3 ? args[3] : null; + final Map> toReorder = getOrder(new File(orderTxtPath)); final Set ignoredJars = libPath == null ? Collections.emptySet() : loadIgnoredJars(libPath); for (String jarUrl : toReorder.keySet()) { - if (ignoredJars.contains(StringUtil.trimStart(jarUrl, "/lib/"))) continue; - if (jarUrl.startsWith("/lib/ant")) continue; final File jarFile = new File(jarsPath, jarUrl); @@ -64,16 +60,16 @@ public class ReorderJarsMain { if (orderedEntries.contains(o1.getName())) { return orderedEntries.contains(o2.getName()) ? orderedEntries.indexOf(o1.getName()) - orderedEntries.indexOf(o2.getName()) : -1; } - if (orderedEntries.contains(o2.getName())) return 1; - return 0; + else { + return orderedEntries.contains(o2.getName()) ? 1 : 0; + } } }); - final File tempJarFile = FileUtil.createTempFile("__reorder__", "__reorder__"); final JBZipFile file = new JBZipFile(tempJarFile); - JBZipEntry sizeEntry = file.getOrCreateEntry(JarMemoryLoader.SIZE_ENTRY); + final JBZipEntry sizeEntry = file.getOrCreateEntry(JarMemoryLoader.SIZE_ENTRY); sizeEntry.setData(ZipShort.getBytes(orderedEntries.size())); for (JBZipEntry entry : entries) { @@ -82,14 +78,24 @@ public class ReorderJarsMain { } file.close(); - final File resultJarFile = new File(destinationHomePath, jarUrl); - resultJarFile.getParentFile().mkdirs(); - FileUtil.rename(tempJarFile, resultJarFile); + final File resultJarFile = new File(destinationPath, jarUrl); + final File resultDir = resultJarFile.getParentFile(); + if (!resultDir.isDirectory() && !resultDir.mkdirs()) { + throw new IOException("Cannot create: " + resultDir); + } + try { + FileUtil.rename(tempJarFile, resultJarFile); + } + catch (Exception e) { + FileUtil.delete(resultJarFile); + throw e; + } FileUtil.delete(tempJarFile); } } - catch (IOException e) { - e.printStackTrace(); + catch (Throwable t) { + t.printStackTrace(); + System.exit(1); } } @@ -119,5 +125,4 @@ public class ReorderJarsMain { } return entriesOrder; } - } diff --git a/platform/util/testSrc/com/intellij/util/io/zip/ReorderJarsTest.java b/platform/util/testSrc/com/intellij/util/io/zip/ReorderJarsTest.java index d9f25c2bcef0..d49667825bbb 100644 --- a/platform/util/testSrc/com/intellij/util/io/zip/ReorderJarsTest.java +++ b/platform/util/testSrc/com/intellij/util/io/zip/ReorderJarsTest.java @@ -1,9 +1,26 @@ +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.intellij.util.io.zip; import com.intellij.openapi.application.PathManager; import com.intellij.openapi.util.io.FileUtil; import com.intellij.util.lang.JarMemoryLoader; -import junit.framework.TestCase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; import sun.misc.Resource; import java.io.File; @@ -11,16 +28,33 @@ import java.io.IOException; import java.util.Arrays; import java.util.List; +import static org.junit.Assert.*; + /** * @author Dmitry Avdeev - * Date: 7/12/11 + * @since 7/12/11 */ -public class ReorderJarsTest extends TestCase { - +public class ReorderJarsTest { private File myTempDirectory; - public void testReordering() throws IOException { + @Before + public void setUp() throws Exception { + myTempDirectory = FileUtil.createTempDirectory("__", "__"); + } + @After + public void tearDown() { + FileUtil.delete(myTempDirectory); + } + + private static String getTestDataPath() { + String homePath = PathManager.getHomePath().replace(File.separatorChar, '/'); + if (new File(homePath + "/community/java/java-tests/testData").exists()) return homePath + "/community/java/java-tests/testData"; + return homePath + "/java/java-tests/testData"; + } + + @Test + public void testReordering() throws IOException { String path = getTestDataPath() + "/ide/plugins/reorderJars"; JBZipFile zipFile = null; try { @@ -34,8 +68,10 @@ public class ReorderJarsTest extends TestCase { } } - ReorderJarsMain.main(new String[] { path + "/order.txt", path, myTempDirectory.getPath() } ); + ReorderJarsMain.main(new String[]{path + "/order.txt", path, myTempDirectory.getPath()}); + File[] files = myTempDirectory.listFiles(); + assertNotNull(files); assertEquals(1, files.length); File file = files[0]; assertEquals("annotations.jar", file.getName()); @@ -65,17 +101,14 @@ public class ReorderJarsTest extends TestCase { assertTrue(Arrays.equals(data, bytes)); } - public static String getTestDataPath() { - String homePath = PathManager.getHomePath().replace(File.separatorChar, '/'); - if (new File(homePath + "/community/java/java-tests/testData").exists()) return homePath + "/community/java/java-tests/testData"; - return homePath + "/java/java-tests/testData"; - } - + @Test public void testPluginXml() throws Exception { String path = getTestDataPath() + "/ide/plugins/reorderJars"; ReorderJarsMain.main(new String[] { path + "/zkmOrder.txt", path, myTempDirectory.getPath() } ); + File[] files = myTempDirectory.listFiles(); + assertNotNull(files); File file = files[0]; assertEquals("zkm.jar", file.getName()); @@ -89,12 +122,5 @@ public class ReorderJarsTest extends TestCase { finally { zipFile.close(); } - - } - - @Override - protected void setUp() throws Exception { - super.setUp(); - myTempDirectory = FileUtil.createTempDirectory("__", "__"); } }