@Override public boolean matches(ItemHint hint) { if (this.index + 1 > hint.getProviders().size()) { return false; } ItemHint.ValueProvider valueProvider = hint.getProviders().get(this.index); if (this.name != null && !this.name.equals(valueProvider.getName())) { return false; } if (this.parameters != null) { for (Map.Entry<String, Object> entry : this.parameters.entrySet()) { if (!IsMapContaining.hasEntry(entry.getKey(), entry.getValue()) .matches(valueProvider.getParameters())) { return false; } } } return true; }
@Test public void mergingOfHintWithProvider() throws Exception { writeAdditionalHints(new ItemHint("simple.theName", Collections.<ItemHint.ValueHint>emptyList(), Arrays.asList( new ItemHint.ValueProvider("first", Collections.<String, Object>singletonMap("target", "org.foo")), new ItemHint.ValueProvider("second", null)))); ConfigurationMetadata metadata = compile(SimpleProperties.class); assertThat(metadata).has(Metadata.withProperty("simple.the-name", String.class) .fromSource(SimpleProperties.class) .withDescription("The name of this simple properties.") .withDefaultValue("boot").withDeprecation(null, null)); assertThat(metadata).has(Metadata.withHint("simple.the-name") .withProvider("first", "target", "org.foo").withProvider("second")); }
@Test public void mergingOfHintWithProvider() throws Exception { writeAdditionalHints(new ItemHint("simple.theName", Collections.<ItemHint.ValueHint>emptyList(), Arrays.asList( new ItemHint.ValueProvider("first", Collections.<String, Object>singletonMap("target", "org.foo")), new ItemHint.ValueProvider("second", null)))); ConfigurationMetadata metadata = compile(SimpleProperties.class); assertThat(metadata, containsProperty("simple.the-name", String.class) .fromSource(SimpleProperties.class) .withDescription("The name of this simple properties.") .withDefaultValue(is("boot")).withDeprecation(null, null)); assertThat(metadata, containsHint("simple.the-name") .withProvider("first", "target", "org.foo").withProvider("second")); }
@Override public boolean matches(Object item) { ConfigurationMetadata metadata = (ConfigurationMetadata) item; ItemHint itemHint = getFirstHintWithName(metadata, this.name); if (itemHint == null) { return false; } for (ValueHintMatcher value : this.values) { if (!value.matches(itemHint)) { return false; } } for (ValueProviderMatcher provider : this.providers) { if (!provider.matches(itemHint)) { return false; } } return true; }
@Override public boolean matches(Object item) { ItemHint hint = (ItemHint) item; if (this.index + 1 > hint.getValues().size()) { return false; } ItemHint.ValueHint valueHint = hint.getValues().get(this.index); if (this.value != null && !this.value.equals(valueHint.getValue())) { return false; } if (this.description != null && !this.description.equals(valueHint.getDescription())) { return false; } return true; }
@Override public boolean matches(Object item) { ItemHint hint = (ItemHint) item; if (this.index + 1 > hint.getProviders().size()) { return false; } ItemHint.ValueProvider valueProvider = hint.getProviders().get(this.index); if (this.name != null && !this.name.equals(valueProvider.getName())) { return false; } if (this.parameters != null) { for (Map.Entry<String, Object> entry : this.parameters.entrySet()) { if (!IsMapContaining.hasEntry(entry.getKey(), entry.getValue()) .matches(valueProvider.getParameters())) { return false; } } } return true; }
@Override public boolean matches(ConfigurationMetadata metadata) { ItemHint itemHint = getFirstHintWithName(metadata, this.name); if (itemHint == null) { return false; } return matches(itemHint, this.valueConditions) && matches(itemHint, this.providerConditions); }
private boolean matches(ItemHint itemHint, List<? extends Condition<ItemHint>> conditions) { for (Condition<ItemHint> condition : conditions) { if (!condition.matches(itemHint)) { return false; } } return true; }
private ItemHint getFirstHintWithName(ConfigurationMetadata metadata, String name) { for (ItemHint hint : metadata.getHints()) { if (name.equals(hint.getName())) { return hint; } } return null; }
@Override public boolean matches(ItemHint value) { if (this.index + 1 > value.getValues().size()) { return false; } ItemHint.ValueHint valueHint = value.getValues().get(this.index); if (this.value != null && !this.value.equals(valueHint.getValue())) { return false; } if (this.description != null && !this.description.equals(valueHint.getDescription())) { return false; } return true; }
@Test public void mergingOfSimpleHint() throws Exception { writeAdditionalHints(ItemHint.newHint("simple.the-name", new ItemHint.ValueHint("boot", "Bla bla"), new ItemHint.ValueHint("spring", null))); ConfigurationMetadata metadata = compile(SimpleProperties.class); assertThat(metadata).has(Metadata.withProperty("simple.the-name", String.class) .fromSource(SimpleProperties.class) .withDescription("The name of this simple properties.") .withDefaultValue("boot").withDeprecation(null, null)); assertThat(metadata).has(Metadata.withHint("simple.the-name") .withValue(0, "boot", "Bla bla").withValue(1, "spring", null)); }
@Test public void mergingOfHintWithNonCanonicalName() throws Exception { writeAdditionalHints(ItemHint.newHint("simple.theName", new ItemHint.ValueHint("boot", "Bla bla"))); ConfigurationMetadata metadata = compile(SimpleProperties.class); assertThat(metadata).has(Metadata.withProperty("simple.the-name", String.class) .fromSource(SimpleProperties.class) .withDescription("The name of this simple properties.") .withDefaultValue("boot").withDeprecation(null, null)); assertThat(metadata).has( Metadata.withHint("simple.the-name").withValue(0, "boot", "Bla bla")); }
@Test public void mergingOfSimpleHint() throws Exception { writeAdditionalHints(ItemHint.newHint("simple.the-name", new ItemHint.ValueHint("boot", "Bla bla"), new ItemHint.ValueHint("spring", null))); ConfigurationMetadata metadata = compile(SimpleProperties.class); assertThat(metadata, containsProperty("simple.the-name", String.class) .fromSource(SimpleProperties.class) .withDescription("The name of this simple properties.") .withDefaultValue(is("boot")).withDeprecation(null, null)); assertThat(metadata, containsHint("simple.the-name") .withValue(0, "boot", "Bla bla").withValue(1, "spring", null)); }
@Test public void mergingOfHintWithNonCanonicalName() throws Exception { writeAdditionalHints(ItemHint.newHint("simple.theName", new ItemHint.ValueHint("boot", "Bla bla"))); ConfigurationMetadata metadata = compile(SimpleProperties.class); assertThat(metadata, containsProperty("simple.the-name", String.class) .fromSource(SimpleProperties.class) .withDescription("The name of this simple properties.") .withDefaultValue(is("boot")).withDeprecation(null, null)); assertThat(metadata, containsHint("simple.the-name").withValue(0, "boot", "Bla bla")); }
@Override public void describeMismatch(Object item, Description description) { ConfigurationMetadata metadata = (ConfigurationMetadata) item; ItemHint itemHint = getFirstHintWithName(metadata, this.name); if (itemHint == null) { description.appendText("missing hint " + this.name); } else { description.appendText("was hint ").appendValue(itemHint); } }
private void writeAdditionalHints(ItemHint... hints) throws IOException { File additionalMetadataFile = createAdditionalMetadataFile(); JSONObject additionalMetadata = new JSONObject(); additionalMetadata.put("hints", hints); writeMetadata(additionalMetadataFile, additionalMetadata); }