|
Added
Link Here
|
| 1 |
package org.eclipse.equinox.p2.tests.ant; |
| 2 |
|
| 3 |
import java.io.*; |
| 4 |
import java.net.URI; |
| 5 |
import java.util.Iterator; |
| 6 |
import org.eclipse.core.runtime.CoreException; |
| 7 |
import org.eclipse.core.runtime.URIUtil; |
| 8 |
import org.eclipse.equinox.internal.p2.metadata.InstallableUnit; |
| 9 |
import org.eclipse.equinox.internal.provisional.p2.artifact.repository.*; |
| 10 |
import org.eclipse.equinox.internal.provisional.p2.core.ProvisionException; |
| 11 |
import org.eclipse.equinox.internal.provisional.p2.core.Version; |
| 12 |
import org.eclipse.equinox.internal.provisional.p2.metadata.IArtifactKey; |
| 13 |
import org.eclipse.equinox.internal.provisional.p2.metadata.IInstallableUnit; |
| 14 |
import org.eclipse.equinox.internal.provisional.p2.metadata.query.InstallableUnitQuery; |
| 15 |
import org.eclipse.equinox.internal.provisional.p2.metadata.repository.IMetadataRepository; |
| 16 |
import org.eclipse.equinox.internal.provisional.p2.metadata.repository.IMetadataRepositoryManager; |
| 17 |
import org.eclipse.equinox.internal.provisional.p2.query.Collector; |
| 18 |
import org.eclipse.equinox.p2.tests.AbstractAntProvisioningTest; |
| 19 |
import org.eclipse.equinox.p2.tests.AbstractProvisioningTest; |
| 20 |
import org.eclipse.equinox.spi.p2.publisher.PublisherHelper; |
| 21 |
import org.eclipse.osgi.util.NLS; |
| 22 |
|
| 23 |
public class MirrorTaskTest extends AbstractAntProvisioningTest { |
| 24 |
private URI destinationRepo; |
| 25 |
private URI sourceRepo2; |
| 26 |
|
| 27 |
public void setUp() throws Exception { |
| 28 |
super.setUp(); |
| 29 |
// Get a random location to create a repository |
| 30 |
destinationRepo = (new File(getTempFolder(), getUniqueString())).toURI(); |
| 31 |
sourceRepo2 = getTestData("loading error data", "testData/mirror/mirrorSourceRepo2").toURI(); |
| 32 |
} |
| 33 |
|
| 34 |
public void tearDown() throws Exception { |
| 35 |
// Remove repository manager references |
| 36 |
getArtifactRepositoryManager().removeRepository(destinationRepo); |
| 37 |
getMetadataRepositoryManager().removeRepository(destinationRepo); |
| 38 |
getArtifactRepositoryManager().removeRepository(sourceRepo2); |
| 39 |
getMetadataRepositoryManager().removeRepository(sourceRepo2); |
| 40 |
// Cleanup disk |
| 41 |
delete(new File(destinationRepo)); |
| 42 |
super.tearDown(); |
| 43 |
} |
| 44 |
|
| 45 |
/* |
| 46 |
* Test that all IUs can be mirrored |
| 47 |
*/ |
| 48 |
public void testMirrorAllIUSpecified() throws ProvisionException { |
| 49 |
AntTaskElement mirror = createMirrorTask(); |
| 50 |
mirror.addElement(createSourceElement(sourceRepo2, sourceRepo2)); |
| 51 |
addAllIUs(mirror, getMetadataRepositoryManager().loadRepository(sourceRepo2, null)); |
| 52 |
runAntTask(); |
| 53 |
|
| 54 |
try { |
| 55 |
assertEquals("Different number of Artifact Keys", getArtifactKeyCount(sourceRepo2), getArtifactKeyCount(destinationRepo)); |
| 56 |
assertEquals("Different number of IUs", getIUCount(sourceRepo2), getIUCount(destinationRepo)); |
| 57 |
} catch (ProvisionException e) { |
| 58 |
fail("Failed to load Repository", e); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/* |
| 63 |
* Test that we only mirror specified IUs & Artifacts |
| 64 |
*/ |
| 65 |
public void testMirrorSomeIUSpecified() { |
| 66 |
AntTaskElement mirror = createMirrorTask(); |
| 67 |
mirror.addElement(createSourceElement(sourceRepo2, sourceRepo2)); |
| 68 |
mirror.addElement(createIUElement("anotherplugin", "1.0.0")); |
| 69 |
|
| 70 |
runAntTask(); |
| 71 |
|
| 72 |
try { |
| 73 |
assertEquals("Wrong number of Artifact Keys", 1, getArtifactKeyCount(destinationRepo)); |
| 74 |
assertEquals("Wrong number of IUs", 1, getIUCount(destinationRepo)); |
| 75 |
} catch (ProvisionException e) { |
| 76 |
fail("Failed to load Repository", e); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/* |
| 81 |
* Test that the proper exception is thrown when no IU is provided |
| 82 |
*/ |
| 83 |
public void testMirrorNoIUNoRepo() { |
| 84 |
AntTaskElement mirror = createMirrorTask(); |
| 85 |
mirror.addElement(createSourceElement(sourceRepo2, null)); |
| 86 |
|
| 87 |
Exception exception = null; |
| 88 |
try { |
| 89 |
runAntTaskWithExceptions(); |
| 90 |
} catch (CoreException e) { |
| 91 |
exception = e; |
| 92 |
} |
| 93 |
if (exception == null) |
| 94 |
fail("No exception thrown"); |
| 95 |
if (!(rootCause(exception) instanceof ProvisionException) && !rootCause(exception).getMessage().contains("No IUs")) |
| 96 |
fail("Exception is of an unexpected type or message", rootCause(exception)); |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
/* |
| 101 |
* Test that all IUs are mirrored when none are specified |
| 102 |
*/ |
| 103 |
public void testMirrorNoIUSpecified() { |
| 104 |
AntTaskElement mirror = createMirrorTask(); |
| 105 |
mirror.addElement(createSourceElement(sourceRepo2, sourceRepo2)); |
| 106 |
|
| 107 |
runAntTask(); |
| 108 |
|
| 109 |
try { |
| 110 |
assertEquals("Different number of Artifact Keys", getArtifactKeyCount(sourceRepo2), getArtifactKeyCount(destinationRepo)); |
| 111 |
assertEquals("Different number of IUs", getIUCount(sourceRepo2), getIUCount(destinationRepo)); |
| 112 |
} catch (ProvisionException e) { |
| 113 |
fail("Failed to load Repository", e); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
/* |
| 118 |
* |
| 119 |
*/ |
| 120 |
public void testBaselineCompareUsingMD5Comparator() { |
| 121 |
//Setup create descriptors with different md5 values |
| 122 |
IArtifactKey dupKey = PublisherHelper.createBinaryArtifactKey("testKeyId", new Version("1.2.3")); |
| 123 |
File artifact1 = getTestData("0.0", "/testData/mirror/mirrorSourceRepo1 with space/content.xml"); |
| 124 |
File artifact2 = getTestData("0.0", "/testData/mirror/mirrorSourceRepo2/content.xml"); |
| 125 |
|
| 126 |
//Setup Copy the file to the baseline |
| 127 |
File repoLocation = getTestFolder(getUniqueString()); |
| 128 |
File baselineLocation = getTestFolder(getUniqueString()); |
| 129 |
File baselineBinaryDirectory = new File(baselineLocation, "binary"); |
| 130 |
baselineBinaryDirectory.mkdir(); |
| 131 |
File baselineContentLocation = new File(baselineBinaryDirectory, "testKeyId_1.2.3"); |
| 132 |
AbstractProvisioningTest.copy("Copying File to baseline", artifact2, baselineContentLocation); |
| 133 |
|
| 134 |
IArtifactDescriptor descriptor1 = PublisherHelper.createArtifactDescriptor(dupKey, artifact1); |
| 135 |
IArtifactDescriptor descriptor2 = PublisherHelper.createArtifactDescriptor(dupKey, baselineContentLocation); |
| 136 |
|
| 137 |
assertEquals("Ensuring Descriptors are the same", descriptor1, descriptor2); |
| 138 |
assertNotSame("Ensuring MD5 values are different", descriptor1.getProperty(IArtifactDescriptor.DOWNLOAD_MD5), descriptor2.getProperty(IArtifactDescriptor.DOWNLOAD_MD5)); |
| 139 |
|
| 140 |
//Setup make repositories |
| 141 |
IArtifactRepository repo = null; |
| 142 |
IArtifactRepository baseline = null; |
| 143 |
try { |
| 144 |
repo = createAndAddIU(repoLocation.toURI(), descriptor1); |
| 145 |
baseline = createAndAddIU(baselineLocation.toURI(), descriptor2); |
| 146 |
} catch (ProvisionException e) { |
| 147 |
fail("Error creating repositories", e); |
| 148 |
} |
| 149 |
|
| 150 |
//Comparator prints to stderr, redirect that to a file |
| 151 |
PrintStream oldErr = System.err; |
| 152 |
PrintStream newErr = null; |
| 153 |
PrintStream oldOut = System.out; |
| 154 |
PrintStream newOut = null; |
| 155 |
try { |
| 156 |
(new File(destinationRepo)).mkdir(); |
| 157 |
newErr = new PrintStream(new FileOutputStream(new File(new File(destinationRepo), "sys.err"))); |
| 158 |
newOut = new PrintStream(new FileOutputStream(new File(new File(destinationRepo), "sys.out"))); |
| 159 |
} catch (FileNotFoundException e) { |
| 160 |
fail("Error redirecting outputs", e); |
| 161 |
} |
| 162 |
|
| 163 |
try { |
| 164 |
System.setErr(newErr); |
| 165 |
System.setOut(newOut); |
| 166 |
|
| 167 |
AntTaskElement mirror = createMirrorTask(); |
| 168 |
mirror.addElement(createSourceElement(repoLocation.toURI(), repoLocation.toURI())); |
| 169 |
mirror.addAttribute("baseline", URIUtil.toUnencodedString(baselineLocation.toURI())); |
| 170 |
mirror.addAttribute("verbose", String.valueOf(true)); |
| 171 |
mirror.addAttribute("compare", String.valueOf(true)); |
| 172 |
|
| 173 |
runAntTaskWithExceptions(); |
| 174 |
//Set compareAgaist |
| 175 |
//String[] args = new String[] {"-source", repoLocation.toURL().toExternalForm(), "-destination", destinationRepo.toURL().toExternalForm(), "-compareAgainst", baselineLocation.toURL().toExternalForm(), "-verbose", "-compare"}; |
| 176 |
//run the mirror application |
| 177 |
//runMirrorApplication("Running with baseline compare", args); |
| 178 |
} catch (Exception e) { |
| 179 |
fail("Running mirror application with baseline compare", rootCause(e)); |
| 180 |
} finally { |
| 181 |
System.setErr(oldErr); |
| 182 |
newErr.close(); |
| 183 |
System.setOut(oldOut); |
| 184 |
newOut.close(); |
| 185 |
} |
| 186 |
|
| 187 |
IArtifactRepository destination = null; |
| 188 |
try { |
| 189 |
destination = getArtifactRepositoryManager().loadRepository(destinationRepo, null); |
| 190 |
} catch (ProvisionException e) { |
| 191 |
fail("Error loading destination", e); |
| 192 |
} |
| 193 |
|
| 194 |
IArtifactDescriptor[] destDescriptors = destination.getArtifactDescriptors(descriptor2.getArtifactKey()); |
| 195 |
assertEquals("Ensuring destination has correct number of descriptors", 1, destDescriptors.length); |
| 196 |
assertEquals("Ensuring destination contains the descriptor from the baseline", descriptor2.getProperty(IArtifactDescriptor.DOWNLOAD_MD5), destDescriptors[0].getProperty(IArtifactDescriptor.DOWNLOAD_MD5)); |
| 197 |
String msg = NLS.bind("have different MD5 sums for {2}", new Object[] {baseline, repo, descriptor1}); |
| 198 |
assertLogContains(msg); |
| 199 |
} |
| 200 |
|
| 201 |
/* |
| 202 |
* Create an IU for a descriptor and the IU+descriptor to the specified repo |
| 203 |
*/ |
| 204 |
protected IArtifactRepository createAndAddIU(URI repoLocation, IArtifactDescriptor descriptor) throws ProvisionException { |
| 205 |
IArtifactRepository artifactRepo = getArtifactRepositoryManager().createRepository(repoLocation, "Repo 1", IArtifactRepositoryManager.TYPE_SIMPLE_REPOSITORY, null); |
| 206 |
artifactRepo.addDescriptor(descriptor); |
| 207 |
|
| 208 |
IMetadataRepository metaRepo = getMetadataRepositoryManager().createRepository(repoLocation, "Repo", IMetadataRepositoryManager.TYPE_SIMPLE_REPOSITORY, null); |
| 209 |
InstallableUnit iu = new InstallableUnit(); |
| 210 |
iu.setId(descriptor.getArtifactKey().getId() + "IU"); |
| 211 |
iu.setVersion(descriptor.getArtifactKey().getVersion()); |
| 212 |
iu.setArtifacts(new IArtifactKey[] {descriptor.getArtifactKey()}); |
| 213 |
metaRepo.addInstallableUnits(new IInstallableUnit[] {iu}); |
| 214 |
|
| 215 |
return artifactRepo; |
| 216 |
} |
| 217 |
|
| 218 |
/* |
| 219 |
* Get the number of artifact keys in a repository |
| 220 |
*/ |
| 221 |
protected int getArtifactKeyCount(URI location) throws ProvisionException { |
| 222 |
return getArtifactRepositoryManager().loadRepository(location, null).getArtifactKeys().length; |
| 223 |
} |
| 224 |
|
| 225 |
/* |
| 226 |
* Get the number of IUs in a repository |
| 227 |
*/ |
| 228 |
protected int getIUCount(URI location) throws ProvisionException { |
| 229 |
return getMetadataRepositoryManager().loadRepository(location, null).query(InstallableUnitQuery.ANY, new Collector(), null).size(); |
| 230 |
} |
| 231 |
|
| 232 |
/* |
| 233 |
* Add all IUs to the parent element |
| 234 |
*/ |
| 235 |
protected void addAllIUs(AntTaskElement parent, IMetadataRepository repo) { |
| 236 |
Collector collector = repo.query(InstallableUnitQuery.ANY, new Collector(), null); |
| 237 |
|
| 238 |
for (Iterator iter = collector.iterator(); iter.hasNext();) { |
| 239 |
IInstallableUnit iu = (IInstallableUnit) iter.next(); |
| 240 |
parent.addElement(createIUElement(iu.getId(), iu.getVersion().toString())); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
/* |
| 245 |
* Create an element from the specified information |
| 246 |
*/ |
| 247 |
protected AntTaskElement createIUElement(String id, String version) { |
| 248 |
AntTaskElement iu = new AntTaskElement("iu"); |
| 249 |
iu.addAttributes(new String[] {"id", id, "version", version}); |
| 250 |
return iu; |
| 251 |
} |
| 252 |
|
| 253 |
/* |
| 254 |
* Create the base mirror task & add it to the script |
| 255 |
*/ |
| 256 |
protected AntTaskElement createMirrorTask() { |
| 257 |
AntTaskElement mirror = new AntTaskElement("p2.mirror"); |
| 258 |
mirror.addElement(getRepositoryElement(destinationRepo, null)); |
| 259 |
addTask(mirror); |
| 260 |
return mirror; |
| 261 |
} |
| 262 |
|
| 263 |
/* |
| 264 |
* Create a source element with the specified repositories |
| 265 |
*/ |
| 266 |
protected AntTaskElement createSourceElement(URI artifact, URI metadata) { |
| 267 |
AntTaskElement source = new AntTaskElement("source"); |
| 268 |
if (artifact != null) |
| 269 |
source.addElement(getRepositoryElement(artifact, AbstractAntProvisioningTest.TYPE_ARTIFACT)); |
| 270 |
if (metadata != null) |
| 271 |
source.addElement(getRepositoryElement(metadata, AbstractAntProvisioningTest.TYPE_METADATA)); |
| 272 |
return source; |
| 273 |
} |
| 274 |
} |