diff options
author | Raphael Moll <ralf@android.com> | 2013-07-16 20:12:02 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2013-07-16 20:12:02 +0000 |
commit | 3b77d166e1de905c81830a0efd16cc0dcb8d7678 (patch) | |
tree | 4b08d9c8078a9b5ac5022a66ddd80bb0b0659b26 | |
parent | 633100893886509f69bade1ab3ed27dec1289a3b (diff) | |
parent | 660d668f86809135760a7ab9e4215cd66ba678b5 (diff) | |
download | base-3b77d166e1de905c81830a0efd16cc0dcb8d7678.tar.gz |
Merge "New NoPreviewRevision type."
6 files changed, 279 insertions, 20 deletions
diff --git a/sdklib/src/main/java/com/android/sdklib/repository/FullRevision.java b/sdklib/src/main/java/com/android/sdklib/repository/FullRevision.java index d81de91984..c7bc84b406 100755 --- a/sdklib/src/main/java/com/android/sdklib/repository/FullRevision.java +++ b/sdklib/src/main/java/com/android/sdklib/repository/FullRevision.java @@ -47,7 +47,7 @@ public class FullRevision implements Comparable<FullRevision> { private final int mPreview; public FullRevision(int major) { - this(major, 0, 0); + this(major, IMPLICIT_MINOR_REV, IMPLICIT_MICRO_REV); } public FullRevision(int major, int minor, int micro) { @@ -99,32 +99,71 @@ public class FullRevision implements Comparable<FullRevision> { @NonNull public static FullRevision parseRevision(@NonNull String revision) throws NumberFormatException { + return parseRevisionImpl(revision, true /*supportMinorMicro*/, true /*supportPreview*/); + } + @NonNull + protected static FullRevision parseRevisionImpl(@NonNull String revision, + boolean supportMinorMicro, + boolean supportPreview) + throws NumberFormatException { if (revision == null) { throw new NumberFormatException("revision is <null>"); //$NON-NLS-1$ } Throwable cause = null; + String error = null; try { Matcher m = FULL_REVISION_PATTERN.matcher(revision); if (m != null && m.matches()) { int major = Integer.parseInt(m.group(1)); + + int minor = IMPLICIT_MINOR_REV; + int micro = IMPLICIT_MICRO_REV; + int preview = NOT_A_PREVIEW; + String s = m.group(2); - int minor = s == null ? IMPLICIT_MINOR_REV : Integer.parseInt(s); + if (s != null) { + if (!supportMinorMicro) { + error = " -- Minor number not supported"; //$NON-NLS-1$ + } else { + minor = Integer.parseInt(s); + } + } + s = m.group(3); - int micro = s == null ? IMPLICIT_MICRO_REV : Integer.parseInt(s); - s = m.group(4); - int preview = s == null ? NOT_A_PREVIEW : Integer.parseInt(s); + if (s != null) { + if (!supportMinorMicro) { + error = " -- Micro number not supported"; //$NON-NLS-1$ + } else { + micro = Integer.parseInt(s); + } + } - return new FullRevision(major, minor, micro, preview); + s = m.group(4); + if (s != null) { + if (!supportPreview) { + error = " -- Preview number not supported"; //$NON-NLS-1$ + } else { + preview = Integer.parseInt(s); + } + } + + if (error == null) { + return new FullRevision(major, minor, micro, preview); + } } } catch (Throwable t) { cause = t; } NumberFormatException n = new NumberFormatException( - "Invalid full revision: " + revision); //$NON-NLS-1$ - n.initCause(cause); + "Invalid revision: " //$NON-NLS-1$ + + revision + + (error == null ? "" : error)); + if (cause != null) { + n.initCause(cause); + } throw n; } diff --git a/sdklib/src/main/java/com/android/sdklib/repository/MajorRevision.java b/sdklib/src/main/java/com/android/sdklib/repository/MajorRevision.java index c953e84933..9ee7b31309 100755 --- a/sdklib/src/main/java/com/android/sdklib/repository/MajorRevision.java +++ b/sdklib/src/main/java/com/android/sdklib/repository/MajorRevision.java @@ -29,7 +29,7 @@ import com.android.annotations.NonNull; public class MajorRevision extends FullRevision { public MajorRevision(int major) { - super(major, 0, 0); + super(major, IMPLICIT_MINOR_REV, IMPLICIT_MICRO_REV); } @Override @@ -47,11 +47,8 @@ public class MajorRevision extends FullRevision { @NonNull public static MajorRevision parseRevision(@NonNull String revision) throws NumberFormatException { - - if (revision == null) { - throw new NumberFormatException("revision is <null>"); //$NON-NLS-1$ - } - - return new MajorRevision(Integer.parseInt(revision.trim())); + FullRevision r = parseRevisionImpl( + revision, false /*supportMinorMicro*/, false /*supportPreview*/); + return new MajorRevision(r.getMajor()); } } diff --git a/sdklib/src/main/java/com/android/sdklib/repository/NoPreviewRevision.java b/sdklib/src/main/java/com/android/sdklib/repository/NoPreviewRevision.java new file mode 100755 index 0000000000..23242f868b --- /dev/null +++ b/sdklib/src/main/java/com/android/sdklib/repository/NoPreviewRevision.java @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * 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.android.sdklib.repository; + +import com.android.annotations.NonNull; + + +/** + * Package multi-part revision number composed of a tuple + * (major.minor.micro) but without support for any optional preview number. + * + * @see FullRevision + */ +public class NoPreviewRevision extends FullRevision { + + public NoPreviewRevision(int major) { + this(major, IMPLICIT_MINOR_REV, IMPLICIT_MICRO_REV); + } + + public NoPreviewRevision(int major, int minor, int micro) { + super(major, minor, micro, NOT_A_PREVIEW); + } + + /** + * Parses a string of format "major.minor.micro" and returns + * a new {@link NoPreviewRevision} for it. All the fields except major are + * optional. + * <p/> + * The parsing is equivalent to the pseudo-BNF/regexp: + * <pre> + * Major/Minor/Micro/Preview := [0-9]+ + * Revision := Major ('.' Minor ('.' Micro)? )? \s* + * </pre> + * + * @param revision A non-null revision to parse. + * @return A new non-null {@link NoPreviewRevision}. + * @throws NumberFormatException if the parsing failed. + */ + @NonNull + public static NoPreviewRevision parseRevision(@NonNull String revision) + throws NumberFormatException { + FullRevision r = parseRevisionImpl( + revision, true /*supportMinorMicro*/, false /*supportPreview*/); + return new NoPreviewRevision(r.getMajor(), r.getMinor(), r.getMicro()); + } +} diff --git a/sdklib/src/test/java/com/android/sdklib/internal/repository/packages/FullRevisionTest.java b/sdklib/src/test/java/com/android/sdklib/internal/repository/packages/FullRevisionTest.java index 28030521d2..386a2ee14d 100755 --- a/sdklib/src/test/java/com/android/sdklib/internal/repository/packages/FullRevisionTest.java +++ b/sdklib/src/test/java/com/android/sdklib/internal/repository/packages/FullRevisionTest.java @@ -100,7 +100,7 @@ public class FullRevisionTest extends TestCase { } catch (NumberFormatException e) { errorMsg = e.getMessage(); } - assertEquals("Invalid full revision: not a number", errorMsg); + assertEquals("Invalid revision: not a number", errorMsg); errorMsg = null; try { @@ -109,7 +109,7 @@ public class FullRevisionTest extends TestCase { } catch (NumberFormatException e) { errorMsg = e.getMessage(); } - assertEquals("Invalid full revision: 5 .6 .7", errorMsg); + assertEquals("Invalid revision: 5 .6 .7", errorMsg); errorMsg = null; try { @@ -118,7 +118,7 @@ public class FullRevisionTest extends TestCase { } catch (NumberFormatException e) { errorMsg = e.getMessage(); } - assertEquals("Invalid full revision: 5.0.0 preview 1", errorMsg); + assertEquals("Invalid revision: 5.0.0 preview 1", errorMsg); errorMsg = null; try { @@ -127,7 +127,7 @@ public class FullRevisionTest extends TestCase { } catch (NumberFormatException e) { errorMsg = e.getMessage(); } - assertEquals("Invalid full revision: 5.1.2 rc 42 ", errorMsg); + assertEquals("Invalid revision: 5.1.2 rc 42 ", errorMsg); } public final void testCompareTo() { diff --git a/sdklib/src/test/java/com/android/sdklib/internal/repository/packages/MajorRevisionTest.java b/sdklib/src/test/java/com/android/sdklib/internal/repository/packages/MajorRevisionTest.java index b6dc5d4f4a..a63550a60f 100755 --- a/sdklib/src/test/java/com/android/sdklib/internal/repository/packages/MajorRevisionTest.java +++ b/sdklib/src/test/java/com/android/sdklib/internal/repository/packages/MajorRevisionTest.java @@ -45,7 +45,7 @@ public class MajorRevisionTest extends TestCase { } catch (NumberFormatException e) { errorMsg = e.getMessage(); } - assertEquals("For input string: \"5.0.0\"", errorMsg); + assertEquals("Invalid revision: 5.0.0 -- Micro number not supported", errorMsg); } public final void testCompareTo() { diff --git a/sdklib/src/test/java/com/android/sdklib/internal/repository/packages/NoPreviewRevisionTest.java b/sdklib/src/test/java/com/android/sdklib/internal/repository/packages/NoPreviewRevisionTest.java new file mode 100755 index 0000000000..fb3225da13 --- /dev/null +++ b/sdklib/src/test/java/com/android/sdklib/internal/repository/packages/NoPreviewRevisionTest.java @@ -0,0 +1,163 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * 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.android.sdklib.internal.repository.packages; + +import com.android.sdklib.repository.NoPreviewRevision; + +import java.util.Arrays; + +import junit.framework.TestCase; + +public class NoPreviewRevisionTest extends TestCase { + + public final void testNoPreviewRevision() { + NoPreviewRevision p = new NoPreviewRevision(5); + assertEquals(5, p.getMajor()); + assertEquals(NoPreviewRevision.IMPLICIT_MINOR_REV, p.getMinor()); + assertEquals(NoPreviewRevision.IMPLICIT_MICRO_REV, p.getMicro()); + assertEquals(NoPreviewRevision.NOT_A_PREVIEW, p.getPreview()); + assertFalse (p.isPreview()); + assertEquals("5", p.toShortString()); + assertEquals(p, NoPreviewRevision.parseRevision("5")); + assertEquals("5.0.0", p.toString()); + assertEquals(p, NoPreviewRevision.parseRevision("5.0.0")); + assertEquals("[5, 0, 0]", Arrays.toString(p.toIntArray(false /*includePreview*/))); + assertEquals("[5, 0, 0, 0]", Arrays.toString(p.toIntArray(true /*includePreview*/))); + + p = new NoPreviewRevision(5, 0, 0); + assertEquals(5, p.getMajor()); + assertEquals(NoPreviewRevision.IMPLICIT_MINOR_REV, p.getMinor()); + assertEquals(NoPreviewRevision.IMPLICIT_MICRO_REV, p.getMicro()); + assertEquals(NoPreviewRevision.NOT_A_PREVIEW, p.getPreview()); + assertFalse (p.isPreview()); + assertEquals("5", p.toShortString()); + assertEquals(p, NoPreviewRevision.parseRevision("5")); + assertEquals("5.0.0", p.toString()); + assertEquals(p, NoPreviewRevision.parseRevision("5.0.0")); + assertEquals("[5, 0, 0]", Arrays.toString(p.toIntArray(false /*includePreview*/))); + assertEquals("[5, 0, 0, 0]", Arrays.toString(p.toIntArray(true /*includePreview*/))); + + p = new NoPreviewRevision(6, 7, 0); + assertEquals(6, p.getMajor()); + assertEquals(7, p.getMinor()); + assertEquals(0, p.getMicro()); + assertEquals(0, p.getPreview()); + assertFalse (p.isPreview()); + assertEquals("6.7", p.toShortString()); + assertEquals(p, NoPreviewRevision.parseRevision("6.7")); + assertEquals("6.7.0", p.toString()); + assertEquals(p, NoPreviewRevision.parseRevision("6.7.0")); + assertEquals("[6, 7, 0]", Arrays.toString(p.toIntArray(false /*includePreview*/))); + assertEquals("[6, 7, 0, 0]", Arrays.toString(p.toIntArray(true /*includePreview*/))); + + p = new NoPreviewRevision(10, 11, 12); + assertEquals(10, p.getMajor()); + assertEquals(11, p.getMinor()); + assertEquals(12, p.getMicro()); + assertEquals(0, p.getPreview()); + assertFalse (p.isPreview()); + assertEquals("10.11.12", p.toShortString()); + assertEquals("10.11.12", p.toString()); + assertEquals(p, NoPreviewRevision.parseRevision("10.11.12")); + assertEquals("[10, 11, 12]", Arrays.toString(p.toIntArray(false /*includePreview*/))); + assertEquals("[10, 11, 12, 0]", Arrays.toString(p.toIntArray(true /*includePreview*/))); + + p = new NoPreviewRevision(10, 11, 12); + assertEquals(10, p.getMajor()); + assertEquals(11, p.getMinor()); + assertEquals(12, p.getMicro()); + assertEquals(NoPreviewRevision.NOT_A_PREVIEW, p.getPreview()); + assertFalse (p.isPreview()); + assertEquals("10.11.12", p.toShortString()); + assertEquals("10.11.12", p.toString()); + assertEquals(p, NoPreviewRevision.parseRevision("10.11.12")); + assertEquals(p, NoPreviewRevision.parseRevision(" 10.11.12")); + assertEquals(p, NoPreviewRevision.parseRevision("10.11.12 ")); + assertEquals(p, NoPreviewRevision.parseRevision(" 10.11.12 ")); + assertEquals("[10, 11, 12]", Arrays.toString(p.toIntArray(false /*includePreview*/))); + assertEquals("[10, 11, 12, 0]", Arrays.toString(p.toIntArray(true /*includePreview*/))); + } + + public final void testParseError() { + String errorMsg = null; + try { + NoPreviewRevision.parseRevision("not a number"); + fail("NoPreviewRevision.parseRevision should thrown NumberFormatException"); + } catch (NumberFormatException e) { + errorMsg = e.getMessage(); + } + assertEquals("Invalid revision: not a number", errorMsg); + + errorMsg = null; + try { + NoPreviewRevision.parseRevision("5 .6 .7"); + fail("NoPreviewRevision.parseRevision should thrown NumberFormatException"); + } catch (NumberFormatException e) { + errorMsg = e.getMessage(); + } + assertEquals("Invalid revision: 5 .6 .7", errorMsg); + + errorMsg = null; + try { + NoPreviewRevision.parseRevision("5.0.0 preview 1"); + fail("NoPreviewRevision.parseRevision should thrown NumberFormatException"); + } catch (NumberFormatException e) { + errorMsg = e.getMessage(); + } + assertEquals("Invalid revision: 5.0.0 preview 1", errorMsg); + + errorMsg = null; + try { + NoPreviewRevision.parseRevision(" 5.1.2 rc 42 "); + fail("NoPreviewRevision.parseRevision should thrown NumberFormatException"); + } catch (NumberFormatException e) { + errorMsg = e.getMessage(); + } + assertEquals("Invalid revision: 5.1.2 rc 42 ", errorMsg); + } + + public final void testCompareTo() { + NoPreviewRevision a4 = new NoPreviewRevision(4); + NoPreviewRevision b4 = new NoPreviewRevision(4, 1, 0); + NoPreviewRevision c5 = new NoPreviewRevision(5); + NoPreviewRevision d5 = new NoPreviewRevision(5, 1, 0); + + NoPreviewRevision t4 = new NoPreviewRevision(4, 0, 0); + NoPreviewRevision u5 = new NoPreviewRevision(5, 0, 0); + + assertEquals(a4, t4); // 4.0.0 == 4.0.0 + assertFalse(a4.equals(b4)); // 4.0.0 != 4.1.0 + assertTrue (a4.compareTo(b4) < 0); // 4.0.0 < 4.1.0 + assertTrue (a4.compareTo(c5) < 0); // 4.0.0 < 5.0.0 + assertTrue (a4.compareTo(d5) < 0); // 4.0.0 < 5.1.0 + + assertTrue (b4.compareTo(a4) > 0); // 4.1.0 > 4.0.0 + assertTrue (b4.compareTo(c5) < 0); // 4.1.0 < 5.0.0 + assertTrue (b4.compareTo(d5) < 0); // 4.1.0 < 5.1.0 + + assertEquals(c5, u5); // 5.0.0 == 5.0.0 + assertFalse(c5.equals(d5)); // 5.0.0 != 5.1.0 + assertTrue (c5.compareTo(a4) > 0); // 5.0.0 > 4.0.0 + assertTrue (c5.compareTo(b4) > 0); // 5.0.0 > 4.1.0 + assertTrue (c5.compareTo(d5) < 0); // 5.0.0 < 5.1.0 + + assertTrue (d5.compareTo(a4) > 0); // 5.1.0 > 4.0.0 + assertTrue (d5.compareTo(b4) > 0); // 5.1.0 > 4.1.0 + assertTrue (d5.compareTo(c5) > 0); // 5.1.0 > 5.0.0 + } + +} |