http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/formats/src/main/java/org/apache/tamaya/format/ConfigurationDataBuilder.java ---------------------------------------------------------------------- diff --git a/modules/formats/src/main/java/org/apache/tamaya/format/ConfigurationDataBuilder.java b/modules/formats/src/main/java/org/apache/tamaya/format/ConfigurationDataBuilder.java deleted file mode 100644 index da5fa35..0000000 --- a/modules/formats/src/main/java/org/apache/tamaya/format/ConfigurationDataBuilder.java +++ /dev/null @@ -1,219 +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.tamaya.format; - -import java.util.*; - - -/** - * Builder for creating {@link org.apache.tamaya.format.ConfigurationData} instances. This class is not thread-safe. - */ -public final class ConfigurationDataBuilder { - - /** The format instance used to read this instance. */ - final ConfigurationFormat format; - /** The resource read. */ - final String resource; - /** - * The properties of the default section (no name). - */ - Map<String, String> defaultProperties; - /** - * A normalized flattened set of this configuration data. - */ - Map<String, String> combinedProperties; - /** - * The sections read. - */ - Map<String, Map<String, String>> namedSections; - - /** - * Private constructor. - * @param resource the configuration resource URL, not null. - * @param format the format that read this data, not null. - */ - private ConfigurationDataBuilder(String resource, ConfigurationFormat format){ - this.format = Objects.requireNonNull(format); - this.resource = Objects.requireNonNull(resource); - } - - /** - * Creates a new instance. - * @param resource the configuration resource URL, not null. - * @param format the format that read this data, not null. - * @return new instance of this class. - */ - public static ConfigurationDataBuilder of(String resource, ConfigurationFormat format){ - return new ConfigurationDataBuilder(resource, format); - } - - /** - * Creates a new instance. - * @param data an existing ConfigurationData instances used to initialize the builder. - * @return new instance of this class from the given configuration. - */ - public static ConfigurationDataBuilder of(ConfigurationData data){ - ConfigurationDataBuilder b = new ConfigurationDataBuilder(data.getResource(), data.getFormat()); - if (data.hasDefaultProperties()) { - b.getDefaultProperties().putAll(data.getDefaultProperties()); - } - if (data.hasCombinedProperties()) { - b.getCombinedProperties().putAll(data.getCombinedProperties()); - } - if (!data.getSections().isEmpty()) { - b.getSections().putAll(data.getSections()); - } - return b; - } - - /** - * Adds (empty) sections,if they are not yet existing. Already existing sections will not be touched. - * @param sections the new sections to put. - * @return the builder for chaining. - */ - public ConfigurationDataBuilder addSections(String... sections){ - for (String section : sections) { - if (!getSections().containsKey(section)) { - getSections().put(section, new HashMap<String, String>()); - } - } - return this; - } - - /** - * Adds a single entry to a target section. - * @param section the target section (will be created if not existing). - * @param key the entry's key - * @param value the entry's value - * @return the builder for chaining. - */ - public ConfigurationDataBuilder addSectionProperty(String section, String key, String value) { - Map<String, String> map = getSections().get(section); - if (map == null) { - map = new HashMap<>(); - getSections().put(section, map); - } - map.put(key, value); - return this; - } - - /** - * Adds a single entry to the <i>default</i> section. - * @param key the entry's key - * @param value the entry's value - * @return the builder for chaining. - */ - public ConfigurationDataBuilder addProperty(String key, String value) { - getDefaultProperties().put(key, value); - return this; - } - - /** - * Adds the given entries to the given section, all existing values will be overridden. - * @param section the target section (will be created if not existing). - * @param properties the entry's data - * @return the builder for chaining. - */ - public ConfigurationDataBuilder addSectionProperties(String section, Map<String, String> properties) { - Map<String, String> map = getSections().get(section); - if (map == null) { - map = new HashMap<>(); - getSections().put(section, map); - } - map.putAll(properties); - return this; - } - - /** - * Adds the given entries to the <i>default</i> section, all existing values will be overridden. - * @param properties the entry's data - * @return the builder for chaining. - */ - public ConfigurationDataBuilder addProperties(Map<String, String> properties) { - getDefaultProperties().putAll(properties); - return this; - } - - /** - * Sets the given entries as the <i>combined</i> properties map, all existing properties of the - * combined map will be overridden. - * - * @param properties the entry's data - * @return the builder for chaining. - */ - public ConfigurationDataBuilder setCombinedProperties(Map<String, String> properties) { - this.combinedProperties = new HashMap<>(properties); - return this; - } - - /** - * Access the current default section, if not present a new instance is initialized. - * - * @return the current default section, never null. - */ - public Map<String, String> getDefaultProperties() { - if (defaultProperties == null) { - defaultProperties = new HashMap<>(); - } - return defaultProperties; - } - - /** - * Access the current combined properties, if not present a new instance is initialized. - * - * @return the current combined properties, never null. - */ - public Map<String, String> getCombinedProperties() { - if (combinedProperties == null) { - combinedProperties = new HashMap<>(); - } - return combinedProperties; - } - - /** - * Access the current named sections, if not present a new instance is initialized. - * - * @return the current named sections, never null. - */ - public Map<String, Map<String, String>> getSections() { - if (namedSections == null) { - namedSections = new HashMap<>(); - } - return namedSections; - } - - /** - * Builds a new {@link org.apache.tamaya.format.ConfigurationData} instance. - * @return a new {@link org.apache.tamaya.format.ConfigurationData} instance, not null. - */ - public ConfigurationData build(){ - return new ConfigurationData(this); - } - - @Override - public String toString() { - return "ConfigurationDataBuilder{" + - "format=" + format + - ", default properties=" + defaultProperties + - ", sections=" + namedSections + - ", combined properties=" + combinedProperties + - ", resource=" + resource + - '}'; - } -}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/formats/src/main/java/org/apache/tamaya/format/ConfigurationFormat.java ---------------------------------------------------------------------- diff --git a/modules/formats/src/main/java/org/apache/tamaya/format/ConfigurationFormat.java b/modules/formats/src/main/java/org/apache/tamaya/format/ConfigurationFormat.java deleted file mode 100644 index 997ef3a..0000000 --- a/modules/formats/src/main/java/org/apache/tamaya/format/ConfigurationFormat.java +++ /dev/null @@ -1,103 +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.tamaya.format; - -import java.io.InputStream; -import java.net.URL; - -/** - * <p>Implementations current this class encapsulate the mechanism how to read a - * resource including interpreting the format correctly (e.g. xml vs. - * properties vs. ini). In most cases file only contains entries of the same priority, which would then - * result in only one {@link org.apache.tamaya.spi.PropertySource}. Complex file formats, however, may contain entries - * of different priorities. In this cases, each ordinal type found typically is returned as a separate section so the - * consuming {@link org.apache.tamaya.spi.PropertySourceProvider} implementation can distribute the different part to - * individual {@link org.apache.tamaya.spi.PropertySource}s.</p> - * - * <h3>Implementation Requirements</h3> - * Implementations of this type must be - * <ul> - * <li>thread-safe</li> - * </ul> - */ -public interface ConfigurationFormat { - - /** - * Get a unique name of the format. This name can be used to access the format. - * @return the (unique) format's name, never null and not empty. - */ - String getName(); - - /** - * Allows the format to examine the given resource, e.g. for a matching file ending. Only, if a format accepts an - * URL, it will be tried for reading the configuration. - * @param url the url to read the configuration data from (could be a file, a server location, a classpath - * resource or something else, not null. - * @return true, if this format accepts the given URL for reading. - */ - boolean accepts(URL url); - - /** - * Reads a configuration from an URL, hereby parsing the given {@link java.io.InputStream}. Dependening on - * the capabilities of the format the returned {@link org.apache.tamaya.format.ConfigurationData} may contain - * different levels of data: - * <ul> - * <li>Only a <i>default</i> section is returned, since the configuration format does not support - * hierarchies. This is the case for properties and xml properties.</li> - * <li>Hierarchical formats such as INI, XML and JSON can map each node to a section. Each section - * can have its own key/value pairs. This allows to map also complex formats in a generic way. A - * format implementation should then additionally flatten the whole data and store it in a accessible as - * {@link ConfigurationData#getCombinedProperties()}. This allows to use the properties as inout to a default mapping, - * which is always appropriate as long as no other semnatics - * are defined in the concrete target scenario.</li> - * <li>More complex custom scenarios should map their configuration data read into different - * sections. Typically the data will be mapped into different {@link org.apache.tamaya.spi.PropertySource} - * instances with different ordinal levels. As an example imagine a custom format that contains sections - * 'defaults', 'global-defaults', 'application', 'server-overrides'.</li> - * <li>Alternate formats</li> - * </ul> - * - * Summarizing implementations common formats should always provide - * <ul> - * <li>the data organized in sections as useful for the given format. If data is organized in one section, it - * should be mapped into the DEFAULT section.</li> - * <li>Formats that do provide multiple sections, should always provide a FLATTENED section as well, where - * all the data is organized as a flattened key/value pairs, enabling a generic mapping to a - * {@link org.apache.tamaya.spi.PropertySource}.</li> - * </ul> - * - * If the configuration format only contains entries of one ordinal type, normally only one single - * instance of PropertySource is returned (the corresponding key/values should end up in the DEFAULT section). - * Nevertheless custom formats may contain different sections or parts, - * where each part maps to a different target ordinal (eg defaults, domain config and app config). In the - * ladder case multiple PropertySources can be returned, each one with its own ordinal and the corresponding - * entries. - * @see org.apache.tamaya.spi.PropertySource - * @param resource a descriptive name for the resource, since an InputStream does not have any) - * @param inputStream the inputStream to read the configuration data from (could be a file, a server location, a classpath - * resource or something else. - * @return the corresponding {@link ConfigurationData} containing sections/properties read, never {@code null}. - * @throws org.apache.tamaya.ConfigException if parsing of the input fails. - */ - ConfigurationData readConfiguration(String resource, InputStream inputStream); - - //X TODO Add support to access a default format to see a correct formatting - //X String getFormatExample(); - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/formats/src/main/java/org/apache/tamaya/format/ConfigurationFormats.java ---------------------------------------------------------------------- diff --git a/modules/formats/src/main/java/org/apache/tamaya/format/ConfigurationFormats.java b/modules/formats/src/main/java/org/apache/tamaya/format/ConfigurationFormats.java deleted file mode 100644 index bc8aabd..0000000 --- a/modules/formats/src/main/java/org/apache/tamaya/format/ConfigurationFormats.java +++ /dev/null @@ -1,187 +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.tamaya.format; - -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashSet; -import java.util.List; -import java.util.Objects; -import java.util.Set; -import java.util.logging.Level; -import java.util.logging.Logger; - -import org.apache.tamaya.spi.ServiceContextManager; - -/** - * Small accessor and management class dealing with {@link org.apache.tamaya.format.ConfigurationFormat} - * instances. - */ -public final class ConfigurationFormats { - /** - * The logger used. - */ - private static final Logger LOG = Logger.getLogger(ConfigurationFormats.class.getName()); - - /** - * Singleton constructor. - */ - private ConfigurationFormats() { - } - - /** - * Get all currently available formats, ordered by priority. - * - * @return the currently available formats, never null. - */ - public static List<ConfigurationFormat> getFormats() { - return ServiceContextManager.getServiceContext().getServices(ConfigurationFormat.class); - } - - /** - * Get all currently available formats, ordered by priority. - * - * @param formatNames available formats to be ordered. - * @return the currently available formats, never null. - */ - public static List<ConfigurationFormat> getFormats(String... formatNames) { - final List<ConfigurationFormat> result = new ArrayList<>(); - final Set<String> names = new HashSet<>(Arrays.asList(formatNames)); - for (final ConfigurationFormat f : getFormats()) { - if (names.contains(f.getName())) { - result.add(f); - } - } - return result; - } - - // Activate for JDK 8... -// /** -// * Get all currently available formats, ordered by priority. -// * -// * @return the currently available formats, never null. -// */ -// public static List<ConfigurationFormat> getFormats(Predicate<String> namePredicate) { -// List<ConfigurationFormat> result = new ArrayList<>(); -// for(ConfigurationFormat f:getFormats()){ -// if(namePredicate.test(f.getName()){ -// result.add(f); -// } -// } -// return result; -// } - - /** - * Get all currently available formats, ordered by priority. - * - * @param url source to read configuration from. - * @return the currently available formats, never null. - */ - public static List<ConfigurationFormat> getFormats(final URL url) { - final List<ConfigurationFormat> formats = getFormats(); - final List<ConfigurationFormat> result = new ArrayList<>(); - for (final ConfigurationFormat f : formats) { - if (f.accepts(url)) { - result.add(f); - } - } - return result; - } - - /** - * Tries to read configuration data from a given URL, hereby traversing all known formats in order of precedence. - * Hereby the formats are first filtered to check if the URL is acceptable, before the input is being parsed. - * - * @param url the url from where to read, not null. - * @return the ConfigurationData read, or null. - * @throws IOException if the resource cannot be read. - */ - public static ConfigurationData readConfigurationData(final URL url) throws IOException { - final List<ConfigurationFormat> formats = getFormats(url); - return readConfigurationData(url, formats.toArray(new ConfigurationFormat[formats.size()])); - } - - /** - * Tries to read configuration data from a given URL, hereby explicitly trying all given formats in order. - * - * @param url the url from where to read, not null. - * @param formats the formats to try. - * @return the ConfigurationData read, or null. - * @throws IOException if the resource cannot be read. - */ - public static ConfigurationData readConfigurationData(URL url, ConfigurationFormat... formats) throws IOException { - return readConfigurationData(url.toString(), url.openStream(), formats); - } - - /** - * @param urls the urls from where to read, not null. - * @param formats the formats to try. - * @return the {@link org.apache.tamaya.format.ConfigurationData} of the files successfully decoded by the - * given formats. - */ - public static Collection<ConfigurationData> getPropertySources(Collection<URL> urls, ConfigurationFormat... formats) { - final List<ConfigurationData> dataRead = new ArrayList<>(); - for (final URL url : urls) { - try { - final ConfigurationData data = readConfigurationData(url, formats); - if (data != null) { - dataRead.add(data); - } - } catch (final Exception e) { - LOG.log(Level.SEVERE, "Error reading file: " + url.toExternalForm(), e); - } - } - return dataRead; - } - - /** - * Tries to read configuration data from a given URL, hereby explicitly trying all given formats in order. - * - * @param resource a descriptive name for the resource, since an InputStream does not have any - * @param inputStream the inputStream from where to read, not null. - * @param formats the formats to try. - * @return the ConfigurationData read, or null. - * @throws IOException if the resource cannot be read. - */ - public static ConfigurationData readConfigurationData(String resource, InputStream inputStream, - ConfigurationFormat... formats) throws IOException { - Objects.requireNonNull(inputStream); - Objects.requireNonNull(resource); - try(InputStreamFactory isFactory = new InputStreamFactory(inputStream)) { - for (final ConfigurationFormat format : formats) { - try (InputStream is = isFactory.createInputStream()) { - final ConfigurationData data = format.readConfiguration(resource, is); - if (data != null) { - return data; - } - } catch (final Exception e) { - LOG.log(Level.INFO, - "Format " + format.getClass().getName() + " failed to read resource " + resource, e); - } - } - } - return null; - } - - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/formats/src/main/java/org/apache/tamaya/format/FlattenedDefaultPropertySource.java ---------------------------------------------------------------------- diff --git a/modules/formats/src/main/java/org/apache/tamaya/format/FlattenedDefaultPropertySource.java b/modules/formats/src/main/java/org/apache/tamaya/format/FlattenedDefaultPropertySource.java deleted file mode 100644 index 9d2097f..0000000 --- a/modules/formats/src/main/java/org/apache/tamaya/format/FlattenedDefaultPropertySource.java +++ /dev/null @@ -1,118 +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.tamaya.format; - -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertyValue; - -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Flattened default PropertySource that uses the flattened config data read from an URL by a - * ${@link org.apache.tamaya.format.ConfigurationFormat}. - */ -public class FlattenedDefaultPropertySource implements PropertySource { - private static final Logger LOG = Logger.getLogger(FlattenedDefaultPropertySource.class.getName()); - private final Map<String, String> properties; - private final ConfigurationData data; - private int defaultOrdinal = 0; - - - /* - * Constructor, uses hereby the flattened config data read from an URL by a - * ${@link org.apache.tamaya.format.ConfigurationFormat}, and if not present falls back to the default section. - */ - public FlattenedDefaultPropertySource(ConfigurationData data) { - this.properties = populateData(data); - this.data = data; - } - - /* - * Constructor, uses hereby the flattened config data read from an URL by a - * ${@link org.apache.tamaya.format.ConfigurationFormat}, and if not present falls back to the default section. - */ - public FlattenedDefaultPropertySource(int defaultOrdinal, ConfigurationData data) { - this.properties = populateData(data); - this.data = data; - this.defaultOrdinal = defaultOrdinal; - } - - protected Map<String, String> populateData(ConfigurationData data) { - Map<String, String> result = data.getCombinedProperties(); - if (result.isEmpty()) { - result = data.getDefaultProperties(); - } - if (result.isEmpty()) { - result = new HashMap<>(); - } - if(result.isEmpty()){ - for (String section : data.getSectionNames()) { - Map<String,String> sectionMap = data.getSection(section); - for(Map.Entry<String,String> en: sectionMap.entrySet()){ - result.put(section + '.' + en.getKey(), en.getValue()); - } - } - } - return Collections.unmodifiableMap(result); - } - - @Override - public String getName() { - String name = this.properties.get("[meta].name"); - if (name == null) { - name = this.data.getResource(); - } - if (name == null) { - name = getClass().getSimpleName(); - } - return name; - } - - @Override - public int getOrdinal() { - String ordinalValue = this.properties.get(PropertySource.TAMAYA_ORDINAL); - if (ordinalValue != null) { - try { - return Integer.parseInt(ordinalValue.trim()); - } catch (Exception e) { - LOG.log(Level.WARNING, "Failed to parse Tamaya ordinal from " + data.getResource(), e); - } - } - return defaultOrdinal; - } - - @Override - public PropertyValue get(String key) { - return PropertyValue.of(key, properties.get(key), getName()); - } - - @Override - public Map<String, String> getProperties() { - return properties; - } - - @Override - public boolean isScannable() { - return true; - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/formats/src/main/java/org/apache/tamaya/format/InputStreamFactory.java ---------------------------------------------------------------------- diff --git a/modules/formats/src/main/java/org/apache/tamaya/format/InputStreamFactory.java b/modules/formats/src/main/java/org/apache/tamaya/format/InputStreamFactory.java deleted file mode 100644 index 912dd08..0000000 --- a/modules/formats/src/main/java/org/apache/tamaya/format/InputStreamFactory.java +++ /dev/null @@ -1,89 +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.tamaya.format; - -import java.io.*; -import java.util.Objects; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Wrapper for a given {@link InputStream} to be able to close - * it via the try-with-resources construct of Java 7. - * - * <h1>Usage example</h1> - * - * <pre> - * public void readIt(InputStream inputStream) { - * try (InputStream is = new ParallelInputStream(inputStream) { - * // Consume the stream - * } - * } - * </pre> - */ -public class InputStreamFactory implements Closeable { - private static final Logger LOG = Logger.getLogger(InputStreamFactory.class.getName()); - - private byte[] data; - - /** - * Creates a new InputStreamFactory. - * - * @param original the InputStream to be read for extract its data into memory. - * @throws IOException if thrown by the original during read. - */ - public InputStreamFactory(InputStream original) throws IOException { - Objects.requireNonNull(original); - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - byte[] bytes = new byte[256]; - try { - int read = original.read(bytes); - while (read > 0) { - bos.write(bytes, 0, read); - read = original.read(bytes); - } - this.data = bos.toByteArray(); - } finally { - try { - original.close(); - } catch (IOException e) { - LOG.log(Level.FINEST, "Error closing stream: " + original, e); - } - } - } - - /** - * Creates a new InputStream with the same data as provided by the InputStream passed on factory creation. - * - * @return a new InputStream , never null. - * @throws IOException if no data is available. - */ - public InputStream createInputStream() throws IOException { - byte[] bytes = this.data; - if (bytes == null) { - throw new IOException("InputStreamFactory is closed."); - } - return new ByteArrayInputStream(bytes); - } - - @Override - public void close() throws IOException { - this.data = null; - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/formats/src/main/java/org/apache/tamaya/format/formats/IniConfigurationFormat.java ---------------------------------------------------------------------- diff --git a/modules/formats/src/main/java/org/apache/tamaya/format/formats/IniConfigurationFormat.java b/modules/formats/src/main/java/org/apache/tamaya/format/formats/IniConfigurationFormat.java deleted file mode 100644 index fe27ba7..0000000 --- a/modules/formats/src/main/java/org/apache/tamaya/format/formats/IniConfigurationFormat.java +++ /dev/null @@ -1,95 +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.tamaya.format.formats; - -import org.apache.tamaya.ConfigException; -import org.apache.tamaya.format.ConfigurationData; -import org.apache.tamaya.format.ConfigurationDataBuilder; -import org.apache.tamaya.format.ConfigurationFormat; - -import java.io.BufferedReader; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.URL; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Implements a ini file format. - */ -public class IniConfigurationFormat implements ConfigurationFormat { - - /** - * The logger. - */ - private final static Logger LOG = Logger.getLogger(IniConfigurationFormat.class.getName()); - - @Override - public String getName() { - return "ini"; - } - - @Override - public boolean accepts(URL url) { - String fileName = url.getFile(); - return fileName.endsWith(".ini") || fileName.endsWith(".INI"); - } - - @Override - public ConfigurationData readConfiguration(String resource, InputStream inputStream) { - ConfigurationDataBuilder builder = ConfigurationDataBuilder.of(resource, this); - try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"))) { - String line = reader.readLine(); - int lineNum = 0; - String section = null; - while (line != null) { - lineNum++; - line = line.trim(); - if (line.isEmpty()) { - line = reader.readLine(); - continue; - } - if (line.startsWith("[")) { - int end = line.indexOf(']'); - if (end < 0) { - throw new ConfigException( - "Invalid INI-Format, ']' expected, at " + lineNum + " in " + resource); - } - section = line.substring(1, end); - } else if (line.trim().startsWith("#")) { - // comment - } else { - int sep = line.indexOf('='); - String key = line.substring(0, sep); - String value = line.substring(sep + 1); - if (section != null) { - builder.addSectionProperty(section, key, value); - } else { - builder.addProperty(key, value); - } - } - line = reader.readLine(); - } - return builder.build(); - } catch (Exception e) { - LOG.log(Level.SEVERE, "Could not read configuration: " + resource, e); - } - return null; - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/formats/src/main/java/org/apache/tamaya/format/formats/PropertiesFormat.java ---------------------------------------------------------------------- diff --git a/modules/formats/src/main/java/org/apache/tamaya/format/formats/PropertiesFormat.java b/modules/formats/src/main/java/org/apache/tamaya/format/formats/PropertiesFormat.java deleted file mode 100644 index 35cef77..0000000 --- a/modules/formats/src/main/java/org/apache/tamaya/format/formats/PropertiesFormat.java +++ /dev/null @@ -1,71 +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.tamaya.format.formats; - -import org.apache.tamaya.format.ConfigurationData; -import org.apache.tamaya.format.ConfigurationDataBuilder; -import org.apache.tamaya.format.ConfigurationFormat; - -import java.io.InputStream; -import java.net.URL; -import java.util.Map; -import java.util.Objects; -import java.util.Properties; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Implementation of a {@link org.apache.tamaya.format.ConfigurationFormat} for -properties files. - * - * @see java.util.Properties#load(java.io.InputStream) - */ -public class PropertiesFormat implements ConfigurationFormat { - /** - * The logger. - */ - private final static Logger LOG = Logger.getLogger(PropertiesFormat.class.getName()); - - - @Override - public String getName() { - return "properties"; - } - - @Override - public boolean accepts(URL url) { - String fileName = url.getFile(); - return fileName.endsWith(".properties") || fileName.endsWith(".PROPERTIES") || - fileName.endsWith(".conf") || fileName.endsWith(".CONF"); - } - - @SuppressWarnings("unchecked") - @Override - public ConfigurationData readConfiguration(String resource, InputStream inputStream) { - Objects.requireNonNull(inputStream); - Objects.requireNonNull(resource); - try { - final Properties p = new Properties(); - p.load(inputStream); - return ConfigurationDataBuilder.of(resource, this).addProperties(Map.class.cast(p)).build(); - } catch (Exception e) { - LOG.log(Level.FINEST, "Failed to read config from resource: " + resource, e); - } - return null; - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/formats/src/main/java/org/apache/tamaya/format/formats/PropertiesXmlFormat.java ---------------------------------------------------------------------- diff --git a/modules/formats/src/main/java/org/apache/tamaya/format/formats/PropertiesXmlFormat.java b/modules/formats/src/main/java/org/apache/tamaya/format/formats/PropertiesXmlFormat.java deleted file mode 100644 index c47b6f6..0000000 --- a/modules/formats/src/main/java/org/apache/tamaya/format/formats/PropertiesXmlFormat.java +++ /dev/null @@ -1,69 +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.tamaya.format.formats; - -import org.apache.tamaya.format.ConfigurationData; -import org.apache.tamaya.format.ConfigurationDataBuilder; -import org.apache.tamaya.format.ConfigurationFormat; - -import java.io.InputStream; -import java.net.URL; -import java.util.*; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Implementation of a {@link org.apache.tamaya.format.ConfigurationFormat} for xml property - * files. - * - * @see java.util.Properties#loadFromXML(java.io.InputStream) - */ -public class PropertiesXmlFormat implements ConfigurationFormat { - /** - * The logger. - */ - private final static Logger LOG = Logger.getLogger(PropertiesXmlFormat.class.getName()); - - @Override - public String getName() { - return "xml-properties"; - } - - @Override - public boolean accepts(URL url) { - String fileName = url.getFile(); - return fileName.endsWith(".xml") || fileName.endsWith(".XML"); - } - - @SuppressWarnings("unchecked") - @Override - public ConfigurationData readConfiguration(String resource, InputStream inputStream) { - Objects.requireNonNull(inputStream); - Objects.requireNonNull(resource); - - try { - final Properties p = new Properties(); - p.loadFromXML(inputStream); - return ConfigurationDataBuilder.of(resource, this).addProperties(Map.class.cast(p)).build(); - } catch (Exception e) { - LOG.log(Level.FINEST, "Failed to read config from resource: " + resource, e); - } - return null; - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/formats/src/main/java/org/apache/tamaya/format/formats/package-info.java ---------------------------------------------------------------------- diff --git a/modules/formats/src/main/java/org/apache/tamaya/format/formats/package-info.java b/modules/formats/src/main/java/org/apache/tamaya/format/formats/package-info.java deleted file mode 100644 index db8987d..0000000 --- a/modules/formats/src/main/java/org/apache/tamaya/format/formats/package-info.java +++ /dev/null @@ -1,23 +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. - */ -/** - * This package provides implementtion of {@link org.apache.tamaya.format.ConfigurationFormat} - * for properties, xml-properties and ini files. - */ -package org.apache.tamaya.format.formats; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/formats/src/main/java/org/apache/tamaya/format/package-info.java ---------------------------------------------------------------------- diff --git a/modules/formats/src/main/java/org/apache/tamaya/format/package-info.java b/modules/formats/src/main/java/org/apache/tamaya/format/package-info.java deleted file mode 100644 index 39b5f0b..0000000 --- a/modules/formats/src/main/java/org/apache/tamaya/format/package-info.java +++ /dev/null @@ -1,28 +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. - */ -/** - * This package provides an abstraction for parsing a configuration - * from an input strem, called {@link org.apache.tamaya.format.ConfigurationFormat} - * and corresponding helper artifacts. - * - * @see org.apache.tamaya.format.ConfigurationFormat - * @see org.apache.tamaya.format.ConfigurationData - * @see org.apache.tamaya.format.ConfigurationFormats - */ -package org.apache.tamaya.format; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/formats/src/main/resources/META-INF/services/org.apache.tamaya.format.ConfigurationFormat ---------------------------------------------------------------------- diff --git a/modules/formats/src/main/resources/META-INF/services/org.apache.tamaya.format.ConfigurationFormat b/modules/formats/src/main/resources/META-INF/services/org.apache.tamaya.format.ConfigurationFormat deleted file mode 100644 index 96e898f..0000000 --- a/modules/formats/src/main/resources/META-INF/services/org.apache.tamaya.format.ConfigurationFormat +++ /dev/null @@ -1,21 +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 current 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. -# -org.apache.tamaya.format.formats.IniConfigurationFormat -org.apache.tamaya.format.formats.PropertiesFormat -org.apache.tamaya.format.formats.PropertiesXmlFormat \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/formats/src/test/java/org/apache/tamaya/format/ConfigurationFormatsTest.java ---------------------------------------------------------------------- diff --git a/modules/formats/src/test/java/org/apache/tamaya/format/ConfigurationFormatsTest.java b/modules/formats/src/test/java/org/apache/tamaya/format/ConfigurationFormatsTest.java deleted file mode 100644 index 0839714..0000000 --- a/modules/formats/src/test/java/org/apache/tamaya/format/ConfigurationFormatsTest.java +++ /dev/null @@ -1,68 +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.tamaya.format; - -import java.util.List; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -/** - * Tests for {@link org.apache.tamaya.format.ConfigurationFormats}. - */ -public class ConfigurationFormatsTest { - - @org.junit.Test - public void testGetFormats() throws Exception { - List<ConfigurationFormat> formats = ConfigurationFormats.getFormats(); - assertNotNull(formats); - assertEquals(formats.size(), 3); - } - - @org.junit.Test - public void testReadConfigurationData() throws Exception { - List<ConfigurationFormat> formats = ConfigurationFormats.getFormats(getClass().getResource("/Test.ini")); - assertNotNull(formats); - assertEquals(formats.size(), 1); - formats = ConfigurationFormats.getFormats(getClass().getResource("/Test.properties")); - assertNotNull(formats); - assertEquals(formats.size(), 1); -// formats = ConfigurationFormats.getFormats(getClass().getResource("/Test.xml")); -// assertNotNull(formats); -// assertEquals(formats.size(), 1); - - } - - @org.junit.Test - public void testReadConfigurationData_URL() throws Exception { - ConfigurationData data = ConfigurationFormats.readConfigurationData(getClass().getResource("/Test.ini")); - assertNotNull(data); - data = ConfigurationFormats.readConfigurationData(getClass().getResource("/Test.properties")); - assertNotNull(data); - } - - @org.junit.Test - public void testReadConfigurationData2() throws Exception { - List<ConfigurationFormat> formats = ConfigurationFormats.getFormats(); - ConfigurationData data = ConfigurationFormats.readConfigurationData(getClass().getResource("/Test.ini"), - formats.toArray(new ConfigurationFormat[formats.size()])); - assertNotNull(data); - System.out.println(data); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/formats/src/test/java/org/apache/tamaya/format/FlattenedDefaultPropertySourceTest.java ---------------------------------------------------------------------- diff --git a/modules/formats/src/test/java/org/apache/tamaya/format/FlattenedDefaultPropertySourceTest.java b/modules/formats/src/test/java/org/apache/tamaya/format/FlattenedDefaultPropertySourceTest.java deleted file mode 100644 index adc94f6..0000000 --- a/modules/formats/src/test/java/org/apache/tamaya/format/FlattenedDefaultPropertySourceTest.java +++ /dev/null @@ -1,98 +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.tamaya.format; - -import org.apache.tamaya.format.formats.PropertiesFormat; -import org.apache.tamaya.spi.PropertySource; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; - -/** - * Tests for {@link org.apache.tamaya.format.FlattenedDefaultPropertySource}. - */ -public class FlattenedDefaultPropertySourceTest { - - @Test - public void testGetName() throws Exception { - FlattenedDefaultPropertySource ps = new FlattenedDefaultPropertySource(createConfigurationData("test1")); - assertEquals("test1", ps.getName()); - } - - private ConfigurationData createConfigurationData(String sourceName) { - return ConfigurationDataBuilder.of(sourceName, new PropertiesFormat()) - .addProperty("a", "aValue").addSectionProperty("section1", "sectionKey1", "sectionValue11") - .addSections("section1", "section12") - .addSectionProperty("section2", "sectionKey1", "sectionValue21").build(); - } - - private ConfigurationData createConfigurationData(String sourceName, int ordinal) { - return ConfigurationDataBuilder.of(sourceName, new PropertiesFormat()) - .addProperty("a", "aValue").addSectionProperty("section1", "sectionKey1", "sectionValue11") - .addSections("section1", "section12").addProperty(PropertySource.TAMAYA_ORDINAL, String.valueOf(ordinal)) - .addSectionProperty("section2", "sectionKey1", "sectionValue21").build(); - } - - private ConfigurationData createConfigurationDataNoDefault(String sourceName) { - return ConfigurationDataBuilder.of(sourceName, new PropertiesFormat()) - .addSectionProperty("section1", "sectionKey1", "sectionValue11") - .addSections("section1", "section12") - .addSectionProperty("section2", "sectionKey1", "sectionValue21").build(); - } - - @Test - public void testGetOrdinal() throws Exception { - FlattenedDefaultPropertySource ps = new FlattenedDefaultPropertySource(createConfigurationData("test1", 11)); - assertEquals(11, ps.getOrdinal()); - } - - @Test - public void testGet() throws Exception { - FlattenedDefaultPropertySource ps = new FlattenedDefaultPropertySource(createConfigurationData("test2")); - assertEquals("aValue", ps.get("a").get("a")); - assertNotNull(ps.get("section1.sectionKey1").get("section1.sectionKey1")); - assertNotNull(ps.get("section2.sectionKey1").get("section2.sectionKey1")); - assertNull(ps.get("sectionKey1")); - ps = new FlattenedDefaultPropertySource(createConfigurationDataNoDefault("test2")); - assertEquals("sectionValue11", ps.get("section1.sectionKey1").get("section1.sectionKey1")); - assertEquals("sectionValue21", ps.get("section2.sectionKey1").get("section2.sectionKey1")); - assertNull(ps.get("a")); - assertNull(ps.get("section1")); - } - - @Test - public void testGetProperties() throws Exception { - FlattenedDefaultPropertySource ps = new FlattenedDefaultPropertySource(createConfigurationData("test3")); - assertNotNull(ps.getProperties()); - assertEquals("aValue", ps.getProperties().get("a")); - assertNotNull(ps.getProperties().get("section1.sectionKey1")); - assertNotNull(ps.getProperties().get("section2.sectionKey1")); - assertNull(ps.getProperties().get("section1.sectionKey2")); - assertNull(ps.getProperties().get("section2.sectionKey2")); - assertNull(ps.getProperties().get("sectionKey1")); - assertNull(ps.getProperties().get("sectionKey2")); - ps = new FlattenedDefaultPropertySource(createConfigurationDataNoDefault("test3")); - assertNotNull(ps.getProperties()); - assertEquals("sectionValue11", ps.getProperties().get("section1.sectionKey1")); - assertEquals("sectionValue21", ps.getProperties().get("section2.sectionKey1")); - assertNull(ps.get("section1")); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/formats/src/test/java/org/apache/tamaya/format/InputStreamFactoryTest.java ---------------------------------------------------------------------- diff --git a/modules/formats/src/test/java/org/apache/tamaya/format/InputStreamFactoryTest.java b/modules/formats/src/test/java/org/apache/tamaya/format/InputStreamFactoryTest.java deleted file mode 100644 index c05da09..0000000 --- a/modules/formats/src/test/java/org/apache/tamaya/format/InputStreamFactoryTest.java +++ /dev/null @@ -1,145 +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.tamaya.format; - -import org.junit.Test; -import org.mockito.ArgumentCaptor; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; - -public class InputStreamFactoryTest { - - @Test(expected = NullPointerException.class) - public void ctorEnforcesNonNullOriginal() throws IOException { - new InputStreamFactory(null); - } - - @Test - public void givenStreamIsClosedInTryWithResourcesConstruct() throws Exception { - InputStream stream = mock(InputStream.class); - doReturn(34).when(stream).read(); - - InputStreamFactory factory = new InputStreamFactory(stream); - verify(stream).close(); - for (int i = 0; i < 100; i++) { - try (InputStream in = factory.createInputStream()) { - in.read(); - } - } - verify(stream).close(); - } - - @Test - public void callToReadIsNotForwardedCallToWrapped() throws IOException { - InputStream stream = new ByteArrayInputStream(new byte[]{1, 2, 3, 4}); - InputStreamFactory closer = new InputStreamFactory(stream); - byte[] byteArray = new byte[4]; - for (int i = 0; i < 100; i++) { - InputStream is = closer.createInputStream(); - assertThat(is.read(byteArray), equalTo(4)); - } - } - - - @Test - public void callToSkipIsForwardedToWrapped() throws IOException { - InputStream stream = new ByteArrayInputStream(new byte[]{1, 2, 3, 4}); - InputStreamFactory closer = new InputStreamFactory(stream); - for (int i = 0; i < 100; i++) { - InputStream is = closer.createInputStream(); - assertThat(is.skip(2L), equalTo(2L)); - } - } - - - @Test - public void callToAvailableIsNotForwardedToWrapped() throws IOException { - InputStream stream = new ByteArrayInputStream(new byte[]{1, 2, 3, 4}); - InputStreamFactory closer = new InputStreamFactory(stream); - for (int i = 0; i < 100; i++) { - InputStream is = closer.createInputStream(); - assertThat(is.available(), equalTo(4)); - } - } - - @Test - public void callToCloseIsNotForwardedToWrapped() throws IOException { - InputStream stream = new ByteArrayInputStream(new byte[]{1, 2, 3, 4}); - InputStreamFactory closer = new InputStreamFactory(stream); - for (int i = 0; i < 100; i++) { - InputStream is = closer.createInputStream(); - is.close(); - } - } - - @Test - public void callToMarkIsNotForwardedToWrapped() throws IOException { -// ArgumentCaptor<Integer> captor = ArgumentCaptor.forClass(Integer.class); - InputStream stream = new ByteArrayInputStream(new byte[]{1, 2, 3, 4}); - InputStreamFactory closer = new InputStreamFactory(stream); - for (int i = 0; i < 100; i++) { - InputStream is = closer.createInputStream(); - is.mark(2); - } - } - - - @Test - public void callToResetIsNotForwardedToWrapped() throws IOException { - InputStream stream = new ByteArrayInputStream(new byte[]{1, 2, 3, 4}); - InputStreamFactory closer = new InputStreamFactory(stream); - for (int i = 0; i < 100; i++) { - InputStream is = closer.createInputStream(); - is.reset(); - } - } - - @Test - public void callToMarkSupportedIsNotForwardedToWrapped() throws IOException { - InputStream stream = new ByteArrayInputStream(new byte[]{1, 2, 3, 4}); - InputStreamFactory closer = new InputStreamFactory(stream); - for (int i = 0; i < 100; i++) { - InputStream is = closer.createInputStream(); - assertThat(is.markSupported(), is(true)); - } - } - - @Test - public void callToReadIsForwardedToWrapped() throws IOException { - InputStream stream = new ByteArrayInputStream(new byte[]{1, 2, 3, 4}); - InputStreamFactory closer = new InputStreamFactory(stream); - for (int i = 0; i < 100; i++) { - InputStream is = closer.createInputStream(); - assertThat(is.read(), equalTo(1)); - assertThat(is.read(), equalTo(2)); - assertThat(is.read(), equalTo(3)); - assertThat(is.read(), equalTo(4)); - } - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/formats/src/test/resources/Test.ini ---------------------------------------------------------------------- diff --git a/modules/formats/src/test/resources/Test.ini b/modules/formats/src/test/resources/Test.ini deleted file mode 100644 index 906a1e6..0000000 --- a/modules/formats/src/test/resources/Test.ini +++ /dev/null @@ -1,26 +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 current 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. -# - -aGeneralEntry=blabla - -[MySection1] -sectionEntry1=value1 - -[MySection2] -sectionEntry2=value2 \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/formats/src/test/resources/Test.properties ---------------------------------------------------------------------- diff --git a/modules/formats/src/test/resources/Test.properties b/modules/formats/src/test/resources/Test.properties deleted file mode 100644 index ced544b..0000000 --- a/modules/formats/src/test/resources/Test.properties +++ /dev/null @@ -1,21 +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 current 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. -# -aGeneralEntry=blabla -MySection1.sectionEntry1=value1 -MySection2.sectionEntry2=value2 \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/formats/yaml/pom.xml ---------------------------------------------------------------------- diff --git a/modules/formats/yaml/pom.xml b/modules/formats/yaml/pom.xml new file mode 100644 index 0000000..0ec6b38 --- /dev/null +++ b/modules/formats/yaml/pom.xml @@ -0,0 +1,119 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.tamaya.ext</groupId> + <artifactId>tamaya-formats-all</artifactId> + <version>0.3-incubating-SNAPSHOT</version> + <relativePath>..</relativePath> + </parent> + <artifactId>tamaya-yaml</artifactId> + <name>Apache Tamaya YAML Support</name> + <packaging>bundle</packaging> + <inceptionYear>2016</inceptionYear> + + <properties> + <jdkVersion>1.7</jdkVersion> + <snakeyaml.version>1.17</snakeyaml.version> + </properties> + + <dependencies> + <dependency> + <groupId>org.apache.tamaya</groupId> + <artifactId>tamaya-api</artifactId> + <version>${project.version}</version> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>org.apache.tamaya</groupId> + <artifactId>tamaya-core</artifactId> + <version>${project.version}</version> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>org.apache.tamaya.ext</groupId> + <artifactId>tamaya-formats</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.yaml</groupId> + <artifactId>snakeyaml</artifactId> + <version>${snakeyaml.version}</version> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + </dependency> + <dependency> + <groupId>org.hamcrest</groupId> + <artifactId>java-hamcrest</artifactId> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <artifactId>maven-dependency-plugin</artifactId> + <executions> + <execution> + <id>copyMain</id> + <phase>process-test-sources</phase> + <goals> + <goal>copy</goal> + </goals> + <configuration> + <outputDirectory>${project.build.directory}</outputDirectory> + <overWriteReleases>false</overWriteReleases> + <overWriteSnapshots>false</overWriteSnapshots> + <overWriteIfNewer>true</overWriteIfNewer> + <stripVersion>true</stripVersion> + <artifactItems> + <artifactItem> + <groupId>org.jboss.arquillian.daemon</groupId> + <artifactId>arquillian-daemon-main</artifactId> + </artifactItem> + </artifactItems> + </configuration> + </execution> + </executions> + </plugin> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + <extensions>true</extensions> + <configuration> + <instructions> + <Export-Package> + org.apache.tamaya.json + </Export-Package> + </instructions> + </configuration> + </plugin> + </plugins> + </build> + + +</project> http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/formats/yaml/src/main/java/org/apache/tamaya/json/YAMLFormat.java ---------------------------------------------------------------------- diff --git a/modules/formats/yaml/src/main/java/org/apache/tamaya/json/YAMLFormat.java b/modules/formats/yaml/src/main/java/org/apache/tamaya/json/YAMLFormat.java new file mode 100644 index 0000000..06e431e --- /dev/null +++ b/modules/formats/yaml/src/main/java/org/apache/tamaya/json/YAMLFormat.java @@ -0,0 +1,156 @@ +/* + * 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.json; + +import org.apache.tamaya.ConfigException; +import org.apache.tamaya.format.ConfigurationData; +import org.apache.tamaya.format.ConfigurationDataBuilder; +import org.apache.tamaya.format.ConfigurationFormat; +import org.yaml.snakeyaml.Yaml; + +import java.io.InputStream; +import java.net.URL; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.logging.Level; +import java.util.logging.Logger; + +import static java.lang.String.format; + + +/** + * Implementation of the {@link org.apache.tamaya.format.ConfigurationFormat} + * able to read configuration properties represented in JSON + * + * @see <a href="http://www.json.org">JSON format specification</a> + */ +public class YAMLFormat implements ConfigurationFormat { + /** + * THe logger. + */ + private static final Logger LOG = Logger.getLogger(YAMLFormat.class.getName()); + + /** + * Constructor, itniaitlizing zhe JSON reader factory. + */ + public YAMLFormat(){ + } + + @Override + public String getName() { + return "yaml"; + } + + @Override + public boolean accepts(URL url) { + return Objects.requireNonNull(url).getPath().endsWith(".yaml"); + } + + @Override + public ConfigurationData readConfiguration(String resource, InputStream inputStream) { + try( InputStream in = inputStream;) { + Map<String, String> values = readConfig(resource, inputStream); + return ConfigurationDataBuilder.of(resource, this).addProperties(values) + .build(); + } catch (Exception e) { + throw new ConfigException("Failed to read data from " + resource, e); + } + } + + /** + * Reads the configuration. + * @param inputStream the input stream, not null. + * @param resource resource URI, not null. + * @return the configuration read from the given resource URI. + * @throws ConfigException if resource URI cannot be read. + */ + protected Map<String, String> readConfig(String resource, InputStream inputStream) { + try{ + Yaml yaml = new Yaml(); + HashMap<String, String> values = new HashMap<>(); + Object config = yaml.load(inputStream); + mapYamlIntoProperties(config, values); + if(LOG.isLoggable(Level.FINEST)){ + LOG.finest("Read data from " + resource + " : " + values); + } + return values; + }catch (Throwable t) { + throw new ConfigException(format("Failed to read properties from %s", resource), t); + } + } + /** + * Reads the configuration. + * @param urlResource soure of the configuration. + * @return the configuration read from the given resource URL. + * @throws ConfigException if resource URL cannot be read. + */ + protected Map<String, String> readConfig(URL urlResource) { + try (InputStream is = urlResource.openStream()) { + return readConfig(urlResource.toExternalForm(), is); + } + catch (Throwable t) { + throw new ConfigException(format("Failed to read properties from %s", urlResource.toExternalForm()), t); + } + } + + private void mapYamlIntoProperties(Object config, HashMap<String, String> values) { + mapYamlIntoProperties("", config, values); + } + + /** + * Maps the given config item (could be a String, a collection type or something else returned by the yaml parser + * to a key/value pair and adds it to {@code values} (hereby honoring the prefix as a key to be used.). + * Collection types are recursively to remapped hereby extending the given prefix as needed and recursively + * delegate mapping of values contained. + * @param prefix the prefix or key evaluated so far, never null (but can be empty for root entries). + * @param config the config value. Could be a single value or a collection type. + * @param values the properties where items identified must be written into. These properties are going to be + * returned as result of the format reading operation ans integrated into the overall configuration + * map. + */ + protected void mapYamlIntoProperties(String prefix, Object config, HashMap<String, String> values) { + // add further data types supported by yaml, e.g. date, ... + if(config instanceof List){ + StringBuilder b = new StringBuilder(); + for(Object val:((List<Object>)config)){ + b.append(mapValueToString(val)); + b.append(","); + } + if(b.length()>0){ + b.setLength(b.length()-1); + } + values.put(prefix, b.toString()); + values.put("_"+prefix+".collection-type", "List"); + } else if(config instanceof Map){ + for(Map.Entry<String,Object> en:((Map<String,Object>)config).entrySet()){ + String newPrefix = prefix.isEmpty()?en.getKey():prefix +"."+en.getKey(); + mapYamlIntoProperties(newPrefix, en.getValue(), values); + } + } else{ + values.put(prefix, mapValueToString(config)); + } + } + + protected String mapValueToString(Object val) { + return String.valueOf(val); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/formats/yaml/src/main/java/org/apache/tamaya/json/YAMLPropertySource.java ---------------------------------------------------------------------- diff --git a/modules/formats/yaml/src/main/java/org/apache/tamaya/json/YAMLPropertySource.java b/modules/formats/yaml/src/main/java/org/apache/tamaya/json/YAMLPropertySource.java new file mode 100644 index 0000000..e29d2e7 --- /dev/null +++ b/modules/formats/yaml/src/main/java/org/apache/tamaya/json/YAMLPropertySource.java @@ -0,0 +1,100 @@ +/* + * 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.json; + +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spi.PropertyValue; + +import java.net.URL; +import java.util.*; +import java.util.logging.Level; +import java.util.logging.Logger; + + + +/** + * Property source based on a JSON file. + */ +public class YAMLPropertySource implements PropertySource { + /** The underlying resource. */ + private final URL urlResource; + /** The values read. */ + private final Map<String, String> values; + /** The evaluated ordinal. */ + private int ordinal; + /** The format implementation used for parsing. */ + private YAMLFormat format = new YAMLFormat(); + + /** + * Constructor, hereby using 0 as the default ordinal. + * @param resource the resource modelled as URL, not null. + */ + public YAMLPropertySource(URL resource) { + this(resource, 0); + } + + /** + * Constructor. + * @param resource the resource modelled as URL, not null. + * @param defaultOrdinal the defaultOrdinal to be used. + */ + public YAMLPropertySource(URL resource, int defaultOrdinal) { + urlResource = Objects.requireNonNull(resource); + this.ordinal = defaultOrdinal; // may be overriden by read... + this.values = format.readConfig(urlResource); + if (this.values.containsKey(TAMAYA_ORDINAL)) { + this.ordinal = Integer.parseInt(this.values.get(TAMAYA_ORDINAL)); + } + } + + @Override + public int getOrdinal() { + PropertyValue configuredOrdinal = get(TAMAYA_ORDINAL); + if(configuredOrdinal!=null){ + try{ + return Integer.parseInt(configuredOrdinal.getValue()); + } catch(Exception e){ + Logger.getLogger(getClass().getName()).log(Level.WARNING, + "Configured Ordinal is not an int number: " + configuredOrdinal, e); + } + } + return ordinal; + } + + @Override + public String getName() { + return urlResource.toExternalForm(); + } + + @Override + public PropertyValue get(String key) { + return PropertyValue.of(key, getProperties().get(key), getName()); + } + + @Override + public Map<String, String> getProperties() { + return Collections.unmodifiableMap(values); + } + + + @Override + public boolean isScannable() { + return true; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/formats/yaml/src/main/resources/META-INF/services/org.apache.tamaya.format.ConfigurationFormat ---------------------------------------------------------------------- diff --git a/modules/formats/yaml/src/main/resources/META-INF/services/org.apache.tamaya.format.ConfigurationFormat b/modules/formats/yaml/src/main/resources/META-INF/services/org.apache.tamaya.format.ConfigurationFormat new file mode 100644 index 0000000..1b5d57d --- /dev/null +++ b/modules/formats/yaml/src/main/resources/META-INF/services/org.apache.tamaya.format.ConfigurationFormat @@ -0,0 +1,19 @@ +# +# 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 current 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. +# +org.apache.tamaya.json.YAMLFormat \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/formats/yaml/src/test/java/org/apache/tamaya/json/YAMLFormatTest.java ---------------------------------------------------------------------- diff --git a/modules/formats/yaml/src/test/java/org/apache/tamaya/json/YAMLFormatTest.java b/modules/formats/yaml/src/test/java/org/apache/tamaya/json/YAMLFormatTest.java new file mode 100644 index 0000000..0f0e589 --- /dev/null +++ b/modules/formats/yaml/src/test/java/org/apache/tamaya/json/YAMLFormatTest.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.tamaya.json; + + +import org.apache.tamaya.format.ConfigurationData; +import org.apache.tamaya.format.FlattenedDefaultPropertySource; +import org.apache.tamaya.spi.PropertySource; +import org.junit.Test; + +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Map; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +public class YAMLFormatTest { + private final YAMLFormat format = new YAMLFormat(); + + @Test + public void testAcceptURL() throws MalformedURLException { + assertTrue(format.accepts(new URL("http://127.0.0.1/anyfile.yaml"))); + } + + @Test + public void testAcceptURL_BC1() throws MalformedURLException { + assertFalse(format.accepts(new URL("http://127.0.0.1/anyfile.YAML"))); + } + + @Test(expected = NullPointerException.class) + public void testAcceptURL_BC2() throws MalformedURLException { + assertFalse(format.accepts(null)); + } + + @Test + public void testAcceptURL_BC3() throws MalformedURLException { + assertFalse(format.accepts(new URL("http://127.0.0.1/anyfile.docx"))); + } + + @Test + public void testRead() throws IOException { + URL configURL = YAMLPropertySourceTest.class.getResource("/configs/valid/contact.yaml"); + assertTrue(format.accepts(configURL)); + ConfigurationData data = format.readConfiguration(configURL.toString(), configURL.openStream()); + assertNotNull(data); + for(Map.Entry<String,String> en:data.getDefaultProperties().entrySet()) { + System.out.println(en.getKey() + " -> " + en.getValue()); + } + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/formats/yaml/src/test/java/org/apache/tamaya/json/YAMLPropertySourceTest.java ---------------------------------------------------------------------- diff --git a/modules/formats/yaml/src/test/java/org/apache/tamaya/json/YAMLPropertySourceTest.java b/modules/formats/yaml/src/test/java/org/apache/tamaya/json/YAMLPropertySourceTest.java new file mode 100644 index 0000000..7f1c7a3 --- /dev/null +++ b/modules/formats/yaml/src/test/java/org/apache/tamaya/json/YAMLPropertySourceTest.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.json; + +import org.apache.tamaya.ConfigException; +import org.apache.tamaya.spi.PropertySource; +import org.hamcrest.CoreMatchers; +import org.junit.Test; + +import java.net.URL; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertEquals; + +public class YAMLPropertySourceTest { + + @Test + public void testYamlWithOrdinal() throws Exception { + URL configURL = YAMLPropertySourceTest.class.getResource("/configs/valid/test-with-prio.yaml"); + + assertThat(configURL, CoreMatchers.notNullValue()); + + YAMLPropertySource source = new YAMLPropertySource(configURL, 4); + assertEquals(source.getOrdinal(), 16784); + } + + @Test + public void testYamlDefaultOrdinal() throws Exception { + URL configURL = YAMLPropertySourceTest.class.getResource("/configs/valid/test.yaml"); + + assertThat(configURL, CoreMatchers.notNullValue()); + + YAMLPropertySource source = new YAMLPropertySource(configURL, 4); + assertEquals(source.getOrdinal(), 4); + } + + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/fe7cd8f1/modules/formats/yaml/src/test/resources/configs/valid/contact.yaml ---------------------------------------------------------------------- diff --git a/modules/formats/yaml/src/test/resources/configs/valid/contact.yaml b/modules/formats/yaml/src/test/resources/configs/valid/contact.yaml new file mode 100644 index 0000000..95d5a03 --- /dev/null +++ b/modules/formats/yaml/src/test/resources/configs/valid/contact.yaml @@ -0,0 +1,46 @@ +# +# 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 current 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. +# +invoice: 34843 +date : 2001-01-23 +bill-to: &id001 + given : Chris + family : Dumars + address: + lines: | + 458 Walkman Dr. + Suite #292 + city : Royal Oak + state : MI + postal : 48046 +ship-to: *id001 +product: + - sku : BL394D + quantity : 4 + description : Basketball + price : 450.00 + - sku : BL4438H + quantity : 1 + description : Super Hoop + price : 2392.00 +tax : 251.42 +total: 4443.52 +comments: > + Late afternoon is best. + Backup contact is Nancy + Billsmer @ 338-4338. \ No newline at end of file
