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: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028: package org.netbeans.api.db.sql.support;
029:
030: import org.netbeans.api.db.sql.support.SQLIdentifiers;
031: import org.netbeans.modules.db.util.DBTestBase;
032:
033: /**
034: * @author <a href="mailto:david@vancouvering.com">David Van Couvering</a>
035: *
036: * This class is a set of tests to make sure we're quoting identifiers
037: * correctly
038: */
039: public class QuoterTest extends DBTestBase {
040:
041: private SQLIdentifiers.Quoter quoter;
042:
043: public QuoterTest(String testName) {
044: super (testName);
045: }
046:
047: public void setUp() throws Exception {
048: super .setUp();
049: quoter = SQLIdentifiers.createQuoter(conn.getMetaData());
050: }
051:
052: public void testNoQuoting() {
053: String identifier = "YOUDONTNEEDTOQUOTEME2334252__1451";
054: String expResult = identifier;
055: String result = quoter.quoteIfNeeded(identifier);
056: assertEquals(expResult, result);
057: }
058:
059: public void testSpaces() throws Exception {
060: String identifier = "YesYou Need to quote me";
061: String expResult = quote(identifier);
062:
063: String result = quoter.quoteIfNeeded(identifier);
064:
065: assertEquals(expResult, result);
066: }
067:
068: public void testCasing() throws Exception {
069: String identifier;
070:
071: // First, find out what kind of casing is done with unquoted
072: // identifiers for this connection
073: int caseRule = getUnquotedCaseRule();
074:
075: switch (caseRule) {
076: case LC_RULE:
077: identifier = "ABCDEFG";
078: break;
079: case UC_RULE:
080: identifier = "abcdefg";
081: break;
082: default:
083: // Nothing to test here
084: return;
085: }
086:
087: String expResult = quote(identifier);
088:
089: String result = quoter.quoteIfNeeded(identifier);
090:
091: assertEquals(expResult, result);
092: }
093:
094: public void testNonAscii() throws Exception {
095: // borrowed translated message from Derby message file :)
096: String identifier = "abcdABCD0934"
097: + "\u4f8b\u5916\u306e\u305f\u3081\u3001\u59cb\u52d5"
098: + "\u306b\u5931\u6557\u3057\u307e\u3057\u305f\u3002 \u8a73\u7d30"
099: + "\u306b\u3064\u3044\u3066\u306f\u3001\u6b21\u306e\u4f8b\u5916"
100: + "\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002"
101: + "09298719871";
102:
103: String expResult = quote(identifier);
104:
105: String result = quoter.quoteIfNeeded(identifier);
106:
107: assertEquals(expResult, result);
108: }
109:
110: public void testDontQuoteQuoted() throws Exception {
111: String identifier = quote("I am already quoted");
112:
113: String expResult = identifier;
114:
115: String result = quoter.quoteIfNeeded(identifier);
116:
117: assertEquals(expResult, result);
118: }
119:
120: public void testNullIdentifier() throws Exception {
121: try {
122: quoter.quoteIfNeeded(null);
123: fail("Expected a NullPointerException");
124: } catch (NullPointerException npe) {
125: // expected
126: }
127: }
128:
129: public void testFirstCharIsUnderbar() throws Exception {
130: String identifier = "_NO_UNDERBAR_AS_FIRST_CHAR";
131:
132: String expResult = quote(identifier);
133:
134: String result = quoter.quoteIfNeeded(identifier);
135:
136: assertEquals(expResult, result);
137: }
138:
139: public void testFirstCharIsNumber() throws Exception {
140: String identifier = "1NO_NUMBER123_AS_FIRST_CHAR";
141:
142: String expResult = quote(identifier);
143:
144: String result = quoter.quoteIfNeeded(identifier);
145:
146: assertEquals(expResult, result);
147: }
148: }
|