PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/projects/netbeans-7.3/db.sql.editor/test/unit/src/org/netbeans/modules/db/sql/editor/completion/TestMetadata.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 512 lines | 394 code | 73 blank | 45 comment | 59 complexity | 122243081acbdb6da7dd8755d975741d MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * If you wish your version of this file to be governed by only the CDDL
  28. * or only the GPL Version 2, indicate your decision by adding
  29. * "[Contributor] elects to include this software in this distribution
  30. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  31. * single choice of license, a recipient has the option to distribute
  32. * your version of this file under either the CDDL, the GPL Version 2 or
  33. * to extend the choice of license to its licensees as provided above.
  34. * However, if you add GPL Version 2 code and therefore, elected the GPL
  35. * Version 2 license, then the option applies only if the new code is
  36. * made subject to such option by the copyright holder.
  37. *
  38. * Contributor(s):
  39. *
  40. * Portions Copyrighted 2008 Sun Microsystems, Inc.
  41. */
  42. package org.netbeans.modules.db.sql.editor.completion;
  43. import java.util.Arrays;
  44. import java.util.Collection;
  45. import java.util.Comparator;
  46. import java.util.LinkedHashMap;
  47. import java.util.List;
  48. import java.util.Map;
  49. import java.util.TreeMap;
  50. import org.netbeans.modules.db.metadata.model.api.Catalog;
  51. import org.netbeans.modules.db.metadata.model.api.Column;
  52. import org.netbeans.modules.db.metadata.model.api.ForeignKey;
  53. import org.netbeans.modules.db.metadata.model.api.Index;
  54. import org.netbeans.modules.db.metadata.model.api.Metadata;
  55. import org.netbeans.modules.db.metadata.model.api.Nullable;
  56. import org.netbeans.modules.db.metadata.model.api.PrimaryKey;
  57. import org.netbeans.modules.db.metadata.model.api.Procedure;
  58. import org.netbeans.modules.db.metadata.model.api.SQLType;
  59. import org.netbeans.modules.db.metadata.model.api.Schema;
  60. import org.netbeans.modules.db.metadata.model.api.Table;
  61. import org.netbeans.modules.db.metadata.model.api.Tuple;
  62. import org.netbeans.modules.db.metadata.model.api.View;
  63. import org.netbeans.modules.db.metadata.model.spi.CatalogImplementation;
  64. import org.netbeans.modules.db.metadata.model.spi.ColumnImplementation;
  65. import org.netbeans.modules.db.metadata.model.spi.MetadataImplementation;
  66. import org.netbeans.modules.db.metadata.model.spi.SchemaImplementation;
  67. import org.netbeans.modules.db.metadata.model.spi.TableImplementation;
  68. import org.netbeans.modules.db.metadata.model.spi.ViewImplementation;
  69. /**
  70. *
  71. * @author Andrei Badea
  72. */
  73. public class TestMetadata extends MetadataImplementation {
  74. private final Map<String, Catalog> catalogs = new TreeMap<String, Catalog>(new NullStringComparator());
  75. private Catalog defaultCatalog;
  76. private TestCatalog defaultCatalogImpl;
  77. public static Metadata create(List<String> spec) {
  78. return new TestMetadata(spec).getMetadata();
  79. }
  80. public TestMetadata(String[] spec) {
  81. this(Arrays.asList(spec));
  82. }
  83. private TestMetadata(List<String> spec) {
  84. parse(spec);
  85. if (defaultCatalog == null) {
  86. throw new IllegalArgumentException();
  87. }
  88. Schema defaultSchema = getDefaultSchema();
  89. if (defaultSchema != null) {
  90. if (!defaultCatalog.getSchemas().contains(defaultSchema)) {
  91. Schema syntheticSchema = defaultCatalog.getSyntheticSchema();
  92. if (syntheticSchema != null && syntheticSchema != defaultSchema) {
  93. throw new IllegalArgumentException();
  94. }
  95. }
  96. }
  97. }
  98. private void parse(List<String> spec) {
  99. TestCatalog catalogImpl = null;
  100. TestSchema schemaImpl = null;
  101. TestTable tableImpl = null;
  102. TestView viewImpl = null;
  103. for (String line : spec) {
  104. int count = 0;
  105. while (count < line.length() && line.charAt(count) == ' ') {
  106. count++;
  107. }
  108. if (count == line.length()) {
  109. continue;
  110. }
  111. String trimmed = line.trim();
  112. boolean _default = false;
  113. if (trimmed.endsWith("*")) {
  114. trimmed = trimmed.replace("*", "");
  115. _default = true;
  116. }
  117. switch (count) {
  118. case 0:
  119. if (trimmed.equals("<unknown>")) {
  120. _default = true;
  121. trimmed = null;
  122. }
  123. catalogImpl = new TestCatalog(trimmed, _default);
  124. Catalog catalog = catalogImpl.getCatalog();
  125. if (catalogs.get(trimmed) != null) {
  126. throw new IllegalArgumentException(line);
  127. }
  128. catalogs.put(trimmed, catalog);
  129. if (_default) {
  130. if (defaultCatalog != null) {
  131. throw new IllegalArgumentException(line);
  132. }
  133. defaultCatalog = catalog;
  134. defaultCatalogImpl = catalogImpl;
  135. }
  136. break;
  137. case 2:
  138. boolean synthetic = false;
  139. if (trimmed.equals("<no-schema>")) {
  140. trimmed = null;
  141. _default = true;
  142. synthetic = true;
  143. }
  144. schemaImpl = new TestSchema(catalogImpl, trimmed, _default, synthetic);
  145. Schema schema = schemaImpl.getSchema();
  146. if (synthetic) {
  147. if (catalogImpl.syntheticSchema != null) {
  148. throw new IllegalArgumentException(line);
  149. }
  150. catalogImpl.syntheticSchema = schema;
  151. } else {
  152. if (catalogImpl.schemas.get(trimmed) != null) {
  153. throw new IllegalArgumentException(line);
  154. }
  155. catalogImpl.schemas.put(trimmed, schema);
  156. }
  157. if (_default) {
  158. if (defaultCatalog != catalogImpl.getCatalog() || catalogImpl.defaultSchema != null) {
  159. throw new IllegalArgumentException(line);
  160. }
  161. catalogImpl.defaultSchema = schema;
  162. }
  163. break;
  164. case 4:
  165. if (schemaImpl == null) {
  166. throw new IllegalArgumentException(line);
  167. }
  168. tableImpl = null;
  169. viewImpl = null;
  170. if (trimmed.endsWith("[view]")) {
  171. String viewName = trimmed.replace("[view]", "");
  172. viewImpl = new TestView(schemaImpl, viewName);
  173. schemaImpl.views.put(viewName, viewImpl.getView());
  174. } else {
  175. tableImpl = new TestTable(schemaImpl, trimmed);
  176. schemaImpl.tables.put(trimmed, tableImpl.getTable());
  177. }
  178. break;
  179. case 6:
  180. if (schemaImpl == null
  181. || (tableImpl == null && viewImpl == null)) {
  182. throw new IllegalArgumentException(line);
  183. }
  184. if (tableImpl != null) {
  185. tableImpl.columns.put(trimmed, new TestColumn(tableImpl, trimmed).getColumn());
  186. } else {
  187. viewImpl.columns.put(trimmed, new TestColumn(tableImpl, trimmed).getColumn());
  188. }
  189. break;
  190. default:
  191. throw new IllegalArgumentException(line);
  192. }
  193. }
  194. }
  195. public Catalog getDefaultCatalog() {
  196. return defaultCatalog;
  197. }
  198. public Collection<Catalog> getCatalogs() {
  199. return catalogs.values();
  200. }
  201. public Catalog getCatalog(String name) {
  202. return catalogs.get(name);
  203. }
  204. public Schema getDefaultSchema() {
  205. return defaultCatalogImpl.getDefaultSchema();
  206. }
  207. public void refresh() {
  208. }
  209. public void refreshTable(String tablename) {
  210. }
  211. static final class TestCatalog extends CatalogImplementation {
  212. private final String name;
  213. private final boolean _default;
  214. Map<String, Schema> schemas = new TreeMap<String, Schema>();
  215. Schema defaultSchema;
  216. Schema syntheticSchema;
  217. public TestCatalog(String name, boolean _default) {
  218. this.name = name;
  219. this._default = _default;
  220. }
  221. public String getName() {
  222. return name;
  223. }
  224. public boolean isDefault() {
  225. return _default;
  226. }
  227. public Schema getSyntheticSchema() {
  228. return syntheticSchema;
  229. }
  230. public Collection<Schema> getSchemas() {
  231. return schemas.values();
  232. }
  233. public Schema getSchema(String name) {
  234. return schemas.get(name);
  235. }
  236. public Schema getDefaultSchema() {
  237. return defaultSchema;
  238. }
  239. @Override
  240. public void refresh() {
  241. throw new UnsupportedOperationException("Not supported yet.");
  242. }
  243. }
  244. static final class TestSchema extends SchemaImplementation {
  245. private final TestCatalog catalogImpl;
  246. private final String name;
  247. private final boolean _default;
  248. private final boolean synthetic;
  249. private final Map<String, Table> tables = new TreeMap<String, Table>();
  250. private final Map<String, View> views = new TreeMap<String, View>();
  251. public TestSchema(TestCatalog catalogImpl, String name, boolean _default, boolean synthetic) {
  252. this.catalogImpl = catalogImpl;
  253. this.name = name;
  254. this._default = _default;
  255. this.synthetic = synthetic;
  256. }
  257. public Catalog getParent() {
  258. return catalogImpl.getCatalog();
  259. }
  260. public String getName() {
  261. return name;
  262. }
  263. public boolean isDefault() {
  264. return _default;
  265. }
  266. public boolean isSynthetic() {
  267. return synthetic;
  268. }
  269. public Collection<Table> getTables() {
  270. return tables.values();
  271. }
  272. public Table getTable(String name) {
  273. return tables.get(name);
  274. }
  275. @Override
  276. public View getView(String name) {
  277. return views.get(name);
  278. }
  279. @Override
  280. public Collection<View> getViews() {
  281. return views.values();
  282. }
  283. @Override
  284. public Procedure getProcedure(String name) {
  285. throw new UnsupportedOperationException("Not supported yet.");
  286. }
  287. @Override
  288. public Collection<Procedure> getProcedures() {
  289. throw new UnsupportedOperationException("Not supported yet.");
  290. }
  291. @Override
  292. public void refresh() {
  293. throw new UnsupportedOperationException("Not supported yet.");
  294. }
  295. }
  296. static final class TestTable extends TableImplementation {
  297. private final TestSchema schemaImpl;
  298. private final String name;
  299. private final Map<String, Column> columns = new LinkedHashMap<String, Column>();
  300. public TestTable(TestSchema schemaImpl, String name) {
  301. this.schemaImpl = schemaImpl;
  302. this.name = name;
  303. }
  304. public Schema getParent() {
  305. return schemaImpl.getSchema();
  306. }
  307. public String getName() {
  308. return name;
  309. }
  310. public Collection<Column> getColumns() {
  311. return columns.values();
  312. }
  313. public Column getColumn(String name) {
  314. return columns.get(name);
  315. }
  316. @Override
  317. public void refresh() {
  318. throw new UnsupportedOperationException("Not supported yet.");
  319. }
  320. @Override
  321. public PrimaryKey getPrimaryKey() {
  322. throw new UnsupportedOperationException("Not supported yet.");
  323. }
  324. @Override
  325. public Index getIndex(String name) {
  326. throw new UnsupportedOperationException("Not supported yet.");
  327. }
  328. @Override
  329. public Collection<Index> getIndexes() {
  330. throw new UnsupportedOperationException("Not supported yet.");
  331. }
  332. @Override
  333. public Collection<ForeignKey> getForeignKeys() {
  334. throw new UnsupportedOperationException("Not supported yet.");
  335. }
  336. @Override
  337. public ForeignKey getForeignKeyByInternalName(String internalName) {
  338. throw new UnsupportedOperationException("Not supported yet.");
  339. }
  340. }
  341. static final class TestView extends ViewImplementation {
  342. private final TestSchema schemaImpl;
  343. private final String name;
  344. private final Map<String, Column> columns = new LinkedHashMap<String, Column>();
  345. public TestView(TestSchema schemaImpl, String name) {
  346. this.schemaImpl = schemaImpl;
  347. this.name = name;
  348. }
  349. @Override
  350. public Schema getParent() {
  351. return schemaImpl.getSchema();
  352. }
  353. @Override
  354. public String getName() {
  355. return name;
  356. }
  357. @Override
  358. public Collection<Column> getColumns() {
  359. return columns.values();
  360. }
  361. @Override
  362. public Column getColumn(String name) {
  363. return columns.get(name);
  364. }
  365. @Override
  366. public void refresh() {
  367. throw new UnsupportedOperationException("Not supported yet.");
  368. }
  369. }
  370. static final class TestColumn extends ColumnImplementation {
  371. private final TestTable tableImpl;
  372. private final TestView viewImpl;
  373. private final String name;
  374. private TestColumn(TestTable tableImpl, String name) {
  375. this.tableImpl = tableImpl;
  376. this.name = name;
  377. this.viewImpl = null;
  378. }
  379. private TestColumn(TestView viewImpl, String name) {
  380. this.viewImpl = viewImpl;
  381. this.name = name;
  382. this.tableImpl = null;
  383. }
  384. public Tuple getParent() {
  385. if (viewImpl != null) {
  386. return viewImpl.getView();
  387. }
  388. return tableImpl.getTable();
  389. }
  390. public String getName() {
  391. return name;
  392. }
  393. @Override
  394. public int getPosition() {
  395. throw new UnsupportedOperationException("Not supported yet.");
  396. }
  397. @Override
  398. public int getLength() {
  399. throw new UnsupportedOperationException("Not supported yet.");
  400. }
  401. @Override
  402. public Nullable getNullable() {
  403. throw new UnsupportedOperationException("Not supported yet.");
  404. }
  405. @Override
  406. public int getPrecision() {
  407. throw new UnsupportedOperationException("Not supported yet.");
  408. }
  409. @Override
  410. public short getRadix() {
  411. throw new UnsupportedOperationException("Not supported yet.");
  412. }
  413. @Override
  414. public short getScale() {
  415. throw new UnsupportedOperationException("Not supported yet.");
  416. }
  417. @Override
  418. public SQLType getType() {
  419. throw new UnsupportedOperationException("Not supported yet.");
  420. }
  421. }
  422. private static final class NullStringComparator implements Comparator<String> {
  423. public int compare(String string1, String string2) {
  424. if (string1 == null) {
  425. if (string2 == null) {
  426. return 0;
  427. } else {
  428. return -1;
  429. }
  430. } else {
  431. if (string2 == null) {
  432. return 1;
  433. } else {
  434. return string1.compareTo(string2);
  435. }
  436. }
  437. }
  438. }
  439. }