@Test public void testDefaultGroupingFilter() { final ArrangementGroupingRule groupingRule = new ArrangementGroupingRule(OVERRIDDEN_METHODS, BY_NAME); final ArrangementAtomMatchCondition condition = new ArrangementAtomMatchCondition(FIELD); final StdArrangementMatchRule rule = new StdArrangementMatchRule(new StdArrangementEntryMatcher(condition), BY_NAME); final StdArrangementSettings settings = new StdArrangementSettings(); settings.addGrouping(groupingRule); settings.addRule(rule); final StdArrangementSettings defaultSettings = new StdArrangementSettings(); defaultSettings.addGrouping(groupingRule); final Element holder = doSerializationTest(settings, defaultSettings); assertTrue(holder.getChildren().size() == 1); assertNull(holder.getChild("groups")); assertNotNull(holder.getChild("rules")); }
@Test public void testDefaultRulesFilter() { final ArrangementGroupingRule groupingRule = new ArrangementGroupingRule(OVERRIDDEN_METHODS, BY_NAME); final ArrangementAtomMatchCondition condition = new ArrangementAtomMatchCondition(FIELD); final StdArrangementMatchRule rule = new StdArrangementMatchRule(new StdArrangementEntryMatcher(condition), BY_NAME); final StdArrangementSettings settings = new StdArrangementSettings(); settings.addGrouping(groupingRule); settings.addRule(rule); final StdArrangementSettings defaultSettings = new StdArrangementSettings(); defaultSettings.addRule(rule); final Element holder = doSerializationTest(settings, defaultSettings); assertTrue(holder.getChildren().size() == 1); assertNotNull(holder.getChild("groups")); assertNull(holder.getChild("rules")); }
@Test public void testEmptyRules() throws Exception { final StdArrangementSettings settings = new StdArrangementSettings(); settings.addGrouping(new ArrangementGroupingRule(OVERRIDDEN_METHODS, BY_NAME)); final StdArrangementSettings defaultSettings = new StdArrangementSettings(); final ArrangementAtomMatchCondition condition = new ArrangementAtomMatchCondition(FIELD); final StdArrangementMatchRule rule = new StdArrangementMatchRule(new StdArrangementEntryMatcher(condition), BY_NAME); defaultSettings.addRule(rule); final Element holder = doSerializationTest(settings, defaultSettings); assertTrue(holder.getChildren().size() == 2); final Element rules = holder.getChild("rules"); assertNotNull(rules); assertTrue(rules.getChildren().isEmpty()); }
@Test public void atomCondition() { ArrangementAtomMatchCondition condition = new ArrangementAtomMatchCondition(FIELD); StdArrangementEntryMatcher matcher = new StdArrangementEntryMatcher(condition); assertEquals(condition, matcher.getCondition()); final TypeAwareArrangementEntry fieldEntry = myMockery.mock(TypeAwareArrangementEntry.class, "field"); final TypeAwareArrangementEntry classEntry = myMockery.mock(TypeAwareArrangementEntry.class, "class"); final ModifierAwareArrangementEntry publicEntry = myMockery.mock(ModifierAwareArrangementEntry.class, "public"); myMockery.checking(new Expectations() {{ allowing(fieldEntry).getTypes(); will(returnValue(ContainerUtilRt.newHashSet(FIELD))); allowing(classEntry).getTypes(); will(returnValue(ContainerUtilRt.newHashSet(CLASS))); allowing(publicEntry).getModifiers(); will(returnValue(ContainerUtilRt.newHashSet(PUBLIC))); }}); assertTrue(matcher.isMatched(fieldEntry)); assertFalse(matcher.isMatched(classEntry)); assertFalse(matcher.isMatched(publicEntry)); }
@Override public int compare(ArrangementMatchCondition c1, ArrangementMatchCondition c2) { boolean isAtom1 = c1 instanceof ArrangementAtomMatchCondition; boolean isAtom2 = c2 instanceof ArrangementAtomMatchCondition; if (isAtom1 ^ isAtom2) { return isAtom1 ? 1 : -1; // Composite conditions before atom conditions. } else if (!isAtom1) { return 0; } ArrangementAtomMatchCondition atom1 = (ArrangementAtomMatchCondition)c1; ArrangementAtomMatchCondition atom2 = (ArrangementAtomMatchCondition)c2; int cmp = atom1.getType().compareTo(atom2.getType()); if (cmp == 0) { cmp = atom1.getValue().toString().compareTo(atom2.getValue().toString()); } return cmp; }
@Nullable public static ArrangementEntryMatcher buildMatcher(@NotNull ArrangementAtomMatchCondition condition) { if (StdArrangementTokenType.ENTRY_TYPE.is(condition.getType())) { return new ByTypeArrangementEntryMatcher(condition); } else if (StdArrangementTokenType.MODIFIER.is(condition.getType())) { return new ByModifierArrangementEntryMatcher(condition); } else if (StdArrangementTokens.Regexp.NAME.equals(condition.getType())) { return new ByNameArrangementEntryMatcher(condition.getValue().toString()); } else if (StdArrangementTokens.Regexp.XML_NAMESPACE.equals(condition.getType())) { return new ByNamespaceArrangementEntryMatcher(condition.getValue().toString()); } else { return null; } }
@Nullable public static ArrangementEntryMatcher buildMatcher(@NotNull ArrangementAtomMatchCondition condition) { if (StdArrangementTokens.EntryType.is(condition.getType())) { return new ByTypeArrangementEntryMatcher(condition.getType()); } else if (StdArrangementTokens.Modifier.is(condition.getType())) { return new ByModifierArrangementEntryMatcher(condition.getType()); } else if (StdArrangementTokens.Regexp.NAME.equals(condition.getType())) { return new ByNameArrangementEntryMatcher(condition.getValue().toString()); } else if (StdArrangementTokens.Regexp.XML_NAMESPACE.equals(condition.getType())) { return new ByNamespaceArrangementEntryMatcher(condition.getValue().toString()); } else { return null; } }
@Nullable public static ArrangementEntryMatcher buildMatcher(@Nonnull ArrangementAtomMatchCondition condition) { if (StdArrangementTokenType.ENTRY_TYPE.is(condition.getType())) { return new ByTypeArrangementEntryMatcher(condition); } else if (StdArrangementTokenType.MODIFIER.is(condition.getType())) { return new ByModifierArrangementEntryMatcher(condition); } else if (StdArrangementTokens.Regexp.NAME.equals(condition.getType())) { return new ByNameArrangementEntryMatcher(condition.getValue().toString()); } else if (StdArrangementTokens.Regexp.XML_NAMESPACE.equals(condition.getType())) { return new ByNamespaceArrangementEntryMatcher(condition.getValue().toString()); } else { return null; } }
private static void and(@NotNull List<StdArrangementMatchRule> matchRules, @NotNull ArrangementSettingsToken... conditions) { if(conditions.length == 1) { matchRules.add(new StdArrangementMatchRule(new StdArrangementEntryMatcher(new ArrangementAtomMatchCondition(conditions[0], conditions[0])))); return; } ArrangementCompositeMatchCondition composite = new ArrangementCompositeMatchCondition(); for(ArrangementSettingsToken condition : conditions) { composite.addOperand(new ArrangementAtomMatchCondition(condition, condition)); } matchRules.add(new StdArrangementMatchRule(new StdArrangementEntryMatcher(composite))); }
private static void and(@NotNull List<StdArrangementMatchRule> matchRules, @NotNull ArrangementSettingsToken... conditions) { if (conditions.length == 1) { matchRules.add(new StdArrangementMatchRule(new StdArrangementEntryMatcher(new ArrangementAtomMatchCondition( conditions[0] )))); return; } ArrangementCompositeMatchCondition composite = new ArrangementCompositeMatchCondition(); for (ArrangementSettingsToken condition : conditions) { composite.addOperand(new ArrangementAtomMatchCondition(condition)); } matchRules.add(new StdArrangementMatchRule(new StdArrangementEntryMatcher(composite))); }
@NotNull protected static StdArrangementMatchRule nameRule(@NotNull String nameFilter, @NotNull ArrangementSettingsToken... tokens) { if (tokens.length == 0) { return new StdArrangementMatchRule(new StdArrangementEntryMatcher(atom(nameFilter))); } else { ArrangementAtomMatchCondition[] conditions = new ArrangementAtomMatchCondition[tokens.length + 1]; conditions[0] = atom(nameFilter); for (int i = 0; i < tokens.length; i++) conditions[i + 1] = atom(tokens[i]); ArrangementMatchCondition compositeCondition = ArrangementUtil.combine(conditions); return new StdArrangementMatchRule(new StdArrangementEntryMatcher(compositeCondition)); } }
@NotNull protected static StdArrangementMatchRule rule(@NotNull ArrangementSettingsToken... conditions) { return rule(ContainerUtil.map(conditions, new Function<ArrangementSettingsToken, ArrangementAtomMatchCondition>() { @Override public ArrangementAtomMatchCondition fun(ArrangementSettingsToken it) { return atom(it); } })); }
private static StdArrangementMatchRule rule(boolean byName, @NotNull ArrangementSettingsToken... tokens) { final List<ArrangementAtomMatchCondition> conditions = new ArrayList<ArrangementAtomMatchCondition>(); for (ArrangementSettingsToken token : tokens) { conditions.add(new ArrangementAtomMatchCondition(token)); } final StdArrangementEntryMatcher matcher = new StdArrangementEntryMatcher(new ArrangementCompositeMatchCondition(conditions)); return byName ? new StdArrangementMatchRule(matcher, BY_NAME) : new StdArrangementMatchRule(matcher); }
@Test public void all() { final StdArrangementSettings settings = new StdArrangementSettings(); settings.addGrouping(new ArrangementGroupingRule(OVERRIDDEN_METHODS, BY_NAME)); final ArrangementAtomMatchCondition condition = new ArrangementAtomMatchCondition(FIELD); settings.addRule(new StdArrangementMatchRule(new StdArrangementEntryMatcher(condition), BY_NAME)); doSerializationTest(settings, new StdArrangementSettings()); }
@Test public void testDefaultFilter() { final StdArrangementSettings settings = new StdArrangementSettings(); settings.addGrouping(new ArrangementGroupingRule(OVERRIDDEN_METHODS, BY_NAME)); final ArrangementAtomMatchCondition condition = new ArrangementAtomMatchCondition(FIELD); settings.addRule(new StdArrangementMatchRule(new StdArrangementEntryMatcher(condition), BY_NAME)); final Element holder = doSerializationTest(settings, settings); assertTrue(holder.getChildren().isEmpty()); }
@Test public void testEmptyGroupings() throws Exception { final StdArrangementSettings settings = new StdArrangementSettings(); final ArrangementAtomMatchCondition condition = new ArrangementAtomMatchCondition(FIELD); settings.addRule(new StdArrangementMatchRule(new StdArrangementEntryMatcher(condition), BY_NAME)); final StdArrangementSettings defaultSettings = new StdArrangementSettings(); defaultSettings.addGrouping(new ArrangementGroupingRule(OVERRIDDEN_METHODS, BY_NAME)); final Element holder = doSerializationTest(settings, defaultSettings); assertTrue(holder.getChildren().size() == 2); final Element groups = holder.getChild("groups"); assertNotNull(groups); assertTrue(groups.getChildren().isEmpty()); }
@Test public void compositeConditionWithName() { ArrangementCompositeMatchCondition condition = new ArrangementCompositeMatchCondition(); condition.addOperand(new ArrangementAtomMatchCondition(METHOD)); condition.addOperand(new ArrangementAtomMatchCondition(SYNCHRONIZED)); condition.addOperand(new ArrangementAtomMatchCondition(StdArrangementTokens.Regexp.NAME, ("get*"))); doTest(condition); }
@Test public void compositeAndCondition() { ArrangementCompositeMatchCondition condition = new ArrangementCompositeMatchCondition(); condition.addOperand(new ArrangementAtomMatchCondition(FIELD)); condition.addOperand(new ArrangementAtomMatchCondition(PUBLIC)); StdArrangementEntryMatcher matcher = new StdArrangementEntryMatcher(condition); assertEquals(condition, matcher.getCondition()); final TypeAwareArrangementEntry fieldEntry = myMockery.mock(TypeAwareArrangementEntry.class, "field"); final ModifierAwareArrangementEntry publicEntry = myMockery.mock(ModifierAwareArrangementEntry.class, "public"); final TypeAndModifierAware privateFieldEntry = myMockery.mock(TypeAndModifierAware.class, "private field"); final TypeAndModifierAware publicMethodEntry = myMockery.mock(TypeAndModifierAware.class, "public method"); final TypeAndModifierAware publicFieldEntry = myMockery.mock(TypeAndModifierAware.class, "public field"); final TypeAndModifierAware publicStaticFieldEntry = myMockery.mock(TypeAndModifierAware.class, "public static field"); myMockery.checking(new Expectations() {{ allowing(fieldEntry).getTypes(); will(returnValue(ContainerUtilRt.newHashSet(FIELD))); allowing(publicEntry).getModifiers(); will(returnValue(ContainerUtilRt.newHashSet(PUBLIC))); allowing(privateFieldEntry).getTypes(); will(returnValue(ContainerUtilRt.newHashSet(FIELD))); allowing(privateFieldEntry).getModifiers(); will(returnValue(ContainerUtilRt.newHashSet(PRIVATE))); allowing(publicMethodEntry).getTypes(); will(returnValue(ContainerUtilRt.newHashSet(METHOD))); allowing(publicMethodEntry).getModifiers(); will(returnValue(ContainerUtilRt.newHashSet(PUBLIC))); allowing(publicFieldEntry).getTypes(); will(returnValue(ContainerUtilRt.newHashSet(FIELD))); allowing(publicFieldEntry).getModifiers(); will(returnValue(ContainerUtilRt.newHashSet(PUBLIC))); allowing(publicStaticFieldEntry).getTypes(); will(returnValue(ContainerUtilRt.newHashSet(FIELD))); allowing(publicStaticFieldEntry).getModifiers(); will(returnValue(ContainerUtilRt.newHashSet(PUBLIC, STATIC))); }}); assertFalse(matcher.isMatched(fieldEntry)); assertFalse(matcher.isMatched(publicEntry)); assertFalse(matcher.isMatched(privateFieldEntry)); assertFalse(matcher.isMatched(publicMethodEntry)); assertTrue(matcher.isMatched(publicFieldEntry)); assertTrue(matcher.isMatched(publicStaticFieldEntry)); }
@Override public boolean isMatched(@NotNull ArrangementEntry entry) { if (entry instanceof ModifierAwareArrangementEntry) { final Set<ArrangementSettingsToken> modifiers = ((ModifierAwareArrangementEntry)entry).getModifiers(); for (ArrangementAtomMatchCondition condition : myModifiers) { final Object value = condition.getValue(); boolean isInverted = value instanceof Boolean && !((Boolean)value); if (isInverted == modifiers.contains(condition.getType())) { return false; } } return true; } return false; }
@Override public void visit(@NotNull ArrangementAtomMatchCondition condition) { ArrangementSettingsToken type = condition.getType(); final Element element = new Element(type.getId()); if (StdArrangementTokenType.REG_EXP.is(type)) { element.setText(StringUtil.escapeStringCharacters(condition.getValue().toString())); } else if (condition.getValue() instanceof Boolean) { element.setText(condition.getValue().toString()); } register(element); }
@Override public boolean isMatched(@NotNull ArrangementEntry entry) { if (entry instanceof TypeAwareArrangementEntry) { final Set<ArrangementSettingsToken> types = ((TypeAwareArrangementEntry)entry).getTypes(); for (ArrangementAtomMatchCondition condition : myTypes) { final Object value = condition.getValue(); boolean isInverted = value instanceof Boolean && !((Boolean)value); if (isInverted == types.contains(condition.getType())) { return false; } } return true; } return false; }
@NotNull public StdArrangementMatchRule createDefaultSectionRule() { final ArrangementAtomMatchCondition type = new ArrangementAtomMatchCondition(START_SECTION); final ArrangementAtomMatchCondition text = new ArrangementAtomMatchCondition(TEXT, createDefaultSectionText()); final ArrangementMatchCondition condition = ArrangementUtil.combine(type, text); return new StdArrangementMatchRule(new StdArrangementEntryMatcher(condition)); }
@Nullable @Override public ArrangementUiComponent build(@NotNull StdArrangementTokenUiRole role, @NotNull List<ArrangementSettingsToken> tokens, @NotNull ArrangementColorsProvider colorsProvider, @NotNull ArrangementStandardSettingsManager settingsManager) { switch (role) { case CHECKBOX: if (tokens.size() != 1) { throw new IllegalArgumentException("Can't build a checkbox token for elements " + tokens); } else { return new ArrangementCheckBoxUiComponent(tokens.get(0)); } case COMBO_BOX: if (tokens.isEmpty()) { throw new IllegalArgumentException("Can't build a combo box token with empty content"); } return new ArrangementComboBoxUiComponent(tokens); case LABEL: if (tokens.size() != 1) { throw new IllegalArgumentException("Can't build a label token for elements " + tokens); } return new ArrangementLabelUiComponent(tokens.get(0)); case TEXT_FIELD: if (tokens.size() != 1) { throw new IllegalArgumentException("Can't build a text field token for elements " + tokens); } return new ArrangementTextFieldUiComponent(tokens.get(0)); case BULB: if (tokens.size() != 1) { throw new IllegalArgumentException("Can't build a bulb token for elements " + tokens); } return new ArrangementAtomMatchConditionComponent( settingsManager, colorsProvider, new ArrangementAtomMatchCondition(tokens.get(0)), null ); } return null; }
@NotNull public static StdArrangementMatchRule attrArrangementRule(@NotNull String nameFilter, @NotNull String namespaceFilter, @NotNull ArrangementSettingsToken orderType) { return new StdArrangementMatchRule(new StdArrangementEntryMatcher(ArrangementUtil.combine( new ArrangementAtomMatchCondition(StdArrangementTokens.Regexp.NAME, nameFilter), new ArrangementAtomMatchCondition(StdArrangementTokens.Regexp.XML_NAMESPACE, namespaceFilter) )), orderType); }
private static void and(@NotNull List<StdArrangementMatchRule> matchRules, @NotNull ArrangementSettingsToken... conditions) { if (conditions.length == 1) { matchRules.add(new StdArrangementMatchRule(new StdArrangementEntryMatcher(new ArrangementAtomMatchCondition( conditions[0], conditions[0] )))); return; } ArrangementCompositeMatchCondition composite = new ArrangementCompositeMatchCondition(); for (ArrangementSettingsToken condition : conditions) { composite.addOperand(new ArrangementAtomMatchCondition(condition, condition)); } matchRules.add(new StdArrangementMatchRule(new StdArrangementEntryMatcher(composite))); }
@Test public void all() { StdArrangementSettings settings = new StdRulePriorityAwareSettings(); settings.addGrouping(new ArrangementGroupingRule(OVERRIDDEN_METHODS, BY_NAME)); ArrangementAtomMatchCondition condition = new ArrangementAtomMatchCondition(FIELD); settings.addRule(new StdArrangementMatchRule(new StdArrangementEntryMatcher(condition), BY_NAME)); Element holder = new Element("holder"); ArrangementSettingsSerializer instance = DefaultArrangementSettingsSerializer.INSTANCE; instance.serialize(settings, holder); ArrangementSettings restored = instance.deserialize(holder); assertEquals(settings, restored); }
@Nullable private ArrangementMatchCondition deserializeAtomCondition(@NotNull Element matcherElement) { String id = matcherElement.getName(); ArrangementSettingsToken token = StdArrangementTokens.byId(id); boolean processInnerText = true; if (token != null && (StdArrangementTokens.General.TYPE.equals(token) || StdArrangementTokens.General.MODIFIER.equals(token))) { // Backward compatibility with old arrangement settings format. id = matcherElement.getText(); if (StringUtil.isEmpty(id)) { LOG.warn("Can't deserialize match condition at legacy format"); return null; } token = StdArrangementTokens.byId(id); processInnerText = false; } if (token == null) { token = myMixin.deserializeToken(id); } if (token == null) { LOG.warn(String.format("Can't deserialize match condition with id '%s'", id)); return null; } Object value = token; String text = matcherElement.getText(); if (text != null && processInnerText) { text = StringUtil.unescapeStringCharacters(matcherElement.getText()); if (!StringUtil.isEmpty(text)) { value = text; } } return new ArrangementAtomMatchCondition(token, value); }
@Override public void visit(@NotNull ArrangementAtomMatchCondition condition) { ArrangementSettingsToken type = condition.getType(); final Element element = new Element(type.getId()); if (StdArrangementTokens.Regexp.is(type)) { element.setText(StringUtil.escapeStringCharacters(condition.getValue().toString())); } register(element); }
@Override public boolean isMatched(@Nonnull ArrangementEntry entry) { if (entry instanceof ModifierAwareArrangementEntry) { final Set<ArrangementSettingsToken> modifiers = ((ModifierAwareArrangementEntry)entry).getModifiers(); for (ArrangementAtomMatchCondition condition : myModifiers) { final Object value = condition.getValue(); boolean isInverted = value instanceof Boolean && !((Boolean)value); if (isInverted == modifiers.contains(condition.getType())) { return false; } } return true; } return false; }
@Nullable private static StdArrangementMatchRule createSectionRule(@Nullable String comment, @Nonnull ArrangementSettingsToken token) { if (StringUtil.isEmpty(comment)) { return null; } final ArrangementAtomMatchCondition type = new ArrangementAtomMatchCondition(token); final ArrangementAtomMatchCondition text = new ArrangementAtomMatchCondition(StdArrangementTokens.Regexp.TEXT, comment); final ArrangementMatchCondition condition = ArrangementUtil.combine(type, text); return new StdArrangementMatchRule(new StdArrangementEntryMatcher(condition)); }
@Nullable private ArrangementMatchCondition deserializeAtomCondition(@Nonnull Element matcherElement) { String id = matcherElement.getName(); ArrangementSettingsToken token = StdArrangementTokens.byId(id); boolean processInnerText = true; if (token != null && (StdArrangementTokens.General.TYPE.equals(token) || StdArrangementTokens.General.MODIFIER.equals(token))) { // Backward compatibility with old arrangement settings format. id = matcherElement.getText(); if (StringUtil.isEmpty(id)) { LOG.warn("Can't deserialize match condition at legacy format"); return null; } token = StdArrangementTokens.byId(id); processInnerText = false; } if (token == null) { token = myMixin.deserializeToken(id); } if (token == null) { LOG.warn(String.format("Can't deserialize match condition with id '%s'", id)); return null; } Object value = token; String text = matcherElement.getText(); if (text != null && processInnerText) { text = StringUtil.unescapeStringCharacters(matcherElement.getText()); if (!StringUtil.isEmpty(text)) { value = text; } } return new ArrangementAtomMatchCondition(token, value); }
@Override public void visit(@Nonnull ArrangementAtomMatchCondition condition) { ArrangementSettingsToken type = condition.getType(); final Element element = new Element(type.getId()); if (StdArrangementTokenType.REG_EXP.is(type)) { element.setText(StringUtil.escapeStringCharacters(condition.getValue().toString())); } register(element); }