001: // Copyright 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.internal.services;
016:
017: import java.util.Collection;
018: import java.util.Set;
019:
020: import org.apache.tapestry.ioc.internal.util.CollectionFactory;
021: import org.apache.tapestry.ioc.internal.util.InternalUtils;
022: import org.testng.Assert;
023: import org.testng.annotations.Test;
024:
025: /**
026: * Tricky to test, since the code is literally hunting around inside its own brain. There's a lot of
027: * room for unintended consequences here.
028: */
029: public class ClassNameLocatorImplTest extends Assert {
030: /**
031: * Use various packages in tapestry-ioc to test this, as those don't change unexpectedly(-ish)
032: * and we know they are in a JAR on the classpath.
033: */
034: @Test
035: public void classes_in_jar_file() {
036: ClassNameLocator locator = new ClassNameLocatorImpl();
037:
038: Collection<String> names = locator
039: .locateClassNames("org.apache.tapestry.ioc.internal.util");
040:
041: assertInList(names, "org.apache.tapestry.ioc.internal.util",
042: "MessagesImpl", "LocalizedNameGenerator", "IdAllocator");
043: assertNotInList(names, "org.apache.tapestry.ioc.internal.util",
044: "Orderer$1", "InheritanceSearch$State");
045: }
046:
047: @Test
048: public void classes_in_subpackage_in_jar_file() {
049: ClassNameLocator locator = new ClassNameLocatorImpl();
050:
051: Collection<String> names = locator
052: .locateClassNames("org.apache.tapestry.ioc");
053:
054: assertInList(names, "org.apache.tapestry.ioc",
055: "internal.Module", "internal.util.MessagesImpl");
056:
057: }
058:
059: /**
060: * This time, we use a selection of classes from tapestry-core, since those will never be
061: * packaged inside a JAR at this time.
062: */
063:
064: @Test
065: public void classes_in_local_folders() {
066: ClassNameLocator locator = new ClassNameLocatorImpl();
067:
068: Collection<String> names = locator
069: .locateClassNames("org.apache.tapestry.corelib.components");
070:
071: assertInList(names, "org.apache.tapestry.corelib.components",
072: "ActionLink", "Label");
073:
074: assertNotInList(names, "org.apache.tapestry.corelib",
075: "Label$1", "Loop$1");
076: }
077:
078: @Test
079: public void classes_in_subpackages_in_local_folders() {
080: ClassNameLocator locator = new ClassNameLocatorImpl();
081:
082: Collection<String> names = locator
083: .locateClassNames("org.apache.tapestry.corelib");
084:
085: assertInList(names, "org.apache.tapestry.corelib",
086: "components.ActionLink", "base.AbstractField",
087: "mixins.RenderInformals");
088:
089: assertNotInList(names, "org.apache.tapestry.corelib",
090: "components.Label$1");
091: }
092:
093: void assertInList(Collection<String> names, String packageName,
094: String... classNames) {
095: Set<String> classNameSet = CollectionFactory.newSet(names);
096:
097: for (String className : classNames) {
098: String fullName = packageName + "." + className;
099:
100: if (classNameSet.contains(fullName))
101: continue;
102:
103: String message = String.format("%s not found in %s.",
104: fullName, InternalUtils.joinSorted(names));
105:
106: throw new AssertionError(message);
107: }
108: }
109:
110: void assertNotInList(Collection<String> names, String packageName,
111: String... classNames) {
112: Set<String> classNameSet = CollectionFactory.newSet(names);
113:
114: for (String className : classNames) {
115: String fullName = packageName + "." + className;
116:
117: if (!classNameSet.contains(fullName))
118: continue;
119:
120: String message = String.format("%s found in %s.", fullName,
121: InternalUtils.joinSorted(names));
122:
123: throw new AssertionError(message);
124: }
125: }
126: }
|