1 /* 2 * Copyright (C) 2008 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.CharMatcher.WHITESPACE; 20 import static org.junit.contrib.truth.Truth.ASSERT; 21 22 import com.google.common.base.Charsets; 23 import com.google.common.collect.ImmutableList; 24 25 import java.io.ByteArrayInputStream; 26 import java.io.ByteArrayOutputStream; 27 import java.io.DataInputStream; 28 import java.io.IOException; 29 import java.net.URL; 30 import java.util.ArrayList; 31 import java.util.List; 32 33 /** 34 * Unit test for {@link Resources}. 35 * 36 * @author Chris Nokleberg 37 */ 38 public class ResourcesTest extends IoTestCase { 39 40 public void testUrlSupplier() throws IOException { 41 try { 42 Resources.newInputStreamSupplier(null); 43 fail("expected NPE"); 44 } catch (NullPointerException expected) { 45 // expected 46 } 47 48 URL url = getClass().getResource("/com/google/common/io/Resources.class"); 49 byte[] data = ByteStreams.toByteArray( 50 Resources.newInputStreamSupplier(url)); 51 assertEquals(0xCAFEBABE, 52 new DataInputStream(new ByteArrayInputStream(data)).readInt()); 53 } 54 55 public void testToString() throws IOException { 56 URL resource = getClass().getResource("testdata/i18n.txt"); 57 assertEquals(I18N, Resources.toString(resource, Charsets.UTF_8)); 58 ASSERT.that(Resources.toString(resource, Charsets.US_ASCII)) 59 .isNotEqualTo(I18N); 60 } 61 62 public void testToToByteArray() throws IOException { 63 URL url = getClass().getResource("/com/google/common/io/Resources.class"); 64 byte[] data = Resources.toByteArray(url); 65 assertEquals(0xCAFEBABE, 66 new DataInputStream(new ByteArrayInputStream(data)).readInt()); 67 } 68 69 public void testReadLines() throws IOException { 70 // TODO(chrisn): Check in a better resource 71 URL resource = getClass().getResource("testdata/i18n.txt"); 72 assertEquals(ImmutableList.of(I18N), 73 Resources.readLines(resource, Charsets.UTF_8)); 74 } 75 76 public void testReadLines_withLineProcessor() throws IOException { 77 URL resource = getClass().getResource("testdata/alice_in_wonderland.txt"); 78 LineProcessor<List<String>> collectAndLowercaseAndTrim = 79 new LineProcessor<List<String>>() { 80 List<String> collector = new ArrayList<String>(); 81 @Override 82 public boolean processLine(String line) { 83 collector.add(WHITESPACE.trimFrom(line)); 84 return true; 85 } 86 87 @Override 88 public List<String> getResult() { 89 return collector; 90 } 91 }; 92 List<String> result = Resources.readLines(resource, Charsets.US_ASCII, 93 collectAndLowercaseAndTrim); 94 assertEquals(3600, result.size()); 95 assertEquals("ALICE'S ADVENTURES IN WONDERLAND", result.get(0)); 96 assertEquals("THE END", result.get(result.size() - 1)); 97 } 98 99 public void testCopyToOutputStream() throws IOException { 100 ByteArrayOutputStream out = new ByteArrayOutputStream(); 101 URL resource = getClass().getResource("testdata/i18n.txt"); 102 Resources.copy(resource, out); 103 assertEquals(I18N, out.toString("UTF-8")); 104 } 105 106 public void testGetResource_notFound() { 107 try { 108 Resources.getResource("no such resource"); 109 fail(); 110 } catch (IllegalArgumentException e) { 111 assertEquals("resource no such resource not found.", e.getMessage()); 112 } 113 } 114 115 public void testGetResource() { 116 assertNotNull( 117 Resources.getResource("com/google/common/io/testdata/i18n.txt")); 118 } 119 120 public void testGetResource_relativePath_notFound() { 121 try { 122 Resources.getResource( 123 getClass(), "com/google/common/io/testdata/i18n.txt"); 124 fail(); 125 } catch (IllegalArgumentException e) { 126 assertEquals("resource com/google/common/io/testdata/i18n.txt" + 127 " relative to com.google.common.io.ResourcesTest not found.", 128 e.getMessage()); 129 } 130 } 131 132 public void testGetResource_relativePath() { 133 assertNotNull(Resources.getResource(getClass(), "testdata/i18n.txt")); 134 } 135 } 136