1 /** 2 * Copyright 2006-2017 the original author or 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 package org.objenesis.tck; 17 18 import static org.junit.Assert.*; 19 20 import java.io.ByteArrayInputStream; 21 import java.io.IOException; 22 23 import org.junit.Before; 24 import org.junit.Test; 25 26 /** 27 * @author Joe Walnes 28 */ 29 public class CandidateLoaderTest { 30 31 private StringBuilder recordedEvents; 32 private CandidateLoader candidateLoader; 33 34 @Before 35 public void setUp() throws Exception { 36 37 recordedEvents = new StringBuilder(); 38 TCK tck = new TCK() { 39 @Override 40 public void registerCandidate(Class<?> candidateClass, String description) { 41 recordedEvents.append("registerCandidate('").append(candidateClass).append("', '") 42 .append(description).append("')\n"); 43 } 44 }; 45 CandidateLoader.ErrorHandler errorHandler = new CandidateLoader.ErrorHandler() { 46 public void classNotFound(String name) { 47 recordedEvents.append("classNotFound('").append(name).append("')\n"); 48 } 49 }; 50 51 candidateLoader = new CandidateLoader(tck, getClass().getClassLoader(), errorHandler); 52 } 53 54 @Test 55 public void testReadsClassesAndDescriptionsFromPropertiesFile() throws IOException { 56 String input = "" + "org.objenesis.tck.CandidateLoaderTest$A = A candidate\n" + "\n" 57 + "# a comment and some whitespace\n" + "\n" 58 + "org.objenesis.tck.CandidateLoaderTest$B = B candidate\n" 59 + "org.objenesis.tck.CandidateLoaderTest$C = C candidate\n"; 60 61 candidateLoader.loadFrom(new ByteArrayInputStream(input.getBytes())); 62 63 assertEquals("" 64 + "registerCandidate('class org.objenesis.tck.CandidateLoaderTest$A', 'A candidate')\n" 65 + "registerCandidate('class org.objenesis.tck.CandidateLoaderTest$B', 'B candidate')\n" 66 + "registerCandidate('class org.objenesis.tck.CandidateLoaderTest$C', 'C candidate')\n", 67 recordedEvents.toString()); 68 } 69 70 @Test 71 public void testReportsMissingClassesToErrorHandler() throws IOException { 72 String input = "" + "org.objenesis.tck.CandidateLoaderTest$A = A candidate\n" 73 + "org.objenesis.tck.CandidateLoaderTest$NonExistant = Dodgy candidate\n" 74 + "org.objenesis.tck.CandidateLoaderTest$C = C candidate\n"; 75 76 candidateLoader.loadFrom(new ByteArrayInputStream(input.getBytes())); 77 78 assertEquals("" 79 + "registerCandidate('class org.objenesis.tck.CandidateLoaderTest$A', 'A candidate')\n" 80 + "classNotFound('org.objenesis.tck.CandidateLoaderTest$NonExistant')\n" 81 + "registerCandidate('class org.objenesis.tck.CandidateLoaderTest$C', 'C candidate')\n", 82 recordedEvents.toString()); 83 } 84 85 @Test 86 public void testLoadsFromResourceInClassPath() throws IOException { 87 // See CandidateLoaderTest-sample.properties. 88 89 candidateLoader.loadFromResource(getClass(), "CandidateLoaderTest-sample.properties"); 90 91 assertEquals("" 92 + "registerCandidate('class org.objenesis.tck.CandidateLoaderTest$A', 'A candidate')\n" 93 + "registerCandidate('class org.objenesis.tck.CandidateLoaderTest$B', 'B candidate')\n", 94 recordedEvents.toString()); 95 } 96 97 @Test 98 public void testThrowsIOExceptionIfResourceNotInClassPath() throws IOException { 99 try { 100 candidateLoader.loadFromResource(getClass(), "Blatently-Bogus.properties"); 101 fail("Expected exception"); 102 } 103 catch(IOException expectedException) { 104 // Good! 105 } 106 } 107 108 // Sample classes. 109 110 public static class A { 111 } 112 113 public static class B { 114 } 115 116 public static class C { 117 } 118 } 119