1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.sdklib.internal.repository.packages; 18 19 import junit.framework.TestCase; 20 21 public class FullRevisionTest extends TestCase { 22 23 public final void testFullRevision() { 24 FullRevision p = new FullRevision(5); 25 assertEquals(5, p.getMajor()); 26 assertEquals(FullRevision.IMPLICIT_MINOR_REV, p.getMinor()); 27 assertEquals(FullRevision.IMPLICIT_MICRO_REV, p.getMicro()); 28 assertEquals(FullRevision.NOT_A_PREVIEW, p.getPreview()); 29 assertFalse (p.isPreview()); 30 assertEquals("5", p.toShortString()); 31 assertEquals(p, FullRevision.parseRevision("5")); 32 assertEquals("5.0.0", p.toString()); 33 assertEquals(p, FullRevision.parseRevision("5.0.0")); 34 35 p = new FullRevision(5, 0, 0, 6); 36 assertEquals(5, p.getMajor()); 37 assertEquals(FullRevision.IMPLICIT_MINOR_REV, p.getMinor()); 38 assertEquals(FullRevision.IMPLICIT_MICRO_REV, p.getMicro()); 39 assertEquals(6, p.getPreview()); 40 assertTrue (p.isPreview()); 41 assertEquals("5 rc6", p.toShortString()); 42 assertEquals(p, FullRevision.parseRevision("5 rc6")); 43 assertEquals("5.0.0 rc6", p.toString()); 44 assertEquals(p, FullRevision.parseRevision("5.0.0 rc6")); 45 46 p = new FullRevision(6, 7, 0); 47 assertEquals(6, p.getMajor()); 48 assertEquals(7, p.getMinor()); 49 assertEquals(0, p.getMicro()); 50 assertEquals(0, p.getPreview()); 51 assertFalse (p.isPreview()); 52 assertEquals("6.7", p.toShortString()); 53 assertEquals(p, FullRevision.parseRevision("6.7")); 54 assertEquals("6.7.0", p.toString()); 55 assertEquals(p, FullRevision.parseRevision("6.7.0")); 56 57 p = new FullRevision(10, 11, 12, FullRevision.NOT_A_PREVIEW); 58 assertEquals(10, p.getMajor()); 59 assertEquals(11, p.getMinor()); 60 assertEquals(12, p.getMicro()); 61 assertEquals(0, p.getPreview()); 62 assertFalse (p.isPreview()); 63 assertEquals("10.11.12", p.toShortString()); 64 assertEquals("10.11.12", p.toString()); 65 assertEquals(p, FullRevision.parseRevision("10.11.12")); 66 67 p = new FullRevision(10, 11, 12, 13); 68 assertEquals(10, p.getMajor()); 69 assertEquals(11, p.getMinor()); 70 assertEquals(12, p.getMicro()); 71 assertEquals(13, p.getPreview()); 72 assertTrue (p.isPreview()); 73 assertEquals("10.11.12 rc13", p.toShortString()); 74 assertEquals("10.11.12 rc13", p.toString()); 75 assertEquals(p, FullRevision.parseRevision("10.11.12 rc13")); 76 assertEquals(p, FullRevision.parseRevision(" 10.11.12 rc13")); 77 assertEquals(p, FullRevision.parseRevision("10.11.12 rc13 ")); 78 assertEquals(p, FullRevision.parseRevision(" 10.11.12 rc13 ")); 79 } 80 81 public final void testParseError() { 82 String errorMsg = null; 83 try { 84 FullRevision.parseRevision("not a number"); 85 fail("FullRevision.parseRevision should thrown NumberFormatException"); 86 } catch (NumberFormatException e) { 87 errorMsg = e.getMessage(); 88 } 89 assertEquals("Invalid full revision: not a number", errorMsg); 90 91 errorMsg = null; 92 try { 93 FullRevision.parseRevision("5 .6 .7"); 94 fail("FullRevision.parseRevision should thrown NumberFormatException"); 95 } catch (NumberFormatException e) { 96 errorMsg = e.getMessage(); 97 } 98 assertEquals("Invalid full revision: 5 .6 .7", errorMsg); 99 100 errorMsg = null; 101 try { 102 FullRevision.parseRevision("5.0.0 preview 1"); 103 fail("FullRevision.parseRevision should thrown NumberFormatException"); 104 } catch (NumberFormatException e) { 105 errorMsg = e.getMessage(); 106 } 107 assertEquals("Invalid full revision: 5.0.0 preview 1", errorMsg); 108 109 errorMsg = null; 110 try { 111 FullRevision.parseRevision(" 5.1.2 rc 42 "); 112 fail("FullRevision.parseRevision should thrown NumberFormatException"); 113 } catch (NumberFormatException e) { 114 errorMsg = e.getMessage(); 115 } 116 assertEquals("Invalid full revision: 5.1.2 rc 42 ", errorMsg); 117 } 118 119 public final void testCompareTo() { 120 FullRevision s4 = new FullRevision(4); 121 FullRevision i4 = new FullRevision(4); 122 FullRevision g5 = new FullRevision(5, 1, 0, 6); 123 FullRevision y5 = new FullRevision(5); 124 FullRevision c5 = new FullRevision(5, 1, 0, 6); 125 FullRevision o5 = new FullRevision(5, 0, 0, 7); 126 FullRevision p5 = new FullRevision(5, 1, 0, 0); 127 128 assertEquals(s4, i4); // 4.0.0-0 == 4.0.0-0 129 assertEquals(g5, c5); // 5.1.0-6 == 5.1.0-6 130 131 assertFalse(y5.equals(p5)); // 5.0.0-0 != 5.1.0-0 132 assertFalse(g5.equals(p5)); // 5.1.0-6 != 5.1.0-0 133 assertTrue (s4.compareTo(i4) == 0); // 4.0.0-0 == 4.0.0-0 134 assertTrue (s4.compareTo(y5) < 0); // 4.0.0-0 < 5.0.0-0 135 assertTrue (y5.compareTo(y5) == 0); // 5.0.0-0 == 5.0.0-0 136 assertTrue (y5.compareTo(p5) < 0); // 5.0.0-0 < 5.1.0-0 137 assertTrue (o5.compareTo(y5) < 0); // 5.0.0-7 < 5.0.0-0 138 assertTrue (p5.compareTo(p5) == 0); // 5.1.0-0 == 5.1.0-0 139 assertTrue (c5.compareTo(p5) < 0); // 5.1.0-6 < 5.1.0-0 140 assertTrue (p5.compareTo(c5) > 0); // 5.1.0-0 > 5.1.0-6 141 assertTrue (p5.compareTo(o5) > 0); // 5.1.0-0 > 5.0.0-7 142 assertTrue (c5.compareTo(o5) > 0); // 5.1.0-6 > 5.0.0-7 143 assertTrue (o5.compareTo(o5) == 0); // 5.0.0-7 > 5.0.0-7 144 } 145 146 } 147