Home | History | Annotate | Download | only in util
      1 /* GENERATED SOURCE. DO NOT MODIFY. */
      2 //  2017 and later: Unicode, Inc. and others.
      3 // License & terms of use: http://www.unicode.org/copyright.html#License
      4 package android.icu.dev.test.util;
      5 
      6 import java.io.BufferedReader;
      7 import java.io.IOException;
      8 import java.util.ArrayList;
      9 import java.util.Collections;
     10 import java.util.List;
     11 
     12 import android.icu.dev.test.AbstractTestLog;
     13 import android.icu.dev.test.TestFmwk;
     14 import android.icu.dev.util.CollectionUtilities;
     15 import android.icu.impl.Utility;
     16 import android.icu.impl.locale.XCldrStub.FileUtilities;
     17 import android.icu.impl.locale.XCldrStub.Splitter;
     18 import android.icu.util.ICUUncheckedIOException;
     19 
     20 abstract public class DataDrivenTestHelper {
     21 
     22     public static final List<String> DEBUG_LINE = Collections.singletonList("@debug");
     23     public static final Splitter SEMICOLON = Splitter.on(';').trimResults();
     24     public static final Splitter EQUAL_SPLIT = Splitter.on('=').trimResults();
     25     public static final String SEPARATOR = " ; \t";
     26 
     27     protected TestFmwk framework = null;
     28     protected int minArgumentCount = 3;
     29     protected int maxArgumentCount = 4;
     30     private List<List<String>> lines = new ArrayList<List<String>>();
     31     private List<String> comments = new ArrayList<String>();
     32 
     33     public DataDrivenTestHelper setFramework(TestFmwk testFramework) {
     34         this.framework = testFramework;
     35         return this;
     36     }
     37 
     38     public <T extends Appendable> T appendLines(T out) {
     39         try {
     40             for (int i = 0; i < lines.size(); ++i) {
     41                 List<String> components = lines.get(i);
     42                 String comment = comments.get(i);
     43                 if (components.isEmpty()) {
     44                     if(!comment.isEmpty()) {
     45                         out.append("# ").append(comment);
     46                     }
     47                 } else {
     48                     String first = components.iterator().next();
     49                     String sep = first.startsWith("@") ? "=" : SEPARATOR;
     50                     out.append(CollectionUtilities.join(components, sep));
     51                     if (!comment.isEmpty()) {
     52                         out.append("\t# ").append(comment);
     53                     }
     54                 }
     55                 out.append('\n');
     56             }
     57             return out;
     58         } catch (IOException e) {
     59             throw new ICUUncheckedIOException(e);
     60         }
     61     }
     62 
     63     protected DataDrivenTestHelper addLine(List<String> arguments, String commentBase) {
     64         lines.add(Collections.unmodifiableList(arguments));
     65         comments.add(commentBase);
     66         return this;
     67     }
     68 
     69     public DataDrivenTestHelper run(Class<?> classFileIsRelativeTo, String file) {
     70         return load(classFileIsRelativeTo, file)
     71             .test();
     72     }
     73 
     74     public boolean isTestLine(List<String> arguments) {
     75         return !arguments.isEmpty() && !arguments.equals(DEBUG_LINE);
     76     }
     77 
     78     public DataDrivenTestHelper test() {
     79         boolean breakpoint = false;
     80         for (int i = 0; i < lines.size(); ++i) {
     81             List<String> arguments = lines.get(i);
     82             String comment = comments.get(i);
     83             if (arguments.isEmpty()) {
     84                 if (!comment.isEmpty()) {
     85                     AbstractTestLog.logln(comment);
     86                 }
     87                 continue;
     88             } else if (arguments.equals(DEBUG_LINE)) {
     89                 breakpoint = true;
     90                 continue;
     91             } else {
     92                 String first = arguments.get(0);
     93                 if (first.startsWith("@")) {
     94                     handleParams(comment, arguments);
     95                     continue;
     96                 }
     97             }
     98             try {
     99                 handle(i, breakpoint, comment, arguments);
    100             } catch (Exception e) {
    101                 e.printStackTrace();
    102                 AbstractTestLog.errln("Illegal data test file entry (" + i + "): " + arguments + " # " + comment);
    103             }
    104             breakpoint = false;
    105         }
    106         return this;
    107     }
    108 
    109     public DataDrivenTestHelper load(Class<?> classFileIsRelativeTo, String file) {
    110         BufferedReader in = null;
    111         try {
    112             in = FileUtilities.openFile(classFileIsRelativeTo, file);
    113             //boolean breakpoint = false;
    114 
    115             while (true) {
    116                 String line = in.readLine();
    117                 if (line == null) {
    118                     break;
    119                 }
    120                 line = line.trim();
    121                 if (line.isEmpty()) {
    122                     addLine(Collections.<String>emptyList(), "");
    123                     continue;
    124                 }
    125                 int hash = line.indexOf('#');
    126                 String comment = "";
    127                 String commentBase = "";
    128                 if (hash >= 0) {
    129                     commentBase = line.substring(hash+1).trim();
    130                     line = line.substring(0,hash).trim();
    131                     comment = "# " + commentBase;
    132                     if (!line.isEmpty()) {
    133                         comment = "\t" + comment;
    134                     }
    135                 }
    136                 if (line.isEmpty()) {
    137                     addLine(Collections.<String>emptyList(), commentBase);
    138                     continue;
    139                 }
    140                 if (line.startsWith("@")) {
    141                     List<String> keyValue = EQUAL_SPLIT.splitToList(line);
    142                     addLine(keyValue, comment);
    143                     continue;
    144                 }
    145                 List<String> arguments = SEMICOLON.splitToList(line);
    146                 if (arguments.size() < minArgumentCount || arguments.size() > maxArgumentCount) {
    147                     AbstractTestLog.errln("Malformed data line:" + line + comment);
    148                     continue;
    149                 }
    150                 addLine(arguments, commentBase);
    151             }
    152         } catch (IOException e) {
    153             throw new ICUUncheckedIOException(e);
    154         } finally {
    155             if (in != null) {
    156                 try {
    157                     in.close();
    158                 } catch (IOException e) {
    159                     throw new ICUUncheckedIOException(e);
    160                 }
    161             }
    162         }
    163         lines = Collections.unmodifiableList(lines); // should do deep unmodifiable...
    164         comments = Collections.unmodifiableList(comments);
    165         return this;
    166     }
    167 
    168     protected boolean assertEquals(String message, Object expected, Object actual) {
    169         return TestFmwk.handleAssert(Utility.equals(expected, actual), message, stringFor(expected), stringFor(actual), null, false);
    170     }
    171 
    172     private final String stringFor(Object obj) {
    173         return obj == null ? "null"
    174             : obj instanceof String ? "\"" + obj + '"'
    175                 : obj instanceof Number ? String.valueOf(obj)
    176                     : obj.getClass().getName() + "<" + obj + ">";
    177     }
    178 
    179     abstract public void handle(int lineNumber, boolean breakpoint, String commentBase, List<String> arguments);
    180 
    181     public void handleParams(String comment, List<String> arguments) {
    182         throw new IllegalArgumentException("Unrecognized parameter: " + arguments);
    183     }
    184 
    185     public List<List<String>> getLines() {
    186         return lines;
    187     }
    188 }