TAMAYA-188: Added explicit converters for File and Path.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/eafcadea Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/eafcadea Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/eafcadea Branch: refs/heads/master Commit: eafcadea6e3ff925d916966462f4f7a37738940d Parents: 77dbcc4 Author: anatole <[email protected]> Authored: Sat Oct 29 14:41:15 2016 +0200 Committer: anatole <[email protected]> Committed: Sat Oct 29 14:41:15 2016 +0200 ---------------------------------------------------------------------- .../core/internal/WrappedPropertySource.java | 130 +++++++++++++++++++ .../core/internal/converters/FileConverter.java | 51 ++++++++ .../core/internal/converters/PathConverter.java | 54 ++++++++ .../org.apache.tamaya.spi.PropertyConverter | 2 + .../internal/converters/URIConverterTest.java | 69 ++++++++++ .../internal/converters/URLConverterTest.java | 69 ++++++++++ 6 files changed, 375 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/eafcadea/code/core/src/main/java/org/apache/tamaya/core/internal/WrappedPropertySource.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/internal/WrappedPropertySource.java b/code/core/src/main/java/org/apache/tamaya/core/internal/WrappedPropertySource.java new file mode 100644 index 0000000..a6f4742 --- /dev/null +++ b/code/core/src/main/java/org/apache/tamaya/core/internal/WrappedPropertySource.java @@ -0,0 +1,130 @@ +/* + * 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.tamaya.core.internal; + +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spi.PropertyValue; + +import java.util.Map; +import java.util.Objects; + +/** + * Property source effectively managed by the configuration context, allowing resetting of ordinal and its + * delegate (e.g. in case of refresh). + */ +class WrappedPropertySource implements PropertySource{ + + private Integer ordinal; + private PropertySource delegate; + private long loaded = System.currentTimeMillis(); + + private WrappedPropertySource(PropertySource delegate) { + this(delegate, null); + } + + private WrappedPropertySource(PropertySource delegate, Integer ordinal) { + this.delegate = Objects.requireNonNull(delegate); + this.ordinal = ordinal; + } + + public static WrappedPropertySource of(PropertySource ps) { + if(ps instanceof WrappedPropertySource){ + return (WrappedPropertySource)ps; + } + return new WrappedPropertySource(ps); + } + + public static WrappedPropertySource of(PropertySource ps, Integer ordinal) { + if(ps instanceof WrappedPropertySource){ + return new WrappedPropertySource(((WrappedPropertySource)ps).getDelegate(), ordinal); + } + return new WrappedPropertySource(ps, ordinal); + } + + @Override + public int getOrdinal() { + if(this.ordinal!=null){ + return this.ordinal; + } + return delegate.getOrdinal(); + } + + public void setOrdinal(Integer ordinal) { + this.ordinal = ordinal; + } + + public void setDelegate(PropertySource delegate) { + this.delegate = Objects.requireNonNull(delegate); + this.loaded = System.currentTimeMillis(); + } + + @Override + public String getName() { + return delegate.getName(); + } + + @Override + public PropertyValue get(String key) { + return delegate.get(key); + } + + @Override + public Map<String, String> getProperties() { + return delegate.getProperties(); + } + + @Override + public boolean isScannable() { + return delegate.isScannable(); + } + + public PropertySource getDelegate() { + return delegate; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof WrappedPropertySource)) return false; + + WrappedPropertySource that = (WrappedPropertySource) o; + + return getDelegate().getName().equals(that.getDelegate().getName()); + + } + + @Override + public int hashCode() { + return getDelegate().getName().hashCode(); + } + + @Override + public String toString() { + return "WrappedPropertySource{" + + "name=" + getName() + + ", ordinal=" + getOrdinal() + + ", scannable=" + isScannable() + + ", loadedAt=" + loaded + + ", delegate-class=" + delegate.getClass().getName() + + ", delegate-ordinal=" + delegate.getOrdinal() + + '}'; + } + + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/eafcadea/code/core/src/main/java/org/apache/tamaya/core/internal/converters/FileConverter.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/internal/converters/FileConverter.java b/code/core/src/main/java/org/apache/tamaya/core/internal/converters/FileConverter.java new file mode 100644 index 0000000..6e4109d --- /dev/null +++ b/code/core/src/main/java/org/apache/tamaya/core/internal/converters/FileConverter.java @@ -0,0 +1,51 @@ +/* + * 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.tamaya.core.internal.converters; + +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; + +import java.io.File; +import java.net.URL; +import java.util.Objects; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Converter, converting from String to URI, using new URL(value). + */ +public class FileConverter implements PropertyConverter<File> { + + private final Logger LOG = Logger.getLogger(getClass().getName()); + + @Override + public File convert(String value, ConversionContext context) { + if(value==null || value.isEmpty()){ + return null; + } + context.addSupportedFormats(getClass(),"<File>"); + String trimmed = Objects.requireNonNull(value).trim(); + try { + return new File(trimmed); + } catch (Exception e) { + LOG.log(Level.FINE, "Unparseable File Name: " + trimmed, e); + } + return null; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/eafcadea/code/core/src/main/java/org/apache/tamaya/core/internal/converters/PathConverter.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/internal/converters/PathConverter.java b/code/core/src/main/java/org/apache/tamaya/core/internal/converters/PathConverter.java new file mode 100644 index 0000000..6c0b287 --- /dev/null +++ b/code/core/src/main/java/org/apache/tamaya/core/internal/converters/PathConverter.java @@ -0,0 +1,54 @@ +/* + * 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.tamaya.core.internal.converters; + +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; + +import java.io.File; +import java.nio.file.FileSystem; +import java.nio.file.FileSystems; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Objects; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Converter, converting from String to URI, using new URL(value). + */ +public class PathConverter implements PropertyConverter<Path> { + + private final Logger LOG = Logger.getLogger(getClass().getName()); + + @Override + public Path convert(String value, ConversionContext context) { + if(value==null || value.isEmpty()){ + return null; + } + context.addSupportedFormats(getClass(),"<File>"); + String trimmed = Objects.requireNonNull(value).trim(); + try { + return FileSystems.getDefault().getPath(value); + } catch (Exception e) { + LOG.log(Level.FINE, "Unparseable Path: " + trimmed, e); + } + return null; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/eafcadea/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter ---------------------------------------------------------------------- diff --git a/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter b/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter index 93f7b11..878e8a7 100644 --- a/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter +++ b/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter @@ -31,3 +31,5 @@ org.apache.tamaya.core.internal.converters.CurrencyConverter org.apache.tamaya.core.internal.converters.NumberConverter org.apache.tamaya.core.internal.converters.URIConverter org.apache.tamaya.core.internal.converters.URLConverter +org.apache.tamaya.core.internal.converters.FileConverter +org.apache.tamaya.core.internal.converters.PathConverter http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/eafcadea/code/core/src/test/java/org/apache/tamaya/core/internal/converters/URIConverterTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/URIConverterTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/URIConverterTest.java new file mode 100644 index 0000000..0ac4ba7 --- /dev/null +++ b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/URIConverterTest.java @@ -0,0 +1,69 @@ +/* + * 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.tamaya.core.internal.converters; + +import org.apache.tamaya.TypeLiteral; +import org.apache.tamaya.spi.ConversionContext; +import org.junit.Test; + +import java.math.BigDecimal; +import java.net.URI; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +/** + * Tests conversion of the {@link ClassConverter}. + */ +public class URIConverterTest { + + ConversionContext context = new ConversionContext.Builder(TypeLiteral.of(URI.class)) + .build(); + + @Test + public void testConvert_URI() throws Exception { + URIConverter converter = new URIConverter(); + assertEquals(new URI("test:path"), converter.convert("test:path", context)); + } + + @Test + public void testConvert_URI_WithSpaces() throws Exception { + URIConverter converter = new URIConverter(); + assertEquals(new URI("test:path"), converter.convert(" test:path\t", context)); + } + + @Test + public void testConvert_URI_WithSpacesBefore() throws Exception { + URIConverter converter = new URIConverter(); + assertEquals(new URI("test:path"), converter.convert(" test:path", context)); + } + + @Test + public void testConvert_URI_WithSpacesAfter() throws Exception { + URIConverter converter = new URIConverter(); + assertEquals(new URI("test:path"), converter.convert("test:path ", context)); + } + + @Test + public void testConvert_NotPresent() throws Exception { + URIConverter converter = new URIConverter(); + assertNull(converter.convert("", context)); + assertNull(converter.convert(null, context)); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/eafcadea/code/core/src/test/java/org/apache/tamaya/core/internal/converters/URLConverterTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/URLConverterTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/URLConverterTest.java new file mode 100644 index 0000000..e740331 --- /dev/null +++ b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/URLConverterTest.java @@ -0,0 +1,69 @@ +/* + * 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.tamaya.core.internal.converters; + +import org.apache.tamaya.TypeLiteral; +import org.apache.tamaya.spi.ConversionContext; +import org.junit.Test; + +import java.net.URI; +import java.net.URL; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +/** + * Tests conversion of the {@link ClassConverter}. + */ +public class URLConverterTest { + + ConversionContext context = new ConversionContext.Builder(TypeLiteral.of(URI.class)) + .build(); + + @Test + public void testConvert_URL() throws Exception { + URLConverter converter = new URLConverter(); + assertEquals(new URL("http://google.com:4000/path"), converter.convert("http://google.com:4000/path", context)); + } + + @Test + public void testConvert_URL_WithSpaces() throws Exception { + URLConverter converter = new URLConverter(); + assertEquals(new URL("http://google.com:4000/path"), converter.convert(" http://google.com:4000/path\t", context)); + } + + @Test + public void testConvert_URL_WithSpacesBefore() throws Exception { + URLConverter converter = new URLConverter(); + assertEquals(new URL("http://google.com:4000/path"), converter.convert(" http://google.com:4000/path", context)); + } + + @Test + public void testConvert_URL_WithSpacesAfter() throws Exception { + URLConverter converter = new URLConverter(); + assertEquals(new URL("http://google.com:4000/path"), converter.convert("http://google.com:4000/path ", context)); + } + + @Test + public void testConvert_NotPresent() throws Exception { + URLConverter converter = new URLConverter(); + assertNull(converter.convert("", context)); + assertNull(converter.convert(null, context)); + } +}
