Home | History | Annotate | Download | only in io
      1 /*
      2  * Copyright (C) 2012 The Guava Authors
      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.google.common.io;
     18 
     19 import static com.google.common.base.Preconditions.checkNotNull;
     20 
     21 import com.google.common.collect.ImmutableList;
     22 import com.google.common.collect.ImmutableMap;
     23 import com.google.common.collect.Lists;
     24 
     25 import junit.framework.TestCase;
     26 
     27 import java.io.IOException;
     28 import java.io.Reader;
     29 import java.io.StringReader;
     30 import java.lang.reflect.Method;
     31 import java.lang.reflect.Modifier;
     32 import java.util.List;
     33 
     34 /**
     35  * @param <S> the source or sink type
     36  * @param <T> the data type (byte[] or String)
     37  * @param <F> the factory type
     38  * @author Colin Decker
     39  */
     40 public class SourceSinkTester<S, T, F extends SourceSinkFactory<S, T>> extends TestCase {
     41 
     42   static final String LOREM_IPSUM = "Lorem ipsum dolor sit amet, consectetur adipiscing "
     43       + "elit. Cras fringilla elit ac ipsum adipiscing vulputate. Maecenas in lorem nulla, ac "
     44       + "sollicitudin quam. Praesent neque elit, sodales quis vestibulum vel, pellentesque nec "
     45       + "erat. Proin cursus commodo lacus eget congue. Aliquam erat volutpat. Fusce ut leo sed "
     46       + "risus tempor vehicula et a odio. Nam aliquet dolor viverra libero rutrum accumsan quis "
     47       + "in augue. Suspendisse id dui in lorem tristique placerat eget vel risus. Sed metus neque, "
     48       + "scelerisque in molestie ac, mattis quis lectus. Pellentesque viverra justo commodo quam "
     49       + "bibendum ut gravida leo accumsan. Nullam malesuada sagittis diam, quis suscipit mauris "
     50       + "euismod vulputate. Pellentesque ultrices tellus sed lorem aliquet pulvinar. Nam lorem "
     51       + "nunc, ultrices at auctor non, scelerisque eget turpis. Nullam eget varius erat. Sed a "
     52       + "lorem id arcu dictum euismod. Fusce lectus odio, elementum ullamcorper mattis viverra, "
     53       + "dictum sit amet lacus.\n"
     54       + "\n"
     55       + "Nunc quis lacus est. Sed aliquam pretium cursus. Sed eu libero eros. In hac habitasse "
     56       + "platea dictumst. Pellentesque molestie, nibh nec iaculis luctus, justo sem lobortis enim, "
     57       + "at feugiat leo magna nec libero. Mauris quis odio eget nisl rutrum cursus nec eget augue. "
     58       + "Sed nec arcu sem. In hac habitasse platea dictumst.";
     59 
     60   static final ImmutableMap<String, String> TEST_STRINGS
     61       = ImmutableMap.<String, String>builder()
     62       .put("empty", "")
     63       .put("1 char", "0")
     64       .put("1 word", "hello")
     65       .put("2 words", "hello world")
     66       .put("\\n line break", "hello\nworld")
     67       .put("\\r line break", "hello\rworld")
     68       .put("\\r\\n line break", "hello\r\nworld")
     69       .put("\\n at EOF", "hello\nworld\n")
     70       .put("\\r at EOF", "hello\nworld\r")
     71       .put("lorem ipsum", LOREM_IPSUM)
     72       .build();
     73 
     74   protected final F factory;
     75   protected final T data;
     76   protected final T expected;
     77 
     78   private final String suiteName;
     79   private final String caseDesc;
     80 
     81   SourceSinkTester(F factory, T data, String suiteName, String caseDesc, Method method) {
     82     super(method.getName());
     83     this.factory = checkNotNull(factory);
     84     this.data = checkNotNull(data);
     85     this.expected = checkNotNull(factory.getExpected(data));
     86     this.suiteName = checkNotNull(suiteName);
     87     this.caseDesc = checkNotNull(caseDesc);
     88   }
     89 
     90   @Override
     91   public String getName() {
     92     return super.getName() + " [" + suiteName + " [" + caseDesc + "]]";
     93   }
     94 
     95   protected static ImmutableList<String> getLines(final String string) {
     96     try {
     97       return new CharSource() {
     98         @Override
     99         public Reader openStream() throws IOException {
    100           return new StringReader(string);
    101         }
    102       }.readLines();
    103     } catch (IOException e) {
    104       throw new AssertionError();
    105     }
    106   }
    107 
    108   @Override
    109   public void tearDown() throws IOException {
    110     factory.tearDown();
    111   }
    112 
    113   static ImmutableList<Method> getTestMethods(Class<?> testClass) {
    114     List<Method> result = Lists.newArrayList();
    115     for (Method method : testClass.getDeclaredMethods()) {
    116       if (Modifier.isPublic(method.getModifiers())
    117           && method.getReturnType() == void.class
    118           && method.getParameterTypes().length == 0
    119           && method.getName().startsWith("test")) {
    120         result.add(method);
    121       }
    122     }
    123     return ImmutableList.copyOf(result);
    124   }
    125 }
    126