001: /**
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */package org.netbeans.modules.j2ee.persistence.unit;
041:
042: import junit.framework.*;
043: import java.util.List;
044: import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit;
045: import org.netbeans.modules.xml.multiview.Error;
046:
047: /**
048: * Tests for the <code>PersistenceValidator</code>.
049: * @author Erno Mononen
050: */
051: public class PersistenceValidatorTest extends PersistenceEditorTestBase {
052:
053: public PersistenceValidatorTest(String testName) {
054: super (testName);
055: }
056:
057: protected void setUp() throws Exception {
058: super .setUp();
059: }
060:
061: /**
062: * Tests that validator reports duplicate names as errors.
063: */
064: public void testValidateNameIsUnique() {
065: PersistenceUnit unit1 = new PersistenceUnit();
066: unit1.setName("name1");
067: dataObject.addPersistenceUnit(unit1);
068: PersistenceUnit unit2 = new PersistenceUnit();
069: unit2.setName("name1");
070: dataObject.addPersistenceUnit(unit2);
071: PersistenceValidator validator = new PersistenceValidatorImpl(
072: dataObject, false);
073: List<Error> errors = validator.validate();
074: assertEquals(2, errors.size());
075: assertEquals(Error.DUPLICATE_VALUE_MESSAGE, errors.get(0)
076: .getErrorType());
077: assertEquals(Error.DUPLICATE_VALUE_MESSAGE, errors.get(1)
078: .getErrorType());
079: }
080:
081: /**
082: * Tests that validator reports usage of exclude-unlisted-classes in
083: * Java SE environments as errors.
084: */
085: public void testValidateExcludeUnlistedClasses() {
086: // Java SE
087: PersistenceValidator javaSEvalidator = new PersistenceValidatorImpl(
088: dataObject, true);
089: PersistenceUnit unit1 = new PersistenceUnit();
090: unit1.setName("unit1");
091: unit1.setExcludeUnlistedClasses(true);
092: dataObject.addPersistenceUnit(unit1);
093: List<Error> errors = javaSEvalidator.validate();
094: assertEquals(1, errors.size());
095: assertEquals(Error.TYPE_WARNING, errors.get(0).getErrorType());
096: // Java EE
097: PersistenceValidator javaEEvalidator = new PersistenceValidatorImpl(
098: dataObject, false);
099: errors = javaEEvalidator.validate();
100: assertTrue(errors.isEmpty());
101: ;
102: }
103:
104: /**
105: * Tests that validator reports usage of jar-files in
106: * Java SE environments as errors.
107: */
108: public void testValidateJarFiles() {
109: // Java SE
110: PersistenceValidator javaSEvalidator = new PersistenceValidatorImpl(
111: dataObject, true);
112: PersistenceUnit unit1 = new PersistenceUnit();
113: unit1.setName("unit1");
114: unit1.addJarFile("my-jar.jar");
115: dataObject.addPersistenceUnit(unit1);
116: List<Error> errors = javaSEvalidator.validate();
117: assertEquals(1, errors.size());
118: assertEquals(Error.TYPE_WARNING, errors.get(0).getErrorType());
119: // Java EE
120: PersistenceValidator javaEEvalidator = new PersistenceValidatorImpl(
121: dataObject, false);
122: errors = javaEEvalidator.validate();
123: assertTrue(errors.isEmpty());
124: ;
125: }
126:
127: /**
128: * Implementation of PersistenceValidator that allows to be specified
129: * whether we're dealing with Java SE environment.
130: */
131: private static class PersistenceValidatorImpl extends
132: PersistenceValidator {
133:
134: private boolean javaSE;
135:
136: public PersistenceValidatorImpl(PUDataObject puDataObject,
137: boolean javaSE) {
138: super (puDataObject);
139: this .javaSE = javaSE;
140: }
141:
142: protected boolean isJavaSE() {
143: return javaSE;
144: }
145:
146: }
147: }
|