001: /******************************************************************
002: Copyright (c) 2005 Andy Jefferson and others. All rights reserved.
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:
016: Contributors:
017: ...
018: *****************************************************************/package org.jpox.metadata;
019:
020: import java.util.ArrayList;
021: import java.util.List;
022:
023: import junit.framework.TestCase;
024:
025: import org.apache.log4j.Category;
026: import org.jpox.PersistenceConfiguration;
027: import org.jpox.OMFContext;
028: import org.jpox.jdo.metadata.JDOMetaDataManager;
029:
030: /**
031: * Component tests for the MetaDataManager class.
032: * @version $Revision: 1.6 $
033: */
034: public class MetaDataManagerTest extends TestCase {
035: protected static final Category LOG = Category
036: .getInstance("JPOX.TEST");
037:
038: /**
039: * Start of the test, so log it and initialise.
040: * @param name Name of the <tt>TestCase</tt>.
041: */
042: public MetaDataManagerTest(String name) {
043: super (name);
044: }
045:
046: /**
047: * Test of the valid locations for a specified package.
048: */
049: public void testLocationsForPackage() {
050: JDOMetaDataManager mgr = new JDOMetaDataManager(new OMFContext(
051: new PersistenceConfiguration() {
052: }));
053:
054: // Try typical JDO package name
055: String packageName = "org.jpox.samples";
056: List locations = mgr.getValidMetaDataLocationsForPackage("jdo",
057: null, packageName);
058: assertTrue(
059: "Locations returned from MetaData Manager was null!",
060: locations != null);
061: List validLocations = new ArrayList();
062: validLocations.add("/META-INF/package.jdo");
063: validLocations.add("/WEB-INF/package.jdo");
064: validLocations.add("/package.jdo");
065: validLocations.add("/org.jdo");
066: validLocations.add("/org/package.jdo");
067: validLocations.add("/org/jpox.jdo");
068: validLocations.add("/org/jpox/package.jdo");
069: validLocations.add("/org/jpox/samples.jdo");
070: validLocations.add("/org/jpox/samples/package.jdo");
071: checkLocations(packageName, locations, validLocations);
072:
073: // Try 1 level package name
074: packageName = "org";
075: locations = mgr.getValidMetaDataLocationsForPackage("jdo",
076: null, packageName);
077: assertTrue(
078: "Locations returned from MetaData Manager was null!",
079: locations != null);
080: validLocations.clear();
081: validLocations.add("/META-INF/package.jdo");
082: validLocations.add("/WEB-INF/package.jdo");
083: validLocations.add("/package.jdo");
084: validLocations.add("/org.jdo");
085: validLocations.add("/org/package.jdo");
086: checkLocations(packageName, locations, validLocations);
087:
088: // Try 0 level package name
089: packageName = "";
090: locations = mgr.getValidMetaDataLocationsForPackage("jdo",
091: null, packageName);
092: assertTrue(
093: "Locations returned from MetaData Manager was null!",
094: locations != null);
095: validLocations.clear();
096: validLocations.add("/META-INF/package.jdo");
097: validLocations.add("/WEB-INF/package.jdo");
098: validLocations.add("/package.jdo");
099: checkLocations(packageName, locations, validLocations);
100:
101: // Try typical ORM package name
102: packageName = "org.jpox.samples";
103: locations = mgr.getValidMetaDataLocationsForPackage("orm",
104: "jpox", packageName);
105: assertTrue(
106: "Locations returned from MetaData Manager was null!",
107: locations != null);
108: validLocations.clear();
109: validLocations.add("/META-INF/package-jpox.orm");
110: validLocations.add("/WEB-INF/package-jpox.orm");
111: validLocations.add("/package-jpox.orm");
112: validLocations.add("/org-jpox.orm");
113: validLocations.add("/org/package-jpox.orm");
114: validLocations.add("/org/jpox-jpox.orm");
115: validLocations.add("/org/jpox/package-jpox.orm");
116: validLocations.add("/org/jpox/samples-jpox.orm");
117: validLocations.add("/org/jpox/samples/package-jpox.orm");
118: checkLocations(packageName, locations, validLocations);
119: }
120:
121: /**
122: * Test of the valid locations for a specified class.
123: */
124: public void testLocationsForClass() {
125: JDOMetaDataManager mgr = new JDOMetaDataManager(new OMFContext(
126: new PersistenceConfiguration() {
127: }));
128:
129: // Try typical JDO class name
130: String className = "org.jpox.samples.store.Product";
131: List locations = mgr.getValidMetaDataLocationsForClass("jdo",
132: null, className);
133: assertTrue(
134: "Locations returned from MetaData Manager was null!",
135: locations != null);
136: List validLocations = new ArrayList();
137: validLocations.add("/META-INF/package.jdo");
138: validLocations.add("/WEB-INF/package.jdo");
139: validLocations.add("/package.jdo");
140: validLocations.add("/org.jdo");
141: validLocations.add("/org/package.jdo");
142: validLocations.add("/org/jpox.jdo");
143: validLocations.add("/org/jpox/package.jdo");
144: validLocations.add("/org/jpox/samples.jdo");
145: validLocations.add("/org/jpox/samples/package.jdo");
146: validLocations.add("/org/jpox/samples/store.jdo");
147: validLocations.add("/org/jpox/samples/store/package.jdo");
148: validLocations.add("/org/jpox/samples/store/Product.jdo");
149: checkLocations(className, locations, validLocations);
150: }
151:
152: private void checkLocations(String packageName, List locations,
153: List validLocations) {
154: assertEquals("Number of valid locations for package "
155: + packageName + " is wrong", locations.size(),
156: validLocations.size());
157: for (int i = 0; i < validLocations.size(); i++) {
158: String validLocation = (String) validLocations.get(i);
159: assertTrue(
160: "Location "
161: + validLocation
162: + " is not returned as a valid location for package "
163: + packageName + ", but should be",
164: locations.contains(validLocation));
165: }
166: }
167: }
|