Home | History | Annotate | Download | only in pool
      1 /*
      2  * Copyright 2016, Google Inc.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are
      7  * met:
      8  *
      9  * Redistributions of source code must retain the above copyright
     10  * notice, this list of conditions and the following disclaimer.
     11  * Redistributions in binary form must reproduce the above
     12  * copyright notice, this list of conditions and the following disclaimer
     13  * in the documentation and/or other materials provided with the
     14  * distribution.
     15  * Neither the name of Google Inc. nor the names of its
     16  * contributors may be used to endorse or promote products derived from
     17  * this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 package org.jf.dexlib2.pool;
     33 
     34 import com.google.common.collect.Lists;
     35 import org.jf.dexlib2.AccessFlags;
     36 import org.jf.dexlib2.AnnotationVisibility;
     37 import org.jf.dexlib2.Opcodes;
     38 import org.jf.dexlib2.dexbacked.raw.MapItem;
     39 import org.jf.dexlib2.dexbacked.raw.RawDexFile;
     40 import org.jf.dexlib2.iface.ClassDef;
     41 import org.jf.dexlib2.iface.Field;
     42 import org.jf.dexlib2.iface.Method;
     43 import org.jf.dexlib2.iface.MethodParameter;
     44 import org.jf.dexlib2.immutable.*;
     45 import org.jf.dexlib2.writer.io.MemoryDataStore;
     46 import org.jf.dexlib2.writer.pool.DexPool;
     47 import org.junit.Assert;
     48 import org.junit.Test;
     49 
     50 import java.io.IOException;
     51 import java.util.List;
     52 
     53 public class RollbackTest {
     54     @Test
     55     public void testRollback() throws IOException {
     56         ClassDef class1 = new ImmutableClassDef("Lcls1;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, null,
     57                 Lists.newArrayList(new ImmutableAnnotation(AnnotationVisibility.RUNTIME, "Lannotation;", null)),
     58                 Lists.<Field>newArrayList(
     59                         new ImmutableField("Lcls1;", "field1", "I", AccessFlags.PUBLIC.getValue(), null, null)
     60                 ),
     61                 Lists.<Method>newArrayList(
     62                         new ImmutableMethod("Lcls1", "method1",
     63                                 Lists.<MethodParameter>newArrayList(new ImmutableMethodParameter("L", null, null)), "V",
     64                                 AccessFlags.PUBLIC.getValue(), null, null))
     65                 );
     66 
     67         ClassDef class2 = new ImmutableClassDef("Lcls2;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, null,
     68                 Lists.newArrayList(new ImmutableAnnotation(AnnotationVisibility.RUNTIME, "Lannotation2;", null)),
     69                 Lists.<Field>newArrayList(
     70                         new ImmutableField("Lcls2;", "field2", "D", AccessFlags.PUBLIC.getValue(), null, null)
     71                 ),
     72                 Lists.<Method>newArrayList(
     73                         new ImmutableMethod("Lcls2;", "method2",
     74                                 Lists.<MethodParameter>newArrayList(new ImmutableMethodParameter("D", null, null)), "V",
     75                                 AccessFlags.PUBLIC.getValue(), null, null))
     76         );
     77 
     78         RawDexFile dexFile1;
     79         {
     80             MemoryDataStore dataStore = new MemoryDataStore();
     81             DexPool dexPool = new DexPool(Opcodes.getDefault());
     82             dexPool.internClass(class1);
     83             dexPool.mark();
     84             dexPool.internClass(class2);
     85             dexPool.reset();
     86             dexPool.writeTo(dataStore);
     87             dexFile1 = new RawDexFile(Opcodes.getDefault(), dataStore.getData());
     88         }
     89 
     90         RawDexFile dexFile2;
     91         {
     92             MemoryDataStore dataStore = new MemoryDataStore();
     93             DexPool dexPool = new DexPool(Opcodes.getDefault());
     94             dexPool.internClass(class1);
     95             dexPool.writeTo(dataStore);
     96             dexFile2 = new RawDexFile(Opcodes.getDefault(), dataStore.getData());
     97         }
     98 
     99         List<MapItem> mapItems1 = dexFile1.getMapItems();
    100         List<MapItem> mapItems2 = dexFile2.getMapItems();
    101         for (int i=0; i<mapItems1.size(); i++) {
    102             Assert.assertEquals(mapItems1.get(i).getType(), mapItems2.get(i).getType());
    103             Assert.assertEquals(mapItems1.get(i).getItemCount(), mapItems2.get(i).getItemCount());
    104         }
    105     }
    106 }
    107