--- /dev/null
+/*\r
+ * Copyright 2000-2011 JetBrains s.r.o.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package jetbrains.buildServer.nuget.tests.server.tools;\r
+\r
+import jetbrains.buildServer.BaseTestCase;\r
+import jetbrains.buildServer.nuget.server.toolRegistry.impl.ToolPacker;\r
+import jetbrains.buildServer.util.FileUtil;\r
+import org.jetbrains.annotations.NotNull;\r
+import org.testng.Assert;\r
+import org.testng.annotations.BeforeMethod;\r
+import org.testng.annotations.Test;\r
+\r
+import java.io.*;\r
+import java.util.Arrays;\r
+import java.util.Set;\r
+import java.util.TreeSet;\r
+import java.util.zip.ZipEntry;\r
+import java.util.zip.ZipInputStream;\r
+\r
+/**\r
+ * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
+ * Date: 16.08.11 1:50\r
+ */\r
+public class ToolPackerTest extends BaseTestCase {\r
+ private ToolPacker myPacker;\r
+\r
+ @BeforeMethod\r
+ @Override\r
+ protected void setUp() throws Exception {\r
+ super.setUp();\r
+ myPacker = new ToolPacker();\r
+ }\r
+\r
+ @Test\r
+ public void testPack() throws IOException {\r
+ final String toolId = "3.4.56.3";\r
+ final File root = createTempDir();\r
+ final File plugin = myPacker.packTool(toolId, root);\r
+\r
+ assertZip(plugin, "teamcity-plugin.xml");\r
+ }\r
+\r
+ @Test\r
+ public void testPack_oneFile() throws IOException {\r
+ final String toolId = "3.4.56.3";\r
+ final File root = createTempDir();\r
+ FileUtil.writeFile(new File(root, "aaa.txt"), "content");\r
+ final File plugin = myPacker.packTool(toolId, root);\r
+\r
+ assertZip(plugin, "teamcity-plugin.xml", "aaa.txt");\r
+ }\r
+\r
+ @Test\r
+ public void testPack_oneFile2() throws IOException {\r
+ final String toolId = "3.4.56.3";\r
+ final File root = createTempDir();\r
+ FileUtil.writeFile(new File(root, "a/b/c/d/aaa.txt"){{getParentFile().mkdirs();}}, "content");\r
+ final File plugin = myPacker.packTool(toolId, root);\r
+\r
+ assertZip(plugin, "teamcity-plugin.xml", "a/b/c/d/aaa.txt");\r
+ }\r
+\r
+ private void assertZip(@NotNull final File zip, @NotNull String... paths) throws IOException {\r
+ Set<String> pathsToContain = new TreeSet<String>(Arrays.asList(paths));\r
+ Set<String> actualPaths = new TreeSet<String>();\r
+ ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zip)));\r
+ try {\r
+ ZipEntry e;\r
+ while((e = zis.getNextEntry()) != null) {\r
+ pathsToContain.remove(e.getName());\r
+ actualPaths.add(e.getName());\r
+ }\r
+ } finally {\r
+ FileUtil.close(zis);\r
+ }\r
+\r
+ Assert.assertTrue(pathsToContain.isEmpty(), "Should contain: " + pathsToContain + ", but was: " + actualPaths);\r
+ }\r
+\r
+\r
+}\r