• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import java.io.IOException;
25 import java.io.Reader;
26 import java.io.StringReader;
27 import java.lang.reflect.Method;
28 import java.lang.reflect.Modifier;
29 import java.util.List;
30 import junit.framework.TestCase;
31 
32 /**
33  * @param <S> the source or sink type
34  * @param <T> the data type (byte[] or String)
35  * @param <F> the factory type
36  * @author Colin Decker
37  */
38 @AndroidIncompatible // Android doesn't understand tests that lack default constructors.
39 public class SourceSinkTester<S, T, F extends SourceSinkFactory<S, T>> extends TestCase {
40 
41   static final String LOREM_IPSUM =
42       "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 "
47           + "quis in augue. Suspendisse id dui in lorem tristique placerat eget vel risus. Sed "
48           + "metus neque, scelerisque in molestie ac, mattis quis lectus. Pellentesque viverra "
49           + "justo commodo quam bibendum ut gravida leo accumsan. Nullam malesuada sagittis diam, "
50           + "quis suscipit mauris euismod vulputate. Pellentesque ultrices tellus sed lorem "
51           + "aliquet pulvinar. Nam lorem nunc, ultrices at auctor non, scelerisque eget turpis. "
52           + "Nullam eget varius erat. Sed a lorem id arcu dictum euismod. Fusce lectus odio, "
53           + "elementum ullamcorper mattis viverra, 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 "
57           + "enim, at feugiat leo magna nec libero. Mauris quis odio eget nisl rutrum cursus nec "
58           + "eget augue. 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           .buildOrThrow();
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 
SourceSinkTester(F factory, T data, String suiteName, String caseDesc, Method method)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
getName()91   public String getName() {
92     return super.getName() + " [" + suiteName + " [" + caseDesc + "]]";
93   }
94 
getLines(final String string)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