KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > unit > PersistenceValidatorTest


1 /**
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.persistence.unit;
21
22 import junit.framework.*;
23 import java.util.List JavaDoc;
24 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit;
25 import org.netbeans.modules.xml.multiview.Error;
26
27 /**
28  * Tests for the <code>PersistenceValidator</code>.
29  * @author Erno Mononen
30  */

31 public class PersistenceValidatorTest extends PersistenceEditorTestBase {
32     
33     public PersistenceValidatorTest(String JavaDoc testName) {
34         super(testName);
35     }
36     
37     protected void setUp() throws Exception JavaDoc {
38         super.setUp();
39     }
40     
41     /**
42      * Tests that validator reports duplicate names as errors.
43      */

44     public void testValidateNameIsUnique() {
45         PersistenceUnit unit1 = new PersistenceUnit();
46         unit1.setName("name1");
47         dataObject.addPersistenceUnit(unit1);
48         PersistenceUnit unit2 = new PersistenceUnit();
49         unit2.setName("name1");
50         dataObject.addPersistenceUnit(unit2);
51         PersistenceValidator validator = new PersistenceValidatorImpl(dataObject, false);
52         List JavaDoc<Error JavaDoc> errors = validator.validate();
53         assertEquals(2, errors.size());
54         assertEquals(Error.DUPLICATE_VALUE_MESSAGE, errors.get(0).getErrorType());
55         assertEquals(Error.DUPLICATE_VALUE_MESSAGE, errors.get(1).getErrorType());
56     }
57
58     
59     /**
60      * Tests that validator reports usage of exclude-unlisted-classes in
61      * Java SE environments as errors.
62      */

63     public void testValidateExcludeUnlistedClasses(){
64         // Java SE
65
PersistenceValidator javaSEvalidator = new PersistenceValidatorImpl(dataObject, true);
66         PersistenceUnit unit1 = new PersistenceUnit();
67         unit1.setName("unit1");
68         unit1.setExcludeUnlistedClasses(true);
69         dataObject.addPersistenceUnit(unit1);
70         List JavaDoc<Error JavaDoc> errors = javaSEvalidator.validate();
71         assertEquals(1, errors.size());
72         assertEquals(Error.TYPE_WARNING, errors.get(0).getErrorType());
73         // Java EE
74
PersistenceValidator javaEEvalidator = new PersistenceValidatorImpl(dataObject, false);
75         errors = javaEEvalidator.validate();
76         assertTrue(errors.isEmpty());;
77     }
78     
79     /**
80      * Tests that validator reports usage of jar-files in
81      * Java SE environments as errors.
82      */

83     public void testValidateJarFiles(){
84         // Java SE
85
PersistenceValidator javaSEvalidator = new PersistenceValidatorImpl(dataObject, true);
86         PersistenceUnit unit1 = new PersistenceUnit();
87         unit1.setName("unit1");
88         unit1.addJarFile("my-jar.jar");
89         dataObject.addPersistenceUnit(unit1);
90         List JavaDoc<Error JavaDoc> errors = javaSEvalidator.validate();
91         assertEquals(1, errors.size());
92         assertEquals(Error.TYPE_WARNING, errors.get(0).getErrorType());
93         // Java EE
94
PersistenceValidator javaEEvalidator = new PersistenceValidatorImpl(dataObject, false);
95         errors = javaEEvalidator.validate();
96         assertTrue(errors.isEmpty());;
97     }
98     
99     /**
100      * Implementation of PersistenceValidator that allows to be specified
101      * whether we're dealing with Java SE environment.
102      */

103     private static class PersistenceValidatorImpl extends PersistenceValidator {
104         
105         private boolean javaSE;
106         
107         public PersistenceValidatorImpl(PUDataObject puDataObject, boolean javaSE){
108             super(puDataObject);
109             this.javaSE = javaSE;
110         }
111
112         protected boolean isJavaSE() {
113             return javaSE;
114         }
115         
116         
117     }
118 }
119
Popular Tags