Home | History | Annotate | Download | only in packages
      1 /*
      2  * Copyright (C) 2011 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 com.android.sdklib.internal.repository.archives.Archive.Arch;
     20 import com.android.sdklib.internal.repository.archives.Archive.Os;
     21 import com.android.sdklib.internal.repository.packages.ExtraPackage;
     22 import com.android.sdklib.repository.PkgProps;
     23 
     24 import java.io.File;
     25 import java.util.Arrays;
     26 import java.util.Properties;
     27 
     28 /**
     29  * Tests {@link ExtraPackage} using anddon-3.xsd: it has a {@code <path>} and {@code <vendor>}.
     30  * (it lacks name-display, vendor-id and vendor-display with are in addon-4.xsd)
     31  */
     32 public class ExtraPackageTest_v3 extends MinToolsPackageTest {
     33 
     34     private static final char PS = File.pathSeparatorChar;
     35 
     36     private ExtraPackage createExtraPackage(Properties props) {
     37         ExtraPackage p = (ExtraPackage) ExtraPackage.create(
     38                 null, //source
     39                 props,
     40                 null, //vendor
     41                 null, //path
     42                 -1, //revision
     43                 null, //license
     44                 null, //description
     45                 null, //descUrl
     46                 Os.ANY, //archiveOs
     47                 Arch.ANY, //archiveArch
     48                 "/local/archive/path" //archiveOsPath
     49                 );
     50         return p;
     51     }
     52 
     53     /** Properties used to "load" the package. When saved, they become different. */
     54     private Properties createLoadedProps() {
     55         Properties props = super.createProps();
     56 
     57         // ExtraPackage properties
     58         props.setProperty(PkgProps.EXTRA_VENDOR, "vendor");
     59         props.setProperty(PkgProps.EXTRA_PATH, "the_path");
     60         props.setProperty(PkgProps.EXTRA_OLD_PATHS, "old_path1;oldpath2");
     61         props.setProperty(PkgProps.EXTRA_MIN_API_LEVEL, "11");
     62         props.setProperty(PkgProps.EXTRA_PROJECT_FILES,
     63                 "path1.jar" + PS + "dir2/jar 2.jar" + PS + "dir/3/path");
     64 
     65         return props;
     66     }
     67 
     68     /** Properties saved by the package. They differ from loaded ones in name and vendor. */
     69     private Properties createSavedProps() {
     70         Properties props = super.createProps();
     71 
     72         // ExtraPackage properties
     73         props.setProperty(PkgProps.EXTRA_VENDOR_ID, "vendor");
     74         props.setProperty(PkgProps.EXTRA_VENDOR_DISPLAY, "vendor");
     75         props.setProperty(PkgProps.EXTRA_NAME_DISPLAY, "Vendor The Path");
     76         props.setProperty(PkgProps.EXTRA_PATH, "the_path");
     77         props.setProperty(PkgProps.EXTRA_OLD_PATHS, "old_path1;oldpath2");
     78         props.setProperty(PkgProps.EXTRA_MIN_API_LEVEL, "11");
     79         props.setProperty(PkgProps.EXTRA_PROJECT_FILES,
     80                 "path1.jar" + PS + "dir2/jar 2.jar" + PS + "dir/3/path");
     81 
     82         return props;
     83     }
     84 
     85     protected void testCreatedExtraPackage(ExtraPackage p) {
     86         super.testCreatedPackage(p);
     87 
     88         // Package properties
     89         // vendor becomes both vendor-id and vendor-display
     90         assertEquals("vendor", p.getVendorId());
     91         assertEquals("vendor", p.getVendorDisplay());
     92         assertEquals("the_path", p.getPath());
     93         // path and vendor are combined in the default display name
     94         assertEquals("Vendor The Path", p.getDisplayName());
     95         assertEquals("[old_path1, oldpath2]", Arrays.toString(p.getOldPaths()));
     96         assertEquals(11, p.getMinApiLevel());
     97         assertEquals(
     98                 "[path1.jar, dir2/jar 2.jar, dir/3/path]",
     99                 Arrays.toString(p.getProjectFiles()));
    100     }
    101 
    102     // ----
    103 
    104     @Override
    105     public final void testCreate() {
    106         Properties props = createLoadedProps();
    107         ExtraPackage p = createExtraPackage(props);
    108 
    109         testCreatedExtraPackage(p);
    110     }
    111 
    112     @Override
    113     public void testSaveProperties() {
    114         Properties props = createLoadedProps();
    115         ExtraPackage p = createExtraPackage(props);
    116 
    117         Properties props2 = new Properties();
    118         p.saveProperties(props2);
    119 
    120         assertEquals(props2, createSavedProps());
    121     }
    122 
    123     public void testSameItemAs() {
    124         Properties props1 = createLoadedProps();
    125         ExtraPackage p1 = createExtraPackage(props1);
    126         assertTrue(p1.sameItemAs(p1));
    127 
    128         // different vendor, same path
    129         Properties props2 = new Properties(props1);
    130         props2.setProperty(PkgProps.EXTRA_VENDOR, "vendor2");
    131         ExtraPackage p2 = createExtraPackage(props2);
    132         assertFalse(p1.sameItemAs(p2));
    133         assertFalse(p2.sameItemAs(p1));
    134 
    135         // different vendor, different path
    136         props2.setProperty(PkgProps.EXTRA_PATH, "new_path2");
    137         p2 = createExtraPackage(props2);
    138         assertFalse(p1.sameItemAs(p2));
    139         assertFalse(p2.sameItemAs(p1));
    140 
    141         // same vendor, but single path using the old paths from p1
    142         Properties props3 = new Properties(props1);
    143         props3.setProperty(PkgProps.EXTRA_OLD_PATHS, "");
    144         props3.setProperty(PkgProps.EXTRA_PATH, "old_path1");
    145         ExtraPackage p3 = createExtraPackage(props3);
    146         assertTrue(p1.sameItemAs(p3));
    147         assertTrue(p3.sameItemAs(p1));
    148 
    149         props3.setProperty(PkgProps.EXTRA_PATH, "oldpath2");
    150         p3 = createExtraPackage(props3);
    151         assertTrue(p1.sameItemAs(p3));
    152         assertTrue(p3.sameItemAs(p1));
    153 
    154         // same vendor, different old paths but there's a path=>old_path match
    155         Properties props4 = new Properties(props1);
    156         props4.setProperty(PkgProps.EXTRA_OLD_PATHS, "new_path4;new_path5");
    157         props4.setProperty(PkgProps.EXTRA_PATH, "old_path1");
    158         ExtraPackage p4 = createExtraPackage(props4);
    159         assertTrue(p1.sameItemAs(p4));
    160         assertTrue(p4.sameItemAs(p1));
    161 
    162         // same vendor, incompatible paths
    163         Properties props5 = new Properties(props1);
    164         // and the only match is between old_paths, which doesn't count.
    165         props5.setProperty(PkgProps.EXTRA_OLD_PATHS, "old_path1;new_path5");
    166         props5.setProperty(PkgProps.EXTRA_PATH, "new_path4");
    167         ExtraPackage p5 = createExtraPackage(props5);
    168         assertFalse(p1.sameItemAs(p5));
    169         assertFalse(p5.sameItemAs(p1));
    170     }
    171 
    172     public void testInstallId() {
    173         Properties props = createLoadedProps();
    174         ExtraPackage p = createExtraPackage(props);
    175 
    176         assertEquals("extra-vendor-the_path", p.installId());
    177     }
    178 }
    179