Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2016 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 import java.lang.annotation.Annotation;
     18 
     19 public class AnnotationTestHelpers {
     20   // Provide custom print function that print a deterministic output.
     21   // Note that Annotation#toString has unspecified order: it prints out the
     22   // fields, which is why we can't rely on it.
     23 
     24   public static String asString(Annotation anno) {
     25     if (anno instanceof Calendar) {
     26       return asString((Calendar)anno);
     27     } else if (anno instanceof Calendars) {
     28       return asString((Calendars)anno);
     29     } else {
     30       if (anno == null) {
     31         return "<null>";
     32       }
     33       // Fall-back, usually would only go here in a test failure.
     34       return anno.toString();
     35     }
     36   }
     37 
     38   public static String asString(Annotation[] annos) {
     39     String msg = "";
     40 
     41     if (annos == null) {
     42       msg += "<null>";
     43     } else if (annos.length == 0) {
     44       msg += "<empty>";
     45     } else {
     46       for (int i = 0; i < annos.length; ++i) {
     47         msg += asString(annos[i]);
     48 
     49         if (i != annos.length - 1) {
     50           msg += ", ";
     51         }
     52       }
     53     }
     54 
     55     return msg;
     56   }
     57 
     58   public static String asString(Calendar calendar) {
     59     if (calendar == null) {
     60       return "<null>";
     61     }
     62 
     63     return "@Calendar(dayOfMonth=" + calendar.dayOfMonth() + ", dayOfWeek=" +
     64       calendar.dayOfWeek() + ", hour=" + calendar.hour() + ")";
     65   }
     66 
     67   public static String asString(Calendars calendars) {
     68     if (calendars == null) {
     69       return "<null>";
     70     }
     71 
     72     String s = "@Calendars(value=[";
     73 
     74     Calendar[] allValues = calendars.value();
     75     for (int i = 0; i < allValues.length; ++i) {
     76       s += asString(allValues[i]);
     77       if (i != allValues.length - 1) {
     78         s += ", ";
     79       }
     80     }
     81 
     82     s += "])";
     83 
     84     return s;
     85   }
     86 }
     87