001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id$ */
019:
020: package org.apache.xmlgraphics.util;
021:
022: import java.util.Iterator;
023:
024: import org.apache.xmlgraphics.image.writer.ImageWriter;
025:
026: import junit.framework.TestCase;
027:
028: /**
029: * Test for the Service class.
030: */
031: public class ServiceTest extends TestCase {
032:
033: /**
034: * Tests the mode where Service returns instances.
035: * @throws Exception in case of an error
036: */
037: public void testWithInstances() throws Exception {
038: Class cls = ImageWriter.class;
039: boolean found = false;
040: Object writer1 = null;
041: Object writer2 = null;
042:
043: //First run: Find a writer implementation (one of the two must be available)
044: Iterator iter = Service.providers(cls);
045: while (iter.hasNext()) {
046: Object obj = iter.next();
047: assertNotNull(obj);
048: String className = obj.getClass().getName();
049: if ("org.apache.xmlgraphics.image.writer.internal.PNGImageWriter"
050: .equals(className)) {
051: writer1 = obj;
052: found = true;
053: break;
054: } else if ("org.apache.xmlgraphics.image.writer.imageio.ImageIOPNGImageWriter"
055: .equals(className)) {
056: writer2 = obj;
057: found = true;
058: break;
059: }
060: }
061: assertTrue("None of the expected classes found", found);
062:
063: //Second run: verify that the same instances are returned
064: iter = Service.providers(cls);
065: while (iter.hasNext()) {
066: Object obj = iter.next();
067: assertNotNull(obj);
068: String className = obj.getClass().getName();
069: if ("org.apache.xmlgraphics.image.writer.internal.PNGImageWriter"
070: .equals(className)) {
071: assertTrue(obj == writer1);
072: break;
073: } else if ("org.apache.xmlgraphics.image.writer.imageio.ImageIOPNGImageWriter"
074: .equals(className)) {
075: assertTrue(obj == writer2);
076: break;
077: }
078: }
079: }
080:
081: /**
082: * Tests the mode where Service returns class names.
083: * @throws Exception in case of an error
084: */
085: public void testWithClassNames() throws Exception {
086: Class cls = ImageWriter.class;
087: boolean found = true;
088: Iterator iter = Service.providers(cls, false);
089: while (iter.hasNext()) {
090: Object obj = iter.next();
091: assertNotNull(obj);
092: assertTrue("Returned object must be a class name",
093: obj instanceof String);
094: if ("org.apache.xmlgraphics.image.writer.internal.PNGImageWriter"
095: .equals(obj)
096: || ("org.apache.xmlgraphics.image.writer.imageio.ImageIOPNGImageWriter"
097: .equals(obj))) {
098: found = true;
099: }
100: }
101: assertTrue("None of the expected classes found", found);
102:
103: //Do it a second time to make sure the cache works as expected
104: iter = Service.providers(cls, false);
105: while (iter.hasNext()) {
106: Object obj = iter.next();
107: assertNotNull(obj);
108: assertTrue("Returned object must be a class name",
109: obj instanceof String);
110: }
111: }
112:
113: }
|