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