aboutsummaryrefslogtreecommitdiff
path: root/android/guava-tests/test/com/google/common/io/ResourcesTest.java
blob: 4344ed1ca21ff8f6776d8d173e3df0dbefe3d436 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * Copyright (C) 2008 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.common.io;

import static com.google.common.base.CharMatcher.whitespace;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;

import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList;
import com.google.common.testing.NullPointerTester;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
import junit.framework.TestSuite;

/**
 * Unit test for {@link Resources}.
 *
 * @author Chris Nokleberg
 */

public class ResourcesTest extends IoTestCase {

  @AndroidIncompatible // wouldn't run anyway, but strip the source entirely because of b/230620681
  public static TestSuite suite() {
    TestSuite suite = new TestSuite();
    suite.addTest(
        ByteSourceTester.tests(
            "Resources.asByteSource[URL]", SourceSinkFactories.urlByteSourceFactory(), true));
    suite.addTest(
        CharSourceTester.tests(
            "Resources.asCharSource[URL, Charset]",
            SourceSinkFactories.urlCharSourceFactory(),
            false));
    suite.addTestSuite(ResourcesTest.class);
    return suite;
  }

  public void testToString() throws IOException {
    URL resource = getClass().getResource("testdata/i18n.txt");
    assertEquals(I18N, Resources.toString(resource, Charsets.UTF_8));
    assertThat(Resources.toString(resource, Charsets.US_ASCII)).isNotEqualTo(I18N);
  }

  public void testToByteArray() throws IOException {
    URL resource = getClass().getResource("testdata/i18n.txt");
    assertThat(Resources.toByteArray(resource)).isEqualTo(I18N.getBytes(Charsets.UTF_8));
  }

  public void testReadLines() throws IOException {
    // TODO(chrisn): Check in a better resource
    URL resource = getClass().getResource("testdata/i18n.txt");
    assertEquals(ImmutableList.of(I18N), Resources.readLines(resource, Charsets.UTF_8));
  }

  public void testReadLines_withLineProcessor() throws IOException {
    URL resource = getClass().getResource("testdata/alice_in_wonderland.txt");
    LineProcessor<List<String>> collectAndLowercaseAndTrim =
        new LineProcessor<List<String>>() {
          List<String> collector = new ArrayList<>();

          @Override
          public boolean processLine(String line) {
            collector.add(whitespace().trimFrom(line));
            return true;
          }

          @Override
          public List<String> getResult() {
            return collector;
          }
        };
    List<String> result =
        Resources.readLines(resource, Charsets.US_ASCII, collectAndLowercaseAndTrim);
    assertEquals(3600, result.size());
    assertEquals("ALICE'S ADVENTURES IN WONDERLAND", result.get(0));
    assertEquals("THE END", result.get(result.size() - 1));
  }

  public void testCopyToOutputStream() throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    URL resource = getClass().getResource("testdata/i18n.txt");
    Resources.copy(resource, out);
    assertEquals(I18N, out.toString("UTF-8"));
  }

  public void testGetResource_notFound() {
    IllegalArgumentException e =
        assertThrows(
            IllegalArgumentException.class, () -> Resources.getResource("no such resource"));
    assertThat(e).hasMessageThat().isEqualTo("resource no such resource not found.");
  }

  public void testGetResource() {
    assertNotNull(Resources.getResource("com/google/common/io/testdata/i18n.txt"));
  }

  public void testGetResource_relativePath_notFound() {
    IllegalArgumentException e =
        assertThrows(
            IllegalArgumentException.class,
            () -> Resources.getResource(getClass(), "com/google/common/io/testdata/i18n.txt"));
    assertThat(e)
        .hasMessageThat()
        .isEqualTo(
            "resource com/google/common/io/testdata/i18n.txt"
                + " relative to com.google.common.io.ResourcesTest not found.");
  }

  public void testGetResource_relativePath() {
    assertNotNull(Resources.getResource(getClass(), "testdata/i18n.txt"));
  }

  public void testGetResource_contextClassLoader() throws IOException {
    // Check that we can find a resource if it is visible to the context class
    // loader, even if it is not visible to the loader of the Resources class.

    File tempFile = createTempFile();
    PrintWriter writer = new PrintWriter(tempFile, "UTF-8");
    writer.println("rud a chur ar an méar fhada");
    writer.close();

    // First check that we can't find it without setting the context loader.
    // This is a sanity check that the test doesn't spuriously pass because
    // the resource is visible to the system class loader.
    assertThrows(IllegalArgumentException.class, () -> Resources.getResource(tempFile.getName()));

    // Now set the context loader to one that should find the resource.
    URL baseUrl = tempFile.getParentFile().toURI().toURL();
    URLClassLoader loader = new URLClassLoader(new URL[] {baseUrl});
    ClassLoader oldContextLoader = Thread.currentThread().getContextClassLoader();
    try {
      Thread.currentThread().setContextClassLoader(loader);
      URL url = Resources.getResource(tempFile.getName());
      String text = Resources.toString(url, Charsets.UTF_8);
      assertEquals("rud a chur ar an méar fhada" + System.lineSeparator(), text);
    } finally {
      Thread.currentThread().setContextClassLoader(oldContextLoader);
    }
  }

  public void testGetResource_contextClassLoaderNull() {
    ClassLoader oldContextLoader = Thread.currentThread().getContextClassLoader();
    try {
      Thread.currentThread().setContextClassLoader(null);
      assertNotNull(Resources.getResource("com/google/common/io/testdata/i18n.txt"));
      assertThrows(IllegalArgumentException.class, () -> Resources.getResource("no such resource"));
    } finally {
      Thread.currentThread().setContextClassLoader(oldContextLoader);
    }
  }

  @AndroidIncompatible // .class files aren't available
  public void testNulls() {
    new NullPointerTester()
        .setDefault(URL.class, classfile(ResourcesTest.class))
        .testAllPublicStaticMethods(Resources.class);
  }

  private static URL classfile(Class<?> c) {
    return c.getResource(c.getSimpleName() + ".class");
  }
}