cleanup, refactoring
Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/0ab79480 Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/0ab79480 Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/0ab79480 Branch: refs/heads/master Commit: 0ab79480cf00bfb5b67bb101b27626fec92b8550 Parents: 09917c3 Author: Andrus Adamchik <and...@objectstyle.com> Authored: Tue Oct 4 14:16:48 2016 +0300 Committer: Andrus Adamchik <and...@objectstyle.com> Committed: Tue Oct 4 14:18:38 2016 +0300 ---------------------------------------------------------------------- .../cayenne/dbsync/filter/NameFilter.java | 27 ++ .../dbsync/filter/NamePatternMatcher.java | 225 +++++++++++ .../dbsync/reverse/FiltersConfigBuilder.java | 359 ----------------- .../cayenne/dbsync/reverse/NameFilter.java | 27 -- .../dbsync/reverse/NamePatternMatcher.java | 225 ----------- .../reverse/db/DbAttributesPerSchemaLoader.java | 2 +- .../cayenne/dbsync/reverse/db/DbLoader.java | 2 +- .../reverse/filters/FiltersConfigBuilder.java | 353 +++++++++++++++++ .../reverse/filters/IncludeTableFilter.java | 2 +- .../dbsync/reverse/filters/PatternFilter.java | 6 +- .../dbsync/filter/NamePatternMatcherTest.java | 73 ++++ .../reverse/FiltersConfigBuilderTest.java | 391 ------------------ .../filters/FiltersConfigBuilderTest.java | 392 +++++++++++++++++++ .../reverse/filters/PatternFilterTest.java | 50 +-- .../cayenne/gen/ClassGenerationAction.java | 2 +- .../cayenne/tools/AntDataPortDelegate.java | 2 +- .../CayenneGeneratorEntityFilterAction.java | 2 +- .../cayenne/tools/CayenneGeneratorTask.java | 2 +- .../apache/cayenne/tools/DbImporterTask.java | 2 +- .../cayenne/tools/NamePatternMatcherTest.java | 74 ---- .../modeler/dialog/db/DbLoaderHelper.java | 2 +- .../dialog/db/ReverseEngineeringController.java | 2 +- .../cayenne/tools/CayenneGeneratorMojo.java | 2 +- .../apache/cayenne/tools/DbImporterMojo.java | 2 +- 24 files changed, 1110 insertions(+), 1116 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cayenne/blob/0ab79480/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/filter/NameFilter.java ---------------------------------------------------------------------- diff --git a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/filter/NameFilter.java b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/filter/NameFilter.java new file mode 100644 index 0000000..eabb8ee --- /dev/null +++ b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/filter/NameFilter.java @@ -0,0 +1,27 @@ +/***************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + ****************************************************************/ +package org.apache.cayenne.dbsync.filter; + +/** + * @since 4.0. + */ +public interface NameFilter { + + boolean isIncluded(String string); +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/0ab79480/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/filter/NamePatternMatcher.java ---------------------------------------------------------------------- diff --git a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/filter/NamePatternMatcher.java b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/filter/NamePatternMatcher.java new file mode 100644 index 0000000..211b62e --- /dev/null +++ b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/filter/NamePatternMatcher.java @@ -0,0 +1,225 @@ +/***************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + ****************************************************************/ + +package org.apache.cayenne.dbsync.filter; + +import org.apache.cayenne.util.CayenneMapEntry; +import org.apache.commons.logging.Log; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; + +/** + * Provides name pattern matching functionality. + * + * @since 1.2 + */ +public class NamePatternMatcher implements NameFilter { + + private static final String[] EMPTY_ARRAY = new String[0]; + private static final Pattern COMMA = Pattern.compile(","); + + private final Pattern[] itemIncludeFilters; + private final Pattern[] itemExcludeFilters; + + public static NamePatternMatcher build(Log logger, String includePattern, String excludePattern) { + return new NamePatternMatcher(createPatterns(logger, includePattern), createPatterns(logger, excludePattern)); + } + + public NamePatternMatcher(Pattern[] itemIncludeFilters, Pattern[] itemExcludeFilters) { + this.itemIncludeFilters = itemIncludeFilters; + this.itemExcludeFilters = itemExcludeFilters; + } + + /** + * Applies preconfigured list of filters to the list, removing entities that do not + * pass the filter. + * + * @deprecated since 3.0 still used by AntDataPortDelegate, which itself should + * probably be deprecated + */ + @Deprecated + public List<?> filter(List<?> items) { + if (items == null || items.isEmpty()) { + return items; + } + + if (itemIncludeFilters.length == 0 && itemExcludeFilters.length == 0) { + return items; + } + + Iterator<?> it = items.iterator(); + while (it.hasNext()) { + CayenneMapEntry entity = (CayenneMapEntry) it.next(); + + if (!passedIncludeFilter(entity.getName())) { + it.remove(); + continue; + } + + if (!passedExcludeFilter(entity.getName())) { + it.remove(); + } + } + + return items; + } + + /** + * Returns an array of Patterns. Takes a comma-separated list of patterns, attempting + * to convert them to the java.util.regex.Pattern syntax. E.g. + * <p> + * <code>"billing_*,user?"</code> will become an array of two expressions: + * <p> + * <code>^billing_.*$</code><br> + * <code>^user.?$</code><br> + */ + public static Pattern[] createPatterns(Log logger, String patternString) { + if (patternString == null) { + return new Pattern[0]; + } + String[] patternStrings = tokenizePattern(patternString); + List<Pattern> patterns = new ArrayList<Pattern>(patternStrings.length); + + for (String patternString1 : patternStrings) { + + // test the pattern + try { + patterns.add(Pattern.compile(patternString1)); + } catch (PatternSyntaxException e) { + + if (logger != null) { + logger.warn("Ignoring invalid pattern [" + patternString1 + "], reason: " + e.getMessage()); + } + } + } + + return patterns.toArray(new Pattern[patterns.size()]); + } + + /** + * Returns an array of valid regular expressions. Takes a comma-separated list of + * patterns, attempting to convert them to the java.util.regex.Pattern syntax. E.g. + * <p> + * <code>"billing_*,user?"</code> will become an array of two expressions: + * <p> + * <code>^billing_.*$</code><br> + * <code>^user.?$</code><br> + */ + public static String[] tokenizePattern(String pattern) { + if (pattern == null || pattern.isEmpty()) { + return EMPTY_ARRAY; + } + + String[] patterns = COMMA.split(pattern); + if (patterns.length == 0) { + return EMPTY_ARRAY; + } + + for (int i = 0; i < patterns.length; i++) { + // convert * into regex syntax + // e.g. abc*x becomes ^abc.*x$ + // or abc?x becomes ^abc.?x$ + patterns[i] = "^" + patterns[i].replaceAll("[*?]", ".$0") + "$"; + } + + return patterns; + } + + /** + * Returns true if a given object property satisfies the include/exclude patterns. + * + * @since 3.0 + */ + @Override + public boolean isIncluded(String string) { + return passedIncludeFilter(string) && passedExcludeFilter(string); + } + + /** + * Returns true if an object matches any one of the "include" patterns, or if there is + * no "include" patterns defined. + * + * @since 3.0 + */ + private boolean passedIncludeFilter(String item) { + if (itemIncludeFilters.length == 0) { + return true; + } + + for (Pattern itemIncludeFilter : itemIncludeFilters) { + if (itemIncludeFilter.matcher(item).find()) { + return true; + } + } + + return false; + } + + /** + * Returns true if an object does not match any one of the "exclude" patterns, or if + * there is no "exclude" patterns defined. + * + * @since 3.0 + */ + private boolean passedExcludeFilter(String item) { + if (itemExcludeFilters.length == 0) { + return true; + } + + for (Pattern itemExcludeFilter : itemExcludeFilters) { + if (itemExcludeFilter.matcher(item).find()) { + return false; + } + } + + return true; + } + + public static String replaceWildcardInStringWithString( + String wildcard, + String pattern, + String replacement) { + + if (pattern == null || wildcard == null) { + return pattern; + } + + StringBuilder buffer = new StringBuilder(); + int lastPos = 0; + int wildCardPos = pattern.indexOf(wildcard); + while (wildCardPos != -1) { + if (lastPos != wildCardPos) { + buffer.append(pattern.substring(lastPos, wildCardPos)); + } + buffer.append(replacement); + lastPos += wildCardPos + wildcard.length(); + wildCardPos = pattern.indexOf(wildcard, lastPos); + } + + if (lastPos < pattern.length()) { + buffer.append(pattern.substring(lastPos)); + } + + return buffer.toString(); + } +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/0ab79480/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/FiltersConfigBuilder.java ---------------------------------------------------------------------- diff --git a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/FiltersConfigBuilder.java b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/FiltersConfigBuilder.java deleted file mode 100644 index 4f72f6d..0000000 --- a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/FiltersConfigBuilder.java +++ /dev/null @@ -1,359 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * <p/> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p/> - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - ****************************************************************/ -package org.apache.cayenne.dbsync.reverse; - -import org.apache.cayenne.dbimport.Catalog; -import org.apache.cayenne.dbimport.ExcludeColumn; -import org.apache.cayenne.dbimport.ExcludeProcedure; -import org.apache.cayenne.dbimport.ExcludeTable; -import org.apache.cayenne.dbimport.IncludeColumn; -import org.apache.cayenne.dbimport.IncludeProcedure; -import org.apache.cayenne.dbimport.IncludeTable; -import org.apache.cayenne.dbimport.PatternParam; -import org.apache.cayenne.dbimport.ReverseEngineering; -import org.apache.cayenne.dbimport.Schema; -import org.apache.cayenne.dbsync.reverse.filters.CatalogFilter; -import org.apache.cayenne.dbsync.reverse.filters.FiltersConfig; -import org.apache.cayenne.dbsync.reverse.filters.IncludeTableFilter; -import org.apache.cayenne.dbsync.reverse.filters.PatternFilter; -import org.apache.cayenne.dbsync.reverse.filters.SchemaFilter; -import org.apache.cayenne.dbsync.reverse.filters.TableFilter; - -import java.util.Collection; -import java.util.LinkedList; -import java.util.SortedSet; -import java.util.TreeSet; -import java.util.regex.Pattern; - -/** - * @since 4.0 - */ -public final class FiltersConfigBuilder { - - private final ReverseEngineering engineering; - - public FiltersConfigBuilder(ReverseEngineering engineering) { - this.engineering = engineering; - } - - public FiltersConfig build() { - compact(); - - return new FiltersConfig(transformCatalogs(engineering.getCatalogs())); - } - - private CatalogFilter[] transformCatalogs(Collection<Catalog> catalogs) { - CatalogFilter[] catalogFilters = new CatalogFilter[catalogs.size()]; - int i = 0; - for (Catalog catalog : catalogs) { - catalogFilters[i] = new CatalogFilter(catalog.getName(), transformSchemas(catalog.getSchemas())); - i++; - } - - return catalogFilters; - } - - private SchemaFilter[] transformSchemas(Collection<Schema> schemas) { - SchemaFilter[] schemaFilters = new SchemaFilter[schemas.size()]; - int i = 0; - for (Schema schema : schemas) { - schemaFilters[i] = new SchemaFilter(schema.getName(), - new TableFilter(transformIncludeTable(schema.getIncludeTables()), - transformExcludeTable(schema.getExcludeTables())), - transform(schema.getIncludeProcedures(), schema.getExcludeProcedures())); - i++; - } - - return schemaFilters; - } - - private SortedSet<Pattern> transformExcludeTable(Collection<ExcludeTable> excludeTables) { - SortedSet<Pattern> res = new TreeSet<Pattern>(PatternFilter.PATTERN_COMPARATOR); - for (ExcludeTable exclude : excludeTables) { - res.add(PatternFilter.pattern(exclude.getPattern())); - } - return res; - } - - private SortedSet<IncludeTableFilter> transformIncludeTable(Collection<IncludeTable> includeTables) { - SortedSet<IncludeTableFilter> includeTableFilters = new TreeSet<IncludeTableFilter>(); - for (IncludeTable includeTable : includeTables) { - includeTableFilters.add(new IncludeTableFilter(includeTable.getPattern(), - transform(includeTable.getIncludeColumns(), includeTable.getExcludeColumns()))); - } - - return includeTableFilters; - } - - private PatternFilter transform(Collection<? extends PatternParam> include, - Collection<? extends PatternParam> exclude) { - PatternFilter filter = new PatternFilter(); - - for (PatternParam patternParam : include) { - filter.include(patternParam.getPattern()); - } - - for (PatternParam patternParam : exclude) { - filter.exclude(patternParam.getPattern()); - } - - return filter; - - } - - /** - * Goal of this method transform ReverseEngineering config into more regular form - * From - * <pre> - * ReverseEngineering - * Catalog - * Schema - * IncludeTable - * IncludeColumn - * ExcludeColumn - * ExcludeTable - * IncludeProcedures - * ExcludeProcedures - * IncludeColumn - * ExcludeColumn - * IncludeTable - * IncludeColumn - * ExcludeColumn - * ExcludeTable - * IncludeProcedures - * ExcludeProcedures - * IncludeColumn - * ExcludeColumn - * Schema - * IncludeTable - * IncludeColumn - * ExcludeColumn - * ExcludeTable - * IncludeProcedures - * ExcludeProcedures - * IncludeColumn - * ExcludeColumn - * IncludeTable - * IncludeColumn - * ExcludeColumn - * ExcludeTable - * IncludeProcedures - * ExcludeProcedures - * IncludeColumn - * ExcludeColumn - * </pre> - * Into - * <pre> - * ReverseEngineering - * Catalog - * Schema - * IncludeTable - * IncludeColumn - * ExcludeColumn - * ExcludeTable - * IncludeProcedures - * ExcludeProcedures - * </pre> - */ - void compact() { - addEmptyElements(); - - compactColumnFilters(); - compactTableFilter(); - compactProcedureFilter(); - compactSchemas(); - } - - private void compactSchemas() { - for (Catalog catalog : engineering.getCatalogs()) { - catalog.getSchemas().addAll(engineering.getSchemas()); - } - engineering.setSchemas(null); - } - - private void compactProcedureFilter() { - Collection<IncludeProcedure> engIncludeProcedures = engineering.getIncludeProcedures(); - Collection<ExcludeProcedure> engExcludeProcedures = engineering.getExcludeProcedures(); - - engineering.setIncludeProcedures(null); - engineering.setExcludeProcedures(null); - - for (Catalog catalog : engineering.getCatalogs()) { - Collection<IncludeProcedure> catalogIncludeProcedures = catalog.getIncludeProcedures(); - Collection<ExcludeProcedure> catalogExcludeProcedures = catalog.getExcludeProcedures(); - - catalog.setIncludeProcedures(null); - catalog.setExcludeProcedures(null); - - for (Schema schema : catalog.getSchemas()) { - if (engIncludeProcedures != null) { - schema.getIncludeProcedures().addAll(engIncludeProcedures); - schema.getIncludeProcedures().addAll(catalogIncludeProcedures); - } - if (engExcludeProcedures != null) { - schema.getExcludeProcedures().addAll(engExcludeProcedures); - schema.getExcludeProcedures().addAll(catalogExcludeProcedures); - } - } - } - - for (Schema schema : engineering.getSchemas()) { - schema.getIncludeProcedures().addAll(engIncludeProcedures); - schema.getExcludeProcedures().addAll(engExcludeProcedures); - } - } - - private void compactTableFilter() { - Collection<IncludeTable> engIncludeTables = engineering.getIncludeTables(); - Collection<ExcludeTable> engExcludeTables = engineering.getExcludeTables(); - - engineering.setIncludeTables(null); - engineering.setExcludeTables(null); - - for (Catalog catalog : engineering.getCatalogs()) { - Collection<IncludeTable> catalogIncludeTables = catalog.getIncludeTables(); - Collection<ExcludeTable> catalogExcludeTables = catalog.getExcludeTables(); - - catalog.setIncludeTables(null); - catalog.setExcludeTables(null); - - for (Schema schema : catalog.getSchemas()) { - if (engIncludeTables != null) { - schema.getIncludeTables().addAll(engIncludeTables); - schema.getIncludeTables().addAll(catalogIncludeTables); - } - if (engExcludeTables != null) { - schema.getExcludeTables().addAll(engExcludeTables); - schema.getExcludeTables().addAll(catalogExcludeTables); - } - } - } - - for (Schema schema : engineering.getSchemas()) { - schema.getIncludeTables().addAll(engIncludeTables); - schema.getExcludeTables().addAll(engExcludeTables); - } - } - - private void compactColumnFilters() { - Collection<IncludeColumn> engIncludeColumns = engineering.getIncludeColumns(); - Collection<ExcludeColumn> engExcludeColumns = engineering.getExcludeColumns(); - - engineering.setIncludeColumns(null); - engineering.setExcludeColumns(null); - - for (Catalog catalog : engineering.getCatalogs()) { - Collection<IncludeColumn> catalogIncludeColumns = catalog.getIncludeColumns(); - Collection<ExcludeColumn> catalogExcludeColumns = catalog.getExcludeColumns(); - - catalog.setIncludeColumns(null); - catalog.setExcludeColumns(null); - - for (Schema schema : catalog.getSchemas()) { - Collection<IncludeColumn> schemaIncludeColumns = schema.getIncludeColumns(); - Collection<ExcludeColumn> schemaExcludeColumns = schema.getExcludeColumns(); - - schema.setIncludeColumns(null); - schema.setExcludeColumns(null); - - if (schema != null) { - for (IncludeTable includeTable : schema.getIncludeTables()) { - if (engIncludeColumns != null) { - includeTable.getIncludeColumns().addAll(engIncludeColumns); - includeTable.getIncludeColumns().addAll(catalogIncludeColumns); - includeTable.getIncludeColumns().addAll(schemaIncludeColumns); - } - if (engExcludeColumns != null) { - includeTable.getExcludeColumns().addAll(engExcludeColumns); - includeTable.getExcludeColumns().addAll(catalogExcludeColumns); - includeTable.getExcludeColumns().addAll(schemaExcludeColumns); - } - } - } - } - - if (catalog.getIncludeTables() != null) { - for (IncludeTable includeTable : catalog.getIncludeTables()) { - includeTable.getIncludeColumns().addAll(engIncludeColumns); - includeTable.getIncludeColumns().addAll(catalogIncludeColumns); - - includeTable.getExcludeColumns().addAll(engExcludeColumns); - includeTable.getExcludeColumns().addAll(catalogExcludeColumns); - } - } - } - - for (Schema schema : engineering.getSchemas()) { - Collection<IncludeColumn> schemaIncludeColumns = schema.getIncludeColumns(); - Collection<ExcludeColumn> schemaExcludeColumns = schema.getExcludeColumns(); - - schema.setIncludeColumns(null); - schema.setExcludeColumns(null); - - for (IncludeTable includeTable : schema.getIncludeTables()) { - includeTable.getIncludeColumns().addAll(engIncludeColumns); - includeTable.getIncludeColumns().addAll(schemaIncludeColumns); - - includeTable.getExcludeColumns().addAll(engExcludeColumns); - includeTable.getExcludeColumns().addAll(schemaExcludeColumns); - } - } - - if (engineering.getIncludeTables() != null) { - for (IncludeTable includeTable : engineering.getIncludeTables()) { - includeTable.getIncludeColumns().addAll(engIncludeColumns); - includeTable.getExcludeColumns().addAll(engExcludeColumns); - } - } - } - - private void addEmptyElements() { - if (engineering.getCatalogs().isEmpty()) { - engineering.addCatalog(new Catalog()); - } - - for (Catalog catalog : engineering.getCatalogs()) { - if (catalog.getSchemas().isEmpty() - && engineering.getSchemas().isEmpty()) { - catalog.addSchema(new Schema()); - } - - for (Schema schema : catalog.getSchemas()) { - if (schema.getIncludeTables().isEmpty() - && catalog.getIncludeTables().isEmpty() - && engineering.getIncludeTables().isEmpty()) { - - schema.addIncludeTable(new IncludeTable()); - } - } - } - - if (engineering.getSchemas() == null) { - engineering.setSchemas(new LinkedList<Schema>()); - } - - for (Schema schema : engineering.getSchemas()) { - if (schema.getIncludeTables().isEmpty() - && engineering.getIncludeTables().isEmpty()) { - - schema.addIncludeTable(new IncludeTable()); - } - } - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/0ab79480/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/NameFilter.java ---------------------------------------------------------------------- diff --git a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/NameFilter.java b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/NameFilter.java deleted file mode 100644 index 89b8330..0000000 --- a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/NameFilter.java +++ /dev/null @@ -1,27 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - ****************************************************************/ -package org.apache.cayenne.dbsync.reverse; - -/** - * @since 4.0. - */ -public interface NameFilter { - - boolean isIncluded(String string); -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/0ab79480/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/NamePatternMatcher.java ---------------------------------------------------------------------- diff --git a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/NamePatternMatcher.java b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/NamePatternMatcher.java deleted file mode 100644 index cb9faad..0000000 --- a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/NamePatternMatcher.java +++ /dev/null @@ -1,225 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - ****************************************************************/ - -package org.apache.cayenne.dbsync.reverse; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.regex.Pattern; -import java.util.regex.PatternSyntaxException; - -import org.apache.cayenne.util.CayenneMapEntry; -import org.apache.commons.logging.Log; - -/** - * Provides name pattern matching functionality. - * - * @since 1.2 - */ -public class NamePatternMatcher implements NameFilter { - - private static final String[] EMPTY_ARRAY = new String[0]; - private static final Pattern COMMA = Pattern.compile(","); - - private final Pattern[] itemIncludeFilters; - private final Pattern[] itemExcludeFilters; - - public static NamePatternMatcher build(Log logger, String includePattern, String excludePattern) { - return new NamePatternMatcher(createPatterns(logger, includePattern), createPatterns(logger, excludePattern)); - } - - public NamePatternMatcher(Pattern[] itemIncludeFilters, Pattern[] itemExcludeFilters) { - this.itemIncludeFilters = itemIncludeFilters; - this.itemExcludeFilters = itemExcludeFilters; - } - - /** - * Applies preconfigured list of filters to the list, removing entities that do not - * pass the filter. - * - * @deprecated since 3.0 still used by AntDataPortDelegate, which itself should - * probably be deprecated - */ - @Deprecated - public List<?> filter(List<?> items) { - if (items == null || items.isEmpty()) { - return items; - } - - if (itemIncludeFilters.length == 0 && itemExcludeFilters.length == 0) { - return items; - } - - Iterator<?> it = items.iterator(); - while (it.hasNext()) { - CayenneMapEntry entity = (CayenneMapEntry) it.next(); - - if (!passedIncludeFilter(entity.getName())) { - it.remove(); - continue; - } - - if (!passedExcludeFilter(entity.getName())) { - it.remove(); - } - } - - return items; - } - - /** - * Returns an array of Patterns. Takes a comma-separated list of patterns, attempting - * to convert them to the java.util.regex.Pattern syntax. E.g. - * <p> - * <code>"billing_*,user?"</code> will become an array of two expressions: - * <p> - * <code>^billing_.*$</code><br> - * <code>^user.?$</code><br> - */ - public static Pattern[] createPatterns(Log logger, String patternString) { - if (patternString == null) { - return new Pattern[0]; - } - String[] patternStrings = tokenizePattern(patternString); - List<Pattern> patterns = new ArrayList<Pattern>(patternStrings.length); - - for (String patternString1 : patternStrings) { - - // test the pattern - try { - patterns.add(Pattern.compile(patternString1)); - } catch (PatternSyntaxException e) { - - if (logger != null) { - logger.warn("Ignoring invalid pattern [" + patternString1 + "], reason: " + e.getMessage()); - } - } - } - - return patterns.toArray(new Pattern[patterns.size()]); - } - - /** - * Returns an array of valid regular expressions. Takes a comma-separated list of - * patterns, attempting to convert them to the java.util.regex.Pattern syntax. E.g. - * <p> - * <code>"billing_*,user?"</code> will become an array of two expressions: - * <p> - * <code>^billing_.*$</code><br> - * <code>^user.?$</code><br> - */ - public static String[] tokenizePattern(String pattern) { - if (pattern == null || pattern.isEmpty()) { - return EMPTY_ARRAY; - } - - String[] patterns = COMMA.split(pattern); - if (patterns.length == 0) { - return EMPTY_ARRAY; - } - - for (int i = 0; i < patterns.length; i++) { - // convert * into regex syntax - // e.g. abc*x becomes ^abc.*x$ - // or abc?x becomes ^abc.?x$ - patterns[i] = "^" + patterns[i].replaceAll("[*?]", ".$0") + "$"; - } - - return patterns; - } - - /** - * Returns true if a given object property satisfies the include/exclude patterns. - * - * @since 3.0 - */ - @Override - public boolean isIncluded(String string) { - return passedIncludeFilter(string) && passedExcludeFilter(string); - } - - /** - * Returns true if an object matches any one of the "include" patterns, or if there is - * no "include" patterns defined. - * - * @since 3.0 - */ - private boolean passedIncludeFilter(String item) { - if (itemIncludeFilters.length == 0) { - return true; - } - - for (Pattern itemIncludeFilter : itemIncludeFilters) { - if (itemIncludeFilter.matcher(item).find()) { - return true; - } - } - - return false; - } - - /** - * Returns true if an object does not match any one of the "exclude" patterns, or if - * there is no "exclude" patterns defined. - * - * @since 3.0 - */ - private boolean passedExcludeFilter(String item) { - if (itemExcludeFilters.length == 0) { - return true; - } - - for (Pattern itemExcludeFilter : itemExcludeFilters) { - if (itemExcludeFilter.matcher(item).find()) { - return false; - } - } - - return true; - } - - public static String replaceWildcardInStringWithString( - String wildcard, - String pattern, - String replacement) { - - if (pattern == null || wildcard == null) { - return pattern; - } - - StringBuilder buffer = new StringBuilder(); - int lastPos = 0; - int wildCardPos = pattern.indexOf(wildcard); - while (wildCardPos != -1) { - if (lastPos != wildCardPos) { - buffer.append(pattern.substring(lastPos, wildCardPos)); - } - buffer.append(replacement); - lastPos += wildCardPos + wildcard.length(); - wildCardPos = pattern.indexOf(wildcard, lastPos); - } - - if (lastPos < pattern.length()) { - buffer.append(pattern.substring(lastPos)); - } - - return buffer.toString(); - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/0ab79480/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/db/DbAttributesPerSchemaLoader.java ---------------------------------------------------------------------- diff --git a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/db/DbAttributesPerSchemaLoader.java b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/db/DbAttributesPerSchemaLoader.java index 77896c8..1ac0b07 100644 --- a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/db/DbAttributesPerSchemaLoader.java +++ b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/db/DbAttributesPerSchemaLoader.java @@ -82,7 +82,7 @@ public class DbAttributesPerSchemaLoader extends DbAttributesBaseLoader { * loading since already done pattern matching once and exactly * know all tables that we want to process */ - if (columnFilter == null || !columnFilter.isInclude(columnName)) { + if (columnFilter == null || !columnFilter.isIncluded(columnName)) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Skip column '" + tableName + "." + columnName + "' (Path: " + getCatalog() + "/" + getSchema() + "; Filter: " + columnFilter + ")"); http://git-wip-us.apache.org/repos/asf/cayenne/blob/0ab79480/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/db/DbLoader.java ---------------------------------------------------------------------- diff --git a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/db/DbLoader.java b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/db/DbLoader.java index 2821adb..7f9b780 100644 --- a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/db/DbLoader.java +++ b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/db/DbLoader.java @@ -541,7 +541,7 @@ public class DbLoader { procedure.setCatalog(rs.getString("PROCEDURE_CAT")); procedure.setSchema(rs.getString("PROCEDURE_SCHEM")); - if (!filters.proceduresFilter(procedure.getCatalog(), procedure.getSchema()).isInclude( + if (!filters.proceduresFilter(procedure.getCatalog(), procedure.getSchema()).isIncluded( procedure.getName())) { LOGGER.info("skipping Cayenne PK procedure: " + name); continue; http://git-wip-us.apache.org/repos/asf/cayenne/blob/0ab79480/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/filters/FiltersConfigBuilder.java ---------------------------------------------------------------------- diff --git a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/filters/FiltersConfigBuilder.java b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/filters/FiltersConfigBuilder.java new file mode 100644 index 0000000..ae6b1ee --- /dev/null +++ b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/filters/FiltersConfigBuilder.java @@ -0,0 +1,353 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.cayenne.dbsync.reverse.filters; + +import org.apache.cayenne.dbimport.Catalog; +import org.apache.cayenne.dbimport.ExcludeColumn; +import org.apache.cayenne.dbimport.ExcludeProcedure; +import org.apache.cayenne.dbimport.ExcludeTable; +import org.apache.cayenne.dbimport.IncludeColumn; +import org.apache.cayenne.dbimport.IncludeProcedure; +import org.apache.cayenne.dbimport.IncludeTable; +import org.apache.cayenne.dbimport.PatternParam; +import org.apache.cayenne.dbimport.ReverseEngineering; +import org.apache.cayenne.dbimport.Schema; + +import java.util.Collection; +import java.util.LinkedList; +import java.util.SortedSet; +import java.util.TreeSet; +import java.util.regex.Pattern; + +/** + * @since 4.0 + */ +public final class FiltersConfigBuilder { + + private final ReverseEngineering engineering; + + public FiltersConfigBuilder(ReverseEngineering engineering) { + this.engineering = engineering; + } + + public FiltersConfig build() { + compact(); + + return new FiltersConfig(transformCatalogs(engineering.getCatalogs())); + } + + private CatalogFilter[] transformCatalogs(Collection<Catalog> catalogs) { + CatalogFilter[] catalogFilters = new CatalogFilter[catalogs.size()]; + int i = 0; + for (Catalog catalog : catalogs) { + catalogFilters[i] = new CatalogFilter(catalog.getName(), transformSchemas(catalog.getSchemas())); + i++; + } + + return catalogFilters; + } + + private SchemaFilter[] transformSchemas(Collection<Schema> schemas) { + SchemaFilter[] schemaFilters = new SchemaFilter[schemas.size()]; + int i = 0; + for (Schema schema : schemas) { + schemaFilters[i] = new SchemaFilter(schema.getName(), + new TableFilter(transformIncludeTable(schema.getIncludeTables()), + transformExcludeTable(schema.getExcludeTables())), + transform(schema.getIncludeProcedures(), schema.getExcludeProcedures())); + i++; + } + + return schemaFilters; + } + + private SortedSet<Pattern> transformExcludeTable(Collection<ExcludeTable> excludeTables) { + SortedSet<Pattern> res = new TreeSet<Pattern>(PatternFilter.PATTERN_COMPARATOR); + for (ExcludeTable exclude : excludeTables) { + res.add(PatternFilter.pattern(exclude.getPattern())); + } + return res; + } + + private SortedSet<IncludeTableFilter> transformIncludeTable(Collection<IncludeTable> includeTables) { + SortedSet<IncludeTableFilter> includeTableFilters = new TreeSet<IncludeTableFilter>(); + for (IncludeTable includeTable : includeTables) { + includeTableFilters.add(new IncludeTableFilter(includeTable.getPattern(), + transform(includeTable.getIncludeColumns(), includeTable.getExcludeColumns()))); + } + + return includeTableFilters; + } + + private PatternFilter transform(Collection<? extends PatternParam> include, + Collection<? extends PatternParam> exclude) { + PatternFilter filter = new PatternFilter(); + + for (PatternParam patternParam : include) { + filter.include(patternParam.getPattern()); + } + + for (PatternParam patternParam : exclude) { + filter.exclude(patternParam.getPattern()); + } + + return filter; + + } + + /** + * Goal of this method transform ReverseEngineering config into more regular form + * From + * <pre> + * ReverseEngineering + * Catalog + * Schema + * IncludeTable + * IncludeColumn + * ExcludeColumn + * ExcludeTable + * IncludeProcedures + * ExcludeProcedures + * IncludeColumn + * ExcludeColumn + * IncludeTable + * IncludeColumn + * ExcludeColumn + * ExcludeTable + * IncludeProcedures + * ExcludeProcedures + * IncludeColumn + * ExcludeColumn + * Schema + * IncludeTable + * IncludeColumn + * ExcludeColumn + * ExcludeTable + * IncludeProcedures + * ExcludeProcedures + * IncludeColumn + * ExcludeColumn + * IncludeTable + * IncludeColumn + * ExcludeColumn + * ExcludeTable + * IncludeProcedures + * ExcludeProcedures + * IncludeColumn + * ExcludeColumn + * </pre> + * Into + * <pre> + * ReverseEngineering + * Catalog + * Schema + * IncludeTable + * IncludeColumn + * ExcludeColumn + * ExcludeTable + * IncludeProcedures + * ExcludeProcedures + * </pre> + */ + void compact() { + addEmptyElements(); + + compactColumnFilters(); + compactTableFilter(); + compactProcedureFilter(); + compactSchemas(); + } + + private void compactSchemas() { + for (Catalog catalog : engineering.getCatalogs()) { + catalog.getSchemas().addAll(engineering.getSchemas()); + } + engineering.setSchemas(null); + } + + private void compactProcedureFilter() { + Collection<IncludeProcedure> engIncludeProcedures = engineering.getIncludeProcedures(); + Collection<ExcludeProcedure> engExcludeProcedures = engineering.getExcludeProcedures(); + + engineering.setIncludeProcedures(null); + engineering.setExcludeProcedures(null); + + for (Catalog catalog : engineering.getCatalogs()) { + Collection<IncludeProcedure> catalogIncludeProcedures = catalog.getIncludeProcedures(); + Collection<ExcludeProcedure> catalogExcludeProcedures = catalog.getExcludeProcedures(); + + catalog.setIncludeProcedures(null); + catalog.setExcludeProcedures(null); + + for (Schema schema : catalog.getSchemas()) { + if (engIncludeProcedures != null) { + schema.getIncludeProcedures().addAll(engIncludeProcedures); + schema.getIncludeProcedures().addAll(catalogIncludeProcedures); + } + if (engExcludeProcedures != null) { + schema.getExcludeProcedures().addAll(engExcludeProcedures); + schema.getExcludeProcedures().addAll(catalogExcludeProcedures); + } + } + } + + for (Schema schema : engineering.getSchemas()) { + schema.getIncludeProcedures().addAll(engIncludeProcedures); + schema.getExcludeProcedures().addAll(engExcludeProcedures); + } + } + + private void compactTableFilter() { + Collection<IncludeTable> engIncludeTables = engineering.getIncludeTables(); + Collection<ExcludeTable> engExcludeTables = engineering.getExcludeTables(); + + engineering.setIncludeTables(null); + engineering.setExcludeTables(null); + + for (Catalog catalog : engineering.getCatalogs()) { + Collection<IncludeTable> catalogIncludeTables = catalog.getIncludeTables(); + Collection<ExcludeTable> catalogExcludeTables = catalog.getExcludeTables(); + + catalog.setIncludeTables(null); + catalog.setExcludeTables(null); + + for (Schema schema : catalog.getSchemas()) { + if (engIncludeTables != null) { + schema.getIncludeTables().addAll(engIncludeTables); + schema.getIncludeTables().addAll(catalogIncludeTables); + } + if (engExcludeTables != null) { + schema.getExcludeTables().addAll(engExcludeTables); + schema.getExcludeTables().addAll(catalogExcludeTables); + } + } + } + + for (Schema schema : engineering.getSchemas()) { + schema.getIncludeTables().addAll(engIncludeTables); + schema.getExcludeTables().addAll(engExcludeTables); + } + } + + private void compactColumnFilters() { + Collection<IncludeColumn> engIncludeColumns = engineering.getIncludeColumns(); + Collection<ExcludeColumn> engExcludeColumns = engineering.getExcludeColumns(); + + engineering.setIncludeColumns(null); + engineering.setExcludeColumns(null); + + for (Catalog catalog : engineering.getCatalogs()) { + Collection<IncludeColumn> catalogIncludeColumns = catalog.getIncludeColumns(); + Collection<ExcludeColumn> catalogExcludeColumns = catalog.getExcludeColumns(); + + catalog.setIncludeColumns(null); + catalog.setExcludeColumns(null); + + for (Schema schema : catalog.getSchemas()) { + Collection<IncludeColumn> schemaIncludeColumns = schema.getIncludeColumns(); + Collection<ExcludeColumn> schemaExcludeColumns = schema.getExcludeColumns(); + + schema.setIncludeColumns(null); + schema.setExcludeColumns(null); + + if (schema != null) { + for (IncludeTable includeTable : schema.getIncludeTables()) { + if (engIncludeColumns != null) { + includeTable.getIncludeColumns().addAll(engIncludeColumns); + includeTable.getIncludeColumns().addAll(catalogIncludeColumns); + includeTable.getIncludeColumns().addAll(schemaIncludeColumns); + } + if (engExcludeColumns != null) { + includeTable.getExcludeColumns().addAll(engExcludeColumns); + includeTable.getExcludeColumns().addAll(catalogExcludeColumns); + includeTable.getExcludeColumns().addAll(schemaExcludeColumns); + } + } + } + } + + if (catalog.getIncludeTables() != null) { + for (IncludeTable includeTable : catalog.getIncludeTables()) { + includeTable.getIncludeColumns().addAll(engIncludeColumns); + includeTable.getIncludeColumns().addAll(catalogIncludeColumns); + + includeTable.getExcludeColumns().addAll(engExcludeColumns); + includeTable.getExcludeColumns().addAll(catalogExcludeColumns); + } + } + } + + for (Schema schema : engineering.getSchemas()) { + Collection<IncludeColumn> schemaIncludeColumns = schema.getIncludeColumns(); + Collection<ExcludeColumn> schemaExcludeColumns = schema.getExcludeColumns(); + + schema.setIncludeColumns(null); + schema.setExcludeColumns(null); + + for (IncludeTable includeTable : schema.getIncludeTables()) { + includeTable.getIncludeColumns().addAll(engIncludeColumns); + includeTable.getIncludeColumns().addAll(schemaIncludeColumns); + + includeTable.getExcludeColumns().addAll(engExcludeColumns); + includeTable.getExcludeColumns().addAll(schemaExcludeColumns); + } + } + + if (engineering.getIncludeTables() != null) { + for (IncludeTable includeTable : engineering.getIncludeTables()) { + includeTable.getIncludeColumns().addAll(engIncludeColumns); + includeTable.getExcludeColumns().addAll(engExcludeColumns); + } + } + } + + private void addEmptyElements() { + if (engineering.getCatalogs().isEmpty()) { + engineering.addCatalog(new Catalog()); + } + + for (Catalog catalog : engineering.getCatalogs()) { + if (catalog.getSchemas().isEmpty() + && engineering.getSchemas().isEmpty()) { + catalog.addSchema(new Schema()); + } + + for (Schema schema : catalog.getSchemas()) { + if (schema.getIncludeTables().isEmpty() + && catalog.getIncludeTables().isEmpty() + && engineering.getIncludeTables().isEmpty()) { + + schema.addIncludeTable(new IncludeTable()); + } + } + } + + if (engineering.getSchemas() == null) { + engineering.setSchemas(new LinkedList<Schema>()); + } + + for (Schema schema : engineering.getSchemas()) { + if (schema.getIncludeTables().isEmpty() + && engineering.getIncludeTables().isEmpty()) { + + schema.addIncludeTable(new IncludeTable()); + } + } + } +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/0ab79480/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/filters/IncludeTableFilter.java ---------------------------------------------------------------------- diff --git a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/filters/IncludeTableFilter.java b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/filters/IncludeTableFilter.java index 7272b2d..f431b3b 100644 --- a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/filters/IncludeTableFilter.java +++ b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/filters/IncludeTableFilter.java @@ -38,7 +38,7 @@ public class IncludeTableFilter implements Comparable<IncludeTableFilter> { } public boolean isIncludeColumn (String name) { - return columnsFilter.isInclude(name); + return columnsFilter.isIncluded(name); } @Override http://git-wip-us.apache.org/repos/asf/cayenne/blob/0ab79480/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/filters/PatternFilter.java ---------------------------------------------------------------------- diff --git a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/filters/PatternFilter.java b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/filters/PatternFilter.java index ee1d9e0..feb90de 100644 --- a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/filters/PatternFilter.java +++ b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/filters/PatternFilter.java @@ -33,7 +33,7 @@ public class PatternFilter { public static final PatternFilter INCLUDE_EVERYTHING = new PatternFilter() { @Override - public boolean isInclude(String obj) { + public boolean isIncluded(String obj) { return true; } @@ -45,7 +45,7 @@ public class PatternFilter { public static final PatternFilter INCLUDE_NOTHING = new PatternFilter() { @Override - public boolean isInclude(String obj) { + public boolean isIncluded(String obj) { return false; } @@ -102,7 +102,7 @@ public class PatternFilter { return exclude(pattern(p)); } - public boolean isInclude(String obj) { + public boolean isIncluded(String obj) { boolean include = includes.isEmpty(); for (Pattern p : includes) { if (p != null) { http://git-wip-us.apache.org/repos/asf/cayenne/blob/0ab79480/cayenne-dbsync/src/test/java/org/apache/cayenne/dbsync/filter/NamePatternMatcherTest.java ---------------------------------------------------------------------- diff --git a/cayenne-dbsync/src/test/java/org/apache/cayenne/dbsync/filter/NamePatternMatcherTest.java b/cayenne-dbsync/src/test/java/org/apache/cayenne/dbsync/filter/NamePatternMatcherTest.java new file mode 100644 index 0000000..d99ba09 --- /dev/null +++ b/cayenne-dbsync/src/test/java/org/apache/cayenne/dbsync/filter/NamePatternMatcherTest.java @@ -0,0 +1,73 @@ +/***************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + ****************************************************************/ + +package org.apache.cayenne.dbsync.filter; + +import org.junit.Test; + +import static org.apache.cayenne.dbsync.filter.NamePatternMatcher.replaceWildcardInStringWithString; +import static org.junit.Assert.assertEquals; + +public class NamePatternMatcherTest { + + /** + * Test pattern expansion. + */ + @Test + public void testReplaceWildcardInStringWithString() throws Exception { + assertEquals(null, replaceWildcardInStringWithString("*", null, "Entity")); + assertEquals("*.java", replaceWildcardInStringWithString(null, "*.java", "Entity")); + assertEquals("Entity.java", replaceWildcardInStringWithString("*", "*.java", "Entity")); + assertEquals("java.Entity", replaceWildcardInStringWithString("*", "java.*", "Entity")); + assertEquals("Entity.Entity", replaceWildcardInStringWithString("*", "*.*", "Entity")); + assertEquals("EntityEntity", replaceWildcardInStringWithString("*", "**", "Entity")); + assertEquals("EditEntityReport.vm", replaceWildcardInStringWithString("*", "Edit*Report.vm", "Entity")); + assertEquals("Entity", replaceWildcardInStringWithString("*", "*", "Entity")); + } + + /** + * Test tokenizing + */ + @Test + public void testTokenizer() { + + String[] nullFilters = NamePatternMatcher.tokenizePattern(null); + assertEquals(0, nullFilters.length); + + String[] filters = NamePatternMatcher.tokenizePattern("billing_*,user?"); + assertEquals(2, filters.length); + assertEquals("^billing_.*$", filters[0]); + assertEquals("^user.?$", filters[1]); + } + + /** + * Test tokenizing + */ + @Test + public void testTokenizerEntities() { + + String includePattern = "Organization,SecGroup,SecIndividual"; + + String[] filters = NamePatternMatcher.tokenizePattern(includePattern); + assertEquals(3, filters.length); + assertEquals("^Organization$", filters[0]); + assertEquals("^SecGroup$", filters[1]); + assertEquals("^SecIndividual$", filters[2]); + } +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/0ab79480/cayenne-dbsync/src/test/java/org/apache/cayenne/dbsync/reverse/FiltersConfigBuilderTest.java ---------------------------------------------------------------------- diff --git a/cayenne-dbsync/src/test/java/org/apache/cayenne/dbsync/reverse/FiltersConfigBuilderTest.java b/cayenne-dbsync/src/test/java/org/apache/cayenne/dbsync/reverse/FiltersConfigBuilderTest.java deleted file mode 100644 index 5b1a0e7..0000000 --- a/cayenne-dbsync/src/test/java/org/apache/cayenne/dbsync/reverse/FiltersConfigBuilderTest.java +++ /dev/null @@ -1,391 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.cayenne.dbsync.reverse; - -import org.apache.cayenne.dbimport.Catalog; -import org.apache.cayenne.dbimport.ExcludeColumn; -import org.apache.cayenne.dbimport.ExcludeProcedure; -import org.apache.cayenne.dbimport.ExcludeTable; -import org.apache.cayenne.dbimport.IncludeColumn; -import org.apache.cayenne.dbimport.IncludeProcedure; -import org.apache.cayenne.dbimport.IncludeTable; -import org.apache.cayenne.dbimport.ReverseEngineering; -import org.apache.cayenne.dbimport.Schema; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class FiltersConfigBuilderTest { - - @Test - public void testCompact_01() { - ReverseEngineering engineering = new ReverseEngineering(); - engineering.addIncludeTable(new IncludeTable("table1")); - engineering.addIncludeTable(new IncludeTable("table2")); - engineering.addIncludeTable(new IncludeTable("table3")); - - engineering.addIncludeColumn(new IncludeColumn("includeColumn")); - - FiltersConfigBuilder builder = new FiltersConfigBuilder(engineering); - builder.compact(); - assertEquals( - "ReverseEngineering: \n" + - " Catalog: null\n" + - " Schema: null\n" + - " IncludeTable: table1\n" + - " IncludeColumn: includeColumn\n" + - " IncludeTable: table2\n" + - " IncludeColumn: includeColumn\n" + - " IncludeTable: table3\n" + - " IncludeColumn: includeColumn\n", engineering.toString()); - } - - @Test - public void testCompact_02() { - ReverseEngineering engineering = new ReverseEngineering(); - engineering.addCatalog(new Catalog("catalogName")); - engineering.addSchema(new Schema("schemaName01")); - engineering.addSchema(new Schema("schemaName02")); - - engineering.addIncludeTable(new IncludeTable("table1")); - engineering.addExcludeTable(new ExcludeTable("table2")); - - engineering.addIncludeColumn(new IncludeColumn("includeColumn")); - - FiltersConfigBuilder builder = new FiltersConfigBuilder(engineering); - builder.compact(); - assertEquals( - "ReverseEngineering: \n" + - " Catalog: catalogName\n" + - " Schema: schemaName01\n" + - " IncludeTable: table1\n" + - " IncludeColumn: includeColumn\n" + - " ExcludeTable: table2\n" + - " Schema: schemaName02\n" + - " IncludeTable: table1\n" + - " IncludeColumn: includeColumn\n" + - " ExcludeTable: table2\n", engineering.toString()); - } - - @Test - public void testCompact_03() { - ReverseEngineering engineering = new ReverseEngineering(); - engineering.addCatalog(new Catalog("APP1")); - engineering.addCatalog(new Catalog("APP2")); - - engineering.addExcludeTable(new ExcludeTable("SYS_.*")); - engineering.addExcludeColumn(new ExcludeColumn("calculated_.*")); - - FiltersConfigBuilder builder = new FiltersConfigBuilder(engineering); - builder.compact(); - assertEquals( - "ReverseEngineering: \n" + - " Catalog: APP1\n" + - " Schema: null\n" + - " IncludeTable: null\n" + - " ExcludeColumn: calculated_.*\n" + - " ExcludeTable: SYS_.*\n" + - " Catalog: APP2\n" + - " Schema: null\n" + - " IncludeTable: null\n" + - " ExcludeColumn: calculated_.*\n" + - " ExcludeTable: SYS_.*\n", engineering.toString()); - } - - @Test - public void testCompact_04() { - ReverseEngineering engineering = new ReverseEngineering(); - engineering.addSchema(new Schema("s")); - - FiltersConfigBuilder builder = new FiltersConfigBuilder(engineering); - builder.compact(); - assertEquals( - "ReverseEngineering: \n" + - " Catalog: null\n" + - " Schema: s\n" + - " IncludeTable: null\n", engineering.toString()); - } - - @Test - public void testCompact_full() { - ReverseEngineering engineering = new ReverseEngineering(); - Catalog cat01 = new Catalog("cat_01"); - - Schema sch01 = new Schema("sch_01"); - - sch01.addIncludeTable(includeTable("t1", "c11", "c12")); - sch01.addExcludeTable(new ExcludeTable("t2")); - sch01.addIncludeProcedure(new IncludeProcedure("p1")); - sch01.addExcludeProcedure(new ExcludeProcedure("p2")); - sch01.addIncludeColumn(new IncludeColumn("c_x1")); - sch01.addExcludeColumn(new ExcludeColumn("c_x2")); - - cat01.addSchema(sch01); - - cat01.addIncludeTable(includeTable("t3", "c31", "c32")); - cat01.addExcludeTable(new ExcludeTable("t4")); - cat01.addIncludeProcedure(new IncludeProcedure("p3")); - cat01.addExcludeProcedure(new ExcludeProcedure("p4")); - cat01.addIncludeColumn(new IncludeColumn("c_xx1")); - cat01.addExcludeColumn(new ExcludeColumn("c_xx2")); - - engineering.addCatalog(cat01); - - Schema sch02 = new Schema("sch_02"); - - sch02.addIncludeTable(includeTable("t5", "c51", "c52")); - sch02.addExcludeTable(new ExcludeTable("t6")); - sch02.addIncludeProcedure(new IncludeProcedure("p5")); - sch02.addExcludeProcedure(new ExcludeProcedure("p6")); - sch02.addIncludeColumn(new IncludeColumn("c2_x1")); - sch02.addExcludeColumn(new ExcludeColumn("c2_x2")); - - engineering.addSchema(sch02); - - engineering.addIncludeTable(includeTable("t7", "c71", "c72")); - engineering.addExcludeTable(new ExcludeTable("t8")); - engineering.addIncludeProcedure(new IncludeProcedure("p7")); - engineering.addExcludeProcedure(new ExcludeProcedure("p8")); - engineering.addIncludeColumn(new IncludeColumn("c_xxx1")); - engineering.addExcludeColumn(new ExcludeColumn("c_xxx2")); - - FiltersConfigBuilder builder = new FiltersConfigBuilder(engineering); - assertEquals("Original ReverseEngineering should be", - "ReverseEngineering: \n" + - " Catalog: cat_01\n" + - " Schema: sch_01\n" + - " IncludeTable: t1\n" + - " IncludeColumn: c11\n" + - " ExcludeColumn: c12\n" + - " ExcludeTable: t2\n" + - " IncludeColumn: c_x1\n" + - " ExcludeColumn: c_x2\n" + - " IncludeProcedure: p1\n" + - " ExcludeProcedure: p2\n" + - " IncludeTable: t3\n" + - " IncludeColumn: c31\n" + - " ExcludeColumn: c32\n" + - " ExcludeTable: t4\n" + - " IncludeColumn: c_xx1\n" + - " ExcludeColumn: c_xx2\n" + - " IncludeProcedure: p3\n" + - " ExcludeProcedure: p4\n" + - " Schema: sch_02\n" + - " IncludeTable: t5\n" + - " IncludeColumn: c51\n" + - " ExcludeColumn: c52\n" + - " ExcludeTable: t6\n" + - " IncludeColumn: c2_x1\n" + - " ExcludeColumn: c2_x2\n" + - " IncludeProcedure: p5\n" + - " ExcludeProcedure: p6\n" + - " IncludeTable: t7\n" + - " IncludeColumn: c71\n" + - " ExcludeColumn: c72\n" + - " ExcludeTable: t8\n" + - " IncludeColumn: c_xxx1\n" + - " ExcludeColumn: c_xxx2\n" + - " IncludeProcedure: p7\n" + - " ExcludeProcedure: p8\n", engineering.toString()); - - - builder.compact(); - assertEquals( - "ReverseEngineering: \n" + - " Catalog: cat_01\n" + - " Schema: sch_01\n" + - " IncludeTable: t1\n" + - " IncludeColumn: c11\n" + - " IncludeColumn: c_xxx1\n" + - " IncludeColumn: c_xx1\n" + - " IncludeColumn: c_x1\n" + - " ExcludeColumn: c12\n" + - " ExcludeColumn: c_xxx2\n" + - " ExcludeColumn: c_xx2\n" + - " ExcludeColumn: c_x2\n" + - " IncludeTable: t7\n" + - " IncludeColumn: c71\n" + - " IncludeColumn: c_xxx1\n" + - " ExcludeColumn: c72\n" + - " ExcludeColumn: c_xxx2\n" + - " IncludeTable: t3\n" + - " IncludeColumn: c31\n" + - " IncludeColumn: c_xxx1\n" + - " IncludeColumn: c_xx1\n" + - " ExcludeColumn: c32\n" + - " ExcludeColumn: c_xxx2\n" + - " ExcludeColumn: c_xx2\n" + - " ExcludeTable: t2\n" + - " ExcludeTable: t8\n" + - " ExcludeTable: t4\n" + - " IncludeProcedure: p1\n" + - " IncludeProcedure: p7\n" + - " IncludeProcedure: p3\n" + - " ExcludeProcedure: p2\n" + - " ExcludeProcedure: p8\n" + - " ExcludeProcedure: p4\n" + - " Schema: sch_02\n" + - " IncludeTable: t5\n" + - " IncludeColumn: c51\n" + - " IncludeColumn: c_xxx1\n" + - " IncludeColumn: c2_x1\n" + - " ExcludeColumn: c52\n" + - " ExcludeColumn: c_xxx2\n" + - " ExcludeColumn: c2_x2\n" + - " IncludeTable: t7\n" + - " IncludeColumn: c71\n" + - " IncludeColumn: c_xxx1\n" + - " ExcludeColumn: c72\n" + - " ExcludeColumn: c_xxx2\n" + - " ExcludeTable: t6\n" + - " ExcludeTable: t8\n" + - " IncludeProcedure: p5\n" + - " IncludeProcedure: p7\n" + - " ExcludeProcedure: p6\n" + - " ExcludeProcedure: p8\n", engineering.toString()); - } - - protected IncludeTable includeTable(String name, String incCol, String excCol) { - IncludeTable incTable01 = new IncludeTable(name); - incTable01.addIncludeColumn(new IncludeColumn(incCol)); - incTable01.addExcludeColumn(new ExcludeColumn(excCol)); - return incTable01; - } - - /*@Test - public void testEmptyDbEntitiesFilters() throws Exception { - ReverseEngineering engineering = new ReverseEngineering(); - FiltersConfig executions = new FiltersConfigBuilder(engineering).build(); - - assertEquals("If nothing was configured we have to import everything. Filter %/%/% true/true/true", - new FiltersConfig(eFilters(path(), TRUE, TRUE, NULL)), - executions); - } - - @Test - public void testOnlyOneCatalogDbEntitiesFilters() throws Exception { - ReverseEngineering engineering = new ReverseEngineering(); - engineering.addCatalog(new Catalog("catalog_01")); - FiltersConfig executions = new FiltersConfigBuilder(engineering).build(); - - - assertEquals(new FiltersConfig(eFilters(path("catalog_01", null), TRUE, TRUE, NULL)), - executions); - } - - @Test - public void testCatalogDbEntitiesFilters() throws Exception { - ReverseEngineering engineering = new ReverseEngineering(); - engineering.addCatalog(new Catalog("catalog_01")); - engineering.addCatalog(new Catalog("catalog_02").schema(new Schema("schema_01"))); - engineering.addCatalog(new Catalog("catalog_02").schema(new Schema("schema_02"))); - engineering.addCatalog(new Catalog("catalog_02").schema(new Schema("schema_03"))); - engineering.addCatalog(new Catalog("catalog_03").schema(new Schema("schema_01"))); - engineering.addCatalog(new Catalog("catalog_03").schema(new Schema("schema_01"))); - engineering.addCatalog(new Catalog("catalog_03").schema(new Schema("schema_01"))); - engineering.addCatalog(new Catalog("catalog_03").schema(new Schema("schema_01"))); - FiltersConfig executions = new FiltersConfigBuilder(engineering).build(); - - - assertEquals(new FiltersConfig( - eFilters(path("catalog_01", null), TRUE, TRUE, NULL), - eFilters(path("catalog_02", "schema_01"), TRUE, TRUE, NULL), - eFilters(path("catalog_02", "schema_02"), TRUE, TRUE, NULL), - eFilters(path("catalog_02", "schema_03"), TRUE, TRUE, NULL), - eFilters(path("catalog_03", "schema_01"), TRUE, TRUE, NULL) - ), - executions); - } - - @Test - public void testSchemaDbEntitiesFilters() throws Exception { - ReverseEngineering engineering = new ReverseEngineering(); - engineering.addSchema(new Schema("schema_01")); - engineering.addSchema(new Schema("schema_02")); - engineering.addSchema(new Schema("schema_03")); - FiltersConfig executions = new FiltersConfigBuilder(engineering).build(); - - - assertEquals(new FiltersConfig( - eFilters(path(null, "schema_01"), TRUE, TRUE, NULL), - eFilters(path(null, "schema_02"), TRUE, TRUE, NULL), - eFilters(path(null, "schema_03"), TRUE, TRUE, NULL) - ), - executions); - } - - @Test - public void testFiltersDbEntitiesFilters() throws Exception { - ReverseEngineering engineering = new ReverseEngineering(); - engineering.addIncludeTable(new IncludeTable("IncludeTable")); - engineering.addIncludeColumn(new IncludeColumn("IncludeColumn")); - engineering.addIncludeProcedure(new IncludeProcedure("IncludeProcedure")); - engineering.addExcludeTable(new ExcludeTable("ExcludeTable")); - engineering.addExcludeColumn(new ExcludeColumn("ExcludeColumn")); - engineering.addExcludeProcedure(new ExcludeProcedure("ExcludeProcedure")); - - FiltersConfig executions = new FiltersConfigBuilder(engineering).build(); - - assertEquals(new FiltersConfig( - eFilters(path(), - list(include("IncludeTable"), exclude("ExcludeTable")), - list(include("IncludeColumn"), exclude("ExcludeColumn")), - list(include("IncludeProcedure"), exclude("ExcludeProcedure"))), - eFilters(path(null, null, "IncludeTable"), NULL, TRUE, NULL) - ), - executions); - } - - @Test - public void testComplexConfiguration() throws Exception { - IncludeTable table = new IncludeTable("table"); - table.addIncludeColumn(new IncludeColumn("column")); - - Schema schema = new Schema("schema"); - schema.addIncludeTable(table); - - Catalog catalog = new Catalog("catalog"); - catalog.addSchema(schema); - - ReverseEngineering engineering = new ReverseEngineering(); - engineering.addCatalog(catalog); - - FiltersConfig executions = new FiltersConfigBuilder(engineering).build(); - - assertEquals(new FiltersConfig( - eFilters(path("catalog", "schema"), include("table"), NULL, NULL), - eFilters(path("catalog", "schema", "table"), NULL, include("column"), NULL) - ), - executions); - } - - @Test - public void testAddNull() throws Exception { - FiltersConfigBuilder builder = new FiltersConfigBuilder(new ReverseEngineering()); - DbPath path = new DbPath(); - builder.add(new EntityFilters(path, NULL, NULL, NULL)); - builder.add(new EntityFilters(path, NULL, NULL, NULL)); - builder.add(new EntityFilters(path, NULL, NULL, NULL)); - builder.add(new EntityFilters(path, NULL, NULL, NULL)); - - EntityFilters filter = builder.build().filter(path); - assertFalse(filter.isEmpty()); - }*/ -} \ No newline at end of file