|
Line 0
Link Here
|
|
|
1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2010 SAP AG |
| 3 |
* |
| 4 |
* All rights reserved. This program and the accompanying materials |
| 5 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 6 |
* and Apache License v2.0 which accompanies this distribution. |
| 7 |
* The Eclipse Public License is available at |
| 8 |
* http://www.eclipse.org/legal/epl-v10.html |
| 9 |
* and the Apache License v2.0 is available at |
| 10 |
* http://www.opensource.org/licenses/apache2.0.php. |
| 11 |
* You may elect to redistribute this code under either of these licenses. |
| 12 |
* |
| 13 |
* Contributors: |
| 14 |
* Violeta Georgieva - initial contribution |
| 15 |
*******************************************************************************/ |
| 16 |
|
| 17 |
package org.eclipse.gemini.web.internal.url; |
| 18 |
|
| 19 |
import static org.junit.Assert.assertEquals; |
| 20 |
import static org.junit.Assert.assertTrue; |
| 21 |
|
| 22 |
import java.io.File; |
| 23 |
import java.io.FileInputStream; |
| 24 |
import java.io.FileOutputStream; |
| 25 |
import java.io.IOException; |
| 26 |
import java.io.InputStream; |
| 27 |
import java.io.OutputStream; |
| 28 |
import java.io.PrintWriter; |
| 29 |
import java.net.URL; |
| 30 |
import java.util.ArrayList; |
| 31 |
import java.util.List; |
| 32 |
import java.util.jar.Attributes; |
| 33 |
import java.util.jar.JarFile; |
| 34 |
import java.util.jar.Manifest; |
| 35 |
|
| 36 |
import org.eclipse.gemini.web.internal.url.DirTransformer.DirTransformerCallback; |
| 37 |
import org.eclipse.virgo.util.io.IOUtils; |
| 38 |
import org.eclipse.virgo.util.io.PathReference; |
| 39 |
import org.junit.Test; |
| 40 |
|
| 41 |
public class DirTransformerTest { |
| 42 |
|
| 43 |
private static final String WEB_INF = "WEB-INF"; |
| 44 |
|
| 45 |
private static final String META_INF = "META-INF"; |
| 46 |
|
| 47 |
private static final String WEB_XML = "web.xml"; |
| 48 |
|
| 49 |
private static final String MANIFEST_MF = "MANIFEST.MF"; |
| 50 |
|
| 51 |
private static final String HEADER_1 = "Manifest-Version: 1.0"; |
| 52 |
|
| 53 |
private static final String HEADER_2 = "Class-Path: "; |
| 54 |
|
| 55 |
private static final String HEADER_3 = "Custom-Header: test"; |
| 56 |
|
| 57 |
private static final String SOURCE_URL = "file:target/test-classes/web-app-dir"; |
| 58 |
|
| 59 |
private static final String TARGET_URL = "file:target/test-classes/temp/web-app-dir"; |
| 60 |
|
| 61 |
@Test |
| 62 |
public void testTransformManifestAdded() throws Exception { |
| 63 |
URL directory = new URL(SOURCE_URL); |
| 64 |
URL tempDirectory = new URL(TARGET_URL); |
| 65 |
|
| 66 |
// Create content |
| 67 |
PathReference webAppDir = new PathReference(directory.getPath()); |
| 68 |
PathReference webXml = webAppDir.newChild(WEB_INF + File.separator + WEB_XML); |
| 69 |
webXml.createFile(); |
| 70 |
PathReference manifest = webAppDir.newChild(JarFile.MANIFEST_NAME); |
| 71 |
|
| 72 |
final List<PathReference> transformedFiles = new ArrayList<PathReference>(); |
| 73 |
DirTransformer transformer = new DirTransformer(new DirTransformerCallback() { |
| 74 |
|
| 75 |
public boolean transformFile(InputStream inputStream, PathReference toFile) throws IOException { |
| 76 |
transformedFiles.add(toFile); |
| 77 |
return false; |
| 78 |
} |
| 79 |
}); |
| 80 |
|
| 81 |
PathReference tempWebAppDir = new PathReference(tempDirectory.getPath()); |
| 82 |
transformer.transform(directory, tempDirectory, false); |
| 83 |
assertEquals(1, transformedFiles.size()); |
| 84 |
assertTrue(!manifest.exists()); |
| 85 |
assertTrue(!transformedFiles.contains(manifest)); |
| 86 |
assertTrue(tempWebAppDir.delete(true)); |
| 87 |
transformedFiles.clear(); |
| 88 |
|
| 89 |
transformer.transform(directory, tempDirectory, true); |
| 90 |
assertEquals(2, transformedFiles.size()); |
| 91 |
assertTrue(!manifest.exists()); |
| 92 |
assertTrue(transformedFiles.contains(tempWebAppDir.newChild(JarFile.MANIFEST_NAME))); |
| 93 |
|
| 94 |
assertTrue(tempWebAppDir.delete(true)); |
| 95 |
assertTrue(webAppDir.delete(true)); |
| 96 |
} |
| 97 |
|
| 98 |
@Test |
| 99 |
public void testTransformManifestChanged() throws Exception { |
| 100 |
URL directory = new URL(SOURCE_URL); |
| 101 |
URL tempDirectory = new URL(TARGET_URL); |
| 102 |
|
| 103 |
// Create content |
| 104 |
PathReference webAppDir = new PathReference(directory.getPath()); |
| 105 |
PathReference manifest = webAppDir.newChild(JarFile.MANIFEST_NAME); |
| 106 |
manifest.getParent().createDirectory(); |
| 107 |
createManifest(manifest.toFile(), HEADER_1, HEADER_2); |
| 108 |
|
| 109 |
DirTransformer transformer = new DirTransformer(new DirTransformerCallback() { |
| 110 |
|
| 111 |
public boolean transformFile(InputStream inputStream, PathReference toFile) throws IOException { |
| 112 |
if (MANIFEST_MF.equals(toFile.getName()) && META_INF.equals(toFile.getParent().getName())) { |
| 113 |
toFile.getParent().createDirectory(); |
| 114 |
createManifest(toFile.toFile(), HEADER_3); |
| 115 |
return true; |
| 116 |
} else { |
| 117 |
return false; |
| 118 |
} |
| 119 |
} |
| 120 |
}); |
| 121 |
|
| 122 |
transformer.transform(directory, tempDirectory); |
| 123 |
PathReference tempWebAppDir = new PathReference(tempDirectory.getPath()); |
| 124 |
checkManifest(tempWebAppDir.newChild(JarFile.MANIFEST_NAME).toFile()); |
| 125 |
|
| 126 |
assertTrue(tempWebAppDir.delete(true)); |
| 127 |
assertTrue(webAppDir.delete(true)); |
| 128 |
} |
| 129 |
|
| 130 |
@Test |
| 131 |
public void testTransformNoChanges() throws Exception { |
| 132 |
URL directory = new URL(SOURCE_URL); |
| 133 |
URL tempDirectory = new URL(TARGET_URL); |
| 134 |
|
| 135 |
// Create content |
| 136 |
PathReference webAppDir = new PathReference(directory.getPath()); |
| 137 |
PathReference webXml = webAppDir.newChild(WEB_INF + File.separator + WEB_XML); |
| 138 |
webXml.createFile(); |
| 139 |
|
| 140 |
DirTransformer transformer; |
| 141 |
try { |
| 142 |
transformer = new DirTransformer(null); |
| 143 |
} catch (Exception e) { |
| 144 |
assertTrue("Callback must not be null".equals(e.getMessage())); |
| 145 |
} |
| 146 |
|
| 147 |
transformer = new DirTransformer(new DirTransformerCallback() { |
| 148 |
|
| 149 |
public boolean transformFile(InputStream inputStream, PathReference toFile) throws IOException { |
| 150 |
return false; |
| 151 |
} |
| 152 |
}); |
| 153 |
|
| 154 |
transformer.transform(directory, tempDirectory); |
| 155 |
PathReference tempWebAppDir = new PathReference(tempDirectory.getPath()); |
| 156 |
assertDirsSame(webAppDir.toFile(), tempWebAppDir.toFile()); |
| 157 |
|
| 158 |
assertTrue(tempWebAppDir.delete(true)); |
| 159 |
assertTrue(webAppDir.delete(true)); |
| 160 |
} |
| 161 |
|
| 162 |
private void assertDirsSame(File source, File destination) throws IOException { |
| 163 |
assertEquals(source.getName(), destination.getName()); |
| 164 |
assertEquals(source.length(), destination.length()); |
| 165 |
|
| 166 |
File[] sourceFiles = source.listFiles(); |
| 167 |
File[] destinationFiles = destination.listFiles(); |
| 168 |
assertEquals(sourceFiles.length, destinationFiles.length); |
| 169 |
|
| 170 |
for (int i = 0; i < sourceFiles.length; i++) { |
| 171 |
File sourceFile = sourceFiles[i]; |
| 172 |
File destinationFile = destinationFiles[i]; |
| 173 |
assertEquals(sourceFile.getName(), destinationFile.getName()); |
| 174 |
assertEquals(sourceFile.length(), destinationFile.length()); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
private void checkManifest(File manifestFile) throws IOException { |
| 179 |
InputStream is = new FileInputStream(manifestFile); |
| 180 |
Manifest manifest = new Manifest(is); |
| 181 |
Attributes attr = manifest.getMainAttributes(); |
| 182 |
String value = attr.getValue("Custom-Header"); |
| 183 |
assertEquals("test", value); |
| 184 |
assertEquals(1, attr.size()); |
| 185 |
IOUtils.closeQuietly(is); |
| 186 |
} |
| 187 |
|
| 188 |
private void createManifest(File manifest, String... headers) throws IOException { |
| 189 |
OutputStream outputStream = null; |
| 190 |
PrintWriter writer = null; |
| 191 |
try { |
| 192 |
outputStream = new FileOutputStream(manifest); |
| 193 |
writer = new PrintWriter(manifest); |
| 194 |
for (String header : headers) { |
| 195 |
writer.println(header); |
| 196 |
} |
| 197 |
writer.println(); |
| 198 |
} finally { |
| 199 |
IOUtils.closeQuietly(writer); |
| 200 |
IOUtils.closeQuietly(outputStream); |
| 201 |
} |
| 202 |
} |
| 203 |
} |