├── .github ├── dependabot.yml └── workflows │ ├── codeql-analysis.yml │ └── maven.yml ├── .gitignore ├── LICENSE.txt ├── README.md ├── changelog.md ├── constretto-api ├── pom.xml └── src │ ├── main │ └── java │ │ └── org │ │ └── constretto │ │ ├── ConfigurationDefaultValueFactory.java │ │ ├── ConstrettoConfiguration.java │ │ ├── GenericConverter.java │ │ ├── Property.java │ │ ├── ValueConverter.java │ │ ├── annotation │ │ ├── Configuration.java │ │ ├── ConfigurationSource.java │ │ ├── Configure.java │ │ └── Tags.java │ │ ├── exception │ │ ├── ConstrettoConversionException.java │ │ ├── ConstrettoException.java │ │ └── ConstrettoExpressionException.java │ │ ├── model │ │ ├── CArray.java │ │ ├── CObject.java │ │ ├── CPrimitive.java │ │ └── CValue.java │ │ └── resolver │ │ └── ConfigurationContextResolver.java │ └── test │ └── java │ └── org │ └── constretto │ └── model │ ├── CArrayTest.java │ ├── CObjectTest.java │ └── CPrimitiveTest.java ├── constretto-core ├── pom.xml └── src │ ├── main │ └── java │ │ └── org │ │ └── constretto │ │ ├── ConfigurationStore.java │ │ ├── ConstrettoBuilder.java │ │ ├── internal │ │ ├── ConstrettoUtils.java │ │ ├── DefaultConstrettoConfiguration.java │ │ ├── GenericCollectionTypeResolver.java │ │ ├── GenericTypeResolver.java │ │ ├── MethodParameter.java │ │ ├── converter │ │ │ ├── BooleanValueConverter.java │ │ │ ├── ByteValueConverter.java │ │ │ ├── DoubleValueConverter.java │ │ │ ├── FileValueConverter.java │ │ │ ├── FloatValueConverter.java │ │ │ ├── InetAddressValueConverter.java │ │ │ ├── InputStreamValueConverter.java │ │ │ ├── IntegerValueConverter.java │ │ │ ├── LocaleValueConverter.java │ │ │ ├── LongValueConverter.java │ │ │ ├── PropertyFileValueConverter.java │ │ │ ├── ShortValueConverter.java │ │ │ ├── StringValueConverter.java │ │ │ ├── UriValueConverter.java │ │ │ ├── UrlValueConverter.java │ │ │ └── ValueConverterRegistry.java │ │ ├── introspect │ │ │ ├── ArgumentDescription.java │ │ │ ├── ArgumentDescriptionFactory.java │ │ │ └── Constructors.java │ │ ├── resolver │ │ │ └── DefaultConfigurationContextResolver.java │ │ └── store │ │ │ ├── EncryptedPropertiesStore.java │ │ │ ├── IniFileConfigurationStore.java │ │ │ ├── JsonStore.java │ │ │ ├── NestedConfigurationStore.java │ │ │ ├── ObjectConfigurationStore.java │ │ │ ├── PropertiesStore.java │ │ │ ├── SystemPropertiesStore.java │ │ │ ├── YamlStore.java │ │ │ └── ldap │ │ │ ├── LdapConfigurationStore.java │ │ │ └── LdapConfigurationStoreBuilder.java │ │ ├── model │ │ ├── ClassPathResource.java │ │ ├── ConfigurationValue.java │ │ ├── FileResource.java │ │ ├── GsonParser.java │ │ ├── Parser.java │ │ ├── Resource.java │ │ ├── TaggedPropertySet.java │ │ └── UrlResource.java │ │ ├── resolver │ │ └── PredefinedConfigurationContextResolver.java │ │ └── util │ │ └── StaticlyCachedConfiguration.java │ └── test │ ├── java │ └── org │ │ └── constretto │ │ ├── ConstrettoBuilderTest.java │ │ ├── ConstrettoConfigurationTest.java │ │ ├── LeadingCharsTest.java │ │ ├── configs │ │ ├── Config.java │ │ ├── TaggedConfig.java │ │ └── UntaggedConfig.java │ │ ├── internal │ │ ├── ConstrettoUtilsTest.java │ │ ├── ConstructorAnnotationClassTest.java │ │ ├── FieldAnnotationSubClassTest.java │ │ ├── GenericCollectionTypeResolverTest.java │ │ ├── MethodAnnotationSubClassTest.java │ │ ├── NonLocalConfigurationClass.java │ │ ├── NonLocalConfigurationClassMultipleConstructors.java │ │ ├── converter │ │ │ ├── EnumValueConverterRegistryTest.java │ │ │ ├── FileConverterTest.java │ │ │ └── ValueConverterRegistryTest.java │ │ ├── introspect │ │ │ └── ArgumentDescriptionFactoryTest.java │ │ ├── provider │ │ │ ├── AbstractConfigurationProviderLookupTest.java │ │ │ ├── ConfigurationAnnotationsTest.java │ │ │ ├── ConstrettoConfigurationTest.java │ │ │ ├── DefaultValuesInAnnotationsTest.java │ │ │ ├── EncryptedPropertiesLookupTest.java │ │ │ ├── IniFileStoreLookupTest.java │ │ │ ├── PropertyFileStoreLookupTest.java │ │ │ ├── ValueConversionTest.java │ │ │ └── helper │ │ │ │ ├── ConfiguredUsingDefaults.java │ │ │ │ ├── ConfiguredUsingListsAndMaps.java │ │ │ │ ├── ContagiousConfigurationMethod.java │ │ │ │ ├── DataSourceConfiguration.java │ │ │ │ └── DataSourceConfigurationWithNatives.java │ │ ├── store │ │ │ ├── AbstractConfigurationStoreTest.java │ │ │ ├── EncryptedPropertiesStoreTest.java │ │ │ ├── IniFileConfigurationStoreTest.java │ │ │ ├── JsonStoreTest.java │ │ │ ├── ObjectConfigurationStoreTest.java │ │ │ ├── PropertiesStoreInputStreamCloseTest.java │ │ │ ├── PropertiesStoreTest.java │ │ │ ├── SystemPropertiesStoreTest.java │ │ │ ├── YamlStoreTest.java │ │ │ ├── helper │ │ │ │ ├── DefaultCustomerDataSourceConfigurer.java │ │ │ │ ├── DevelopmentCustomerDataSourceConfigurer.java │ │ │ │ ├── GenericDataSourceConfigurer.java │ │ │ │ └── ProductionCustomerDataSourceConfigurer.java │ │ │ └── ldap │ │ │ │ ├── LdapConfigurationStoreBuilderTest.java │ │ │ │ ├── LdapConfigurationStoreEmbeddedLdapTest.java │ │ │ │ └── LdapConfigurationStoreTest.java │ │ └── util │ │ │ └── StaticlyCachedConfigurationTest.java │ │ ├── model │ │ ├── ClassPathResourceTest.java │ │ ├── ConfigurationValueTest.java │ │ ├── FileResourceTest.java │ │ ├── TaggedPropertySetTest.java │ │ └── UrlResourceTest.java │ │ └── resolver │ │ └── PredefinedConfigurationContextResolverTest.java │ └── resources │ ├── annotatedTest.properties │ ├── cache1-override1.ini │ ├── cache1-override2.ini │ ├── cache1.ini │ ├── cache2.ini │ ├── cache3-override1.properties │ ├── cache3-override2.properties │ ├── cache3.properties │ ├── constretto.ldif │ ├── dynamic.properties │ ├── encrypted.properties │ ├── jsonTest.json │ ├── leading-chars-strip.properties │ ├── org │ └── constretto │ │ └── internal │ │ └── provider │ │ └── helper │ │ ├── provider-test-overloaded.ini │ │ ├── provider-test-overloaded.properties │ │ ├── provider-test.ini │ │ └── provider-test.properties │ ├── subClassData.properties │ ├── test-with-array-and-map.properties │ ├── test.ini │ ├── test.properties │ ├── yamlTest.yaml │ └── yamlTest2.yaml ├── constretto-test ├── pom.xml └── src │ ├── main │ └── java │ │ └── org │ │ └── constretto │ │ └── test │ │ ├── ConstrettoJUnit4ClassRunner.java │ │ ├── ConstrettoRule.java │ │ ├── extender │ │ ├── ConstrettoTagRuleExtender.java │ │ ├── Extenders.java │ │ └── RuleExtender.java │ │ └── extractors │ │ ├── ConstrettoEnvironmentExtractor.java │ │ ├── ConstrettoTagExtractor.java │ │ └── TagExtractor.java │ └── test │ └── java │ └── org │ └── constretto │ └── test │ ├── ConstrettoJUnit4ClassRunnerTest.java │ ├── ConstrettoRuleClassRuleTest.java │ └── ConstrettoRuleTest.java └── pom.xml /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: maven 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | open-pull-requests-limit: 20 8 | -------------------------------------------------------------------------------- /.github/workflows/codeql-analysis.yml: -------------------------------------------------------------------------------- 1 | # For most projects, this workflow file will not need changing; you simply need 2 | # to commit it to your repository. 3 | # 4 | # You may wish to alter this file to override the set of languages analyzed, 5 | # or to provide custom queries or build logic. 6 | # 7 | # ******** NOTE ******** 8 | # We have attempted to detect the languages in your repository. Please check 9 | # the `language` matrix defined below to confirm you have the correct set of 10 | # supported CodeQL languages. 11 | # 12 | name: "CodeQL" 13 | 14 | on: 15 | push: 16 | branches: [ "master" ] 17 | pull_request: 18 | # The branches below must be a subset of the branches above 19 | branches: [ "master" ] 20 | schedule: 21 | - cron: '30 9 * * 1' 22 | 23 | jobs: 24 | analyze: 25 | name: Analyze 26 | runs-on: ubuntu-latest 27 | permissions: 28 | actions: read 29 | contents: read 30 | security-events: write 31 | 32 | strategy: 33 | fail-fast: false 34 | matrix: 35 | language: [ 'java' ] 36 | # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ] 37 | # Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support 38 | 39 | steps: 40 | - name: Checkout repository 41 | uses: actions/checkout@v3 42 | 43 | # Initializes the CodeQL tools for scanning. 44 | - name: Initialize CodeQL 45 | uses: github/codeql-action/init@v2 46 | with: 47 | languages: ${{ matrix.language }} 48 | # If you wish to specify custom queries, you can do so here or in a config file. 49 | # By default, queries listed here will override any specified in a config file. 50 | # Prefix the list here with "+" to use these queries and those in the config file. 51 | 52 | # Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs 53 | # queries: security-extended,security-and-quality 54 | 55 | 56 | # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). 57 | # If this step fails, then you should remove it and run the build manually (see below) 58 | - name: Autobuild 59 | uses: github/codeql-action/autobuild@v2 60 | 61 | # ℹ️ Command-line programs to run using the OS shell. 62 | # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun 63 | 64 | # If the Autobuild fails above, remove it and uncomment the following three lines. 65 | # modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance. 66 | 67 | # - run: | 68 | # echo "Run, Build Application using script" 69 | # ./location_of_script_within_repo/buildscript.sh 70 | 71 | - name: Perform CodeQL Analysis 72 | uses: github/codeql-action/analyze@v2 73 | -------------------------------------------------------------------------------- /.github/workflows/maven.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven 3 | 4 | name: Java CI with Maven 5 | 6 | on: 7 | push: 8 | branches: [ master ] 9 | pull_request: 10 | branches: [ master ] 11 | 12 | jobs: 13 | build: 14 | runs-on: ubuntu-latest 15 | strategy: 16 | matrix: 17 | java-version: [ "8", "11" ] 18 | 19 | steps: 20 | - uses: actions/checkout@v3 21 | - name: Set up Java ${{ matrix.java-version }} 22 | uses: actions/setup-java@v3 23 | with: 24 | distribution: 'zulu' 25 | java-version: ${{ matrix.java-version }} 26 | - name: Cache Maven packages 27 | uses: actions/cache@v2 28 | with: 29 | path: ~/.m2 30 | key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} 31 | restore-keys: ${{ runner.os }}-m2 32 | - name: Build with Maven and Java ${{ matrix.java-version }} 33 | run: mvn -B package --no-transfer-progress --file pom.xml 34 | - name: Generate JaCoCo report ${{ matrix.java-version }} 35 | run: mvn -B jacoco:report --no-transfer-progress --file pom.xml 36 | - name: Upload coverage to Codecov 37 | uses: codecov/codecov-action@v3 38 | with: 39 | flags: java-${{ matrix.java-version }} 40 | verbose: true 41 | deploy: 42 | if: ${{ success() && github.event_name != 'pull_request' }} 43 | runs-on: ubuntu-latest 44 | needs: build 45 | steps: 46 | - uses: actions/checkout@v3 47 | - name: Set up Java 8 48 | uses: actions/setup-java@v3 49 | with: 50 | distribution: 'zulu' 51 | java-version: 8 52 | server-id: sonatype-nexus-snapshots 53 | server-username: MAVEN_USERNAME 54 | server-password: MAVEN_PASSWORD 55 | - name: Run maven deploy 56 | run: mvn -B -DskipTests=true deploy --no-transfer-progress --file pom.xml 57 | env: 58 | MAVEN_USERNAME: ${{ secrets.SONATYPE_USER }} 59 | MAVEN_PASSWORD: ${{ secrets.SONATYPE_PWD }} 60 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | *.iml 3 | *.iws 4 | *.ipr 5 | .classpath 6 | .settings 7 | .project 8 | bin 9 | .clover 10 | .clover/coverage.db 11 | .idea 12 | atlassian-ide-plugin.xml -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright 2008 the original author or authors. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. -------------------------------------------------------------------------------- /changelog.md: -------------------------------------------------------------------------------- 1 | ## What's new in 2.2.2 2 | * Support for YAML store contributed by [hamnis](//github.com/hamnis) - pull request #48 3 | * Change scope of Constretto-test in the Constretto Spring module to "test" - pull request #47 4 | 5 | ## What's new in 2.2.1 6 | * Support for specifying default values for Spring property placeholders (i.e `${missingValue:default}`). Contributed by [PavlikPolivka](https://github.com/PavlikPolivka) - pull request #46 7 | * Updated Spring dependencies to v. 3.2.11 8 | 9 | ## What's new in 2.2.0 10 | * Improved support for Spring Java Config (issue #41) [example](constretto-spring/src/test/java/org/constretto/spring/ConstrettoJavaConfigTest.java) 11 | * Added support for a simple mapping api method as contributed by [kenglxn](https://github.com/kenglxn) in pull request [#41](https://github.com/constretto/constretto-core/pull/43) 12 | * Upgraded Spring dependency to 3.2.9.RELEASE (4.x will be supported in the next major version) 13 | * Removed direct dependency on commons-logging (the constretto-spring module still has a transitive dependency on it in through its dependency on Spring-Core) 14 | * Upgraded AspectJ to 1.7.4 15 | * Upgraded Commons Beanutils to 1.9.1 16 | * No longer depends on commons-logging. Depends on slf4j-api instead, making it easier to configure your own logging backend 17 | 18 | 19 | ## What's new in 2.1.4 20 | * Change StaticlyCachedConfiguration by adding the SystemPropertiesStore (contributed by @kolstae SHA: 0f0d6eda3a010ca5f9f16afbbd4e2d892ef9d117 ) 21 | 22 | ## What's new in 2.1.3 23 | * Fixed issue reported by @jhberges bad handling of leading characters in values by upgrading GSon. 24 | Be aware if you rely on stripping leading chars in property values as it will no longer work. 25 | * Upgraded jasypt dependency to version 1.9.1 26 | 27 | ## What's new in 2.1.2 28 | * Improved handling of generic fields and method parameter injections (thanks again to @ahaarrestad) 29 | 30 | ## What’s new in 2.1.1 31 | * Bugfix contributed by @ahaarrestad. Resolving properties to Map is finally working :-) 32 | 33 | ## What’s new in 2.1 34 | * Improved support for Junit 4.X after refactoring the [JUnit Rule](https://github.com/junit-team/junit/wiki/Rules) [ConstrettoRule](constretto-test/src/main/java/org/constretto/test/ConstrettoRule.java) added in 2.0.4 to be used as a @ClassRule. 35 | As a consequence the constretto-test-junit4 module has been merged into the constretto-test module. 36 | Look at the [example](#using-the-constrettorule-in-a-junit-test) for details 37 | * LDAP configuration support. You can add configuration either by using DSN or by providing a LDAP search. [Example](#using-the-ldapconfigurationstore) 38 | * NOTE: Constretto will not close or even handle LDAP connection issues for you. The will make it easier to integrate with Spring LDAP or other third-party libraries. 39 | * The development of this feature has been sponsored by NextGenTel AS 40 | * Dropped support for Spring 2.X in favour of the latest Spring 3.2 release. 41 | * The @Configure annotation can now be used on [constructors](#constructor-injection) (w/o Spring) 42 | * The reconfigure() call on the [ConstrettoConfiguration](constretto-api/src/main/java/org/constretto/ConstrettoConfiguration.java) interface has been deprecated as it is not thread-safe. 43 | * It will be completely removed in the next release 44 | * As always: special thanks goes to those who made contributions to this release 45 | -------------------------------------------------------------------------------- /constretto-api/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 12 | constretto 13 | org.constretto 14 | 3.0.0-SNAPSHOT 15 | 16 | 4.0.0 17 | constretto-api 18 | Constretto :: API - ${project.version} 19 | 20 | 21 | commons-lang 22 | commons-lang 23 | 24 | 25 | junit 26 | junit 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /constretto-api/src/main/java/org/constretto/ConfigurationDefaultValueFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto; 17 | 18 | /** 19 | * @author Kaare Nilsen 20 | */ 21 | public interface ConfigurationDefaultValueFactory { 22 | 23 | T getDefaultValue(); 24 | } -------------------------------------------------------------------------------- /constretto-api/src/main/java/org/constretto/GenericConverter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto; 17 | 18 | import org.constretto.exception.ConstrettoConversionException; 19 | import org.constretto.model.CValue; 20 | 21 | /** 22 | * @author Kaare Nilsen 23 | */ 24 | public interface GenericConverter { 25 | 26 | T fromValue(CValue value) throws ConstrettoConversionException; 27 | } 28 | -------------------------------------------------------------------------------- /constretto-api/src/main/java/org/constretto/Property.java: -------------------------------------------------------------------------------- 1 | package org.constretto; 2 | 3 | /** 4 | * @author Kaare Nilsen 5 | */ 6 | public class Property { 7 | private final String key; 8 | private final String value; 9 | 10 | public Property(String key, String value) { 11 | this.value = value; 12 | this.key = key; 13 | } 14 | 15 | public String getKey() { 16 | return key; 17 | } 18 | 19 | public String getValue() { 20 | return value; 21 | } 22 | 23 | @Override 24 | public boolean equals(Object o) { 25 | if (this == o) return true; 26 | if (!(o instanceof Property)) return false; 27 | 28 | Property property = (Property) o; 29 | 30 | if (!key.equals(property.key)) return false; 31 | if (!value.equals(property.value)) return false; 32 | 33 | return true; 34 | } 35 | 36 | @Override 37 | public int hashCode() { 38 | int result = key.hashCode(); 39 | result = 31 * result + value.hashCode(); 40 | return result; 41 | } 42 | 43 | @Override 44 | public String toString() { 45 | return "Property{" + 46 | "key='" + key + '\'' + 47 | ", value='" + value + '\'' + 48 | '}'; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /constretto-api/src/main/java/org/constretto/ValueConverter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto; 17 | 18 | import org.constretto.exception.ConstrettoConversionException; 19 | 20 | import java.util.List; 21 | 22 | /** 23 | * @author Kaare Nilsen 24 | */ 25 | public interface ValueConverter { 26 | 27 | T fromString(String value) throws ConstrettoConversionException; 28 | } 29 | -------------------------------------------------------------------------------- /constretto-api/src/main/java/org/constretto/annotation/Configuration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.annotation; 17 | 18 | import org.constretto.ConfigurationDefaultValueFactory; 19 | 20 | import java.lang.annotation.*; 21 | 22 | /** 23 | * This annotation is picked up by Constretto, and applies to fields that are declared as private, public or default, 24 | * including those inherited from superclasses. 25 | * 26 | * @author Kaare Nilsen 27 | */ 28 | @Target({ElementType.PARAMETER, ElementType.FIELD}) 29 | @Retention(RetentionPolicy.RUNTIME) 30 | @Inherited 31 | public @interface Configuration { 32 | 33 | /** 34 | * The expression of the property to lookup in constretto. 35 | *

36 | * It is important that environment prefixes is not used in this attribute

37 | */ 38 | String value() default ""; 39 | 40 | /** 41 | * A description of the usage of the property. Not directly used in the runtime environment, but may be 42 | * useful for documentation or monitoring situations 43 | */ 44 | String description() default ""; 45 | 46 | /** 47 | * States the default value to be injected if no value found associated for the expression specified in the 48 | * value attribute. 49 | *

50 | * When a default value is set, the required attribute will be ignored.

51 | */ 52 | String defaultValue() default "N/A"; 53 | 54 | /** 55 | * Use when more complex default values needs to be injected of no value found associated with the expression 56 | * specified in the value attribute 57 | */ 58 | Class> defaultValueFactory() default EmptyValueFactory.class; 59 | 60 | /** 61 | * Declares whether it is required to find the specified property key. 62 | *

63 | * Defaults to true.

64 | */ 65 | boolean required() default true; 66 | 67 | // 68 | // helper 69 | // 70 | class EmptyValueFactory implements ConfigurationDefaultValueFactory { 71 | public Object getDefaultValue() { 72 | return null; 73 | } 74 | } 75 | } -------------------------------------------------------------------------------- /constretto-api/src/main/java/org/constretto/annotation/ConfigurationSource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.annotation; 17 | 18 | import java.lang.annotation.*; 19 | 20 | /** 21 | * @author Kaare Nilsen 22 | */ 23 | @Target({ElementType.TYPE, ElementType.FIELD}) 24 | @Retention(RetentionPolicy.RUNTIME) 25 | @Inherited 26 | public @interface ConfigurationSource { 27 | String tag() default ""; 28 | 29 | String basePath() default ""; 30 | } 31 | -------------------------------------------------------------------------------- /constretto-api/src/main/java/org/constretto/annotation/Configure.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.annotation; 17 | 18 | import java.lang.annotation.*; 19 | 20 | /** 21 | * This annotation is picked up by Constretto, and applies to public methods, including 22 | * annotated methods inherited from superclasses. 23 | * 24 | * @author Kaare Nilsen 25 | */ 26 | @Target({ElementType.METHOD, ElementType.CONSTRUCTOR}) 27 | @Retention(RetentionPolicy.RUNTIME) 28 | @Inherited 29 | public @interface Configure { 30 | } -------------------------------------------------------------------------------- /constretto-api/src/main/java/org/constretto/annotation/Tags.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.annotation; 17 | 18 | import java.lang.annotation.*; 19 | 20 | /** 21 | * @author Kaare Nilsen 22 | * @author Thor Åge Eldby 23 | */ 24 | @Target({ElementType.TYPE, ElementType.FIELD}) 25 | @Retention(RetentionPolicy.RUNTIME) 26 | @Inherited 27 | public @interface Tags { 28 | 29 | String[] value() default {}; 30 | 31 | } 32 | -------------------------------------------------------------------------------- /constretto-api/src/main/java/org/constretto/exception/ConstrettoConversionException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.exception; 17 | 18 | /** 19 | * Thrown when a ValueConverter cannot perform conversion succesfully 20 | * 21 | * @author Kaare Nilsen 22 | */ 23 | public class ConstrettoConversionException extends ConstrettoException { 24 | private final String value; 25 | private Class targetClass; 26 | 27 | 28 | public ConstrettoConversionException(String value, Class targetClass, Throwable cause) { 29 | super(cause); 30 | this.value = value; 31 | this.targetClass = targetClass; 32 | } 33 | 34 | public ConstrettoConversionException(String value, Class targetClass, String message) { 35 | super(message); 36 | this.value = value; 37 | this.targetClass = targetClass; 38 | } 39 | } -------------------------------------------------------------------------------- /constretto-api/src/main/java/org/constretto/exception/ConstrettoException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.exception; 17 | 18 | /** 19 | * @author Kaare Nilsen 20 | */ 21 | public class ConstrettoException extends RuntimeException { 22 | 23 | public ConstrettoException(String message, Throwable cause) { 24 | super(message, cause); 25 | } 26 | 27 | public ConstrettoException(String message) { 28 | super(message); 29 | } 30 | 31 | public ConstrettoException(Throwable cause) { 32 | super(cause); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /constretto-api/src/main/java/org/constretto/exception/ConstrettoExpressionException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.exception; 17 | 18 | import java.util.List; 19 | 20 | /** 21 | * Thrown when a expression could not be found, or the expression is illegal in it self. 22 | * 23 | * @author Kaare Nilsen 24 | */ 25 | public class ConstrettoExpressionException extends ConstrettoException { 26 | private final String expression; 27 | private List currentTags; 28 | 29 | public ConstrettoExpressionException(String expression, List currentTags) { 30 | super("Expression [" + expression + "] not found in Configuration using tags " + currentTags); 31 | this.expression = expression; 32 | this.currentTags = currentTags; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /constretto-api/src/main/java/org/constretto/model/CArray.java: -------------------------------------------------------------------------------- 1 | package org.constretto.model; 2 | 3 | import org.apache.commons.lang.StringUtils; 4 | 5 | import java.util.*; 6 | 7 | /** 8 | * @author Kaare Nilsen 9 | */ 10 | public class CArray extends CValue { 11 | private final List data; 12 | 13 | public CArray(final List data) { 14 | 15 | if (data == null) { 16 | throw new NullPointerException("The \"data\" argument can not be null"); 17 | } 18 | this.data = Arrays.asList(data.toArray(new CValue[]{})); 19 | } 20 | 21 | public List data() { 22 | return data == null ? Collections.emptyList() : Collections.unmodifiableList(data); 23 | } 24 | 25 | @Override 26 | public Set referencedKeys() { 27 | Set referencedKeys = new HashSet(); 28 | for (CValue value : data) { 29 | if (value != null) { 30 | referencedKeys.addAll(value.referencedKeys()); 31 | } 32 | } 33 | return referencedKeys; 34 | } 35 | 36 | @Override 37 | public void replace(String key, String resolvedValue) { 38 | for (CValue value : data) { 39 | if (value != null) { 40 | value.replace(key, resolvedValue); 41 | } 42 | } 43 | } 44 | 45 | @Override 46 | public boolean equals(final Object o) { 47 | if (this == o) return true; 48 | if (o == null || getClass() != o.getClass()) return false; 49 | 50 | final CArray cArray = (CArray) o; 51 | 52 | if (data != null ? !data.equals(cArray.data) : cArray.data != null) return false; 53 | 54 | return true; 55 | } 56 | 57 | @Override 58 | public int hashCode() { 59 | return data != null ? data.hashCode() : 0; 60 | } 61 | 62 | @Override 63 | public String toString() { 64 | return "[" + StringUtils.join(data, ',') + "]"; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /constretto-api/src/main/java/org/constretto/model/CObject.java: -------------------------------------------------------------------------------- 1 | package org.constretto.model; 2 | 3 | import org.apache.commons.lang.StringUtils; 4 | 5 | import java.util.*; 6 | 7 | /** 8 | * @author Kaare Nilsen 9 | */ 10 | public class CObject extends CValue { 11 | private final Map data; 12 | 13 | public CObject(Map data) { 14 | 15 | if (data == null) { 16 | throw new NullPointerException("The \"data\" argument can not be null"); 17 | } 18 | this.data = data; 19 | } 20 | 21 | public Map data() { 22 | return data == null ? Collections.emptyMap() : Collections.unmodifiableMap(data); 23 | } 24 | 25 | @Override 26 | public Set referencedKeys() { 27 | Set referencedKeys = new HashSet(); 28 | for (CValue value : data.values()) { 29 | if (value != null) { 30 | referencedKeys.addAll(value.referencedKeys()); 31 | } 32 | } 33 | return referencedKeys; 34 | } 35 | 36 | @Override 37 | public void replace(String key, String resolvedValue) { 38 | for (CValue value : data.values()) { 39 | if (value != null) { 40 | value.replace(key, resolvedValue); 41 | } 42 | } 43 | } 44 | 45 | @Override 46 | public String toString() { 47 | StringBuilder stringBuilder = new StringBuilder("{"); 48 | final int length = data.entrySet().size(); 49 | int elementsAdded = 0; 50 | for (Map.Entry entry : data.entrySet()) { 51 | stringBuilder.append(entry.getKey()); 52 | stringBuilder.append(':'); 53 | stringBuilder.append(entry.getValue() == null ? "null" : entry.getValue().toString()); 54 | elementsAdded++; 55 | if (elementsAdded < length) { 56 | stringBuilder.append(", "); 57 | } 58 | } 59 | return stringBuilder.append("}").toString(); 60 | } 61 | 62 | @Override 63 | public boolean equals(final Object o) { 64 | if (this == o) return true; 65 | if (o == null || getClass() != o.getClass()) return false; 66 | 67 | final CObject cObject = (CObject) o; 68 | 69 | if (data != null ? !data.equals(cObject.data) : cObject.data != null) return false; 70 | 71 | return true; 72 | } 73 | 74 | @Override 75 | public int hashCode() { 76 | return data != null ? data.hashCode() : 0; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /constretto-api/src/main/java/org/constretto/model/CPrimitive.java: -------------------------------------------------------------------------------- 1 | package org.constretto.model; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | import java.util.regex.Matcher; 6 | import java.util.regex.Pattern; 7 | 8 | /** 9 | * @author Kaare Nilsen 10 | */ 11 | public class CPrimitive extends CValue { 12 | private static final Pattern VARIABLE_PATTERN = Pattern.compile("#\\{(.*?)}"); 13 | private String value; 14 | 15 | public CPrimitive(String value) { 16 | 17 | if(value == null) { 18 | throw new NullPointerException("The \"value\" argument can not be null"); 19 | } 20 | this.value = value; 21 | } 22 | 23 | public String value() { 24 | return value; 25 | } 26 | 27 | @Override 28 | public Set referencedKeys() { 29 | Set referencedKeys = new HashSet(); 30 | Matcher matcher = VARIABLE_PATTERN.matcher(value); 31 | while (matcher.find()) { 32 | String group = matcher.group(1); 33 | referencedKeys.add(group); 34 | } 35 | return referencedKeys; 36 | } 37 | 38 | @Override 39 | public void replace(String key, String resolvedValue) { 40 | if (value != null) { 41 | value = value.replaceAll("#\\{" + key + "\\}", Matcher.quoteReplacement(resolvedValue)); 42 | } 43 | } 44 | 45 | @Override 46 | public String toString() { 47 | return value; 48 | } 49 | 50 | @Override 51 | public boolean equals(final Object o) { 52 | 53 | if (this == o) return true; 54 | if (o == null || getClass() != o.getClass()) return false; 55 | 56 | final CPrimitive that = (CPrimitive) o; 57 | 58 | if (value != null ? !value.equals(that.value) : that.value != null) return false; 59 | 60 | return true; 61 | } 62 | 63 | @Override 64 | public int hashCode() { 65 | 66 | return value != null ? value.hashCode() : 0; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /constretto-api/src/main/java/org/constretto/model/CValue.java: -------------------------------------------------------------------------------- 1 | package org.constretto.model; 2 | 3 | import java.util.Set; 4 | 5 | /** 6 | * @author Kaare Nilsen 7 | */ 8 | public abstract class CValue { 9 | 10 | public abstract Set referencedKeys(); 11 | 12 | public boolean containsVariables() { 13 | return !referencedKeys().isEmpty(); 14 | } 15 | 16 | public boolean isArray(){ 17 | return this instanceof CArray; 18 | } 19 | 20 | public boolean isObject(){ 21 | return this instanceof CObject; 22 | } 23 | 24 | public boolean isPrimitive(){ 25 | return this instanceof CPrimitive; 26 | } 27 | 28 | public abstract void replace(String key, String resolvedValue); 29 | 30 | 31 | } 32 | -------------------------------------------------------------------------------- /constretto-api/src/main/java/org/constretto/resolver/ConfigurationContextResolver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.resolver; 17 | 18 | import java.util.List; 19 | 20 | /** 21 | * @author Kristoffer Moum 22 | */ 23 | public interface ConfigurationContextResolver { 24 | 25 | List getTags(); 26 | 27 | } 28 | -------------------------------------------------------------------------------- /constretto-api/src/test/java/org/constretto/model/CArrayTest.java: -------------------------------------------------------------------------------- 1 | package org.constretto.model; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | 6 | import java.util.Arrays; 7 | 8 | import static org.junit.Assert.assertArrayEquals; 9 | import static org.junit.Assert.assertEquals; 10 | import static org.junit.Assert.assertNotNull; 11 | 12 | /** 13 | * @author zapodot at gmail dot com 14 | */ 15 | public class CArrayTest { 16 | 17 | public static final String VALUE_ONE = "1"; 18 | public static final String VALUE_TWO = "2"; 19 | public static final CPrimitive PRIMITIVE_ONE = new CPrimitive(VALUE_ONE); 20 | public static final CPrimitive PRIMITIVE_TWO = new CPrimitive(VALUE_TWO); 21 | private CArray cArray; 22 | 23 | @Before 24 | public void setUp() throws Exception { 25 | cArray = new CArray(Arrays.asList(PRIMITIVE_ONE, PRIMITIVE_TWO)); 26 | } 27 | 28 | @Test 29 | public void testData() throws Exception { 30 | assertArrayEquals(new CValue[]{PRIMITIVE_ONE, PRIMITIVE_TWO}, cArray.data().toArray(new CValue[]{})); 31 | } 32 | 33 | @Test 34 | public void testReferencedKeys() throws Exception { 35 | assertEquals(0, cArray.referencedKeys().size()); 36 | } 37 | 38 | @Test(expected = NullPointerException.class) 39 | public void testNull() throws Exception { 40 | new CArray(null); 41 | } 42 | 43 | 44 | @Test 45 | public void testReplace() throws Exception { 46 | final CArray arrayWithKey = new CArray(Arrays.asList(new CPrimitive("#{key}"))); 47 | assertEquals(1, arrayWithKey.referencedKeys().size()); 48 | arrayWithKey.replace("key", VALUE_ONE); 49 | assertArrayEquals(new CValue[]{PRIMITIVE_ONE}, arrayWithKey.data().toArray(new CValue[]{})); 50 | 51 | 52 | } 53 | 54 | @Test 55 | public void testToString() throws Exception { 56 | 57 | assertEquals("[" + VALUE_ONE + "," + VALUE_TWO + "]", cArray.toString()); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /constretto-api/src/test/java/org/constretto/model/CObjectTest.java: -------------------------------------------------------------------------------- 1 | package org.constretto.model; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | 6 | import java.util.LinkedHashMap; 7 | import java.util.Map; 8 | 9 | import static org.junit.Assert.assertEquals; 10 | 11 | /** 12 | * @author zapodot at gmail dot com 13 | */ 14 | public class CObjectTest { 15 | 16 | private CObject cObject; 17 | private Map values; 18 | 19 | @Before 20 | public void setUp() throws Exception { 21 | values = new LinkedHashMap(); 22 | values.put("key1", new CPrimitive("1")); 23 | values.put("key2", new CPrimitive("2")); 24 | this.cObject = new CObject(values); 25 | 26 | } 27 | 28 | @Test 29 | public void testAllOperations() throws Exception { 30 | 31 | final CObject cObject = this.cObject; 32 | assertEquals(values, cObject.data()); 33 | assertEquals("{key1:1, key2:2}", cObject.toString()); 34 | } 35 | 36 | @Test 37 | public void testEquals() throws Exception { 38 | assertEquals(cObject, new CObject(values)); 39 | 40 | } 41 | 42 | @Test 43 | public void testHashCode() throws Exception { 44 | assertEquals(cObject.hashCode(), new CObject(values).hashCode()); 45 | 46 | } 47 | 48 | @Test(expected = NullPointerException.class) 49 | public void testNull() throws Exception { 50 | new CArray(null); 51 | 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /constretto-api/src/test/java/org/constretto/model/CPrimitiveTest.java: -------------------------------------------------------------------------------- 1 | package org.constretto.model; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.assertEquals; 6 | 7 | public final class CPrimitiveTest { 8 | @Test 9 | public void replace() throws Exception { 10 | final String key = "my-key"; 11 | final CPrimitive primitive = new CPrimitive("#{" + key + "}"); 12 | final String expectedValue = "C:\\temp\\"; 13 | 14 | primitive.replace(key, expectedValue); 15 | 16 | assertEquals(expectedValue, primitive.value()); 17 | } 18 | 19 | @Test 20 | public void testEquals() throws Exception { 21 | 22 | assertEquals(new CPrimitive("1"), new CPrimitive("1")); 23 | 24 | } 25 | 26 | @Test(expected = NullPointerException.class) 27 | public void testNull() throws Exception { 28 | new CPrimitive(null); 29 | 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/ConfigurationStore.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto; 17 | 18 | import org.constretto.model.TaggedPropertySet; 19 | 20 | import java.util.Collection; 21 | 22 | /** 23 | * @author Kaare Nilsen 24 | */ 25 | public interface ConfigurationStore { 26 | 27 | Collection parseConfiguration(); 28 | } 29 | -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/internal/converter/BooleanValueConverter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.converter; 17 | 18 | import org.constretto.ValueConverter; 19 | import org.constretto.exception.ConstrettoConversionException; 20 | 21 | import java.util.HashSet; 22 | import java.util.Set; 23 | 24 | /** 25 | * @author Kaare Nilsen 26 | */ 27 | public class BooleanValueConverter implements ValueConverter { 28 | 29 | private static Set validStrings = new HashSet() {{ 30 | add("true"); 31 | add("false"); 32 | }}; 33 | 34 | public Boolean fromString(String value) throws ConstrettoConversionException { 35 | if (!validStrings.contains(value.toLowerCase())) { 36 | throw new ConstrettoConversionException(value, Boolean.class, "valid values are \"true\" and \"false\" ignoring case."); 37 | } 38 | return Boolean.valueOf(value); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/internal/converter/ByteValueConverter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.converter; 17 | 18 | import org.constretto.ValueConverter; 19 | import org.constretto.exception.ConstrettoConversionException; 20 | 21 | /** 22 | * @author Kaare Nilsen 23 | */ 24 | public class ByteValueConverter implements ValueConverter { 25 | 26 | public Byte fromString(String value) throws ConstrettoConversionException { 27 | try { 28 | return Byte.parseByte(value); 29 | } catch (NumberFormatException e) { 30 | throw new ConstrettoConversionException(value, Byte.class, e); 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/internal/converter/DoubleValueConverter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.converter; 17 | 18 | import org.constretto.ValueConverter; 19 | import org.constretto.exception.ConstrettoConversionException; 20 | 21 | /** 22 | * @author Kaare Nilsen 23 | */ 24 | public class DoubleValueConverter implements ValueConverter { 25 | 26 | public Double fromString(String value) throws ConstrettoConversionException { 27 | try { 28 | return Double.parseDouble(value); 29 | } catch (NumberFormatException e) { 30 | throw new ConstrettoConversionException(value, Double.class, e); 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/internal/converter/FileValueConverter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.converter; 17 | 18 | import org.constretto.ValueConverter; 19 | import org.constretto.exception.ConstrettoConversionException; 20 | 21 | import java.io.File; 22 | 23 | /** 24 | * @author trygvis 25 | */ 26 | public class FileValueConverter implements ValueConverter { 27 | private final File basedir; 28 | private final boolean convertToAbsolute; 29 | 30 | public FileValueConverter() { 31 | this(null); 32 | } 33 | 34 | public FileValueConverter(File basedir) { 35 | this(basedir, false); 36 | } 37 | 38 | public FileValueConverter(File basedir, boolean convertToAbsolute) { 39 | this.basedir = basedir; 40 | this.convertToAbsolute = convertToAbsolute; 41 | } 42 | 43 | public File fromString(String value) throws ConstrettoConversionException { 44 | File f = new File(value); 45 | 46 | if (basedir == null) { 47 | return convertToAbsolute(f); 48 | } 49 | 50 | if (f.isAbsolute()) { 51 | return f; 52 | } 53 | 54 | return convertToAbsolute(new File(basedir, value)); 55 | } 56 | 57 | private File convertToAbsolute(File f) { 58 | return convertToAbsolute ? f.getAbsoluteFile() : f; 59 | } 60 | } -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/internal/converter/FloatValueConverter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.converter; 17 | 18 | import org.constretto.ValueConverter; 19 | import org.constretto.exception.ConstrettoConversionException; 20 | 21 | /** 22 | * @author Kaare Nilsen 23 | */ 24 | public class FloatValueConverter implements ValueConverter { 25 | 26 | public Float fromString(String value) throws ConstrettoConversionException { 27 | try { 28 | return Float.parseFloat(value); 29 | } catch (NumberFormatException e) { 30 | throw new ConstrettoConversionException(value, Float.class, e); 31 | } 32 | } 33 | 34 | } -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/internal/converter/InetAddressValueConverter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.converter; 17 | 18 | import org.constretto.ValueConverter; 19 | import org.constretto.exception.ConstrettoConversionException; 20 | 21 | import java.net.InetAddress; 22 | import java.net.UnknownHostException; 23 | 24 | /** 25 | * @author trygvis 26 | */ 27 | public class InetAddressValueConverter implements ValueConverter { 28 | 29 | public InetAddress fromString(String value) throws ConstrettoConversionException { 30 | try { 31 | return InetAddress.getByName(value); 32 | } catch (UnknownHostException e) { 33 | throw new ConstrettoConversionException(value, InetAddress.class, e); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/internal/converter/InputStreamValueConverter.java: -------------------------------------------------------------------------------- 1 | package org.constretto.internal.converter; 2 | 3 | import org.constretto.ValueConverter; 4 | import org.constretto.exception.ConstrettoConversionException; 5 | import org.constretto.model.Resource; 6 | 7 | import java.io.InputStream; 8 | 9 | /** 10 | * @author Thor Åge Eldby (teldby) 11 | */ 12 | public class InputStreamValueConverter implements ValueConverter { 13 | 14 | public InputStream fromString(String resourceName) throws ConstrettoConversionException { 15 | return Resource.create(resourceName).getInputStream(); 16 | } 17 | } -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/internal/converter/IntegerValueConverter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.converter; 17 | 18 | import org.constretto.ValueConverter; 19 | import org.constretto.exception.ConstrettoConversionException; 20 | 21 | /** 22 | * @author Kaare Nilsen 23 | */ 24 | public class IntegerValueConverter implements ValueConverter { 25 | 26 | public Integer fromString(String value) throws ConstrettoConversionException { 27 | try { 28 | return Integer.parseInt(value); 29 | } catch (NumberFormatException e) { 30 | throw new ConstrettoConversionException(value, Integer.class, e); 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/internal/converter/LocaleValueConverter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.converter; 17 | 18 | import org.constretto.ValueConverter; 19 | import org.constretto.exception.ConstrettoConversionException; 20 | import org.constretto.internal.ConstrettoUtils; 21 | 22 | import java.util.Locale; 23 | 24 | /** 25 | * @author Kaare Nilsen 26 | */ 27 | public class LocaleValueConverter implements ValueConverter { 28 | public Locale fromString(String value) throws ConstrettoConversionException { 29 | try { 30 | return ConstrettoUtils.toLocale(value); 31 | } catch (IllegalArgumentException e) { 32 | throw new ConstrettoConversionException(value, Locale.class, e); 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/internal/converter/LongValueConverter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.converter; 17 | 18 | import org.constretto.ValueConverter; 19 | import org.constretto.exception.ConstrettoConversionException; 20 | 21 | /** 22 | * @author Kaare Nilsen 23 | */ 24 | public class LongValueConverter implements ValueConverter { 25 | 26 | public Long fromString(String value) throws ConstrettoConversionException { 27 | try { 28 | return Long.decode(value); 29 | } catch (NumberFormatException e) { 30 | throw new ConstrettoConversionException(value, Long.class, e); 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/internal/converter/PropertyFileValueConverter.java: -------------------------------------------------------------------------------- 1 | package org.constretto.internal.converter; 2 | 3 | import org.constretto.ValueConverter; 4 | import org.constretto.exception.ConstrettoConversionException; 5 | import org.constretto.model.Resource; 6 | 7 | import java.io.IOException; 8 | import java.io.InputStream; 9 | import java.util.Properties; 10 | 11 | /** 12 | * @author Thor Åge Eldby (teldby) 13 | */ 14 | public class PropertyFileValueConverter implements ValueConverter { 15 | 16 | public Properties fromString(String resourceName) throws ConstrettoConversionException { 17 | try { 18 | Properties properties = new Properties(); 19 | InputStream stream = Resource.create(resourceName).getInputStream(); 20 | if (resourceName.endsWith(".xml")) { 21 | properties.loadFromXML(stream); 22 | } else { 23 | properties.load(stream); 24 | } 25 | return properties; 26 | } catch (IOException e) { 27 | throw new ConstrettoConversionException(resourceName, Properties.class, e); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/internal/converter/ShortValueConverter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.converter; 17 | 18 | import org.constretto.ValueConverter; 19 | import org.constretto.exception.ConstrettoConversionException; 20 | 21 | /** 22 | * @author Kaare Nilsen 23 | */ 24 | public class ShortValueConverter implements ValueConverter { 25 | 26 | public Short fromString(String value) throws ConstrettoConversionException { 27 | try { 28 | return Short.parseShort(value); 29 | } catch (NumberFormatException e) { 30 | throw new ConstrettoConversionException(value, Short.class, e); 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/internal/converter/StringValueConverter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.converter; 17 | 18 | import org.constretto.ValueConverter; 19 | import org.constretto.exception.ConstrettoConversionException; 20 | 21 | /** 22 | * @author Kaare Nilsen 23 | */ 24 | public class StringValueConverter implements ValueConverter { 25 | 26 | public String fromString(String value) throws ConstrettoConversionException { 27 | return value; 28 | } 29 | } -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/internal/converter/UriValueConverter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.converter; 17 | 18 | import org.constretto.ValueConverter; 19 | import org.constretto.exception.ConstrettoConversionException; 20 | 21 | import java.net.URI; 22 | import java.net.URISyntaxException; 23 | import java.net.URL; 24 | 25 | /** 26 | * @author Tom Palmer 27 | */ 28 | public class UriValueConverter implements ValueConverter { 29 | 30 | public URI fromString(String value) throws ConstrettoConversionException { 31 | try { 32 | return new URI(value); 33 | } catch (URISyntaxException e) { 34 | throw new ConstrettoConversionException(value, URL.class, e.getMessage()); 35 | } 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/internal/converter/UrlValueConverter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.converter; 17 | 18 | import org.constretto.ValueConverter; 19 | import org.constretto.exception.ConstrettoConversionException; 20 | 21 | import java.net.MalformedURLException; 22 | import java.net.URL; 23 | 24 | /** 25 | * @author trygvis 26 | */ 27 | public class UrlValueConverter implements ValueConverter { 28 | 29 | public URL fromString(String value) throws ConstrettoConversionException { 30 | try { 31 | return new URL(value); 32 | } catch (MalformedURLException e) { 33 | throw new ConstrettoConversionException(value, URL.class, e.getMessage()); 34 | } 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/internal/introspect/ArgumentDescription.java: -------------------------------------------------------------------------------- 1 | package org.constretto.internal.introspect; 2 | 3 | import org.constretto.annotation.Configuration; 4 | 5 | import java.lang.annotation.Annotation; 6 | import java.util.Arrays; 7 | import java.util.Collections; 8 | import java.util.List; 9 | 10 | /** 11 | * Parameter name 12 | * 13 | * @author Sondre Eikanger Kvalø 14 | */ 15 | public class ArgumentDescription { 16 | 17 | private final String name; 18 | private final List annotations; 19 | private final Class type; 20 | 21 | public ArgumentDescription(final String name, final Annotation[] annotations, final Class type) { 22 | this.name = name; 23 | 24 | this.annotations = annotations == null || annotations.length == 0 ? Collections.emptyList() : Arrays.asList(annotations); 25 | this.type = type; 26 | } 27 | 28 | public String getName() { 29 | return name; 30 | } 31 | 32 | public Annotation[] getAnnotations() { 33 | return annotations.toArray(new Annotation[]{}); 34 | } 35 | 36 | public Class getType() { 37 | return type; 38 | } 39 | 40 | public String constrettoConfigurationKeyCandidate() { 41 | Configuration configuration = findConfigurationParameter(); 42 | if(configuration != null && ! configuration.value().isEmpty()) { 43 | return configuration.value(); 44 | } else { 45 | return getName(); 46 | } 47 | } 48 | 49 | private Configuration findConfigurationParameter() { 50 | for(Annotation annotation: annotations) { 51 | if(annotation instanceof Configuration) { 52 | return (Configuration) annotation; 53 | } 54 | } 55 | return null; 56 | } 57 | 58 | @Override 59 | public boolean equals(final Object o) { 60 | if (this == o) return true; 61 | if (o == null || getClass() != o.getClass()) return false; 62 | 63 | final ArgumentDescription that = (ArgumentDescription) o; 64 | 65 | if (annotations != null ? !annotations.equals(that.annotations) : that.annotations != null) return false; 66 | if (name != null ? !name.equals(that.name) : that.name != null) return false; 67 | if (type != null ? !type.equals(that.type) : that.type != null) return false; 68 | 69 | return true; 70 | } 71 | 72 | @Override 73 | public int hashCode() { 74 | int result = name != null ? name.hashCode() : 0; 75 | result = 31 * result + (annotations != null ? annotations.hashCode() : 0); 76 | result = 31 * result + (type != null ? type.hashCode() : 0); 77 | return result; 78 | } 79 | 80 | @Override 81 | public String toString() { 82 | final StringBuilder sb = new StringBuilder("ArgumentDescription{"); 83 | sb.append("name='").append(name).append('\''); 84 | sb.append(", annotations=").append(annotations); 85 | sb.append(", type=").append(type); 86 | sb.append('}'); 87 | return sb.toString(); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/internal/introspect/ArgumentDescriptionFactory.java: -------------------------------------------------------------------------------- 1 | package org.constretto.internal.introspect; 2 | 3 | import com.thoughtworks.paranamer.BytecodeReadingParanamer; 4 | import com.thoughtworks.paranamer.Paranamer; 5 | 6 | import java.lang.annotation.Annotation; 7 | import java.lang.reflect.AccessibleObject; 8 | import java.lang.reflect.Constructor; 9 | import java.lang.reflect.Method; 10 | 11 | /** 12 | * Utility for resolving arguments 13 | * 14 | * @author Sondre Eikanger Kvalø 15 | */ 16 | public class ArgumentDescriptionFactory { 17 | 18 | private final Paranamer paranamer = new BytecodeReadingParanamer(); 19 | private final A accessibleObject; 20 | private final Annotation[][] annotations; 21 | private final Class[] parameterTypes; 22 | 23 | private ArgumentDescriptionFactory(A accessibleObject, Annotation[][] annotations, Class[] parameterTypes) { 24 | this.accessibleObject = accessibleObject; 25 | this.annotations = annotations; 26 | this.parameterTypes = parameterTypes; 27 | 28 | } 29 | 30 | public ArgumentDescription[] resolveParameters() { 31 | final String[] names = paranamer.lookupParameterNames(accessibleObject); 32 | ArgumentDescription[] descriptions = new ArgumentDescription[names.length]; 33 | for(int i = 0; i < names.length; i++) { 34 | descriptions[i] = new ArgumentDescription(names[i], annotations[i], parameterTypes[i]); 35 | } 36 | return descriptions; 37 | } 38 | 39 | public static ArgumentDescriptionFactory create(final Constructor constructor) { 40 | return new ArgumentDescriptionFactory(constructor, constructor.getParameterAnnotations(), constructor.getParameterTypes()); 41 | } 42 | 43 | public static ArgumentDescriptionFactory forMethod(final Method method) { 44 | return new ArgumentDescriptionFactory(method, method.getParameterAnnotations(), method.getParameterTypes()); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/internal/introspect/Constructors.java: -------------------------------------------------------------------------------- 1 | package org.constretto.internal.introspect; 2 | 3 | import org.constretto.annotation.Configure; 4 | 5 | import java.lang.annotation.Annotation; 6 | import java.lang.reflect.Constructor; 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | 10 | /** 11 | * Utility for working with constructors 12 | * @author Sondre Eikanger Kvalø 13 | */ 14 | public class Constructors { 15 | 16 | public static Constructor[] findConstructorsWithAnnotation(Class clazz, Class annotation) { 17 | if(clazz == null) { 18 | return null; 19 | } 20 | Constructor[] constructors = clazz.getConstructors(); 21 | List> annotatedConstructors = new ArrayList>(); 22 | for(Constructor constructor: constructors) { 23 | if(constructor.isAnnotationPresent(annotation)) { 24 | annotatedConstructors.add(constructor); 25 | } 26 | } 27 | return annotatedConstructors.isEmpty() ? null : annotatedConstructors.toArray(new Constructor[]{}); 28 | } 29 | 30 | public static Constructor[] findConstructorsWithConfigureAnnotation(Class clazz) { 31 | return findConstructorsWithAnnotation(clazz, Configure.class); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/internal/resolver/DefaultConfigurationContextResolver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.resolver; 17 | 18 | import org.constretto.resolver.ConfigurationContextResolver; 19 | 20 | import java.util.ArrayList; 21 | import java.util.Arrays; 22 | import java.util.Collections; 23 | import java.util.List; 24 | 25 | /** 26 | * -DCONSTRETTO_TAGS=a,b,c,d 27 | * 28 | * @author Kristoffer Moum 29 | */ 30 | public class DefaultConfigurationContextResolver implements ConfigurationContextResolver { 31 | 32 | public static final String TAGS = "CONSTRETTO_TAGS"; 33 | 34 | public List getTags() { 35 | String tags = getFromSystemPropertyOrSystemEnv(); 36 | if (tags != null) { 37 | return new ArrayList() { 38 | { 39 | addAll(Arrays.asList(getFromSystemPropertyOrSystemEnv().split(","))); 40 | } 41 | }; 42 | } else { 43 | return Collections.emptyList(); 44 | } 45 | } 46 | 47 | private String getFromSystemPropertyOrSystemEnv() { 48 | String assemblyEnvironment = System.getProperty(TAGS); 49 | if (assemblyEnvironment == null) { 50 | assemblyEnvironment = System.getenv(TAGS); 51 | } 52 | return assemblyEnvironment; 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/internal/store/EncryptedPropertiesStore.java: -------------------------------------------------------------------------------- 1 | package org.constretto.internal.store; 2 | 3 | import org.jasypt.encryption.pbe.PBEStringEncryptor; 4 | import org.jasypt.encryption.pbe.StandardPBEStringEncryptor; 5 | import org.jasypt.properties.EncryptableProperties; 6 | 7 | import java.util.Properties; 8 | 9 | /** 10 | * Has support for jasypt encrypted properties. 11 | * See Jasypt CLI and Jasypt Configuration Files 12 | * 13 | * @author Ole-Martin Mørk (olemartin@openadex.com) 14 | */ 15 | public class EncryptedPropertiesStore extends PropertiesStore { 16 | private final PBEStringEncryptor encryptor; 17 | 18 | /** 19 | * Creates a new instance 20 | * 21 | * @param passwordProperty the name of the system property to read from 22 | */ 23 | public EncryptedPropertiesStore(String passwordProperty) { 24 | encryptor = new StandardPBEStringEncryptor(); 25 | encryptor.setPassword(getFromSystemPropertyOrSystemEnv(passwordProperty)); 26 | } 27 | 28 | 29 | private String getFromSystemPropertyOrSystemEnv(String key) { 30 | String password = System.getProperty(key); 31 | if (password == null) { 32 | password = System.getenv(key); 33 | } 34 | return password; 35 | } 36 | 37 | /** 38 | * Uses jasypt to parse the properties 39 | * 40 | * @param props the properties currently read 41 | * @return an instance of {@link org.jasypt.properties.EncryptableProperties} 42 | */ 43 | @Override 44 | protected Properties parseProperties(Properties props) { 45 | return new EncryptableProperties(props, encryptor); 46 | } 47 | } -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/internal/store/IniFileConfigurationStore.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.store; 17 | 18 | import org.constretto.ConfigurationStore; 19 | import org.constretto.exception.ConstrettoException; 20 | import org.constretto.model.ConfigurationValue; 21 | import org.constretto.model.Resource; 22 | import org.constretto.model.TaggedPropertySet; 23 | import org.ini4j.IniPreferences; 24 | 25 | import java.util.*; 26 | import java.util.prefs.BackingStoreException; 27 | import java.util.prefs.Preferences; 28 | 29 | /** 30 | * @author Kaare Nilsen 31 | * @author Kristoffer Moum 32 | */ 33 | public class IniFileConfigurationStore implements ConfigurationStore { 34 | private static final String DEFAULT_TAG = "default"; 35 | private List resources = new ArrayList(); 36 | 37 | public IniFileConfigurationStore() { 38 | } 39 | 40 | private IniFileConfigurationStore(List resources) { 41 | this.resources = resources; 42 | } 43 | 44 | public IniFileConfigurationStore addResource(Resource resource) { 45 | resources.add(resource); 46 | return new IniFileConfigurationStore(resources); 47 | } 48 | 49 | public List parseConfiguration() { 50 | List taggedPropertySets = new ArrayList(); 51 | for (Resource r : resources) { 52 | if (r.exists()) { 53 | Preferences prefs = load(r); 54 | List tags = getChildren(prefs); 55 | for (String tag : tags) { 56 | Preferences node = prefs.node(tag); 57 | List keysPerNode = getKeys(node); 58 | Map properties = new HashMap(); 59 | 60 | for (String key : keysPerNode) { 61 | String value = node.get(key, null); 62 | properties.put(key, value); 63 | } 64 | if (tag.equals(DEFAULT_TAG)) { 65 | tag = ConfigurationValue.DEFAULT_TAG; 66 | } 67 | TaggedPropertySet taggedPropertySet = new TaggedPropertySet(tag, properties, getClass()); 68 | taggedPropertySets.add(taggedPropertySet); 69 | } 70 | } 71 | } 72 | return taggedPropertySets; 73 | } 74 | 75 | private List getKeys(Preferences p) { 76 | try { 77 | return Arrays.asList(p.keys()); 78 | } catch (BackingStoreException e) { 79 | throw new ConstrettoException(e); 80 | } 81 | } 82 | 83 | private List getChildren(Preferences p) { 84 | try { 85 | return Arrays.asList(p.childrenNames()); 86 | } catch (BackingStoreException e) { 87 | throw new ConstrettoException(e); 88 | } 89 | } 90 | 91 | private Preferences load(Resource resource) { 92 | try { 93 | return new IniPreferences(resource.getInputStream()); 94 | } catch (Exception e) { 95 | throw new ConstrettoException(e); 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/internal/store/JsonStore.java: -------------------------------------------------------------------------------- 1 | package org.constretto.internal.store; 2 | 3 | import org.constretto.ConfigurationStore; 4 | import org.constretto.exception.ConstrettoException; 5 | import org.constretto.model.Resource; 6 | import org.constretto.model.TaggedPropertySet; 7 | 8 | import java.io.IOException; 9 | import java.io.InputStream; 10 | import java.nio.charset.Charset; 11 | import java.util.*; 12 | 13 | import static java.util.Collections.addAll; 14 | 15 | public class JsonStore implements ConfigurationStore { 16 | public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); 17 | private Map resources; 18 | 19 | public JsonStore() { 20 | resources = new HashMap(); 21 | } 22 | 23 | private JsonStore(JsonStore old, String key, TaggedResource resource) { 24 | resources = new HashMap(); 25 | resources.putAll(old.resources); 26 | resources.put(key, resource); 27 | } 28 | 29 | public JsonStore addResource(Resource resource, String key, String... tags) { 30 | List tagList = new ArrayList(); 31 | addAll(tagList, tags); 32 | return new JsonStore(this, key, new TaggedResource(resource, tagList)); 33 | } 34 | 35 | public Collection parseConfiguration() { 36 | List properties = new ArrayList(); 37 | for (Map.Entry entry : resources.entrySet()) { 38 | TaggedResource taggedResource = entry.getValue(); 39 | if (taggedResource.resource.exists()) { 40 | if (taggedResource.tags.isEmpty()) { 41 | HashMap property = new HashMap(); 42 | property.put(entry.getKey(), readJson(taggedResource.resource)); 43 | properties.add(new TaggedPropertySet(property, JsonStore.class)); 44 | } else { 45 | for (String tag : taggedResource.tags) { 46 | HashMap property = new HashMap(); 47 | property.put(entry.getKey(), readJson(taggedResource.resource)); 48 | properties.add(new TaggedPropertySet(tag, property, JsonStore.class)); 49 | } 50 | } 51 | } 52 | } 53 | return properties; 54 | } 55 | 56 | private String readJson(Resource resource) { 57 | try { 58 | StringBuilder out = new StringBuilder(); 59 | byte[] b = new byte[4096]; 60 | InputStream inputStream = resource.getInputStream(); 61 | for (int n; (n = inputStream.read(b)) != -1; ) { 62 | out.append(new String(b, 0, n, DEFAULT_CHARSET)); 63 | } 64 | return out.toString(); 65 | } catch (IOException e) { 66 | throw new ConstrettoException("Could not read json file", e); 67 | } 68 | 69 | } 70 | 71 | private static class TaggedResource { 72 | public final Resource resource; 73 | public final List tags = new ArrayList(); 74 | 75 | private TaggedResource(Resource resource, List tags) { 76 | this.tags.addAll(tags); 77 | this.resource = resource; 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/internal/store/NestedConfigurationStore.java: -------------------------------------------------------------------------------- 1 | package org.constretto.internal.store; 2 | 3 | import org.constretto.ConfigurationStore; 4 | import org.constretto.ConstrettoConfiguration; 5 | import org.constretto.model.TaggedPropertySet; 6 | 7 | import java.util.Arrays; 8 | import java.util.Collection; 9 | 10 | /** 11 | * A special kind of ConfigurationStore that contains a nested ConstrettoConfiguration instance. 12 | * 13 | * @author zapodot 14 | * @since 3.0 15 | */ 16 | public class NestedConfigurationStore implements ConfigurationStore { 17 | 18 | private ConstrettoConfiguration configuration; 19 | 20 | public NestedConfigurationStore(final ConstrettoConfiguration configuration) { 21 | this.configuration = configuration; 22 | } 23 | 24 | @Override 25 | public Collection parseConfiguration() { 26 | 27 | return Arrays.asList(new TaggedPropertySet(configuration.asMap(), getClass())); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/internal/store/SystemPropertiesStore.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.store; 17 | 18 | import org.constretto.ConfigurationStore; 19 | import org.constretto.model.ConfigurationValue; 20 | import org.constretto.model.TaggedPropertySet; 21 | 22 | import java.util.*; 23 | 24 | import static java.lang.System.getProperties; 25 | import static java.lang.System.getProperty; 26 | 27 | /** 28 | * @author Kaare Nilsen 29 | */ 30 | public class SystemPropertiesStore implements ConfigurationStore { 31 | 32 | public List parseConfiguration() { 33 | final Map properties = new HashMap(); 34 | Properties systemProperties = getProperties(); 35 | Set> systemEnv = System.getenv().entrySet(); 36 | for (Map.Entry envEntry : systemEnv) { 37 | properties.put(envEntry.getKey(),envEntry.getValue()); 38 | } 39 | for (Object key : systemProperties.keySet()) { 40 | String keyAsString = (String) key; 41 | properties.put(keyAsString, getProperty(keyAsString)); 42 | } 43 | 44 | return new ArrayList() { 45 | { 46 | add(new TaggedPropertySet(ConfigurationValue.ALL_TAG, properties, SystemPropertiesStore.class)); 47 | } 48 | }; 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/internal/store/YamlStore.java: -------------------------------------------------------------------------------- 1 | package org.constretto.internal.store; 2 | 3 | import com.google.gson.Gson; 4 | import org.constretto.ConfigurationStore; 5 | import org.constretto.model.Resource; 6 | import org.constretto.model.TaggedPropertySet; 7 | import org.yaml.snakeyaml.Yaml; 8 | import org.yaml.snakeyaml.nodes.*; 9 | 10 | import java.io.InputStreamReader; 11 | import java.nio.charset.Charset; 12 | import java.util.*; 13 | 14 | import static java.util.Collections.addAll; 15 | 16 | public class YamlStore implements ConfigurationStore { 17 | public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); 18 | private Map resources; 19 | 20 | public YamlStore() { 21 | resources = new HashMap(); 22 | } 23 | 24 | private YamlStore(YamlStore old, String key, TaggedResource resource) { 25 | resources = new HashMap(); 26 | resources.putAll(old.resources); 27 | resources.put(key, resource); 28 | } 29 | 30 | public YamlStore addResource(Resource resource, String key, String... tags) { 31 | List tagList = new ArrayList(); 32 | addAll(tagList, tags); 33 | return new YamlStore(this, key, new TaggedResource(resource, tagList)); 34 | } 35 | 36 | public Collection parseConfiguration() { 37 | List properties = new ArrayList(); 38 | for (Map.Entry entry : resources.entrySet()) { 39 | TaggedResource taggedResource = entry.getValue(); 40 | if (taggedResource.resource.exists()) { 41 | if (taggedResource.tags.isEmpty()) { 42 | HashMap property = new HashMap(); 43 | property.put(entry.getKey(), yamlToJson(taggedResource.resource)); 44 | properties.add(new TaggedPropertySet(property, YamlStore.class)); 45 | } else { 46 | for (String tag : taggedResource.tags) { 47 | HashMap property = new HashMap(); 48 | property.put(entry.getKey(), yamlToJson(taggedResource.resource)); 49 | properties.add(new TaggedPropertySet(tag, property, YamlStore.class)); 50 | } 51 | } 52 | } 53 | } 54 | return properties; 55 | } 56 | 57 | private String yamlToJson(Resource resource) { 58 | Yaml yaml = new Yaml(); 59 | Map data = yaml.loadAs(new InputStreamReader(resource.getInputStream(), DEFAULT_CHARSET), Map.class); 60 | return new Gson().toJson(data); 61 | } 62 | 63 | private static class TaggedResource { 64 | public final Resource resource; 65 | public final List tags = new ArrayList(); 66 | 67 | private TaggedResource(Resource resource, List tags) { 68 | this.tags.addAll(tags); 69 | this.resource = resource; 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/model/ClassPathResource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. Licensed under the Apache 3 | * License, Version 2.0 (the "License"); you may not use this file except in 4 | * compliance with the License. You may obtain a copy of the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law 6 | * or agreed to in writing, software distributed under the License is 7 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 8 | * KIND, either express or implied. See the License for the specific language 9 | * governing permissions and limitations under the License. 10 | */ 11 | package org.constretto.model; 12 | 13 | import java.io.InputStream; 14 | 15 | /** 16 | * @author Kaare Nilsen 17 | */ 18 | public class ClassPathResource extends Resource { 19 | public ClassPathResource(String path) { 20 | super(path); 21 | } 22 | 23 | @Override 24 | public InputStream getInputStream() { 25 | ClassLoader classLoader = this.getClass().getClassLoader(); 26 | String location; 27 | if (path.startsWith(CLASSPATH_PREFIX)) { 28 | location = path.substring(CLASSPATH_PREFIX.length(), path.length()); 29 | } else { 30 | location = path; 31 | } 32 | return classLoader.getResourceAsStream(location); 33 | } 34 | 35 | @Override 36 | public boolean exists() { 37 | InputStream is = getInputStream(); 38 | boolean result = is != null; 39 | try{ 40 | if (is != null) { 41 | is.close(); 42 | } 43 | } catch (Exception e) { 44 | // Do not care 45 | } 46 | return result; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/model/ConfigurationValue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.model; 17 | 18 | /** 19 | * @author Kaare Nilsen 20 | */ 21 | public class ConfigurationValue { 22 | public static final String DEFAULT_TAG = "[default-tag]"; 23 | public static final String ALL_TAG = "[all-tag]"; 24 | 25 | 26 | private final String tag; 27 | private CValue value; 28 | 29 | 30 | public ConfigurationValue(CValue value, String tag) { 31 | this.value = value; 32 | this.tag = tag; 33 | } 34 | 35 | public ConfigurationValue(CValue value) { 36 | this.value = value; 37 | this.tag = DEFAULT_TAG; 38 | } 39 | 40 | public CValue value() { 41 | return value; 42 | } 43 | 44 | public String tag() { 45 | return tag; 46 | } 47 | 48 | 49 | public String toString() { 50 | return "ConfigurationValue{" + 51 | "tag='" + tag + '\'' + 52 | ", value='" + value + '\'' + 53 | '}'; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/model/FileResource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. Licensed under the Apache 3 | * License, Version 2.0 (the "License"); you may not use this file except in 4 | * compliance with the License. You may obtain a copy of the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law 6 | * or agreed to in writing, software distributed under the License is 7 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 8 | * KIND, either express or implied. See the License for the specific language 9 | * governing permissions and limitations under the License. 10 | */ 11 | package org.constretto.model; 12 | 13 | import org.constretto.exception.ConstrettoException; 14 | 15 | import java.io.File; 16 | import java.io.FileInputStream; 17 | import java.io.FileNotFoundException; 18 | import java.io.InputStream; 19 | 20 | /** 21 | * @author Kaare Nilsen 22 | */ 23 | public class FileResource extends Resource { 24 | public FileResource(String path) { 25 | super(path); 26 | } 27 | 28 | @Override 29 | public boolean exists() { 30 | return new File(extractFileNameFromFileResource(path)).exists(); 31 | } 32 | 33 | @Override 34 | public InputStream getInputStream() { 35 | String fileName = extractFileNameFromFileResource(path); 36 | try { 37 | return new FileInputStream(new File(fileName)); 38 | } catch (FileNotFoundException e) { 39 | throw new ConstrettoException("Could not read file from path: " + path); 40 | } 41 | } 42 | 43 | private String extractFileNameFromFileResource(String path) { 44 | String fileName; 45 | if (path.startsWith(FILE_PREFIX)) { 46 | fileName = path.substring(FILE_PREFIX.length(), path.length()); 47 | } else { 48 | fileName = path; 49 | } 50 | return fileName; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/model/GsonParser.java: -------------------------------------------------------------------------------- 1 | package org.constretto.model; 2 | 3 | import com.google.gson.*; 4 | 5 | import java.lang.reflect.Type; 6 | import java.util.ArrayList; 7 | import java.util.HashMap; 8 | import java.util.List; 9 | import java.util.Map; 10 | import java.util.regex.Matcher; 11 | import java.util.regex.Pattern; 12 | 13 | /** 14 | * @author Kaare Nilsen 15 | */ 16 | public class GsonParser implements Parser { 17 | private final GsonBuilder builder = new GsonBuilder(); 18 | 19 | public GsonParser() { 20 | builder.registerTypeAdapter(CValue.class, new JsonDeserializer() { 21 | 22 | private CArray handleArray(JsonArray jsonArray) { 23 | List values = new ArrayList(); 24 | for (JsonElement jsonElement : jsonArray) { 25 | values.add(handle(jsonElement)); 26 | } 27 | return new CArray(values); 28 | } 29 | 30 | private CObject handleObject(JsonObject jsonObject) { 31 | Map values = new HashMap(); 32 | for (Map.Entry entry : jsonObject.entrySet()) { 33 | values.put(entry.getKey(), handle(entry.getValue())); 34 | } 35 | return new CObject(values); 36 | } 37 | 38 | private CValue handle(JsonElement json) { 39 | if (json.isJsonNull()) return null; 40 | else if (json.isJsonPrimitive()) return new CPrimitive(json.getAsString()); 41 | else if (json.isJsonArray()) return handleArray(json.getAsJsonArray()); 42 | else return handleObject(json.getAsJsonObject()); 43 | } 44 | 45 | public CValue deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { 46 | return handle(json); 47 | } 48 | }); 49 | } 50 | 51 | public CValue parse(String value) { 52 | try { 53 | CValue cValue = builder.create().fromJson(value, CValue.class); 54 | if (cValue == null) return new CPrimitive(value); 55 | else return cValue; 56 | } catch (Exception e) { 57 | return new CPrimitive(value); 58 | } 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/model/Parser.java: -------------------------------------------------------------------------------- 1 | package org.constretto.model; 2 | 3 | /** 4 | * @author Kaare Nilsen 5 | */ 6 | public interface Parser { 7 | CValue parse(String value); 8 | } 9 | -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/model/Resource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. Licensed under the Apache 3 | * License, Version 2.0 (the "License"); you may not use this file except in 4 | * compliance with the License. You may obtain a copy of the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law 6 | * or agreed to in writing, software distributed under the License is 7 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 8 | * KIND, either express or implied. See the License for the specific language 9 | * governing permissions and limitations under the License. 10 | */ 11 | package org.constretto.model; 12 | 13 | import org.constretto.exception.ConstrettoException; 14 | 15 | import java.io.InputStream; 16 | 17 | /** 18 | * @author Kaare Nilsen 19 | */ 20 | public abstract class Resource { 21 | public static final String CLASSPATH_PREFIX = "classpath:"; 22 | public static final String FILE_PREFIX = "file:"; 23 | final String path; 24 | 25 | protected Resource(String path) { 26 | if (path == null) { 27 | throw new ConstrettoException("Resources with a null value for the 'path' argument is not allowed. "); 28 | } 29 | this.path = path; 30 | } 31 | 32 | 33 | public static Resource create(String path) { 34 | if (path.startsWith(CLASSPATH_PREFIX)) { 35 | return new ClassPathResource(path); 36 | } else if (path.startsWith(FILE_PREFIX)) { 37 | return new FileResource(path); 38 | } else { 39 | return new UrlResource(path); 40 | } 41 | } 42 | 43 | public abstract boolean exists(); 44 | 45 | public abstract InputStream getInputStream(); 46 | 47 | @Override 48 | public String toString() { 49 | final StringBuilder sb = new StringBuilder(); 50 | sb.append(getClass().getSimpleName()); 51 | sb.append("{path='").append(path).append('\''); 52 | sb.append('}'); 53 | return sb.toString(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/model/TaggedPropertySet.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.model; 17 | 18 | import org.constretto.ConfigurationStore; 19 | 20 | import java.util.Map; 21 | 22 | /** 23 | * @author Kaare Nilsen 24 | */ 25 | public class TaggedPropertySet { 26 | private final Class storeClass; 27 | private final String tag; 28 | private final Map properties; 29 | 30 | public TaggedPropertySet(Map properties, Class storeClass) { 31 | this.tag = ConfigurationValue.DEFAULT_TAG; 32 | this.properties = properties; 33 | this.storeClass = storeClass; 34 | } 35 | 36 | public TaggedPropertySet(String tag, Map properties, Class storeClass) { 37 | if (tag == null){ 38 | throw new IllegalArgumentException("Tag cannot be null"); 39 | } 40 | this.tag = tag; 41 | this.properties = properties; 42 | this.storeClass = storeClass; 43 | } 44 | 45 | public Map getProperties() { 46 | return properties; 47 | } 48 | 49 | public String tag() { 50 | return tag; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/model/UrlResource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. Licensed under the Apache 3 | * License, Version 2.0 (the "License"); you may not use this file except in 4 | * compliance with the License. You may obtain a copy of the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law 6 | * or agreed to in writing, software distributed under the License is 7 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 8 | * KIND, either express or implied. See the License for the specific language 9 | * governing permissions and limitations under the License. 10 | */ 11 | package org.constretto.model; 12 | 13 | import org.constretto.exception.ConstrettoException; 14 | 15 | import java.io.IOException; 16 | import java.io.InputStream; 17 | import java.net.HttpURLConnection; 18 | import java.net.MalformedURLException; 19 | import java.net.URL; 20 | 21 | /** 22 | * @author Kaare Nilsen 23 | */ 24 | public class UrlResource extends Resource { 25 | public UrlResource(String path) { 26 | super(path); 27 | } 28 | 29 | @Override 30 | public boolean exists() { 31 | try { 32 | HttpURLConnection.setFollowRedirects(true); 33 | HttpURLConnection con = (HttpURLConnection) new URL(path).openConnection(); 34 | con.setRequestMethod("HEAD"); 35 | int responseCode = con.getResponseCode(); 36 | return (responseCode == HttpURLConnection.HTTP_OK); 37 | } catch (Exception e) { 38 | return false; 39 | } 40 | } 41 | 42 | @Override 43 | public InputStream getInputStream() { 44 | try { 45 | URL url = new URL(path); 46 | return url.openStream(); 47 | } catch (MalformedURLException ex) { 48 | throw new ConstrettoException("Could not load URL. Path tried: [" + path + "]", ex); 49 | } catch (IOException e) { 50 | throw new ConstrettoException("Woot", e); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /constretto-core/src/main/java/org/constretto/resolver/PredefinedConfigurationContextResolver.java: -------------------------------------------------------------------------------- 1 | package org.constretto.resolver; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.Collections; 6 | import java.util.List; 7 | 8 | /** 9 | * @author zapodot 10 | */ 11 | public class PredefinedConfigurationContextResolver implements ConfigurationContextResolver { 12 | 13 | private List tags; 14 | 15 | private PredefinedConfigurationContextResolver(final List tags) { 16 | this.tags = cloneTagList(tags); 17 | } 18 | 19 | private static List cloneTagList(final List tags) { 20 | if (tags == null) { 21 | return Collections.emptyList(); 22 | } else { 23 | List newList = new ArrayList<>(tags.size()); 24 | for (String tag : tags) { 25 | newList.add(tag); 26 | } 27 | return newList; 28 | } 29 | } 30 | 31 | @Override 32 | public List getTags() { 33 | return cloneTagList(this.tags); 34 | } 35 | 36 | public static PredefinedConfigurationContextResolver empty() { 37 | return new PredefinedConfigurationContextResolver(Collections.emptyList()); 38 | } 39 | 40 | public static PredefinedConfigurationContextResolver usingTags(final String... tags) { 41 | if (tags == null) { 42 | return empty(); 43 | } else { 44 | return usingTagsList(Arrays.asList(tags)); 45 | } 46 | } 47 | 48 | public static PredefinedConfigurationContextResolver usingTagsList(final List tags) { 49 | return new PredefinedConfigurationContextResolver(tags); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/ConstrettoBuilderTest.java: -------------------------------------------------------------------------------- 1 | package org.constretto; 2 | 3 | import org.constretto.configs.TaggedConfig; 4 | import org.constretto.configs.UntaggedConfig; 5 | import org.constretto.resolver.ConfigurationContextResolver; 6 | import org.constretto.resolver.PredefinedConfigurationContextResolver; 7 | import org.junit.Test; 8 | 9 | import java.util.Map; 10 | 11 | import static org.junit.Assert.*; 12 | 13 | /** 14 | * @author zapodot 15 | */ 16 | public class ConstrettoBuilderTest { 17 | 18 | public static final ConfigurationContextResolver STATIC_CONTEXT_RESOLVER = PredefinedConfigurationContextResolver.usingTags(TaggedConfig.TAG); 19 | 20 | @Test 21 | public void testWithSystemProperties() throws Exception { 22 | final Map configuration = ConstrettoBuilder.withSystemProperties().getConfiguration().asMap(); 23 | assertFalse(configuration.isEmpty()); 24 | 25 | assertEquals(configuration.size(), ConstrettoBuilder.withSystemProperties(STATIC_CONTEXT_RESOLVER).getConfiguration().asMap().size()); 26 | 27 | } 28 | 29 | 30 | @Test 31 | public void testEmpty() throws Exception { 32 | final ConstrettoConfiguration emptyConfiguration = ConstrettoBuilder.empty().getConfiguration(); 33 | assertTrue(emptyConfiguration.asMap().isEmpty()); 34 | 35 | assertEquals(emptyConfiguration.asMap().size(), ConstrettoBuilder.empty(STATIC_CONTEXT_RESOLVER).getConfiguration().asMap().size()); 36 | } 37 | 38 | @Test 39 | public void testFromExistingConfiguration() throws Exception { 40 | final ConstrettoConfiguration preExistingConfiguration = ConstrettoBuilder.empty(STATIC_CONTEXT_RESOLVER) 41 | .createObjectConfigurationStore() 42 | .addObject(new UntaggedConfig()) 43 | .addObject(new TaggedConfig()) 44 | .done().getConfiguration(); 45 | 46 | assertEquals(TaggedConfig.TAGGED_VALUE, preExistingConfiguration.evaluateToString("value")); 47 | final ConstrettoConfiguration configuration = ConstrettoBuilder.fromExistingConfiguration(preExistingConfiguration, PredefinedConfigurationContextResolver.empty()).getConfiguration(); 48 | assertEquals(TaggedConfig.TAGGED_VALUE, configuration.evaluateToString("value")); 49 | 50 | } 51 | 52 | 53 | } -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/LeadingCharsTest.java: -------------------------------------------------------------------------------- 1 | package org.constretto; 2 | 3 | import org.constretto.model.Resource; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | 7 | import static org.junit.Assert.assertEquals; 8 | 9 | /** 10 | * Tests for issue reported by jhberges concerning stripping of leading chars from properties. 11 | * 12 | * @author zapodot at gmail dot com 13 | * @see Issue 36 14 | */ 15 | public class LeadingCharsTest { 16 | 17 | private ConstrettoConfiguration constrettoConfiguration; 18 | 19 | @Before 20 | public void setUp() throws Exception { 21 | this.constrettoConfiguration = new ConstrettoBuilder(false) 22 | .createPropertiesStore() 23 | .addResource(Resource.create("classpath:leading-chars-strip.properties")) 24 | .done() 25 | .getConfiguration(); 26 | 27 | } 28 | 29 | @Test 30 | public void testLeadingZeros() throws Exception { 31 | assertEquals("0051", constrettoConfiguration.evaluateToString("leading.zero")); 32 | 33 | } 34 | 35 | @Test 36 | public void testLeadingPlus() throws Exception { 37 | assertEquals("+47", constrettoConfiguration.evaluateToString("leading.plus")); 38 | 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/configs/Config.java: -------------------------------------------------------------------------------- 1 | package org.constretto.configs; 2 | 3 | /** 4 | * @author sondre 5 | */ 6 | public abstract class Config { 7 | abstract public String getValue(); 8 | } 9 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/configs/TaggedConfig.java: -------------------------------------------------------------------------------- 1 | package org.constretto.configs; 2 | 3 | import org.constretto.annotation.ConfigurationSource; 4 | 5 | /** 6 | * @author sondre 7 | */ 8 | @ConfigurationSource(tag = TaggedConfig.TAG) 9 | public class TaggedConfig extends Config { 10 | 11 | public static final String TAGGED_VALUE = "taggedValue"; 12 | public static final String TAG = "tag"; 13 | 14 | @Override 15 | public String getValue() { 16 | return TAGGED_VALUE; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/configs/UntaggedConfig.java: -------------------------------------------------------------------------------- 1 | package org.constretto.configs; 2 | 3 | import org.constretto.annotation.ConfigurationSource; 4 | 5 | /** 6 | * @author sondre 7 | */ 8 | @ConfigurationSource 9 | public class UntaggedConfig extends Config { 10 | public static final String UNTAGGED_VALUE = "untaggedValue"; 11 | 12 | @Override 13 | public String getValue() { 14 | return UNTAGGED_VALUE; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/internal/ConstrettoUtilsTest.java: -------------------------------------------------------------------------------- 1 | package org.constretto.internal; 2 | 3 | import org.junit.Test; 4 | 5 | import java.util.Arrays; 6 | import java.util.List; 7 | 8 | import static org.junit.Assert.assertEquals; 9 | 10 | /** 11 | * @author sondre 12 | */ 13 | public class ConstrettoUtilsTest { 14 | 15 | public static final String VALUE_ONE = "value1"; 16 | public static final String VALUE_TWO = "value2"; 17 | 18 | @Test 19 | public void testAsCsv() throws Exception { 20 | final String csvText = ConstrettoUtils.asCsv(new String[]{VALUE_ONE, VALUE_TWO}); 21 | assertEquals(VALUE_ONE + "," + VALUE_TWO, csvText); 22 | } 23 | 24 | @Test 25 | public void testFromCSV() throws Exception { 26 | final List values = ConstrettoUtils.fromCSV(VALUE_ONE + "," + VALUE_TWO); 27 | assertEquals(Arrays.asList(VALUE_ONE, VALUE_TWO), values); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/internal/ConstructorAnnotationClassTest.java: -------------------------------------------------------------------------------- 1 | package org.constretto.internal; 2 | 3 | import org.constretto.ConstrettoBuilder; 4 | import org.constretto.ConstrettoConfiguration; 5 | import org.constretto.annotation.Configure; 6 | import org.constretto.exception.ConstrettoException; 7 | import org.constretto.model.Resource; 8 | import org.junit.Before; 9 | import org.junit.Test; 10 | 11 | import java.io.Serializable; 12 | 13 | import static org.junit.Assert.assertEquals; 14 | 15 | /** 16 | * @author Sondre Eikanger Kvalø 17 | */ 18 | public class ConstructorAnnotationClassTest { 19 | 20 | ConstrettoConfiguration config; 21 | 22 | @Before 23 | public void before() { 24 | config = new ConstrettoBuilder() 25 | .createPropertiesStore() 26 | .addResource(Resource.create("classpath:subClassData.properties")) 27 | .done() 28 | .getConfiguration(); 29 | } 30 | 31 | 32 | @Test 33 | public void testInjectOnConstructor() throws Exception { 34 | NonLocalConfigurationClass nonLocalConfigurationClassObject = config.as(NonLocalConfigurationClass.class); 35 | assertEquals("value", nonLocalConfigurationClassObject.getValue()); 36 | 37 | } 38 | 39 | 40 | /** 41 | * Because we can not inject parameter values to @Configure annotated constructors in inner classes, 42 | * we expect a ConstrettoException 43 | * 44 | * @throws Exception 45 | */ 46 | @Test(expected = ConstrettoException.class) 47 | public void testInjectOnConstructorInnerClass() throws Exception { 48 | config.as(InnerClassWithNoDefaultConstructor.class); 49 | } 50 | 51 | @Test(expected = ConstrettoException.class) 52 | public void testInjectOnInterface() throws Exception { 53 | config.as(Serializable.class); 54 | } 55 | 56 | @Test(expected = ConstrettoException.class) 57 | public void testInjectOnAnonymousClass() throws Exception { 58 | Serializable serializableInstance = new Serializable() {}; 59 | config.as(serializableInstance.getClass()); 60 | } 61 | 62 | @Test(expected = ConstrettoException.class) 63 | public void testInjectOnClassWithMulitpleAnnotatedConstructors() throws Exception { 64 | config.as(NonLocalConfigurationClassMultipleConstructors.class); 65 | 66 | } 67 | 68 | private class InnerClassWithNoDefaultConstructor { 69 | 70 | private String property; 71 | 72 | @Configure 73 | public InnerClassWithNoDefaultConstructor(final String value) { 74 | this.property = value; 75 | } 76 | 77 | public String getProperty() { 78 | return property; 79 | } 80 | } 81 | 82 | 83 | } -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/internal/FieldAnnotationSubClassTest.java: -------------------------------------------------------------------------------- 1 | package org.constretto.internal; 2 | 3 | import org.constretto.ConstrettoBuilder; 4 | import org.constretto.ConstrettoConfiguration; 5 | import org.constretto.annotation.Configuration; 6 | import org.constretto.model.Resource; 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | 10 | import static org.junit.Assert.assertEquals; 11 | 12 | /** 13 | * @author Tor Arne Kvaløy 14 | */ 15 | 16 | public class FieldAnnotationSubClassTest { 17 | 18 | ConstrettoConfiguration config; 19 | 20 | @Before 21 | public void before() { 22 | config = new ConstrettoBuilder() 23 | .createPropertiesStore() 24 | .addResource(Resource.create("classpath:subClassData.properties")) 25 | .done() 26 | .getConfiguration(); 27 | } 28 | 29 | @Test 30 | public void testSubClass() { 31 | SubClazz subClazz = new SubClazz(); 32 | config.on(subClazz); 33 | 34 | assertEquals("value", subClazz.value); 35 | assertEquals("sub value", subClazz.subValue); 36 | } 37 | 38 | @Test 39 | public void testSubSubClass() { 40 | SubSubClazz subSubClazz = new SubSubClazz(); 41 | config.on(subSubClazz); 42 | 43 | assertEquals("value", subSubClazz.value); 44 | assertEquals("sub value", subSubClazz.subValue); 45 | assertEquals("sub sub value", subSubClazz.subSubValue); 46 | } 47 | 48 | class Clazz { 49 | @Configuration 50 | public String value; 51 | } 52 | 53 | class SubClazz extends Clazz { 54 | @Configuration 55 | public String subValue; 56 | } 57 | 58 | class SubSubClazz extends SubClazz { 59 | @Configuration 60 | public String subSubValue; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/internal/MethodAnnotationSubClassTest.java: -------------------------------------------------------------------------------- 1 | package org.constretto.internal; 2 | 3 | import org.constretto.ConstrettoBuilder; 4 | import org.constretto.ConstrettoConfiguration; 5 | import org.constretto.annotation.Configure; 6 | import org.constretto.model.Resource; 7 | import org.junit.Assert; 8 | import org.junit.Before; 9 | import org.junit.Test; 10 | 11 | /** 12 | * @author Tor Arne Kvaløy 13 | */ 14 | public class MethodAnnotationSubClassTest { 15 | 16 | ConstrettoConfiguration config; 17 | 18 | @Before 19 | public void before() { 20 | config = new ConstrettoBuilder() 21 | .createPropertiesStore() 22 | .addResource(Resource.create("classpath:subClassData.properties")) 23 | .done() 24 | .getConfiguration(); 25 | } 26 | 27 | @Test 28 | public void testSubClass() { 29 | 30 | SubClazz subClazz = new SubClazz(); 31 | config.on(subClazz); 32 | 33 | Assert.assertEquals("value", subClazz.value); 34 | Assert.assertEquals("sub value", subClazz.subValue); 35 | } 36 | 37 | @Test 38 | public void testSubSubClass() { 39 | 40 | SubSubClazz subSubClazz = new SubSubClazz(); 41 | config.on(subSubClazz); 42 | 43 | Assert.assertEquals("value", subSubClazz.value); 44 | Assert.assertEquals("sub value", subSubClazz.subValue); 45 | Assert.assertEquals("sub sub value", subSubClazz.subSubValue); 46 | } 47 | 48 | 49 | @Test 50 | public void checkThatDefaultMethodIsNotSet() { 51 | SubSubClazz subSubClazz = new SubSubClazz(); 52 | config.on(subSubClazz); 53 | 54 | Assert.assertNull(subSubClazz.defaultMethodValue); 55 | } 56 | 57 | @Test 58 | public void checkThatPrivateMethodIsNotSet() throws Exception { 59 | SubSubClazz subSubClazz = new SubSubClazz(); 60 | config.on(subSubClazz); 61 | 62 | Assert.assertNull(subSubClazz.privateMethodValue); 63 | } 64 | 65 | class Clazz { 66 | public String value; 67 | public String defaultMethodValue; 68 | public String privateMethodValue; 69 | 70 | @Configure 71 | public void setValue(String value) { 72 | this.value = value; 73 | } 74 | 75 | @Configure 76 | void setDefaultMethodValue(String defaultMethodValue) { 77 | this.defaultMethodValue = defaultMethodValue; 78 | } 79 | 80 | @Configure 81 | private void setPrivateMethodValue(String privateMethodValue) { 82 | this.privateMethodValue = privateMethodValue; 83 | } 84 | } 85 | 86 | class SubClazz extends Clazz { 87 | public String subValue; 88 | 89 | @Configure 90 | public void setSubValue(String subValue) { 91 | this.subValue = subValue; 92 | } 93 | } 94 | 95 | class SubSubClazz extends SubClazz { 96 | public String subSubValue; 97 | 98 | @Configure 99 | public void setSubSubValue(String subSubValue) { 100 | this.subSubValue = subSubValue; 101 | } 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/internal/NonLocalConfigurationClass.java: -------------------------------------------------------------------------------- 1 | package org.constretto.internal; 2 | 3 | import org.constretto.annotation.Configure; 4 | 5 | /** 6 | * Used to test @Configure annotated constructors. 7 | * 8 | * @author Sondre Eikanger Kvalø 9 | */ 10 | public class NonLocalConfigurationClass { 11 | 12 | private String value; 13 | 14 | @Configure 15 | public NonLocalConfigurationClass(final String value) { 16 | this.value = value; 17 | } 18 | 19 | public String getValue() { 20 | return value; 21 | } 22 | } -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/internal/NonLocalConfigurationClassMultipleConstructors.java: -------------------------------------------------------------------------------- 1 | package org.constretto.internal; 2 | 3 | import org.constretto.annotation.Configure; 4 | 5 | /** 6 | * 7 | * @author zapodot 8 | */ 9 | public class NonLocalConfigurationClassMultipleConstructors { 10 | 11 | private String value; 12 | private String someOtherValue; 13 | 14 | @Configure 15 | public NonLocalConfigurationClassMultipleConstructors(final String value) { 16 | this.value = value; 17 | } 18 | 19 | @Configure 20 | public NonLocalConfigurationClassMultipleConstructors(final String value, final String someOtherValue) { 21 | this.value = value; 22 | this.someOtherValue = someOtherValue; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/internal/converter/EnumValueConverterRegistryTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.converter; 17 | 18 | import org.constretto.exception.ConstrettoConversionException; 19 | import org.constretto.model.CPrimitive; 20 | import org.junit.Test; 21 | 22 | import static org.junit.Assert.assertEquals; 23 | 24 | /** 25 | * @author Tom Palmer 26 | */ 27 | public class EnumValueConverterRegistryTest { 28 | 29 | @Test 30 | public void conversionOfKnownEnumValue() throws Exception { 31 | MockEnum value1 = (MockEnum) ValueConverterRegistry.convert(MockEnum.class, MockEnum.class, new CPrimitive("Value1")); 32 | assertEquals("Wrong value.", value1, MockEnum.Value1); 33 | } 34 | 35 | @Test(expected = ConstrettoConversionException.class) 36 | public void conversionOfUnknownEnumValue() throws Exception { 37 | ValueConverterRegistry.convert(MockEnum.class, MockEnum.class,new CPrimitive("Unknown")); 38 | } 39 | 40 | private enum MockEnum { 41 | Value1 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/internal/converter/FileConverterTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.converter; 17 | 18 | import org.constretto.model.CPrimitive; 19 | import org.junit.Test; 20 | 21 | import java.io.File; 22 | 23 | import static org.constretto.internal.converter.ValueConverterRegistry.convert; 24 | import static org.junit.Assert.*; 25 | 26 | /** 27 | * @author Kaare Nilsen 28 | */ 29 | public class FileConverterTest { 30 | 31 | @Test 32 | public void simpleFileConversion() { 33 | File file = new File("."); 34 | File convertedFile = (File) convert(File.class, File.class, new CPrimitive(".")); 35 | assertEquals(file, convertedFile); 36 | assertTrue(convertedFile.isDirectory()); 37 | assertTrue(convertedFile.canRead()); 38 | } 39 | 40 | 41 | @Test 42 | public void fileConversionWithBaseDir() { 43 | File file = new File("./pom.xml"); 44 | File convertedFile = new FileValueConverter(new File(".")).fromString("pom.xml"); 45 | assertEquals(file, convertedFile); 46 | assertFalse(convertedFile.isDirectory()); 47 | assertTrue(convertedFile.canRead()); 48 | } 49 | 50 | @Test 51 | public void fileConversionWithBaseDirAndAbsoluteFile() { 52 | File file = new File("./pom.xml").getAbsoluteFile(); 53 | File convertedFile = new FileValueConverter(new File("."), true).fromString("pom.xml"); 54 | assertEquals(file, convertedFile); 55 | assertFalse(convertedFile.isDirectory()); 56 | assertTrue(convertedFile.canRead()); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/internal/converter/ValueConverterRegistryTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.converter; 17 | 18 | import java.util.ArrayList; 19 | import java.util.HashMap; 20 | import java.util.List; 21 | import java.util.Map; 22 | import java.util.Set; 23 | 24 | import org.constretto.exception.ConstrettoException; 25 | import org.constretto.model.CArray; 26 | import org.constretto.model.CObject; 27 | import org.constretto.model.CPrimitive; 28 | import org.constretto.model.CValue; 29 | import org.junit.Assert; 30 | import org.junit.Test; 31 | 32 | /** 33 | * @author Asbjørn Aarrestad 34 | */ 35 | public class ValueConverterRegistryTest { 36 | 37 | @SuppressWarnings("unchecked") 38 | @Test 39 | public void simpleListConversion() { 40 | final List list = new ArrayList(); 41 | list.add(new CPrimitive("hei")); 42 | final List convertedList = (List) ValueConverterRegistry.convert(String.class, String.class, new CArray(list)); 43 | 44 | Assert.assertNotNull(convertedList); 45 | Assert.assertTrue(convertedList.size() == 1); 46 | } 47 | 48 | 49 | @SuppressWarnings("unchecked") 50 | @Test 51 | public void simpleMapConversion() { 52 | final Map map = new HashMap(); 53 | map.put("hei", new CPrimitive("hallo")); 54 | final Map convertedMap = (Map) ValueConverterRegistry.convert(String.class, String.class, new CObject(map)); 55 | 56 | Assert.assertNotNull(convertedMap); 57 | Assert.assertTrue(convertedMap.size() == 1); 58 | } 59 | 60 | @Test 61 | public void mapListConversion() { 62 | final List list = new ArrayList(); 63 | final Map data = new HashMap(); 64 | data.put("key", new CPrimitive("value")); 65 | list.add(new CObject(data)); 66 | 67 | final List> convertedList = (List>) ValueConverterRegistry.convert(String.class, 68 | String.class, new CArray(list)); 69 | 70 | Assert.assertNotNull(convertedList); 71 | Assert.assertTrue(convertedList.size() == 1); 72 | final Map element = convertedList.get(0); 73 | Assert.assertTrue(element.size() == 1); 74 | Assert.assertEquals("value", element.get("key")); 75 | } 76 | 77 | @Test(expected = ConstrettoException.class) 78 | public void testIllegalDataTypeConversion() throws Exception { 79 | ValueConverterRegistry.convert(String.class, String.class, new CValue() { 80 | @Override 81 | public Set referencedKeys() { 82 | return null; //To change body of implemented methods use File | Settings | File Templates. 83 | } 84 | 85 | @Override 86 | public void replace(final String key, final String resolvedValue) { 87 | //To change body of implemented methods use File | Settings | File Templates. 88 | } 89 | }); 90 | 91 | } 92 | 93 | @Test(expected = ConstrettoException.class) 94 | public void testConvertUnsopportedClassConversion() { 95 | final Map map = new HashMap(); 96 | map.put("hei", new CPrimitive("hallo")); 97 | ValueConverterRegistry.convert(getClass(), getClass(), new CObject(map)); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/internal/introspect/ArgumentDescriptionFactoryTest.java: -------------------------------------------------------------------------------- 1 | package org.constretto.internal.introspect; 2 | 3 | import org.constretto.annotation.Configure; 4 | import org.constretto.internal.NonLocalConfigurationClass; 5 | import org.junit.Test; 6 | 7 | import java.lang.annotation.Annotation; 8 | import java.lang.reflect.Constructor; 9 | import java.lang.reflect.Method; 10 | 11 | import static org.junit.Assert.assertArrayEquals; 12 | import static org.junit.Assert.assertEquals; 13 | import static org.junit.Assert.assertNull; 14 | 15 | /** 16 | * 17 | * @author zapodot 18 | */ 19 | public class ArgumentDescriptionFactoryTest { 20 | 21 | public static class ConfigurableClass { 22 | private String value; 23 | 24 | @Configure 25 | public void setValue(final String value) { 26 | this.value = value; 27 | } 28 | 29 | } 30 | @Test 31 | public void testForMethod() throws Exception { 32 | Method method = ConfigurableClass.class.getMethod("setValue", String.class); 33 | final ArgumentDescription[] argumentDescriptions = ArgumentDescriptionFactory.forMethod(method).resolveParameters(); 34 | assertEquals(1, argumentDescriptions.length); 35 | assertEquals(String.class, argumentDescriptions[0].getType()); 36 | assertEquals("value", argumentDescriptions[0].getName()); 37 | } 38 | 39 | @Test 40 | public void testForConstrutor() throws Exception { 41 | final Constructor[] constructors = Constructors.findConstructorsWithConfigureAnnotation( 42 | NonLocalConfigurationClass.class); 43 | assertEquals(1, constructors.length); 44 | final ArgumentDescription[] argumentDescriptions = ArgumentDescriptionFactory.create(constructors[0]).resolveParameters(); 45 | assertEquals("value", argumentDescriptions[0].getName()); 46 | assertEquals(String.class, argumentDescriptions[0].getType()); 47 | } 48 | 49 | @Test 50 | public void testForConstructorWithoutAnnotations() throws Exception { 51 | final Constructor[] constructors = Constructors.findConstructorsWithConfigureAnnotation( 52 | NonLocalConfigurationClass.class); 53 | final Constructor theConstructor = constructors[0]; 54 | final ArgumentDescription argumentDescription = new ArgumentDescription(theConstructor.getName(), null, theConstructor.getDeclaringClass()); 55 | assertArrayEquals(new Annotation[]{}, argumentDescription.getAnnotations()); 56 | 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/internal/provider/ConstrettoConfigurationTest.java: -------------------------------------------------------------------------------- 1 | package org.constretto.internal.provider; 2 | 3 | import org.constretto.ConstrettoBuilder; 4 | import org.constretto.ConstrettoConfiguration; 5 | import org.constretto.Property; 6 | import org.constretto.model.Resource; 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | 13 | import static org.junit.Assert.assertEquals; 14 | import static org.junit.Assert.assertTrue; 15 | 16 | /** 17 | * @author Kaare Nilsen 18 | */ 19 | public class ConstrettoConfigurationTest { 20 | private ConstrettoConfiguration constrettoConfiguration; 21 | 22 | 23 | @Before 24 | public void loadConfiguration() { 25 | constrettoConfiguration = new ConstrettoBuilder(false) 26 | .addCurrentTag("production") 27 | .createPropertiesStore() 28 | .addResource(Resource.create("classpath:test.properties")) 29 | .done() 30 | .getConfiguration(); 31 | } 32 | 33 | @Test 34 | public void lookupCompositeElementUsingTagAndLookupKeyHasNoTag() { 35 | assertEquals("http://constretto.org/child", constrettoConfiguration.evaluateToString("url.child")); 36 | } 37 | 38 | @Test 39 | public void configurationIterator(){ 40 | List expected = new ArrayList(){{ 41 | add(new Property("somedb.username","user1")); 42 | add(new Property("datasources.customer.password","password")); 43 | add(new Property("url.child","http://constretto.org/child")); 44 | add(new Property("base-url","http://constretto.org")); 45 | }}; 46 | 47 | List actual = new ArrayList(); 48 | for (Property property : constrettoConfiguration) { 49 | actual.add(property); 50 | } 51 | assertEquals(expected.size(),actual.size()); 52 | assertTrue(actual.containsAll(expected)); 53 | } 54 | 55 | 56 | } 57 | 58 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/internal/provider/DefaultValuesInAnnotationsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.provider; 17 | 18 | import org.constretto.ConstrettoBuilder; 19 | import org.constretto.ConstrettoConfiguration; 20 | import org.constretto.internal.provider.helper.ContagiousConfigurationMethod; 21 | import org.junit.Test; 22 | 23 | import static org.junit.Assert.assertEquals; 24 | 25 | /** 26 | * @author Kaare Nilsen 27 | */ 28 | public class DefaultValuesInAnnotationsTest { 29 | 30 | @Test 31 | public void defaultValuesShouldNotBeContagious() { 32 | ConstrettoConfiguration configuration = new ConstrettoBuilder().getConfiguration(); 33 | ContagiousConfigurationMethod testObject = configuration.as(ContagiousConfigurationMethod.class); 34 | assertEquals("default-value", testObject.valueWithDefault()); 35 | assertEquals(null, testObject.missingValue()); 36 | assertEquals(null, testObject.otherMissingValue()); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/internal/provider/EncryptedPropertiesLookupTest.java: -------------------------------------------------------------------------------- 1 | package org.constretto.internal.provider; 2 | 3 | import org.constretto.ConstrettoBuilder; 4 | import org.constretto.ConstrettoConfiguration; 5 | import org.constretto.model.Resource; 6 | import org.junit.Test; 7 | 8 | import static org.junit.Assert.assertEquals; 9 | 10 | /** 11 | * 12 | */ 13 | public class EncryptedPropertiesLookupTest { 14 | private static final String PASSWORD_PROPERTY = "PASSWORD"; 15 | 16 | @Test 17 | public void prepareTests() { 18 | System.setProperty(PASSWORD_PROPERTY, "constretto"); 19 | ConstrettoBuilder constrettoBuilder = new ConstrettoBuilder(); 20 | constrettoBuilder 21 | .createEncryptedPropertiesStore(PASSWORD_PROPERTY) 22 | .addResource(Resource.create("classpath:encrypted.properties")) 23 | .done(); 24 | ConstrettoConfiguration config = constrettoBuilder.getConfiguration(); 25 | assertEquals("Testing a property", config.evaluateToString("encrypted_property")); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/internal/provider/IniFileStoreLookupTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.provider; 17 | 18 | import org.constretto.ConstrettoBuilder; 19 | import org.constretto.ConstrettoConfiguration; 20 | import org.constretto.model.ClassPathResource; 21 | 22 | /** 23 | * @author Kaare Nilsen 24 | */ 25 | public class IniFileStoreLookupTest extends AbstractConfigurationProviderLookupTest { 26 | 27 | @Override 28 | public ConstrettoConfiguration prepareTests(String... tags) { 29 | ConstrettoBuilder constrettoBuilder = new ConstrettoBuilder(); 30 | constrettoBuilder 31 | .createIniFileConfigurationStore() 32 | .addResource(new ClassPathResource(("org/constretto/internal/provider/helper/provider-test.ini"))) 33 | .addResource(new ClassPathResource(("org/constretto/internal/provider/helper/provider-test-overloaded.ini"))) 34 | .done() 35 | .createSystemPropertiesStore(); 36 | for (String tag : tags) { 37 | constrettoBuilder.addCurrentTag(tag); 38 | } 39 | return constrettoBuilder.getConfiguration(); 40 | } 41 | } -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/internal/provider/PropertyFileStoreLookupTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.provider; 17 | 18 | import org.constretto.ConstrettoBuilder; 19 | import org.constretto.ConstrettoConfiguration; 20 | import org.constretto.model.ClassPathResource; 21 | 22 | /** 23 | * @author Kaare Nilsen 24 | */ 25 | public class PropertyFileStoreLookupTest extends AbstractConfigurationProviderLookupTest { 26 | 27 | @Override 28 | public ConstrettoConfiguration prepareTests(String... tags) { 29 | ConstrettoBuilder constrettoBuilder = new ConstrettoBuilder(); 30 | constrettoBuilder 31 | .createPropertiesStore() 32 | .addResource(new ClassPathResource(("org/constretto/internal/provider/helper/provider-test.properties"))) 33 | .addResource(new ClassPathResource(("org/constretto/internal/provider/helper/provider-test-overloaded.properties"))) 34 | .done() 35 | .createSystemPropertiesStore(); 36 | for (String tag : tags) { 37 | constrettoBuilder.addCurrentTag(tag); 38 | } 39 | return constrettoBuilder.getConfiguration(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/internal/provider/helper/ConfiguredUsingDefaults.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.provider.helper; 17 | 18 | import org.constretto.ConfigurationDefaultValueFactory; 19 | import org.constretto.annotation.Configuration; 20 | import org.constretto.annotation.Configure; 21 | 22 | /** 23 | * @author Kaare Nilsen 24 | */ 25 | public class ConfiguredUsingDefaults { 26 | 27 | @Configuration(defaultValue = "default-username") 28 | private String strangeUserName; 29 | 30 | @Configuration(defaultValue = "default-vendor") 31 | private String vendor; 32 | 33 | private String password; 34 | private Integer version; 35 | 36 | 37 | @Configure 38 | public void configureMe( 39 | @Configuration(defaultValue = "default-password") String strangePassword, 40 | @Configuration(defaultValueFactory = VersionDefaultValueFactory.class) Integer strangeVersion) { 41 | this.password = strangePassword; 42 | this.version = strangeVersion; 43 | } 44 | 45 | 46 | public String getStrangeUserName() { 47 | return strangeUserName; 48 | } 49 | 50 | public String getPassword() { 51 | return password; 52 | } 53 | 54 | public Integer getVersion() { 55 | return version; 56 | } 57 | 58 | public String getVendor() { 59 | return vendor; 60 | } 61 | 62 | public static class VersionDefaultValueFactory implements ConfigurationDefaultValueFactory { 63 | public Integer getDefaultValue() { 64 | return Integer.MIN_VALUE; 65 | } 66 | } 67 | } -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/internal/provider/helper/ConfiguredUsingListsAndMaps.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.provider.helper; 17 | 18 | import org.constretto.annotation.Configuration; 19 | import org.constretto.annotation.Configure; 20 | 21 | import java.util.List; 22 | import java.util.Map; 23 | 24 | /** 25 | * @author Kaare Nilsen 26 | */ 27 | public class ConfiguredUsingListsAndMaps { 28 | 29 | @Configuration("map") 30 | public Map mapFromField; 31 | @Configuration("array") 32 | public List arrayFromField; 33 | 34 | public Map mapFromMethod; 35 | public List arrayFromMethod; 36 | 37 | 38 | @Configure 39 | public void configureMe( 40 | @Configuration Map map, 41 | @Configuration List array) { 42 | this.arrayFromMethod = array; 43 | this.mapFromMethod = map; 44 | } 45 | } -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/internal/provider/helper/ContagiousConfigurationMethod.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.provider.helper; 17 | 18 | import org.constretto.annotation.Configuration; 19 | import org.constretto.annotation.Configure; 20 | 21 | /** 22 | * @author Kaare Nilsen 23 | */ 24 | public class ContagiousConfigurationMethod { 25 | 26 | private String missingValue; 27 | private String otherMissingValue; 28 | private String valueWithDefault; 29 | 30 | @Configure 31 | public void configure(@Configuration(value = "missing-value", required = false) String missingValue, 32 | @Configuration(value = "i-have-default", defaultValue = "default-value") String valueWithDefault, 33 | @Configuration(value = "other-missing-value", required = false) String otherMissingValue) { 34 | this.missingValue = missingValue; 35 | this.valueWithDefault = valueWithDefault; 36 | this.otherMissingValue = otherMissingValue; 37 | } 38 | 39 | public String missingValue() { 40 | return missingValue; 41 | } 42 | 43 | public String otherMissingValue() { 44 | return otherMissingValue; 45 | } 46 | 47 | public String valueWithDefault() { 48 | return valueWithDefault; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/internal/provider/helper/DataSourceConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.provider.helper; 17 | 18 | import org.constretto.annotation.Configuration; 19 | import org.constretto.annotation.Configure; 20 | 21 | /** 22 | * @author Kaare Nilsen 23 | */ 24 | public class DataSourceConfiguration { 25 | 26 | private String myUrl; 27 | private String myPassword; 28 | private Integer version; 29 | 30 | @Configuration 31 | private String vendor; 32 | 33 | @Configuration("username") 34 | private String myUsername; 35 | 36 | 37 | @Configure 38 | public void configureMe(@Configuration String url, @Configuration("password") String secret) { 39 | this.myUrl = url; 40 | this.myPassword = secret; 41 | } 42 | 43 | public String getUrl() { 44 | return myUrl; 45 | } 46 | 47 | public String getUsername() { 48 | return myUsername; 49 | } 50 | 51 | public String getPassword() { 52 | return myPassword; 53 | } 54 | 55 | public String getVendor() { 56 | return vendor; 57 | } 58 | 59 | public Integer getVersion() { 60 | return version; 61 | } 62 | 63 | @Configure 64 | public void setVersion(Integer version) { 65 | this.version = version; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/internal/provider/helper/DataSourceConfigurationWithNatives.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.provider.helper; 17 | 18 | import org.constretto.annotation.Configuration; 19 | import org.constretto.annotation.Configure; 20 | 21 | /** 22 | * @author Kaare Nilsen 23 | */ 24 | public class DataSourceConfigurationWithNatives { 25 | 26 | private int version; 27 | 28 | @Configuration("version") 29 | private int otherVersion; 30 | 31 | 32 | @Configure 33 | public void configureMe(@Configuration int version) { 34 | this.version = version; 35 | } 36 | 37 | 38 | public int getVersion() { 39 | return version; 40 | } 41 | 42 | public int getOtherVersion() { 43 | return otherVersion; 44 | } 45 | } -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/internal/store/AbstractConfigurationStoreTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.store; 17 | 18 | import org.constretto.ConfigurationStore; 19 | import org.constretto.model.ConfigurationValue; 20 | import org.constretto.model.TaggedPropertySet; 21 | import org.junit.Test; 22 | 23 | import java.util.Collection; 24 | 25 | import static org.junit.Assert.*; 26 | 27 | /** 28 | * @author Thor Åge Eldby 29 | */ 30 | public abstract class AbstractConfigurationStoreTest { 31 | 32 | @Test 33 | public void load() { 34 | ConfigurationStore store = getStore(); 35 | Collection props = store.parseConfiguration(); 36 | assertNotNull(props); 37 | assertEquals("Unexpected number of tags loaded for " + store, 3, props.size()); 38 | for (TaggedPropertySet prop : props) { 39 | String value = prop.getProperties().get("somedb.username"); 40 | if (prop.tag().equals(ConfigurationValue.DEFAULT_TAG)) { 41 | assertEquals("user0", value); 42 | } else if (prop.tag().equals("production")) { 43 | assertEquals("user1", value); 44 | } else if (prop.tag().equals("systest")) { 45 | assertEquals("user2", value); 46 | } else { 47 | fail("Unexpected tag " + prop.tag()); 48 | } 49 | assertNull(prop.getProperties().get("barekudd")); 50 | } 51 | } 52 | 53 | abstract protected ConfigurationStore getStore(); 54 | 55 | } 56 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/internal/store/EncryptedPropertiesStoreTest.java: -------------------------------------------------------------------------------- 1 | package org.constretto.internal.store; 2 | 3 | import org.constretto.model.Resource; 4 | import org.constretto.model.TaggedPropertySet; 5 | import org.jasypt.util.text.BasicTextEncryptor; 6 | import org.junit.Before; 7 | import org.junit.Rule; 8 | import org.junit.Test; 9 | import org.junit.contrib.java.lang.system.ProvideSystemProperty; 10 | 11 | import java.util.List; 12 | import java.util.Properties; 13 | 14 | import static org.junit.Assert.assertEquals; 15 | 16 | /** 17 | * @author zapodot 18 | */ 19 | public class EncryptedPropertiesStoreTest { 20 | 21 | public static final String PROPERTY_KEY = "encryptionKey"; 22 | public static final String ENCRYPTION_KEY = "constretto"; 23 | public static final String KEY = "key"; 24 | public static final String UNENCRYPTED_VALUE = "a value"; 25 | 26 | @Rule 27 | public ProvideSystemProperty provideSystemProperty = new ProvideSystemProperty(PROPERTY_KEY, ENCRYPTION_KEY); 28 | 29 | private String encryptedValue; 30 | private Properties encryptedProperties; 31 | 32 | @Before 33 | public void setUp() throws Exception { 34 | final BasicTextEncryptor basicTextEncryptor = new BasicTextEncryptor(); 35 | basicTextEncryptor.setPassword(ENCRYPTION_KEY); 36 | encryptedValue = basicTextEncryptor.encrypt(UNENCRYPTED_VALUE); 37 | encryptedProperties = new Properties(); 38 | encryptedProperties.put(KEY, encryptedValue); 39 | 40 | } 41 | 42 | @Test 43 | public void testParseProperties() throws Exception { 44 | 45 | final EncryptedPropertiesStore encryptedPropertiesStore = new EncryptedPropertiesStore(PROPERTY_KEY); 46 | final Properties encrypted = encryptedPropertiesStore.parseProperties(encryptedProperties); 47 | assertEquals(encryptedValue, encrypted.getProperty(KEY)); 48 | 49 | } 50 | 51 | @Test 52 | public void testParseConfigurationFromResource() throws Exception { 53 | 54 | final EncryptedPropertiesStore encryptedPropertiesStore = new EncryptedPropertiesStore(PROPERTY_KEY); 55 | final List taggedPropertySets = encryptedPropertiesStore 56 | .addResource(Resource.create("classpath:encrypted.properties")) 57 | .parseConfiguration(); 58 | assertEquals("Testing a property", taggedPropertySets.get(0).getProperties().get("encrypted_property")); 59 | 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/internal/store/IniFileConfigurationStoreTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.store; 17 | 18 | import org.constretto.ConfigurationStore; 19 | import org.constretto.model.ClassPathResource; 20 | 21 | /** 22 | * @author Kristoffer Moum 23 | */ 24 | public class IniFileConfigurationStoreTest extends AbstractConfigurationStoreTest { 25 | 26 | @Override 27 | protected ConfigurationStore getStore() { 28 | return new IniFileConfigurationStore().addResource(new ClassPathResource("test.ini")); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/internal/store/JsonStoreTest.java: -------------------------------------------------------------------------------- 1 | package org.constretto.internal.store; 2 | 3 | import org.constretto.ConstrettoBuilder; 4 | import org.constretto.ConstrettoConfiguration; 5 | import org.constretto.GenericConverter; 6 | import org.constretto.exception.ConstrettoConversionException; 7 | import org.constretto.model.CObject; 8 | import org.constretto.model.CPrimitive; 9 | import org.constretto.model.CValue; 10 | import org.constretto.model.Resource; 11 | import org.junit.Test; 12 | 13 | import java.util.Map; 14 | 15 | import static org.constretto.internal.converter.ValueConverterRegistry.convertPrimitive; 16 | import static org.junit.Assert.assertEquals; 17 | import static org.junit.Assert.assertFalse; 18 | 19 | public class JsonStoreTest { 20 | 21 | @Test 22 | public void jsonShouldParseCorrectly() { 23 | ConstrettoConfiguration conf = new ConstrettoBuilder() 24 | .createJsonConfigurationStore() 25 | .addResource(Resource.create("classpath:jsonTest.json"), "person") 26 | .done() 27 | .getConfiguration(); 28 | Person person = conf.evaluateWith(new PersonJsonConverter(), "person"); 29 | assertEquals(new Person("Kaare", 29), person); 30 | } 31 | 32 | @Test 33 | public void jsonShouldAllowNonExistingResources() { 34 | ConstrettoConfiguration conf = new ConstrettoBuilder() 35 | .createJsonConfigurationStore() 36 | .addResource(Resource.create("classpath:nonExisiting.json"), "person") 37 | .addResource(Resource.create("file:/nonExisiting.json"), "person") 38 | .addResource(Resource.create("http:/nonExisiting.com"), "person") 39 | .done() 40 | .getConfiguration(); 41 | 42 | assertFalse(conf.hasValue("person")); 43 | } 44 | 45 | 46 | private class PersonJsonConverter implements GenericConverter { 47 | public Person fromValue(CValue value) throws ConstrettoConversionException { 48 | if (value instanceof CObject) { 49 | Map data = ((CObject) value).data(); 50 | String name = convertPrimitive(String.class, (CPrimitive) data.get("name")); 51 | Integer age = convertPrimitive(Integer.class, (CPrimitive) data.get("age")); 52 | return new Person(name, age); 53 | 54 | } else { 55 | throw new ConstrettoConversionException(value.toString(), Person.class, "Exptected Json Object but found: " + value.getClass().getSimpleName()); 56 | } 57 | } 58 | } 59 | 60 | private class Person { 61 | public final String name; 62 | public final Integer age; 63 | 64 | private Person(String name, Integer age) { 65 | this.age = age; 66 | this.name = name; 67 | } 68 | 69 | public boolean equals(Object o) { 70 | if (this == o) return true; 71 | if (o == null || getClass() != o.getClass()) return false; 72 | 73 | Person person = (Person) o; 74 | 75 | if (age != null ? !age.equals(person.age) : person.age != null) return false; 76 | if (name != null ? !name.equals(person.name) : person.name != null) return false; 77 | 78 | return true; 79 | } 80 | 81 | public int hashCode() { 82 | int result = name != null ? name.hashCode() : 0; 83 | result = 31 * result + (age != null ? age.hashCode() : 0); 84 | return result; 85 | } 86 | 87 | public String toString() { 88 | return "Person{" + 89 | "name='" + name + '\'' + 90 | ", age=" + age + 91 | '}'; 92 | } 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/internal/store/PropertiesStoreInputStreamCloseTest.java: -------------------------------------------------------------------------------- 1 | package org.constretto.internal.store; 2 | 3 | import org.junit.Assert; 4 | import org.constretto.exception.ConstrettoException; 5 | import org.constretto.model.Resource; 6 | import org.junit.Test; 7 | import org.mockito.internal.stubbing.answers.ThrowsException; 8 | 9 | import java.io.IOException; 10 | import java.io.InputStream; 11 | 12 | import static org.mockito.Mockito.*; 13 | 14 | /** 15 | * 16 | * @author Thor Åge Eldby 24 | */ 25 | public class PropertiesStoreTest extends AbstractConfigurationStoreTest { 26 | 27 | @Override 28 | protected ConfigurationStore getStore() { 29 | Resource props = new ClassPathResource("test.properties"); 30 | return new PropertiesStore().addResource(props); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/internal/store/SystemPropertiesStoreTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.store; 17 | 18 | import org.constretto.model.TaggedPropertySet; 19 | import org.junit.Test; 20 | 21 | import java.util.List; 22 | 23 | import static org.junit.Assert.assertEquals; 24 | import static org.junit.Assert.assertNotNull; 25 | 26 | /** 27 | * @author Thor Åge Eldby 28 | */ 29 | public class SystemPropertiesStoreTest { 30 | 31 | @Test 32 | public void load() { 33 | System.setProperty("somedb.username", "user0"); 34 | SystemPropertiesStore store = new SystemPropertiesStore(); 35 | List set = store.parseConfiguration(); 36 | assertNotNull(set); 37 | assertEquals(1, set.size()); 38 | assertEquals("user0", set.get(0).getProperties().get("somedb.username")); 39 | assertEquals(System.getenv().get("USER"), set.get(0).getProperties().get("USER")); 40 | System.setProperty("USER", "mrTest"); 41 | store = new SystemPropertiesStore(); 42 | set = store.parseConfiguration(); 43 | assertEquals("mrTest", set.get(0).getProperties().get("USER")); 44 | } 45 | 46 | 47 | 48 | } 49 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/internal/store/helper/DefaultCustomerDataSourceConfigurer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.store.helper; 17 | 18 | import org.constretto.annotation.ConfigurationSource; 19 | 20 | /** 21 | * @author Kaare Nilsen 22 | */ 23 | @ConfigurationSource(basePath = "datasources.customer") 24 | public class DefaultCustomerDataSourceConfigurer { 25 | 26 | public String getUrl() { 27 | return "default-url"; 28 | } 29 | 30 | public String getUsername() { 31 | return "default-username"; 32 | } 33 | 34 | public String getPassword() { 35 | return "default-password"; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/internal/store/helper/DevelopmentCustomerDataSourceConfigurer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.store.helper; 17 | 18 | import org.constretto.annotation.ConfigurationSource; 19 | 20 | /** 21 | * @author Kaare Nilsen 22 | */ 23 | @ConfigurationSource(basePath = "datasources.customer", tag = "development") 24 | public class DevelopmentCustomerDataSourceConfigurer { 25 | 26 | public String getUrl() { 27 | return "development-url"; 28 | } 29 | 30 | public String getUsername() { 31 | return "development-username"; 32 | } 33 | 34 | public String getPassword() { 35 | return "development-password"; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/internal/store/helper/GenericDataSourceConfigurer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.store.helper; 17 | 18 | 19 | /** 20 | * @author Kaare Nilsen 21 | */ 22 | public class GenericDataSourceConfigurer { 23 | 24 | public String getUrl() { 25 | return "generic-url"; 26 | } 27 | 28 | public String getUsername() { 29 | return "generic-username"; 30 | } 31 | 32 | public String getPassword() { 33 | return "generic-password"; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/internal/store/helper/ProductionCustomerDataSourceConfigurer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.store.helper; 17 | 18 | import org.constretto.annotation.ConfigurationSource; 19 | 20 | /** 21 | * @author Kaare Nilsen 22 | */ 23 | @ConfigurationSource(basePath = "datasources.customer", tag = "production") 24 | public class ProductionCustomerDataSourceConfigurer { 25 | 26 | public String getUrl() { 27 | return "production-url"; 28 | } 29 | 30 | public String getUsername() { 31 | return "production-username"; 32 | } 33 | 34 | public String getPassword() { 35 | return "production-password"; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/internal/util/StaticlyCachedConfigurationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.constretto.internal.util; 17 | 18 | import org.constretto.exception.ConstrettoExpressionException; 19 | import org.junit.Before; 20 | import org.junit.Test; 21 | 22 | import static org.constretto.util.StaticlyCachedConfiguration.*; 23 | import static org.junit.Assert.assertEquals; 24 | 25 | /** 26 | * @author Kaare Nilsen 27 | */ 28 | public class StaticlyCachedConfigurationTest { 29 | 30 | @Before 31 | public void setUp() throws Exception { 32 | reset(); 33 | } 34 | 35 | @Test 36 | public void cachedSingleIniFile() { 37 | String value = config("classpath:cache1.ini").evaluateToString("key1"); 38 | assertEquals("value1", value); 39 | assertEquals(0, cacheHits()); 40 | assertEquals(1, cacheMiss()); 41 | value = config("classpath:cache1.ini").evaluateToString("key1"); 42 | assertEquals("value1", value); 43 | assertEquals(1, cacheHits()); 44 | assertEquals(1, cacheMiss()); 45 | } 46 | 47 | @Test 48 | public void cachedMultipleIniFile() { 49 | String value = config("classpath:cache1.ini", "classpath:cache2.ini").evaluateToString("key1"); 50 | assertEquals("value1", value); 51 | String value2 = config("classpath:cache1.ini", "classpath:cache2.ini").evaluateToString("key2"); 52 | assertEquals("value2", value2); 53 | } 54 | 55 | @Test 56 | public void cachedMultipleFilesWithDifferentType() { 57 | String value = config("classpath:cache1.ini", "classpath:cache2.ini", "classpath:cache3.properties").evaluateToString("key1"); 58 | assertEquals("value1", value); 59 | String value2 = config("classpath:cache1.ini", "classpath:cache2.ini", "classpath:cache3.properties").evaluateToString("key2"); 60 | assertEquals("value2", value2); 61 | String value3 = config("classpath:cache1.ini", "classpath:cache2.ini", "classpath:cache3.properties").evaluateToString("key3"); 62 | assertEquals("value3", value3); 63 | } 64 | 65 | @Test 66 | public void cachedSinglePropertyFile() { 67 | String value = config("classpath:cache3.properties").evaluateToString("key3"); 68 | assertEquals("value3", value); 69 | } 70 | 71 | @Test(expected = ConstrettoExpressionException.class) 72 | public void withoutSystemProperties() { 73 | System.setProperty("key1", "sys-prop1"); 74 | try { 75 | config(false, "classpath:cache2.ini").evaluateToString("key1"); 76 | } finally { 77 | System.clearProperty("key1"); 78 | } 79 | } 80 | 81 | @Test 82 | public void withSystemProperties() throws Exception { 83 | final String systemPropertyValue = "sys-prop1"; 84 | System.setProperty("key1", systemPropertyValue); 85 | assertEquals(systemPropertyValue, config(true, "classpath:cache2.ini").evaluateToString("key1")); 86 | System.clearProperty("key1"); 87 | 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/model/ClassPathResourceTest.java: -------------------------------------------------------------------------------- 1 | package org.constretto.model; 2 | 3 | import org.junit.Test; 4 | 5 | import java.io.InputStream; 6 | 7 | import static org.junit.Assert.assertEquals; 8 | import static org.junit.Assert.assertFalse; 9 | import static org.junit.Assert.assertNull; 10 | 11 | /** 12 | * @author Sondre Eikanger Kvalø 13 | */ 14 | public class ClassPathResourceTest { 15 | 16 | public static final String NON_EXISITING_CLASSPATH_RESOURCE = "ttt.properties"; 17 | 18 | @Test 19 | public void testCreateClassPathResourceThatDoNotExist() throws Exception { 20 | final ClassPathResource classPathResource = new ClassPathResource(NON_EXISITING_CLASSPATH_RESOURCE); 21 | assertFalse(classPathResource.exists()); 22 | } 23 | 24 | @Test 25 | public void testOpenNoneExistingClassPathResource() throws Exception { 26 | final ClassPathResource classPathResource = new ClassPathResource(NON_EXISITING_CLASSPATH_RESOURCE); 27 | assertFalse(classPathResource.exists()); 28 | InputStream inputStream = classPathResource.getInputStream(); 29 | assertNull(inputStream); 30 | } 31 | 32 | @Test 33 | public void testToString() throws Exception { 34 | final ClassPathResource classPathResource = new ClassPathResource(NON_EXISITING_CLASSPATH_RESOURCE); 35 | assertEquals("ClassPathResource{path='" + NON_EXISITING_CLASSPATH_RESOURCE + "'}", classPathResource.toString()); 36 | 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/model/ConfigurationValueTest.java: -------------------------------------------------------------------------------- 1 | package org.constretto.model; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.assertEquals; 6 | 7 | /** 8 | * @author Sondre Eikanger Kvalø 9 | */ 10 | public class ConfigurationValueTest { 11 | 12 | public static final String SIMPLE_VALUE = "value"; 13 | 14 | @Test 15 | public void testNoTagGiven() throws Exception { 16 | assertEquals(ConfigurationValue.DEFAULT_TAG, new ConfigurationValue(new CPrimitive(SIMPLE_VALUE)).tag()); 17 | 18 | } 19 | 20 | @Test 21 | public void testValue() throws Exception { 22 | assertEquals(SIMPLE_VALUE, new ConfigurationValue(new CPrimitive(SIMPLE_VALUE)).value().toString()); 23 | } 24 | 25 | @Test 26 | public void testTag() throws Exception { 27 | final String tag = "tag"; 28 | assertEquals(tag, new ConfigurationValue(new CPrimitive(SIMPLE_VALUE), tag).tag()); 29 | } 30 | 31 | @Test 32 | public void testToString() throws Exception { 33 | final String stringDescription = new ConfigurationValue(null, null).toString(); 34 | assertEquals(3, stringDescription.split("null").length); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/model/FileResourceTest.java: -------------------------------------------------------------------------------- 1 | package org.constretto.model; 2 | 3 | import org.constretto.exception.ConstrettoException; 4 | import org.junit.Test; 5 | 6 | import java.io.File; 7 | 8 | import static org.junit.Assert.assertEquals; 9 | import static org.junit.Assert.assertFalse; 10 | import static org.junit.Assert.assertTrue; 11 | 12 | /** 13 | * @author Sondre Eikanger Kvalø 14 | */ 15 | public class FileResourceTest { 16 | 17 | private File file; 18 | 19 | @Test(expected = ConstrettoException.class) 20 | public void testCreateFileResourceForNullPath() throws Exception { 21 | new FileResource(null); 22 | } 23 | 24 | @Test(expected = ConstrettoException.class) 25 | public void testOpenFileResourceThatDoNotExist() throws Exception { 26 | final FileResource fileResource = new FileResource("devNullFile"); 27 | assertFalse(fileResource.exists()); 28 | fileResource.getInputStream(); 29 | } 30 | 31 | @Test 32 | public void checkThatFileResourceExists() throws Exception { 33 | final FileResource nonExisting = new FileResource("devNullFile"); 34 | assertFalse(nonExisting.exists()); 35 | final File file = new File(getClass().getResource("/cache1.ini").toURI()); 36 | assertTrue(file.exists()); 37 | 38 | final FileResource existing = new FileResource(file.toURI().toString()); 39 | assertTrue(existing.exists()); 40 | } 41 | 42 | @Test(expected = ConstrettoException.class) 43 | public void testOpenFileResourceForEmptyPath() throws Exception { 44 | final FileResource fileResource = new FileResource(""); 45 | assertFalse(fileResource.exists()); 46 | fileResource.getInputStream(); 47 | } 48 | 49 | @Test(expected = ConstrettoException.class) 50 | public void testOpenFileResourceForFilePrefixOnlyPath() throws Exception { 51 | final FileResource fileResource = new FileResource("file:"); 52 | assertFalse(fileResource.exists()); 53 | fileResource.getInputStream(); 54 | } 55 | 56 | @Test 57 | public void testToString() throws Exception { 58 | final FileResource fileResource = new FileResource("file:src/test/resources/cache1.ini"); 59 | assertEquals("FileResource{path='file:src/test/resources/cache1.ini'}", fileResource.toString()); 60 | 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/model/TaggedPropertySetTest.java: -------------------------------------------------------------------------------- 1 | package org.constretto.model; 2 | 3 | import org.junit.Test; 4 | 5 | /** 6 | * @author Sondre Eikanger Kvalø 7 | */ 8 | public class TaggedPropertySetTest { 9 | 10 | @Test(expected = IllegalArgumentException.class) 11 | public void testNullTags() throws Exception { 12 | new TaggedPropertySet(null, null, null); 13 | 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/model/UrlResourceTest.java: -------------------------------------------------------------------------------- 1 | package org.constretto.model; 2 | 3 | import org.constretto.exception.ConstrettoException; 4 | import org.junit.Test; 5 | 6 | import static org.junit.Assert.assertEquals; 7 | import static org.junit.Assert.assertFalse; 8 | import static org.junit.Assert.assertTrue; 9 | 10 | /** 11 | * @author Sondre Eikanger Kvalø 12 | */ 13 | public class UrlResourceTest { 14 | 15 | @Test(expected = ConstrettoException.class) 16 | public void testUrlResourceForNullPath() throws Exception { 17 | UrlResource urlResource = new UrlResource(null); 18 | } 19 | 20 | @Test(expected = ConstrettoException.class) 21 | public void testOpenNonExistingUrlResource() throws Exception { 22 | final UrlResource urlResource = new UrlResource("http://donotexist/"); 23 | urlResource.getInputStream(); 24 | } 25 | 26 | @Test(expected = ConstrettoException.class) 27 | public void testOpenMalformedUrlResource() throws Exception { 28 | final UrlResource urlResource = new UrlResource("++malformed:url:resource"); 29 | urlResource.getInputStream(); 30 | } 31 | 32 | @Test 33 | public void validUrlsThatDoNotExistShouldWork() throws Exception { 34 | final UrlResource urlResource = new UrlResource("http://loocalhost/notHere.html"); 35 | assertFalse(urlResource.exists()); 36 | } 37 | 38 | @Test 39 | public void validUrlsThatDoExistShouldWork() throws Exception { 40 | final UrlResource urlResource = new UrlResource("http://google.com"); 41 | assertTrue(urlResource.exists()); 42 | } 43 | 44 | @Test 45 | public void testToString() throws Exception { 46 | final UrlResource urlResource = new UrlResource("http://google.com"); 47 | assertEquals("UrlResource{path='http://google.com'}", urlResource.toString()); 48 | 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /constretto-core/src/test/java/org/constretto/resolver/PredefinedConfigurationContextResolverTest.java: -------------------------------------------------------------------------------- 1 | package org.constretto.resolver; 2 | 3 | import org.junit.Test; 4 | 5 | import java.util.Arrays; 6 | import java.util.List; 7 | 8 | import static org.junit.Assert.assertEquals; 9 | import static org.junit.Assert.assertNotSame; 10 | 11 | /** 12 | * @author zapodot 13 | */ 14 | public class PredefinedConfigurationContextResolverTest { 15 | 16 | @Test 17 | public void testEmpty() throws Exception { 18 | final PredefinedConfigurationContextResolver contextResolver = PredefinedConfigurationContextResolver.empty(); 19 | assertEquals(0, contextResolver.getTags().size()); 20 | 21 | } 22 | 23 | @Test 24 | public void testUsingTags() throws Exception { 25 | final String tag1 = "tag1"; 26 | final String tag2 = "tag2"; 27 | final List tags = PredefinedConfigurationContextResolver.usingTags(tag1, tag2).getTags(); 28 | assertEquals(tag1, tags.get(0)); 29 | assertEquals(tag2, tags.get(1)); 30 | } 31 | 32 | @Test 33 | public void testUsingTagsList() throws Exception { 34 | final String tag1 = "tag1"; 35 | final String tag2 = "tag2"; 36 | final List originalTagList = Arrays.asList(tag1, tag2); 37 | final List tags = PredefinedConfigurationContextResolver.usingTagsList(originalTagList).getTags(); 38 | assertNotSame(tags, originalTagList); 39 | assertEquals(2, tags.size()); 40 | assertEquals(tag1, tags.get(0)); 41 | assertEquals(tag2, tags.get(1)); 42 | } 43 | } -------------------------------------------------------------------------------- /constretto-core/src/test/resources/annotatedTest.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2008 the original author or authors. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | @production.datasource.username=produser 18 | @dev.datasource.username=devuser 19 | -------------------------------------------------------------------------------- /constretto-core/src/test/resources/cache1-override1.ini: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2011 the original author or authors. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | [default] 18 | key1=value1-override1 -------------------------------------------------------------------------------- /constretto-core/src/test/resources/cache1-override2.ini: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2011 the original author or authors. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | [default] 18 | key1=value1-override2 -------------------------------------------------------------------------------- /constretto-core/src/test/resources/cache1.ini: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2011 the original author or authors. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | [default] 18 | key1=value1 -------------------------------------------------------------------------------- /constretto-core/src/test/resources/cache2.ini: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2011 the original author or authors. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | [default] 18 | key2=value2 -------------------------------------------------------------------------------- /constretto-core/src/test/resources/cache3-override1.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2011 the original author or authors. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | key3=value3-override1 -------------------------------------------------------------------------------- /constretto-core/src/test/resources/cache3-override2.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2011 the original author or authors. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | key3=value3-override2 -------------------------------------------------------------------------------- /constretto-core/src/test/resources/cache3.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2011 the original author or authors. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | key3=value3 -------------------------------------------------------------------------------- /constretto-core/src/test/resources/constretto.ldif: -------------------------------------------------------------------------------- 1 | version: 1 2 | 3 | dn: dc=constretto,dc=org 4 | objectClass: domain 5 | objectClass: top 6 | dc: constretto 7 | 8 | dn: ou=groups,dc=constretto,dc=org 9 | objectclass: top 10 | objectclass: organizationalUnit 11 | ou: groups 12 | 13 | dn: cn=role_developer,ou=groups,dc=constretto,dc=org 14 | objectclass: top 15 | objectclass: groupOfUniqueNames 16 | cn: role_developer 17 | uniqueMember: cn=Kaare Nilsen,dc=constretto,dc=org 18 | uniqueMember: cn=Jon-Anders Teigen,dc=constretto,dc=org 19 | 20 | dn: cn=contributors,ou=groups,dc=constretto,dc=org 21 | objectclass: top 22 | objectclass: groupOfUniqueNames 23 | cn: contributors 24 | uniqueMember: cn=Sondre Eikanger Kvalo,dc=constretto,dc=org 25 | 26 | dn: cn=Kaare Nilsen,dc=constretto,dc=org 27 | objectclass: top 28 | objectclass: person 29 | objectclass: organizationalPerson 30 | objectclass: inetOrgPerson 31 | uid: kaarenilsen 32 | userPassword: password 33 | cn: Kaare Nilsen 34 | cn:: S8OlcmUgTmlsc2Vu 35 | sn: Person 36 | description: Tech lead and framework hater :-) 37 | telephoneNumber: +47 22 00 00 00 38 | 39 | dn: cn=Jon-Anders Teigen,dc=constretto,dc=org 40 | objectclass: top 41 | objectclass: person 42 | objectclass: organizationalPerson 43 | objectclass: inetOrgPerson 44 | uid: teigen 45 | userPassword: password 46 | cn: Jon-Anders Teigen 47 | sn: Person 48 | description: Developer 49 | telephoneNumber: +47 22 00 00 10 50 | 51 | dn: cn=Sondre Eikanger Kvalo,dc=constretto,dc=org 52 | objectclass: top 53 | objectclass: person 54 | objectclass: organizationalPerson 55 | objectclass: inetOrgPerson 56 | uid: zapodot 57 | userPassword: password 58 | cn: Sondre Eikanger Kvalo 59 | cn:: U29uZHJlIEVpa2FuZ2VyIEt2YWzDuA== 60 | sn: Person 61 | description: Developer 62 | telephoneNumber: +47 55 00 00 00 63 | -------------------------------------------------------------------------------- /constretto-core/src/test/resources/dynamic.properties: -------------------------------------------------------------------------------- 1 | stagedKey=default value 2 | @test.stagedKey=test value 3 | @prod.stagedKey=prod value 4 | 5 | stagedVariable=default 6 | @test.stagedVariable=test 7 | @prod.stagedVariable=prod 8 | -------------------------------------------------------------------------------- /constretto-core/src/test/resources/encrypted.properties: -------------------------------------------------------------------------------- 1 | #encrypted with password constretto 2 | encrypted_property=ENC(rdMaCnGRgcMOASpTUWCaQsRdwfG61nn50jm94rf9370=) -------------------------------------------------------------------------------- /constretto-core/src/test/resources/jsonTest.json: -------------------------------------------------------------------------------- 1 | { 2 | "name":"Kaare", 3 | "age":29 4 | } -------------------------------------------------------------------------------- /constretto-core/src/test/resources/leading-chars-strip.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2008 the original author or authors. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | leading.zero=0051 17 | leading.plus=+47 18 | -------------------------------------------------------------------------------- /constretto-core/src/test/resources/org/constretto/internal/provider/helper/provider-test-overloaded.ini: -------------------------------------------------------------------------------- 1 | [default] 2 | i-am=in-the-second-file-in-the-list -------------------------------------------------------------------------------- /constretto-core/src/test/resources/org/constretto/internal/provider/helper/provider-test-overloaded.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Used for testing configuration overloading 3 | # 4 | i-am=in-the-second-file-in-the-list -------------------------------------------------------------------------------- /constretto-core/src/test/resources/org/constretto/internal/provider/helper/provider-test.ini: -------------------------------------------------------------------------------- 1 | [default] 2 | key1=key1-value 3 | 4 | start-value=used 5 | middle-value=middle 6 | end-value=end 7 | 8 | at-start=#{start-value} at the beginning it works 9 | at-end=it works when its at the #{end-value} 10 | in-the-middle=when used in the #{middle-value}, it also works 11 | multiple-replacements=#{start-value} at the beginning and in the #{middle-value}. It also works 12 | circular=but when used in a #{circular}. It better throw an exception before giving a stack overflow :) 13 | transitive=It should not work for #{transitive-circular} either 14 | transitive-circular=#{transitive} 15 | 16 | webservices-base-url=http://webservice 17 | webservice.customer=#{webservices-base-url}/customer 18 | 19 | i-am=in-the-first-file-in-the-list 20 | 21 | [development] 22 | ionlyexistindevelopment=I only exist in development 23 | webservices-base-url=http://development.webservice 24 | 25 | 26 | [production] 27 | webservices-base-url=http://production.webservice -------------------------------------------------------------------------------- /constretto-core/src/test/resources/org/constretto/internal/provider/helper/provider-test.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Used for testing simple java properties 3 | # Also used for testing that properties without tag 4 | # is available for all runtime tag configurations 5 | # 6 | key1=key1-value 7 | 8 | # 9 | # Used for testing that properites with no default tag 10 | # are only available in mathing runtime tag configurations 11 | # 12 | @development.ionlyexistindevelopment=I only exist in development 13 | 14 | # 15 | # Used for testing variable resolving 16 | # 17 | start-value=used 18 | middle-value=middle 19 | end-value=end 20 | 21 | at-start=#{start-value} at the beginning it works 22 | at-end=it works when its at the #{end-value} 23 | in-the-middle=when used in the #{middle-value}, it also works 24 | multiple-replacements=#{start-value} at the beginning and in the #{middle-value}. It also works 25 | circular=but when used in a #{circular}. It better throw an exception before giving a stack overflow :) 26 | transitive=It should not work for #{transitive-circular} either 27 | transitive-circular=#{transitive} 28 | 29 | # 30 | # Used for testing that variable resolving works well together 31 | # with the taggin concept 32 | # 33 | webservices-base-url=http://webservice 34 | @development.webservices-base-url=http://development.webservice 35 | @production.webservices-base-url=http://production.webservice 36 | 37 | webservice.customer=#{webservices-base-url}/customer 38 | 39 | # 40 | # Used for testing configuration overloading 41 | # 42 | i-am=in-the-first-file-in-the-list -------------------------------------------------------------------------------- /constretto-core/src/test/resources/subClassData.properties: -------------------------------------------------------------------------------- 1 | 2 | value=value 3 | subValue=sub value 4 | subSubValue=sub sub value 5 | defaultMethodValue=default method value 6 | privateMethodValue=private method value -------------------------------------------------------------------------------- /constretto-core/src/test/resources/test-with-array-and-map.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2008 the original author or authors. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | simple-list=[1,2,3,4] 18 | simple-map={1:2,2:3} 19 | 20 | 21 | -------------------------------------------------------------------------------- /constretto-core/src/test/resources/test.ini: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2008 the original author or authors. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | [default] 18 | somedb.username=user0 19 | somedb.password=pass0 20 | 21 | [production] 22 | somedb.username=user1 23 | somedb.password=pass1 24 | 25 | [systest] 26 | somedb.username=user2 27 | somedb.password=pass2 -------------------------------------------------------------------------------- /constretto-core/src/test/resources/test.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2008 the original author or authors. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | somedb.username=user0 18 | 19 | @production.somedb.username=user1 20 | 21 | @systest.somedb.username=user2 22 | 23 | datasources.customer.password=password 24 | 25 | url.child=#{base-url}/child 26 | @production.base-url=http://constretto.org 27 | 28 | -------------------------------------------------------------------------------- /constretto-core/src/test/resources/yamlTest.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | { 3 | "name":"Kaare", 4 | "age":29 5 | } -------------------------------------------------------------------------------- /constretto-core/src/test/resources/yamlTest2.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | something unrelated: 123 3 | # A comment 4 | name: Erlend 5 | age: 34 6 | long text: | 7 | And there was much rejoicement 8 | Even if there was brainless humor. 9 | Lorum ipsum and other random text -------------------------------------------------------------------------------- /constretto-test/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | constretto 12 | org.constretto 13 | 3.0.0-SNAPSHOT 14 | 15 | 4.0.0 16 | constretto-test 17 | Constretto :: Test - ${project.version} 18 | 19 | 20 | 21 | org.apache.maven.plugins 22 | maven-compiler-plugin 23 | 24 | 8 25 | 8 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | junit 34 | junit 35 | compile 36 | 37 | 38 | org.constretto 39 | constretto-core 40 | 41 | 42 | org.constretto 43 | constretto-api 44 | 45 | 46 | org.reflections 47 | reflections 48 | 49 | 50 | org.slf4j 51 | slf4j-simple 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /constretto-test/src/main/java/org/constretto/test/ConstrettoJUnit4ClassRunner.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. Licensed under the Apache 3 | * License, Version 2.0 (the "License"); you may not use this file except in 4 | * compliance with the License. You may obtain a copy of the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law 6 | * or agreed to in writing, software distributed under the License is 7 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 8 | * KIND, either express or implied. See the License for the specific language 9 | * governing permissions and limitations under the License. 10 | */ 11 | package org.constretto.test; 12 | 13 | import org.constretto.annotation.Tags; 14 | import org.constretto.internal.ConstrettoUtils; 15 | import org.constretto.internal.resolver.DefaultConfigurationContextResolver; 16 | import org.junit.runner.notification.RunNotifier; 17 | import org.junit.runners.BlockJUnit4ClassRunner; 18 | import org.junit.runners.model.InitializationError; 19 | 20 | /** 21 | * @author Kaare Nilsen 22 | * @author Thor Åge Eldby 23 | */ 24 | public class ConstrettoJUnit4ClassRunner extends BlockJUnit4ClassRunner { 25 | 26 | public ConstrettoJUnit4ClassRunner(Class clazz) throws InitializationError { 27 | super(clazz); 28 | } 29 | 30 | private String changeTagsSystemProperty() { 31 | Tags tags = getTestClass().getJavaClass().getAnnotation(Tags.class); 32 | if (tags != null) { 33 | String tagcsv = ConstrettoUtils.asCsv(tags.value()); 34 | return System.setProperty(DefaultConfigurationContextResolver.TAGS, tagcsv.toString()); 35 | } 36 | return System.getProperty(DefaultConfigurationContextResolver.TAGS); 37 | } 38 | 39 | @Override 40 | public void run(RunNotifier notifier) { 41 | String originalValue = changeTagsSystemProperty(); 42 | super.run(notifier); 43 | if (originalValue == null) { 44 | System.getProperties().remove(DefaultConfigurationContextResolver.TAGS); 45 | } else { 46 | System.setProperty(DefaultConfigurationContextResolver.TAGS, originalValue); 47 | } 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /constretto-test/src/main/java/org/constretto/test/ConstrettoRule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. Licensed under the Apache 3 | * License, Version 2.0 (the "License"); you may not use this file except in 4 | * compliance with the License. You may obtain a copy of the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law 6 | * or agreed to in writing, software distributed under the License is 7 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 8 | * KIND, either express or implied. See the License for the specific language 9 | * governing permissions and limitations under the License. 10 | */ 11 | package org.constretto.test; 12 | 13 | import org.constretto.annotation.Tags; 14 | import org.constretto.test.extractors.ConstrettoEnvironmentExtractor; 15 | import org.constretto.test.extractors.ConstrettoTagExtractor; 16 | import org.junit.rules.TestRule; 17 | import org.junit.runner.Description; 18 | import org.junit.runners.model.Statement; 19 | 20 | import static org.constretto.internal.ConstrettoUtils.asCsv; 21 | import static org.constretto.internal.resolver.DefaultConfigurationContextResolver.TAGS; 22 | 23 | /** 24 | * Sets the CONSTRETTO_TAGS and CONSTRETTO_ENV system properties corresponding to 25 | * the value of the annotations {@link Tags} and {@link org.constretto.spring.annotation.Environment}, respectively, on the test class. 26 | * 27 | * @author Stig Kleppe-Jorgensen, 2013.01.14 28 | */ 29 | public class ConstrettoRule implements TestRule { 30 | 31 | private static final String ASSEMBLY_KEY = "CONSTRETTO_ENV"; 32 | 33 | @Override 34 | public Statement apply(final Statement base, final Description description) { 35 | return new Statement() { 36 | @Override 37 | public void evaluate() throws Throwable { 38 | 39 | final String originalTags = changeTagsSystemProperty(description); 40 | final String originalEnvironment = changeEnvironmentSystemProperty(description); 41 | 42 | try { 43 | base.evaluate(); 44 | } finally { 45 | if (originalTags == null) { 46 | System.getProperties().remove(TAGS); 47 | } else { 48 | System.setProperty(TAGS, originalTags); 49 | } 50 | 51 | if (originalEnvironment == null) { 52 | System.getProperties().remove(ASSEMBLY_KEY); 53 | } else { 54 | System.setProperty(ASSEMBLY_KEY, originalEnvironment); 55 | } 56 | } 57 | } 58 | }; 59 | } 60 | 61 | 62 | private String changeTagsSystemProperty(Description description) { 63 | final String[] tagValue = ConstrettoTagExtractor.findTagValueForDescription(description); 64 | 65 | if (tagValue == null) { 66 | return System.getProperty(TAGS); 67 | } else { 68 | return System.setProperty(TAGS, asCsv(tagValue)); 69 | } 70 | } 71 | 72 | private String changeEnvironmentSystemProperty(Description description) { 73 | 74 | if(constrettoEnvironmentAnnotationIsOnClasspath()) { 75 | final String[] environmentsDeclared = ConstrettoEnvironmentExtractor.extractEnvironmentValue(description); 76 | if (environmentsDeclared != null) { 77 | return System.setProperty(ASSEMBLY_KEY, asCsv(environmentsDeclared)); 78 | } 79 | } 80 | return System.getProperty(ASSEMBLY_KEY); 81 | 82 | } 83 | 84 | private boolean constrettoEnvironmentAnnotationIsOnClasspath() { 85 | try { 86 | Class.forName("org.constretto.spring.annotation.Environment"); 87 | } catch (ClassNotFoundException e) { 88 | return false; 89 | } 90 | return true; 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /constretto-test/src/main/java/org/constretto/test/extender/ConstrettoTagRuleExtender.java: -------------------------------------------------------------------------------- 1 | package org.constretto.test.extender; 2 | 3 | import org.constretto.test.extractors.ConstrettoTagExtractor; 4 | import org.junit.runner.Description; 5 | 6 | import static org.constretto.internal.ConstrettoUtils.asCsv; 7 | import static org.constretto.internal.resolver.DefaultConfigurationContextResolver.TAGS; 8 | 9 | /** 10 | * @author sondre 11 | */ 12 | public class ConstrettoTagRuleExtender implements RuleExtender { 13 | 14 | private String originalValue; 15 | 16 | @Override 17 | public void setup(final Description testDescription) { 18 | final String[] tagValuesForTest = ConstrettoTagExtractor.findTagValueForDescription(testDescription); 19 | this.originalValue = changeTagsSystemProperty(tagValuesForTest); 20 | 21 | } 22 | 23 | private String changeTagsSystemProperty(String[] tags) { 24 | 25 | if (tags == null) { 26 | return System.getProperty(TAGS); 27 | } else { 28 | return System.setProperty(TAGS, asCsv(tags)); 29 | } 30 | } 31 | 32 | 33 | @Override 34 | public void close() { 35 | if (originalValue == null) { 36 | System.clearProperty(TAGS); 37 | } else { 38 | System.setProperty(TAGS, originalValue); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /constretto-test/src/main/java/org/constretto/test/extender/Extenders.java: -------------------------------------------------------------------------------- 1 | package org.constretto.test.extender; 2 | 3 | import org.junit.runner.Description; 4 | import org.reflections.Reflections; 5 | 6 | import java.lang.reflect.InvocationTargetException; 7 | import java.util.Set; 8 | import java.util.function.Function; 9 | import java.util.stream.Collectors; 10 | 11 | /** 12 | * @author zapodot 13 | */ 14 | public class Extenders implements AutoCloseable { 15 | 16 | private Set extenders; 17 | 18 | public Extenders(final Set extenders) { 19 | this.extenders = extenders; 20 | 21 | } 22 | 23 | static Extenders createFromClasspathUsingDescription(final Description testDescription) { 24 | final Set extenders = findRuleExtenderClasses().stream().filter(aClass -> { 25 | try { 26 | return aClass.getDeclaredConstructor() != null; 27 | } catch (NoSuchMethodException e) { 28 | return false; 29 | } 30 | }).map((Function, RuleExtender>) aClass -> { 31 | try { 32 | return aClass.getDeclaredConstructor().newInstance(); 33 | } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { 34 | throw new IllegalStateException("Could not invoke default constructor on RuleExtender", e); 35 | } catch (NoSuchMethodException e) { 36 | throw new IllegalStateException("Could not instantiate RuleExtender", e); 37 | } 38 | }).collect(Collectors.toSet()); 39 | extenders.forEach(ruleExtender -> ruleExtender.setup(testDescription)); 40 | return new Extenders(extenders); 41 | } 42 | 43 | static Extenders createFromKnownSetWithDescription(Set extenders, final Description description) { 44 | for (final RuleExtender ruleExtender : extenders) { 45 | ruleExtender.setup(description); 46 | } 47 | return new Extenders(extenders); 48 | } 49 | 50 | private static Set> findRuleExtenderClasses() { 51 | return new Reflections(RuleExtender.class.getPackage().getName()).getSubTypesOf(RuleExtender.class); 52 | } 53 | 54 | @Override 55 | public void close() { 56 | for (RuleExtender ruleExtender : extenders) { 57 | ruleExtender.close(); 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /constretto-test/src/main/java/org/constretto/test/extender/RuleExtender.java: -------------------------------------------------------------------------------- 1 | package org.constretto.test.extender; 2 | 3 | import org.junit.runner.Description; 4 | 5 | /** 6 | * @author zapodot 7 | */ 8 | public interface RuleExtender extends AutoCloseable { 9 | 10 | /** 11 | * Pre-test hook 12 | */ 13 | void setup(final Description testDescription); 14 | 15 | /** 16 | * Run post tests to do cleanup 17 | */ 18 | @Override 19 | void close(); 20 | } 21 | -------------------------------------------------------------------------------- /constretto-test/src/main/java/org/constretto/test/extractors/ConstrettoEnvironmentExtractor.java: -------------------------------------------------------------------------------- 1 | package org.constretto.test.extractors; 2 | 3 | import org.constretto.exception.ConstrettoException; 4 | import org.junit.runner.Description; 5 | 6 | import java.lang.annotation.Annotation; 7 | import java.lang.reflect.InvocationTargetException; 8 | import java.lang.reflect.Method; 9 | 10 | /** 11 | * Class that helps extracting environment information from a test Class. Used by the {@link org.constretto.test.ConstrettoRule} class. 12 | * 13 | * @author Sondre Eikanger Kvalø 14 | */ 15 | public class ConstrettoEnvironmentExtractor implements TagExtractor { 16 | 17 | @Override 18 | public String[] findTagsForTest(final Description testDescription) { 19 | Class environmentAnnotationType = environmentAnnotation(); 20 | final Annotation envAnnotation = testDescription.getTestClass().getAnnotation(environmentAnnotationType); 21 | if (envAnnotation == null) { 22 | return null; 23 | } else { 24 | try { 25 | final Method valueMethod; 26 | valueMethod = envAnnotation.getClass().getMethod("value"); 27 | return (String[]) valueMethod.invoke(envAnnotation); 28 | } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { 29 | throw new ConstrettoException("Could not extract environment from test class", e); 30 | } 31 | } 32 | 33 | } 34 | 35 | private Class environmentAnnotation() { 36 | try { 37 | return (Class) Class.forName("org.constretto.spring.annotation.Environment"); 38 | } catch (ClassNotFoundException e) { 39 | return null; 40 | } 41 | } 42 | 43 | 44 | /** 45 | * Extracts the value of the {@link org.constretto.spring.annotation.Environment} annotation for the test class containing the given test method. 46 | * 47 | * @param description 48 | * @return an array with the specified values or null 49 | */ 50 | public static String[] extractEnvironmentValue(final Description description) { 51 | return new ConstrettoEnvironmentExtractor().findTagsForTest(description); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /constretto-test/src/main/java/org/constretto/test/extractors/ConstrettoTagExtractor.java: -------------------------------------------------------------------------------- 1 | package org.constretto.test.extractors; 2 | 3 | import org.constretto.annotation.Tags; 4 | import org.junit.runner.Description; 5 | 6 | /** 7 | * Utility for extracting values specified either on the method or class (or suite) level 8 | * 9 | * @author Sondre Eikanger Kvalø 10 | */ 11 | public class ConstrettoTagExtractor implements TagExtractor { 12 | 13 | 14 | @Override 15 | public String[] findTagsForTest(final Description testDescription) { 16 | final Tags tagValues = getClassAnnotation(testDescription); 17 | if (tagValues == null) { 18 | return null; 19 | } else { 20 | return tagValues.value(); 21 | } 22 | } 23 | 24 | public static String[] findTagValueForDescription(final Description description) { 25 | return new ConstrettoTagExtractor().findTagsForTest(description); 26 | } 27 | 28 | private static Tags getClassAnnotation(final Description description) { 29 | return description.getTestClass().getAnnotation(Tags.class); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /constretto-test/src/main/java/org/constretto/test/extractors/TagExtractor.java: -------------------------------------------------------------------------------- 1 | package org.constretto.test.extractors; 2 | 3 | import org.junit.runner.Description; 4 | 5 | /** 6 | * @author zapodot 7 | */ 8 | public interface TagExtractor { 9 | 10 | String[] findTagsForTest(final Description testDescription); 11 | } 12 | -------------------------------------------------------------------------------- /constretto-test/src/test/java/org/constretto/test/ConstrettoJUnit4ClassRunnerTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. Licensed under the Apache 3 | * License, Version 2.0 (the "License"); you may not use this file except in 4 | * compliance with the License. You may obtain a copy of the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law 6 | * or agreed to in writing, software distributed under the License is 7 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 8 | * KIND, either express or implied. See the License for the specific language 9 | * governing permissions and limitations under the License. 10 | */ 11 | package org.constretto.test; 12 | 13 | import org.constretto.ConstrettoBuilder; 14 | import org.constretto.ConstrettoConfiguration; 15 | import org.constretto.annotation.Tags; 16 | import org.junit.Assert; 17 | import org.junit.Test; 18 | import org.junit.runner.RunWith; 19 | 20 | import java.util.List; 21 | 22 | /** 23 | * @author Kaare Nilsen 24 | * @author Thor Åge Eldby 25 | */ 26 | @Tags({"purejunit", "test"}) 27 | @RunWith(ConstrettoJUnit4ClassRunner.class) 28 | public class ConstrettoJUnit4ClassRunnerTest { 29 | 30 | @Tags 31 | List currentEnvironment; 32 | 33 | @Test 34 | public void givenEnvironmentAnnotationOnTestClassWhenRunningTestThenConstrettoKnowsEnvironment() { 35 | ConstrettoConfiguration configuration = new ConstrettoBuilder().createSystemPropertiesStore().getConfiguration(); 36 | configuration.on(this); 37 | String[] expected = {"purejunit", "test"}; 38 | Assert.assertArrayEquals(expected, currentEnvironment.toArray(new String[0])); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /constretto-test/src/test/java/org/constretto/test/ConstrettoRuleClassRuleTest.java: -------------------------------------------------------------------------------- 1 | package org.constretto.test; 2 | 3 | import org.constretto.ConstrettoBuilder; 4 | import org.constretto.ConstrettoConfiguration; 5 | import org.constretto.annotation.Tags; 6 | import org.junit.ClassRule; 7 | import org.junit.Test; 8 | 9 | import java.util.List; 10 | 11 | import static org.junit.Assert.assertArrayEquals; 12 | 13 | /** 14 | * This source code is the property of NextGenTel AS 15 | * 16 | * @author sek 17 | */ 18 | @Tags({"purejunit", "test"}) 19 | public class ConstrettoRuleClassRuleTest { 20 | 21 | @Tags 22 | List currentEnvironment; 23 | 24 | @ClassRule 25 | public static ConstrettoRule constrettoRule = new ConstrettoRule(); 26 | 27 | @Test 28 | public void testAsClassRule() throws Exception { 29 | String[] expected = {"purejunit", "test"}; 30 | 31 | ConstrettoConfiguration configuration = 32 | new ConstrettoBuilder().createSystemPropertiesStore().getConfiguration(); 33 | configuration.on(this); 34 | 35 | assertArrayEquals(expected, currentEnvironment.toArray(new String[0])); 36 | 37 | 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /constretto-test/src/test/java/org/constretto/test/ConstrettoRuleTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 the original author or authors. Licensed under the Apache 3 | * License, Version 2.0 (the "License"); you may not use this file except in 4 | * compliance with the License. You may obtain a copy of the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law 6 | * or agreed to in writing, software distributed under the License is 7 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 8 | * KIND, either express or implied. See the License for the specific language 9 | * governing permissions and limitations under the License. 10 | */ 11 | package org.constretto.test; 12 | 13 | import org.constretto.ConstrettoBuilder; 14 | import org.constretto.ConstrettoConfiguration; 15 | import org.constretto.annotation.Tags; 16 | import org.junit.Rule; 17 | import org.junit.Test; 18 | 19 | import java.util.List; 20 | 21 | import static org.junit.Assert.assertArrayEquals; 22 | 23 | /** 24 | * Unit test of {@link ConstrettoRule}. 25 | * 26 | * @author Stig Kleppe-Jorgensen, 2013.01.14 27 | */ 28 | @Tags({"purejunit", "test"}) 29 | public class ConstrettoRuleTest { 30 | @Rule 31 | public ConstrettoRule constrettoRule = new ConstrettoRule(); 32 | 33 | @Tags 34 | List currentEnvironment; 35 | 36 | @Test 37 | public void givenEnvironmentAnnotationOnTestClassWhenRunningTestThenConstrettoKnowsEnvironment() { 38 | String[] expected = {"purejunit", "test"}; 39 | 40 | ConstrettoConfiguration configuration = 41 | new ConstrettoBuilder().createSystemPropertiesStore().getConfiguration(); 42 | configuration.on(this); 43 | 44 | assertArrayEquals(expected, currentEnvironment.toArray(new String[0])); 45 | } 46 | } 47 | --------------------------------------------------------------------------------