001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.ivy.plugins.latest;
019:
020: import java.util.ArrayList;
021: import java.util.Arrays;
022: import java.util.Collections;
023: import java.util.Date;
024: import java.util.List;
025:
026: import junit.framework.TestCase;
027:
028: public class LatestRevisionStrategyTest extends TestCase {
029: public void testComparator() {
030: ArtifactInfo[] revs = toMockAI(new String[] { "0.2a", "0.2_b",
031: "0.2rc1", "0.2-final", "1.0-dev1", "1.0-dev2",
032: "1.0-alpha1", "1.0-alpha2", "1.0-beta1", "1.0-beta2",
033: "1.0-gamma", "1.0-rc1", "1.0-rc2", "1.0", "1.0.1",
034: "2.0" });
035:
036: List shuffled = new ArrayList(Arrays.asList(revs));
037: Collections.shuffle(shuffled);
038: Collections
039: .sort(
040: shuffled,
041: new LatestRevisionStrategy().new ArtifactInfoComparator());
042: assertEquals(Arrays.asList(revs), shuffled);
043: }
044:
045: public void testSort() {
046: ArtifactInfo[] revs = toMockAI(new String[] { "0.2a", "0.2_b",
047: "0.2rc1", "0.2-final", "1.0-dev1", "1.0-dev2",
048: "1.0-alpha1", "1.0-alpha2", "1.0-beta1", "1.0-beta2",
049: "1.0-gamma", "1.0-rc1", "1.0-rc2", "1.0", "1.0.1",
050: "2.0" });
051:
052: List shuffled = new ArrayList(Arrays.asList(revs));
053: ArtifactInfo[] shuffledRevs = (ArtifactInfo[]) shuffled
054: .toArray(new ArtifactInfo[revs.length]);
055:
056: LatestRevisionStrategy latestRevisionStrategy = new LatestRevisionStrategy();
057: List sorted = latestRevisionStrategy.sort(shuffledRevs);
058: assertEquals(Arrays.asList(revs), sorted);
059: }
060:
061: public void testFindLatest() {
062: ArtifactInfo[] revs = toMockAI(new String[] { "0.2a", "0.2_b",
063: "0.2rc1", "0.2-final", "1.0-dev1", "1.0-dev2",
064: "1.0-alpha1", "1.0-alpha2", "1.0-beta1", "1.0-beta2",
065: "1.0-gamma", "1.0-rc1", "1.0-rc2", "1.0", "1.0.1",
066: "2.0" });
067:
068: List shuffled = new ArrayList(Arrays.asList(revs));
069: ArtifactInfo[] shuffledRevs = (ArtifactInfo[]) shuffled
070: .toArray(new ArtifactInfo[revs.length]);
071:
072: LatestRevisionStrategy latestRevisionStrategy = new LatestRevisionStrategy();
073: ArtifactInfo latest = latestRevisionStrategy.findLatest(
074: shuffledRevs, new Date());
075: assertNotNull(latest);
076: assertEquals("2.0", latest.getRevision());
077: }
078:
079: public void testSpecialMeaningComparator() {
080: ArtifactInfo[] revs = toMockAI(new String[] { "0.1", "0.2-pre",
081: "0.2-dev", "0.2-rc1", "0.2-final", "0.2-QA", "1.0-dev1" });
082:
083: List shuffled = new ArrayList(Arrays.asList(revs));
084: Collections.shuffle(shuffled);
085: LatestRevisionStrategy latestRevisionStrategy = new LatestRevisionStrategy();
086: LatestRevisionStrategy.SpecialMeaning specialMeaning = new LatestRevisionStrategy.SpecialMeaning();
087: specialMeaning.setName("pre");
088: specialMeaning.setValue(new Integer(-2));
089: latestRevisionStrategy
090: .addConfiguredSpecialMeaning(specialMeaning);
091: specialMeaning = new LatestRevisionStrategy.SpecialMeaning();
092: specialMeaning.setName("QA");
093: specialMeaning.setValue(new Integer(4));
094: latestRevisionStrategy
095: .addConfiguredSpecialMeaning(specialMeaning);
096: Collections.sort(shuffled,
097: latestRevisionStrategy.new ArtifactInfoComparator());
098: assertEquals(Arrays.asList(revs), shuffled);
099: }
100:
101: private static class MockArtifactInfo implements ArtifactInfo {
102:
103: private long _lastModified;
104:
105: private String _rev;
106:
107: public MockArtifactInfo(String rev, long lastModified) {
108: _rev = rev;
109: _lastModified = lastModified;
110: }
111:
112: public String getRevision() {
113: return _rev;
114: }
115:
116: public long getLastModified() {
117: return _lastModified;
118: }
119:
120: public String toString() {
121: return _rev;
122: }
123: }
124:
125: private ArtifactInfo[] toMockAI(String[] revs) {
126: ArtifactInfo[] artifactInfos = new ArtifactInfo[revs.length];
127: for (int i = 0; i < artifactInfos.length; i++) {
128: artifactInfos[i] = new MockArtifactInfo(revs[i], 0);
129: }
130: return artifactInfos;
131: }
132:
133: }
|