├── .circleci └── config.yml ├── .editorconfig ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── pom.xml └── src ├── main ├── java │ └── pl │ │ └── wavesoftware │ │ └── eid │ │ ├── DefaultEid.java │ │ ├── api │ │ ├── Binding.java │ │ ├── Configuration.java │ │ ├── ConfigurationBuilder.java │ │ ├── ConfigurationSystem.java │ │ ├── Configurator.java │ │ ├── Eid.java │ │ ├── EidContainer.java │ │ ├── EidFactories.java │ │ ├── EidFactory.java │ │ ├── EidMessage.java │ │ ├── EidMessageFactory.java │ │ ├── Formatter.java │ │ ├── LazyFactory.java │ │ ├── ReturnTypesAreNonnullByDefault.java │ │ ├── SerializableSupplier.java │ │ ├── Supplier.java │ │ ├── UniqueIdGenerator.java │ │ ├── Validator.java │ │ └── package-info.java │ │ ├── exceptions │ │ ├── EidIllegalArgumentException.java │ │ ├── EidIllegalStateException.java │ │ ├── EidIndexOutOfBoundsException.java │ │ ├── EidNullPointerException.java │ │ ├── EidRuntimeException.java │ │ └── package-info.java │ │ ├── impl │ │ ├── BindingImpl.java │ │ ├── ConfigurationImpl.java │ │ ├── ConfigurationSystemImpl.java │ │ ├── DefaultConfigurator.java │ │ ├── DefaultEidMessage.java │ │ ├── DefaultFormatter.java │ │ ├── DefaultUniqueIdGenerator.java │ │ ├── EidFactoriesImpl.java │ │ ├── EidFactoryImpl.java │ │ ├── EidMessageFactoryImpl.java │ │ ├── EidTextRepresentation.java │ │ ├── InternalChecks.java │ │ ├── Lazy.java │ │ ├── LazyFactoryImpl.java │ │ ├── MessageSupplier.java │ │ ├── MutableConfiguration.java │ │ ├── SerializableLazy.java │ │ ├── TextMessage.java │ │ └── package-info.java │ │ ├── package-info.java │ │ ├── system │ │ ├── BindingChooser.java │ │ ├── EidModule.java │ │ └── package-info.java │ │ └── utils │ │ ├── EidExecutions.java │ │ ├── EidPreconditions.java │ │ ├── EidUtil.java │ │ ├── UnsafeProcedure.java │ │ ├── UnsafeSupplier.java │ │ └── package-info.java └── resources │ └── META-INF │ └── services │ └── pl.wavesoftware.eid.api.Binding └── test ├── groovy └── verify-sonar-issues.groovy ├── java └── pl │ └── wavesoftware │ ├── eid │ ├── ConfigurationContext.java │ ├── ConfiguratorRule.java │ ├── ConstantUniqueIdRule.java │ ├── DisableValidatorState.java │ ├── EidIT.java │ ├── EidTest.java │ ├── api │ │ └── TestConfigurator.java │ ├── exceptions │ │ ├── A.java │ │ ├── AssertionsTest.java │ │ ├── EidIllegalArgumentExceptionTest.java │ │ ├── EidIllegalStateExceptionTest.java │ │ ├── EidIndexOutOfBoundsExceptionTest.java │ │ ├── EidNullPointerExceptionTest.java │ │ ├── EidRuntimeExceptionTest.java │ │ └── ExceptionsTest.java │ ├── impl │ │ ├── BindingImplTest.java │ │ ├── DefaultEidMessageTest.java │ │ ├── DefaultFormatterTest.java │ │ ├── DefaultUniqueIdGeneratorTest.java │ │ ├── EidTextRepresentationTest.java │ │ ├── InternalChecksTest.java │ │ ├── MessageSupplierTest.java │ │ └── TestBinding.java │ ├── system │ │ └── BindingChooserTest.java │ └── utils │ │ ├── EidExecutionsTest.java │ │ ├── EidPreconditionsIT.java │ │ └── EidPreconditionsTest.java │ └── testing │ ├── JavaAgentSkip.java │ ├── JavaVersion.java │ ├── JmhCleaner.java │ └── JvmArgs.java └── resources ├── META-INF └── services │ ├── pl.wavesoftware.eid.api.Binding │ └── pl.wavesoftware.eid.api.Configurator └── logback-test.xml /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | # Java Maven CircleCI 2.0 configuration file 2 | # 3 | # Check https://circleci.com/docs/2.0/language-java/ for more details 4 | # 5 | version: 2 6 | 7 | references: 8 | build: &build 9 | working_directory: ~/repo 10 | 11 | environment: 12 | # Customize the JVM maximum heap limit 13 | MAVEN_OPTS: -Xmx2048m 14 | JACOCO: false 15 | 16 | steps: 17 | - checkout 18 | 19 | # Download and cache dependencies 20 | - restore_cache: 21 | keys: 22 | - v2-dependencies-{{ checksum "pom.xml" }} 23 | # fallback to using the latest cache if no exact match is found 24 | - v2-dependencies- 25 | 26 | - run: 27 | name: Go Offline with Maven 28 | command: mvn verify -DskipTests 29 | 30 | - save_cache: 31 | paths: 32 | - ~/.m2 33 | key: v2-dependencies-{{ checksum "pom.xml" }} 34 | 35 | # run tests! 36 | - run: 37 | name: Run integration tests 38 | command: mvn verify 39 | 40 | workflows: 41 | version: 2 42 | integration-tests: 43 | jobs: 44 | - zulu-6 45 | - zulu-7 46 | - zulu-8 47 | - zulu-9 48 | - zulu-11 49 | 50 | jobs: 51 | zulu-6: 52 | docker: 53 | - image: wavesoftware/circleci-zulujdk:6 54 | <<: *build 55 | zulu-7: 56 | docker: 57 | - image: wavesoftware/circleci-zulujdk:7 58 | <<: *build 59 | zulu-8: 60 | docker: 61 | - image: wavesoftware/circleci-zulujdk:8 62 | <<: *build 63 | zulu-9: 64 | docker: 65 | - image: wavesoftware/circleci-zulujdk:9 66 | <<: *build 67 | zulu-11: 68 | docker: 69 | - image: wavesoftware/circleci-zulujdk:11 70 | <<: *build 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig is awesome: http://EditorConfig.org 2 | 3 | # top-most EditorConfig file 4 | root = true 5 | 6 | # Unix-style newlines with a newline ending every file 7 | [*] 8 | end_of_line = lf 9 | insert_final_newline = true 10 | charset = utf-8 11 | indent_style = space 12 | indent_size = 4 13 | 14 | [*.yml] 15 | indent_size = 2 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Maven 2 | /target 3 | 4 | *.class 5 | 6 | # Package Files # 7 | *.jar 8 | *.war 9 | *.ear 10 | 11 | # Eclipse IDE 12 | /.classpath 13 | /.project 14 | /.settings 15 | 16 | # Netbeans IDE 17 | /nbactions.xml 18 | /nbactions-ci.xml 19 | 20 | # IntiliJ IDE 21 | 22 | /*.iml 23 | /.idea 24 | 25 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | dist: trusty 3 | before_script: mvn dependency:go-offline 4 | script: mvn clean verify --fail-at-end 5 | notifications: 6 | email: 7 | on_failure: change 8 | matrix: 9 | include: 10 | # Quality testing 11 | - jdk: oraclejdk8 12 | env: JACOCO=true RELEASE_CHECKS=true 13 | - jdk: oraclejdk8 14 | env: JACOCO=true COVERALLS=true 15 | - jdk: oraclejdk8 16 | env: JACOCO=true SONAR=publish 17 | script: mvn clean verify sonar:sonar --fail-at-end 18 | # Performance testing 19 | - jdk: openjdk7 20 | env: JACOCO=false 21 | - jdk: openjdk8 22 | env: JACOCO=false 23 | - jdk: oraclejdk9 24 | env: JACOCO=false 25 | - jdk: oraclejdk11 26 | env: JACOCO=false 27 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/DefaultEid.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Wave Software 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 pl.wavesoftware.eid; 17 | 18 | import pl.wavesoftware.eid.api.Configurator; 19 | import pl.wavesoftware.eid.api.EidContainer; 20 | import pl.wavesoftware.eid.api.Eid; 21 | import pl.wavesoftware.eid.api.EidMessage; 22 | import pl.wavesoftware.eid.api.SerializableSupplier; 23 | import pl.wavesoftware.eid.api.Supplier; 24 | import pl.wavesoftware.eid.api.Validator; 25 | 26 | import javax.annotation.Nullable; 27 | 28 | import static pl.wavesoftware.eid.system.EidModule.MODULE; 29 | 30 | /** 31 | *

The Idea

32 | *

33 | * The main idea of this library is to use a set of simple runtime exceptions to 34 | * speedup development and make it more professional in the same time. Those 35 | * exceptions should always take the Exception ID (Eid for short) object on 36 | * construction. The Eid object should be generated by developer while writing 37 | * code and committed in the constructor of an exception. This eid object will 38 | * then be reported when that exception is being displayed or logged. 39 | *


40 | * This approach simplifies the management of exceptions in the application and 41 | * allows developers to focus on functionality and code quality, rather than 42 | * coming up with the correct statement for the exception. 43 | *


44 | * This error number is perfect to be displayed on the error "500" page for your 45 | * application as a bug reference. It's good idea, because it is static, so wil 46 | * l not change in subsequent invocations, but it also do not disclose the 47 | * actual reason why bug occurred. 48 | *


49 | * This approach is best to use with tools and IDE plugins like: 50 | *

56 | *

57 | * Error page can say something like: 58 | *


59 | *

60 | * We are deeply sorry. A fatal error occurred.
61 | * The reference number is: 20150721:100554
62 | *
63 | * Wait a couple of minutes, and try again. If the 64 | * same error number persists, call IT support. 65 | *
66 | *


67 | * That error page is easy to implement, because all those exceptions implement 68 | * {@link EidContainer} interface. 69 | *


70 | * Usage example: 71 | *

 72 |  * throw new EidIllegalStateException("20150721:100554", cause);
 73 |  * 
74 | * Example log: 75 | *
 76 |  * pl.wavesoftware.eid.exceptions.EidIllegalStateException: [20150721:100554]<g0qrwx> => Zipfile in invalid format
 77 |  * 	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 78 |  * 	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
 79 |  *
 80 |  * Caused by: java.util.zip.DataFormatException: Zipfile in invalid format
 81 |  * 	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 82 |  * 	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
 83 |  * 	... 62 more
 84 |  * 
85 | *

86 | * Eid number contains also additional unique ID to distinguish each single 87 | * exception from others with same Eid. 88 | * 89 | *

Caution

90 | *

91 | * Those classes shouldn't be used in any public API or library - those should 92 | * have proper checked exceptions build in design. It's designed to be used for 93 | * in-house development of end user applications which will report bugs in 94 | * standardized error pages or post them to issue tracker. 95 | *


96 | * Remember! You should never catch a runtime exception, in place other then 97 | * appropriate error page. 98 | * 99 | *

Notice

100 | *

101 | * Check out {@code EidPreconditions} class for ease of use utility checks. 102 | *

103 | * You can tweak behavior of Eid with {@link Configurator}. 104 | * 105 | * @author Krzysztof Suszynski 106 | * @see EidContainer 107 | * @see Configurator 108 | * @since 2.0.0 109 | */ 110 | public class DefaultEid implements Eid { 111 | 112 | private static final long serialVersionUID = 20181029193034L; 113 | 114 | private transient String id; 115 | @Nullable 116 | private transient String ref; 117 | private final SerializableSupplier uniqueId = MODULE 118 | .getBinding() 119 | .getFactories() 120 | .getLazyFactory() 121 | .lazy(new Supplier() { 122 | @Override 123 | public String get() { 124 | return MODULE.getBinding() 125 | .getConfigurationSystem() 126 | .getConfiguration() 127 | .getIdGenerator() 128 | .generateUniqId(); 129 | } 130 | }); 131 | 132 | /** 133 | * Constructor a single value of exception ID. 134 | * 135 | * @param id the exception id, should be uniquely generated by developer 136 | */ 137 | public DefaultEid(CharSequence id) { 138 | this.id = validate(id).toString(); 139 | this.ref = null; 140 | } 141 | 142 | /** 143 | * Constructor with two values an exception ID and reference of some sort. 144 | * The second reference can be used to hold reference from external system. 145 | * 146 | * @param id the exception id, should be uniquely generated by developer 147 | * @param ref a reference from external system 148 | */ 149 | public DefaultEid(CharSequence id, CharSequence ref) { 150 | this.id = validate(id).toString(); 151 | this.ref = ref.toString(); 152 | } 153 | 154 | /** 155 | * A static factory method for Eid object that takes a single value of 156 | * exception ID. 157 | * 158 | * @param id the exception id, should be uniquely generated by developer 159 | * @return a created Eid object 160 | */ 161 | public static Eid eid(String id) { 162 | return new DefaultEid(id); 163 | } 164 | 165 | @Override 166 | public String getId() { 167 | return id; 168 | } 169 | 170 | @Override 171 | @Nullable 172 | public String getRef() { 173 | return ref; 174 | } 175 | 176 | @Override 177 | public boolean hasRef() { 178 | return getRef() != null; 179 | } 180 | 181 | @Override 182 | public String getUnique() { 183 | return uniqueId.get(); 184 | } 185 | 186 | @Override 187 | public EidMessage message( 188 | CharSequence messageTemplate, 189 | Object... templateArguments 190 | ) { 191 | return MODULE.getBinding() 192 | .getFactories() 193 | .getMessageFactory() 194 | .create( 195 | this, 196 | messageTemplate, 197 | templateArguments 198 | ); 199 | } 200 | 201 | @Override 202 | public String toString() { 203 | return MODULE.getBinding() 204 | .getConfigurationSystem() 205 | .getConfiguration() 206 | .getFormatter() 207 | .format(this); 208 | } 209 | 210 | /* 211 | Suppress warnings id here for null check. Users can pass null event if it's 212 | forbidden. 213 | */ 214 | @SuppressWarnings({"ConstantConditions", "squid:S2583"}) 215 | private static CharSequence validate(CharSequence id) { 216 | if (id == null) { 217 | throw new IllegalArgumentException("Exception ID can't be null"); 218 | } 219 | if (isInvalid(id)) { 220 | throw new IllegalArgumentException( 221 | "Invalid ID given as an Exception ID: " + id 222 | ); 223 | } 224 | return id; 225 | } 226 | 227 | private static boolean isInvalid(CharSequence id) { 228 | Validator validator = getValidator(); 229 | return validator != null && !validator.isValid(id); 230 | } 231 | 232 | @Nullable 233 | private static Validator getValidator() { 234 | return MODULE.getBinding() 235 | .getConfigurationSystem() 236 | .getConfiguration() 237 | .getValidator(); 238 | } 239 | } 240 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/api/Binding.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.api; 18 | 19 | /** 20 | * A binding of Eid library. This interface can be used to completely 21 | * replace original binding with your custom implementation. 22 | * 23 | *

To replace implementation use {@link java.util.ServiceLoader} like: 24 | * 25 | *

META-INF/services/pl.wavesoftware.eid.api.Binding
26 | *

27 | * In that file, place a fully qualified class name of your class that implements 28 | * {@link Binding} interface. It should be called first time you reference an 29 | * {@link Eid}, or and of the eid exceptions, or utility preconditions class. 30 | * 31 | *

Usually it's way easier to just reconfigure existing configuration using 32 | * {@link Configurator} interface. 33 | * 34 | * @author Krzysztof Suszynski 35 | * @see Configurator 36 | * @see java.util.ServiceLoader 37 | * @since 2.0.0 38 | */ 39 | public interface Binding { 40 | 41 | /** 42 | * Gets a configuration system 43 | * 44 | * @return a configuration system 45 | */ 46 | ConfigurationSystem getConfigurationSystem(); 47 | 48 | /** 49 | * Get bound Eid factories to be used to perform various Eid 50 | * related operations. 51 | * 52 | * @return a factories for various Eid related things. 53 | */ 54 | EidFactories getFactories(); 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/api/Configuration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.api; 18 | 19 | import javax.annotation.Nullable; 20 | import java.util.Locale; 21 | import java.util.TimeZone; 22 | 23 | /** 24 | * Represents a configuration of Eid library. To reconfigure use 25 | * {@link Configurator} interface. 26 | * 27 | * @author Krzysztof Suszynski 28 | * @since 2.0.0 29 | * @see Configurator 30 | */ 31 | public interface Configuration { 32 | /** 33 | * Gets a Eid formatter 34 | * 35 | * @return a formatter of Eid 36 | */ 37 | Formatter getFormatter(); 38 | 39 | /** 40 | * Gets a Eid unique ID generator 41 | * 42 | * @return unique ID generator 43 | */ 44 | UniqueIdGenerator getIdGenerator(); 45 | 46 | /** 47 | * Gets a Eid validator if set. Returns null if validator wasn't configured. 48 | * 49 | * @return an Eid validator, or null 50 | */ 51 | @Nullable 52 | Validator getValidator(); 53 | 54 | /** 55 | * Gets a locale to be used when formatting texts. Returns null if locale 56 | * isn't set. 57 | * 58 | * @return a locale to be used when formatting texts, or null 59 | */ 60 | @Nullable 61 | Locale getLocale(); 62 | 63 | /** 64 | * Gets a time zone to be used when formatting texts. Returns null if time 65 | * zone isn't set. 66 | * 67 | * @return a time zone to be used when formatting texts, or null 68 | */ 69 | @Nullable 70 | TimeZone getTimeZone(); 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/api/ConfigurationBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.api; 18 | 19 | import javax.annotation.Nullable; 20 | import java.util.Locale; 21 | import java.util.TimeZone; 22 | 23 | /** 24 | * Use {@link Configurator} to configure Eid settings. 25 | * 26 | * @author Krzysztof Suszynski 27 | * @see Configurator 28 | * @since 2.0.0 29 | */ 30 | public interface ConfigurationBuilder { 31 | 32 | /** 33 | * Configures a unique ID generator used by Eid objects to generate unique 34 | * part of Eid number. 35 | * 36 | * @param generator a generator of unique ID 37 | * @return a self reference for ease of use 38 | */ 39 | ConfigurationBuilder uniqueIdGenerator(UniqueIdGenerator generator); 40 | 41 | /** 42 | * Configures a formatter to be used by Eid numbers. 43 | * 44 | * @param formatter a formatter to be used by Eid formatting and displaying 45 | * @return a self reference for ease of use 46 | */ 47 | ConfigurationBuilder formatter(Formatter formatter); 48 | 49 | /** 50 | * Sets a locale to be used when formatting texts. If not set default system 51 | * locale will be used (platform specific). See {@link Locale#getDefault()}. 52 | * 53 | * @param locale a locale to be used, if {@code null} was given default 54 | * locale will be used 55 | * @return a self reference for ease of use 56 | * @see Locale#getDefault() 57 | */ 58 | ConfigurationBuilder locale(@Nullable Locale locale); 59 | 60 | /** 61 | * Sets a time zone to be used when formatting texts. If not set, default 62 | * system time zone will be used (platform specific). See 63 | * {@link TimeZone#getDefault()}. 64 | * 65 | * @param zone a time zone to be used, if {@code null} was given default 66 | * time zone will be used 67 | * @return a self reference for ease of use 68 | * @see TimeZone#getDefault() 69 | */ 70 | ConfigurationBuilder timezone(@Nullable TimeZone zone); 71 | 72 | /** 73 | * Configures a validator that will be called on each Eid number. By 74 | * default, there is no validator configured for maximum speed. Using this 75 | * validator you can enforce a standardization of Eid numbers. 76 | * 77 | * @param validator a validator to be used, if {@code null} was given 78 | * validator will not be used. 79 | * @return a self reference for ease of use 80 | */ 81 | ConfigurationBuilder validator(@Nullable Validator validator); 82 | 83 | /** 84 | * Gets an object that is a future configuration, to be used to cross 85 | * configure elements of the configuration. 86 | *

87 | * It might throw exceptions if configuration isn't completed so use it 88 | * only if configuration is done. 89 | * 90 | * @return a future configuration 91 | */ 92 | Configuration getFutureConfiguration(); 93 | } 94 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/api/ConfigurationSystem.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.api; 18 | 19 | /** 20 | * A configuration provider interface. 21 | * 22 | * @author Krzysztof Suszynski 23 | * @since 2.0.0 24 | */ 25 | public interface ConfigurationSystem { 26 | /** 27 | * Get a configuration object 28 | * 29 | * @return a configuration object 30 | */ 31 | Configuration getConfiguration(); 32 | 33 | /** 34 | * Configures an Eid library programmatically. Note, that method returns a 35 | * configurator that can be used to restore configuration to the state 36 | * before you invoke this configuration method. 37 | * 38 | * @param configurator a configurator to use to configure Eid library 39 | * @return a reference to a configurator that can be used to restore 40 | * previous configuration 41 | */ 42 | Configurator configure(Configurator configurator); 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/api/Configurator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.api; 18 | 19 | import java.util.ServiceLoader; 20 | 21 | /** 22 | * You can use this interface with Java's {@link ServiceLoader} class. 23 | *

24 | * To do that, create on your classpath, a file: 25 | *

META-INF/services/pl.wavesoftware.eid.api.Configurator
26 | * 27 | * In that file, place a fully qualified class name of your class that implements 28 | * {@link Configurator} interface. It should be called first time you 29 | * reference an {@link Eid}, or and of the eid exceptions, or utility 30 | * preconditions class. 31 | * 32 | * @author Krzysztof Suszynski 33 | * @since 2.0.0 34 | * @see ServiceLoader 35 | */ 36 | public interface Configurator { 37 | 38 | /** 39 | * Configures an Eid configuration. 40 | * 41 | * @param configuration a configuration to be configured 42 | */ 43 | void configure(ConfigurationBuilder configuration); 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/api/Eid.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.api; 18 | 19 | import javax.annotation.Nullable; 20 | import java.io.Serializable; 21 | 22 | /** 23 | *

The Idea

24 | *

25 | * The main idea of this library is to use a set of simple runtime exceptions to 26 | * speedup development and make it more professional in the same time. Those 27 | * exceptions should always take the Exception ID (Eid for short) object on 28 | * construction. The Eid object should be generated by developer while writing 29 | * code and committed in the constructor of an exception. This eid object will 30 | * then be reported when that exception is being displayed or logged. 31 | *


32 | * This approach simplifies the management of exceptions in the application and 33 | * allows developers to focus on functionality and code quality, rather than 34 | * coming up with the correct statement for the exception. 35 | *


36 | * This error number is perfect to be displayed on the error "500" page for your 37 | * application as a bug reference. It's good idea, because it is static, so wil 38 | * l not change in subsequent invocations, but it also do not disclose the 39 | * actual reason why bug occurred. 40 | *


41 | * This approach is best to use with tools and IDE plugins like: 42 | *

48 | *

49 | * Error page can say something like: 50 | *


51 | *

52 | * We are deeply sorry. A fatal error occurred.
53 | * The reference number is: 20150721:100554
54 | *
55 | * Wait a couple of minutes, and try again. If the 56 | * same error number persists, call IT support. 57 | *
58 | *


59 | * That error page is easy to implement, because all those exceptions implement 60 | * {@link EidContainer} interface. 61 | *


62 | * Usage example: 63 | *

 64 |  * throw new EidIllegalStateException("20150721:100554", cause);
 65 |  * 
66 | * Example log: 67 | *
 68 |  * pl.wavesoftware.eid.exceptions.EidIllegalStateException: [20150721:100554]<g0qrwx> => Zipfile in invalid format
 69 |  * 	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 70 |  * 	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
 71 |  *
 72 |  * Caused by: java.util.zip.DataFormatException: Zipfile in invalid format
 73 |  * 	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 74 |  * 	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
 75 |  * 	... 62 more
 76 |  * 
77 | *

78 | * Eid number contains also additional unique ID to distinguish each single 79 | * exception from others with same Eid. 80 | * 81 | *

Caution

82 | *

83 | * Those classes shouldn't be used in any public API or library - those should 84 | * have proper checked exceptions build in design. It's designed to be used for 85 | * in-house development of end user applications which will report bugs in 86 | * standardized error pages or post them to issue tracker. 87 | *


88 | * Remember! You should never catch a runtime exception, in place other then 89 | * appropriate error page. 90 | * 91 | *

Notice

92 | *

93 | * Check out {@code EidPreconditions} class for ease of use utility checks. 94 | *

95 | * You can tweak behavior of Eid with {@link Configurator}. 96 | * 97 | * @author Krzysztof Suszynski 98 | * @see EidContainer 99 | * @see Configurator 100 | * @since 2.0.0 101 | */ 102 | public interface Eid extends Serializable { 103 | /** 104 | * Getter for constant Exception ID 105 | * 106 | * @return ID of exception 107 | */ 108 | String getId(); 109 | 110 | /** 111 | * Get an external reference passed to Exception ID 112 | * 113 | * @return an external reference, or null if not set 114 | */ 115 | @Nullable 116 | String getRef(); 117 | 118 | /** 119 | * Do this Exception ID has an external reference number? 120 | * 121 | * @return true, if has an external reference 122 | */ 123 | boolean hasRef(); 124 | 125 | /** 126 | * Gets unique generated string for this instance of Eid 127 | * 128 | * @return a unique string 129 | */ 130 | String getUnique(); 131 | 132 | /** 133 | * Creates a message object from this Eid, with given message template and 134 | * template arguments. Message template and arguments will be interpolated 135 | * using {@link java.text.MessageFormat#format(String, Object...)} method. 136 | * 137 | * @param messageTemplate a message format 138 | * @param templateArguments a message format arguments that will be 139 | * interpolated to format string 140 | * @return a message object 141 | */ 142 | EidMessage message( 143 | CharSequence messageTemplate, 144 | Object... templateArguments 145 | ); 146 | } 147 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/api/EidContainer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 pl.wavesoftware.eid.api; 17 | 18 | /** 19 | * Indicate that object contains a {@link Eid} object 20 | * 21 | * @author Krzysztof Suszynski 22 | * @see Eid 23 | */ 24 | public interface EidContainer { 25 | 26 | /** 27 | * Retrieves a Eid object 28 | * 29 | * @return Eid object 30 | */ 31 | Eid getEid(); 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/api/EidFactories.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.api; 18 | 19 | /** 20 | * Represents an Eid library bound factories. 21 | * 22 | * @author Krzysztof Suszynski 23 | * @since 2.0.0 24 | */ 25 | public interface EidFactories { 26 | 27 | /** 28 | * Gets an Eid message factory. 29 | * 30 | * @return a factory for Eid messages. 31 | */ 32 | EidMessageFactory getMessageFactory(); 33 | 34 | /** 35 | * Gets a factory for lazy objects. 36 | * 37 | * @return a lazy objects factory. 38 | */ 39 | LazyFactory getLazyFactory(); 40 | 41 | /** 42 | * Gets a Eid object factory. 43 | * 44 | * @return a factory for Eid objects 45 | */ 46 | EidFactory getEidFactory(); 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/api/EidFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.api; 18 | 19 | /** 20 | * An Eid object factory 21 | * 22 | * @author Krzysztof Suszynski 23 | * @since 2.0.0 24 | */ 25 | public interface EidFactory { 26 | /** 27 | * Constructs a single value of exception ID. 28 | * 29 | * @param id the exception id, should be uniquely generated by developer 30 | * @return a created Eid object 31 | */ 32 | Eid create(CharSequence id); 33 | 34 | /** 35 | * Constructs with two values an exception ID and reference of some sort. 36 | * The second reference can be used to hold reference from external system. 37 | * 38 | * @param id the exception id, should be uniquely generated by developer 39 | * @param ref a reference from external system 40 | * @return a created Eid object 41 | */ 42 | Eid create(CharSequence id, CharSequence ref); 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/api/EidMessage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.api; 18 | 19 | /** 20 | * The Eid message represents a message that contains a user given message 21 | * with arguments, as well as Eid object. 22 | * 23 | * @author Krzysztof Suszynski 24 | * @since 2.0.0 25 | */ 26 | public interface EidMessage extends CharSequence, EidContainer { 27 | 28 | /** 29 | * Get a formatted message 30 | * 31 | * @return a formatted message 32 | */ 33 | CharSequence getFormattedMessage(); 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/api/EidMessageFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.api; 18 | 19 | /** 20 | * Represents a factory for Eid messages. 21 | * 22 | * @author Krzysztof Suszynski 23 | * @since 2.0.0 24 | */ 25 | public interface EidMessageFactory { 26 | /** 27 | * Create a Exception ID message, from Eid, message template and 28 | * template arguments. 29 | * 30 | * @param eid an Exception ID object 31 | * @param messageTemplate message template in form accepted by 32 | * {@link java.text.MessageFormat#format(String, Object...)} 33 | * @param templateArguments a template arguments to be used to interpolate 34 | * into message 35 | * @return a create message with Eid number 36 | */ 37 | EidMessage create( 38 | Eid eid, 39 | CharSequence messageTemplate, 40 | Object[] templateArguments 41 | ); 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/api/Formatter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.api; 18 | 19 | /** 20 | * Represents a formatter that will be used to display an Eid number in logs 21 | * or screen. 22 | * 23 | * @author Krzysztof Suszynski 24 | * @since 2.0.0 25 | */ 26 | public interface Formatter { 27 | /** 28 | * Formats an Eid number to string 29 | * 30 | * @param eid an eid number 31 | * @return a string with formatted Eid number 32 | */ 33 | String format(Eid eid); 34 | 35 | /** 36 | * Formats an Eid paired with a message. 37 | * 38 | * @param eid an eid number to format 39 | * @param message a message to be pair to eid 40 | * @return a string with formatted eid and message 41 | */ 42 | String format(Eid eid, String message); 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/api/LazyFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.api; 18 | 19 | import java.io.Serializable; 20 | 21 | /** 22 | * A factory for a lazy objects. 23 | * 24 | * @author Krzysztof Suszynski 25 | * @since 2.0.0 26 | */ 27 | public interface LazyFactory { 28 | /** 29 | * Creates a lazy supplier of a given supplier of serializable value 30 | * 31 | * @param supplier a supplier of serializable value 32 | * @param a serializable type 33 | * @return a lazy, serializable, supplier 34 | */ 35 | SerializableSupplier lazy(Supplier supplier); 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/api/ReturnTypesAreNonnullByDefault.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.api; 18 | 19 | import javax.annotation.Nonnull; 20 | import javax.annotation.meta.TypeQualifierDefault; 21 | import java.lang.annotation.ElementType; 22 | import java.lang.annotation.Retention; 23 | import java.lang.annotation.RetentionPolicy; 24 | 25 | /** 26 | * This annotation can be applied to a package, class or method to indicate that 27 | * the method return types in that element are nonnull by default unless 28 | * there is: 29 | *

36 | * 37 | * @author Krzysztof Suszynski 38 | * @since 2016-03-26 39 | */ 40 | @Nonnull 41 | @TypeQualifierDefault(ElementType.METHOD) 42 | @Retention(RetentionPolicy.RUNTIME) 43 | public @interface ReturnTypesAreNonnullByDefault { 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/api/SerializableSupplier.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.api; 18 | 19 | import java.io.Serializable; 20 | 21 | /** 22 | * A serializable supplier. 23 | * 24 | * @author Krzysztof Suszynski 25 | * @since 2.0.0 26 | * @param a type of this supplier, must be serializable 27 | */ 28 | public interface SerializableSupplier 29 | extends Supplier, Serializable { 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/api/Supplier.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.api; 18 | 19 | /** 20 | * A supplier, inspired by Java 8 type of the same name. 21 | * 22 | * @author Krzysztof Suszynski 23 | * @since 2.0.0 24 | * @param a type of object that this supplier provides 25 | */ 26 | public interface Supplier { 27 | /** 28 | * Gets an object 29 | * 30 | * @return an object 31 | */ 32 | T get(); 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/api/UniqueIdGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.api; 18 | 19 | /** 20 | * It is used to generate unique ID for each EID object. It's mustn't be secure 21 | * because it just indicate EID object while logging. 22 | * 23 | * @author Krzysztof Suszynski 24 | * @since 1.0.0 25 | */ 26 | public interface UniqueIdGenerator { 27 | 28 | /** 29 | * Generates a unique string ID 30 | * 31 | * @return a generated unique ID 32 | */ 33 | String generateUniqId(); 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/api/Validator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.api; 18 | 19 | /** 20 | * Implementing this validator and configuring it can help you assure that every 21 | * Eid in your project, conform to your rules. 22 | *


23 | * Keep in mind that validation adds a little bit of cost, to every Eid creation, 24 | * even if that particular Eid don't turn up to be used. 25 | *


26 | * To use your {@code Validator}, use {@link Configurator}. 27 | * 28 | * @author Krzysztof Suszynski 29 | * @since 2.0.0 30 | * @see Configurator 31 | */ 32 | public interface Validator { 33 | /** 34 | * Checks an ID that will be used as an Exception ID. 35 | * 36 | * @param id an id to check 37 | * @return true, if given ID is valid 38 | */ 39 | boolean isValid(CharSequence id); 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/api/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | /** 18 | * @author Krzysztof Suszynski 19 | * @since 2018-10-31 20 | */ 21 | @ParametersAreNonnullByDefault 22 | @ReturnTypesAreNonnullByDefault 23 | package pl.wavesoftware.eid.api; 24 | 25 | import javax.annotation.ParametersAreNonnullByDefault; 26 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/exceptions/EidIllegalStateException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Wave Software 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 pl.wavesoftware.eid.exceptions; 17 | 18 | import pl.wavesoftware.eid.api.Eid; 19 | import pl.wavesoftware.eid.api.EidMessage; 20 | 21 | import javax.annotation.Nullable; 22 | 23 | /** 24 | * This exception is Eid version of Java's {@link IllegalStateException} that 25 | * holds an {@link Eid} object. It can be used to process it in application 26 | * central error handler. 27 | *

28 | * Caution! This class shouldn't be used in any public API or 29 | * library. It is designed to be used for in-house development of end user 30 | * applications which will report bugs in standardized error pages or post them 31 | * to issue tracker. 32 | * 33 | * @see IllegalStateException 34 | * @see EidRuntimeException 35 | * @author Krzysztof Suszynski 36 | * @since 0.1.0 37 | */ 38 | public class EidIllegalStateException extends EidRuntimeException { 39 | 40 | private static final long serialVersionUID = -9876432123423443L; 41 | 42 | /** 43 | * Constructs a new runtime exception with the specified Exception ID as 44 | * it's detail message, that's {@code new Eid(eid).toString()}. 45 | *

46 | * The cause is not initialized, and may subsequently be initialized by 47 | * a call to {@link #initCause}. 48 | * 49 | * @param eid an exception ID as character sequence 50 | */ 51 | public EidIllegalStateException(CharSequence eid) { 52 | super(eid); 53 | } 54 | 55 | /** 56 | * Constructs a new runtime exception with the specified Exception ID and 57 | * detail message. 58 | *

59 | * The cause is not initialized, and may subsequently be initialized by a 60 | * call to {@link #initCause}. 61 | * 62 | * @param eid an exception ID as character sequence 63 | * @param message the detail message. The detail message is saved for 64 | * later retrieval by the {@link #getMessage()} method. 65 | */ 66 | public EidIllegalStateException(CharSequence eid, String message) { 67 | super(eid, message); 68 | } 69 | 70 | /** 71 | * Constructs a new runtime exception with the specified Eid message. 72 | *

73 | * The cause is not initialized, and may subsequently be initialized by a 74 | * call to {@link #initCause}. 75 | * 76 | * @param message the Eid message. The detail message is saved for 77 | * later retrieval by the {@link #getMessage()} method. 78 | */ 79 | public EidIllegalStateException(EidMessage message) { 80 | super(message); 81 | } 82 | 83 | /** 84 | * Constructs a new runtime exception with the specified Exception ID, 85 | * detail message and cause. 86 | * 87 | *

88 | * Note that the detail message associated with cause is 89 | * not automatically incorporated in this runtime exception's 90 | * detail message. 91 | * 92 | * @param eid an exception ID as character sequence 93 | * @param message the detail message (which is saved for later retrieval 94 | * by the {@link #getMessage()} method). 95 | * @param cause the cause (which is saved for later retrieval by the 96 | * {@link #getCause()} method). (A null value is 97 | * permitted, and indicates that the cause is nonexistent or 98 | */ 99 | public EidIllegalStateException( 100 | CharSequence eid, String message, @Nullable Throwable cause 101 | ) { 102 | super(eid, message, cause); 103 | } 104 | 105 | /** 106 | * Constructs a new runtime exception with the specified Exception ID, 107 | * detail message and cause. 108 | * 109 | *

110 | * Note that the detail message associated with cause is 111 | * not automatically incorporated in this runtime exception's 112 | * detail message. 113 | * 114 | * @param message the Eid message (which is saved for later retrieval 115 | * by the {@link #getMessage()} method). 116 | * @param cause the cause (which is saved for later retrieval by the 117 | * {@link #getCause()} method). (A null value is 118 | * permitted, and indicates that the cause is nonexistent or 119 | */ 120 | public EidIllegalStateException( 121 | EidMessage message, @Nullable Throwable cause 122 | ) { 123 | super(message, cause); 124 | } 125 | 126 | /** 127 | * Constructs a new runtime exception with the specified Exception ID and 128 | * cause. 129 | *

130 | * A detail message of eid.toString() + (cause==null ? "" : cause.toString()) 131 | * (which typically contains the class and detail message of 132 | * cause). 133 | *

134 | * This constructor is useful for runtime exceptions that are little more 135 | * than wrappers for other throwables. 136 | * 137 | * @param eid an exception ID as character sequence 138 | * @param cause the cause (which is saved for later retrieval by the 139 | * {@link #getCause()} method). (A null value is 140 | * permitted, and indicates that the cause is nonexistent or 141 | */ 142 | public EidIllegalStateException( 143 | CharSequence eid, @Nullable Throwable cause 144 | ) { 145 | super(eid, cause); 146 | } 147 | 148 | /** 149 | * Constructs a new runtime exception with the specified Exception ID as 150 | * it's detail message, that's {@code eid.toString()}. 151 | *

152 | * The cause is not initialized, and may subsequently be initialized by 153 | * a call to {@link #initCause}. 154 | * 155 | * @param id an exception ID 156 | */ 157 | public EidIllegalStateException(Eid id) { 158 | super(id); 159 | } 160 | 161 | /** 162 | * Constructs a new runtime exception with the specified Exception ID and 163 | * detail message. 164 | *

165 | * The cause is not initialized, and may subsequently be initialized by a 166 | * call to {@link #initCause}. 167 | * 168 | * @param id an exception ID 169 | * @param message the detail message. The detail message is saved for 170 | * later retrieval by the {@link #getMessage()} method. 171 | */ 172 | public EidIllegalStateException(Eid id, String message) { 173 | super(id, message); 174 | } 175 | 176 | /** 177 | * Constructs a new runtime exception with the specified Exception ID, 178 | * detail message and cause. 179 | * 180 | *

181 | * Note that the detail message associated with cause is 182 | * not automatically incorporated in this runtime exception's 183 | * detail message. 184 | * 185 | * @param id an exception ID 186 | * @param message the detail message (which is saved for later retrieval 187 | * by the {@link #getMessage()} method). 188 | * @param cause the cause (which is saved for later retrieval by the 189 | * {@link #getCause()} method). (A null value is 190 | * permitted, and indicates that the cause is nonexistent or 191 | */ 192 | public EidIllegalStateException( 193 | Eid id, String message, @Nullable Throwable cause 194 | ) { 195 | super(id, message, cause); 196 | } 197 | 198 | /** 199 | * Constructs a new runtime exception with the specified Exception ID and 200 | * cause. 201 | *

202 | * A detail message of eid.toString() + (cause==null ? "" : cause.toString()) 203 | * (which typically contains the class and detail message of 204 | * cause). 205 | *

206 | * This constructor is useful for runtime exceptions that are little more 207 | * than wrappers for other throwables. 208 | * 209 | * @param id an exception ID 210 | * @param cause the cause (which is saved for later retrieval by the 211 | * {@link #getCause()} method). (A null value is 212 | * permitted, and indicates that the cause is nonexistent or 213 | */ 214 | public EidIllegalStateException(Eid id, @Nullable Throwable cause) { 215 | super(id, cause); 216 | } 217 | 218 | } 219 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/exceptions/EidNullPointerException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Wave Software 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 pl.wavesoftware.eid.exceptions; 17 | 18 | import pl.wavesoftware.eid.api.Eid; 19 | import pl.wavesoftware.eid.api.EidMessage; 20 | 21 | import javax.annotation.Nullable; 22 | 23 | /** 24 | * This exception is Eid version of Java's {@link NullPointerException} that 25 | * holds an {@link Eid} object. It can be used to process it in application 26 | * central error handler. 27 | *

28 | * Caution! This class shouldn't be used in any public API or 29 | * library. It is designed to be used for in-house development of end user 30 | * applications which will report bugs in standardized error pages or post them 31 | * to issue tracker. 32 | * 33 | * @see NullPointerException 34 | * @see EidRuntimeException 35 | * @author Krzysztof Suszynski 36 | * @since 0.1.0 37 | */ 38 | public class EidNullPointerException extends EidRuntimeException { 39 | 40 | private static final long serialVersionUID = -9876432123423469L; 41 | 42 | /** 43 | * Constructs a new runtime exception with the specified Exception ID as 44 | * it's detail message, that's {@code new Eid(eid).toString()}. 45 | *

46 | * The cause is not initialized, and may subsequently be initialized by 47 | * a call to {@link #initCause}. 48 | * 49 | * @param eid an exception ID as character sequence 50 | */ 51 | public EidNullPointerException(CharSequence eid) { 52 | super(eid); 53 | } 54 | 55 | /** 56 | * Constructs a new runtime exception with the specified Exception ID and 57 | * detail message. 58 | *

59 | * The cause is not initialized, and may subsequently be initialized by a 60 | * call to {@link #initCause}. 61 | * 62 | * @param eid an exception ID as character sequence 63 | * @param message the detail message. The detail message is saved for 64 | * later retrieval by the {@link #getMessage()} method. 65 | */ 66 | public EidNullPointerException(CharSequence eid, String message) { 67 | super(eid, message); 68 | } 69 | 70 | /** 71 | * Constructs a new runtime exception with the specified Eid message. 72 | *

73 | * The cause is not initialized, and may subsequently be initialized by a 74 | * call to {@link #initCause}. 75 | * 76 | * @param message the Eid message. The detail message is saved for 77 | * later retrieval by the {@link #getMessage()} method. 78 | */ 79 | public EidNullPointerException(EidMessage message) { 80 | super(message); 81 | } 82 | 83 | /** 84 | * Constructs a new runtime exception with the specified Exception ID, 85 | * detail message and cause. 86 | * 87 | *

88 | * Note that the detail message associated with cause is 89 | * not automatically incorporated in this runtime exception's 90 | * detail message. 91 | * 92 | * @param eid an exception ID as character sequence 93 | * @param message the detail message (which is saved for later retrieval 94 | * by the {@link #getMessage()} method). 95 | * @param cause the cause (which is saved for later retrieval by the 96 | * {@link #getCause()} method). (A null value is 97 | * permitted, and indicates that the cause is nonexistent or 98 | */ 99 | public EidNullPointerException( 100 | CharSequence eid, String message, @Nullable Throwable cause 101 | ) { 102 | super(eid, message, cause); 103 | } 104 | 105 | /** 106 | * Constructs a new runtime exception with the specified Exception ID, 107 | * detail message and cause. 108 | * 109 | *

110 | * Note that the detail message associated with cause is 111 | * not automatically incorporated in this runtime exception's 112 | * detail message. 113 | * 114 | * @param message the Eid message (which is saved for later retrieval 115 | * by the {@link #getMessage()} method). 116 | * @param cause the cause (which is saved for later retrieval by the 117 | * {@link #getCause()} method). (A null value is 118 | * permitted, and indicates that the cause is nonexistent or 119 | */ 120 | public EidNullPointerException( 121 | EidMessage message, @Nullable Throwable cause 122 | ) { 123 | super(message, cause); 124 | } 125 | 126 | /** 127 | * Constructs a new runtime exception with the specified Exception ID and 128 | * cause. 129 | *

130 | * A detail message of eid.toString() + (cause==null ? "" : cause.toString()) 131 | * (which typically contains the class and detail message of 132 | * cause). 133 | *

134 | * This constructor is useful for runtime exceptions that are little more 135 | * than wrappers for other throwables. 136 | * 137 | * @param eid an exception ID as character sequence 138 | * @param cause the cause (which is saved for later retrieval by the 139 | * {@link #getCause()} method). (A null value is 140 | * permitted, and indicates that the cause is nonexistent or 141 | */ 142 | public EidNullPointerException( 143 | CharSequence eid, @Nullable Throwable cause 144 | ) { 145 | super(eid, cause); 146 | } 147 | 148 | /** 149 | * Constructs a new runtime exception with the specified Exception ID as 150 | * it's detail message, that's {@code eid.toString()}. 151 | *

152 | * The cause is not initialized, and may subsequently be initialized by 153 | * a call to {@link #initCause}. 154 | * 155 | * @param id an exception ID 156 | */ 157 | public EidNullPointerException(Eid id) { 158 | super(id); 159 | } 160 | 161 | /** 162 | * Constructs a new runtime exception with the specified Exception ID and 163 | * detail message. 164 | *

165 | * The cause is not initialized, and may subsequently be initialized by a 166 | * call to {@link #initCause}. 167 | * 168 | * @param id an exception ID 169 | * @param message the detail message. The detail message is saved for 170 | * later retrieval by the {@link #getMessage()} method. 171 | */ 172 | public EidNullPointerException(Eid id, String message) { 173 | super(id, message); 174 | } 175 | 176 | /** 177 | * Constructs a new runtime exception with the specified Exception ID, 178 | * detail message and cause. 179 | * 180 | *

181 | * Note that the detail message associated with cause is 182 | * not automatically incorporated in this runtime exception's 183 | * detail message. 184 | * 185 | * @param id an exception ID 186 | * @param message the detail message (which is saved for later retrieval 187 | * by the {@link #getMessage()} method). 188 | * @param cause the cause (which is saved for later retrieval by the 189 | * {@link #getCause()} method). (A null value is 190 | * permitted, and indicates that the cause is nonexistent or 191 | */ 192 | public EidNullPointerException( 193 | Eid id, String message, @Nullable Throwable cause 194 | ) { 195 | super(id, message, cause); 196 | } 197 | 198 | /** 199 | * Constructs a new runtime exception with the specified Exception ID and 200 | * cause. 201 | *

202 | * A detail message of eid.toString() + (cause==null ? "" : cause.toString()) 203 | * (which typically contains the class and detail message of 204 | * cause). 205 | *

206 | * This constructor is useful for runtime exceptions that are little more 207 | * than wrappers for other throwables. 208 | * 209 | * @param id an exception ID 210 | * @param cause the cause (which is saved for later retrieval by the 211 | * {@link #getCause()} method). (A null value is 212 | * permitted, and indicates that the cause is nonexistent or 213 | */ 214 | public EidNullPointerException(Eid id, @Nullable Throwable cause) { 215 | super(id, cause); 216 | } 217 | 218 | } 219 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/exceptions/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | /** 18 | * @author Krzysztof Suszynski 19 | * @since 29.03.16 20 | */ 21 | @javax.annotation.ParametersAreNonnullByDefault 22 | @ReturnTypesAreNonnullByDefault 23 | package pl.wavesoftware.eid.exceptions; 24 | 25 | import pl.wavesoftware.eid.api.ReturnTypesAreNonnullByDefault; 26 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/impl/BindingImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.impl; 18 | 19 | import pl.wavesoftware.eid.api.Binding; 20 | import pl.wavesoftware.eid.api.ConfigurationSystem; 21 | import pl.wavesoftware.eid.api.EidFactories; 22 | 23 | /** 24 | * An internal implementation. Do not use manually. 25 | *


26 | * A default binding implementation. 27 | * 28 | * @author Krzysztof Suszynski 29 | * @since 2.0.0 30 | */ 31 | public final class BindingImpl implements Binding { 32 | 33 | private final ConfigurationSystem system = new ConfigurationSystemImpl(); 34 | private final EidFactories factories = new EidFactoriesImpl(this); 35 | 36 | @Override 37 | public ConfigurationSystem getConfigurationSystem() { 38 | return system; 39 | } 40 | 41 | @Override 42 | public EidFactories getFactories() { 43 | return factories; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/impl/ConfigurationImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.impl; 18 | 19 | import pl.wavesoftware.eid.api.Configuration; 20 | import pl.wavesoftware.eid.api.ConfigurationBuilder; 21 | import pl.wavesoftware.eid.api.Formatter; 22 | import pl.wavesoftware.eid.api.UniqueIdGenerator; 23 | import pl.wavesoftware.eid.api.Validator; 24 | 25 | import javax.annotation.Nullable; 26 | import java.util.Locale; 27 | import java.util.TimeZone; 28 | 29 | import static pl.wavesoftware.eid.impl.InternalChecks.checkNotNull; 30 | 31 | /** 32 | * @author Krzysztof Suszynski 33 | * @since 2.0.0 34 | */ 35 | final class ConfigurationImpl implements MutableConfiguration { 36 | 37 | private Formatter formatter; 38 | private UniqueIdGenerator generator; 39 | @Nullable 40 | private Validator validator; 41 | @Nullable 42 | private Locale locale; 43 | @Nullable 44 | private TimeZone zone; 45 | 46 | ConfigurationImpl() { 47 | // nothing here 48 | } 49 | 50 | ConfigurationImpl(MutableConfiguration settings) { 51 | checkNotNull(settings, "20181218:002046"); 52 | this.formatter = settings.getFormatter(); 53 | this.generator = settings.getIdGenerator(); 54 | this.validator = settings.getValidator(); 55 | this.locale = settings.getLocale(); 56 | this.zone = settings.getTimeZone(); 57 | } 58 | 59 | @Override 60 | public ConfigurationBuilder uniqueIdGenerator(UniqueIdGenerator generator) { 61 | this.generator = checkNotNull(generator, "20181218:002002"); 62 | return this; 63 | } 64 | 65 | @Override 66 | public ConfigurationBuilder formatter(Formatter formatter) { 67 | this.formatter = checkNotNull(formatter, "20181218:002018"); 68 | return this; 69 | } 70 | 71 | @Override 72 | public ConfigurationBuilder locale(@Nullable Locale locale) { 73 | this.locale = locale; 74 | return this; 75 | } 76 | 77 | @Override 78 | public ConfigurationBuilder timezone(@Nullable TimeZone zone) { 79 | this.zone = zone; 80 | return this; 81 | } 82 | 83 | @Override 84 | public ConfigurationBuilder validator(@Nullable Validator validator) { 85 | this.validator = validator; 86 | return this; 87 | } 88 | 89 | @Override 90 | public Configuration getFutureConfiguration() { 91 | return this; 92 | } 93 | 94 | @Override 95 | public Formatter getFormatter() { 96 | return formatter; 97 | } 98 | 99 | @Override 100 | public UniqueIdGenerator getIdGenerator() { 101 | return generator; 102 | } 103 | 104 | @Nullable 105 | @Override 106 | public Validator getValidator() { 107 | return validator; 108 | } 109 | 110 | @Nullable 111 | @Override 112 | public Locale getLocale() { 113 | return locale; 114 | } 115 | 116 | @Nullable 117 | @Override 118 | public TimeZone getTimeZone() { 119 | return zone; 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/impl/ConfigurationSystemImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.impl; 18 | 19 | import pl.wavesoftware.eid.api.Configuration; 20 | import pl.wavesoftware.eid.api.ConfigurationBuilder; 21 | import pl.wavesoftware.eid.api.ConfigurationSystem; 22 | import pl.wavesoftware.eid.api.Configurator; 23 | import pl.wavesoftware.eid.api.Supplier; 24 | 25 | import java.util.ServiceLoader; 26 | 27 | /** 28 | * @author Krzysztof Suszynski 29 | * @since 2018-10-29 30 | */ 31 | final class ConfigurationSystemImpl implements ConfigurationSystem { 32 | 33 | private Lazy configuration = 34 | Lazy.of(new MutableConfigurationSupplier()); 35 | 36 | ConfigurationSystemImpl() { 37 | // nothing here 38 | } 39 | 40 | @Override 41 | public Configuration getConfiguration() { 42 | return configuration.get(); 43 | } 44 | 45 | @Override 46 | public Configurator configure(Configurator configurator) { 47 | MutableConfiguration configured = configuration.get(); 48 | MutableConfiguration mutable = new ConfigurationImpl(configured); 49 | configurator.configure(mutable); 50 | configuration = Lazy.of(mutable); 51 | return new RestoreConfigurator(configured); 52 | } 53 | 54 | private static final class MutableConfigurationSupplier 55 | implements Supplier { 56 | 57 | @Override 58 | public MutableConfiguration get() { 59 | return loadConfiguration(); 60 | } 61 | 62 | private static MutableConfiguration loadConfiguration() { 63 | MutableConfiguration mutableConfiguration = new ConfigurationImpl(); 64 | new DefaultConfigurator().configure(mutableConfiguration); 65 | ServiceLoader configurators = 66 | ServiceLoader.load(Configurator.class); 67 | 68 | for (Configurator configurator : configurators) { 69 | configurator.configure(mutableConfiguration); 70 | } 71 | return mutableConfiguration; 72 | } 73 | } 74 | 75 | private static final class RestoreConfigurator 76 | implements Configurator { 77 | private final MutableConfiguration configuration; 78 | 79 | RestoreConfigurator(MutableConfiguration configuration) { 80 | this.configuration = configuration; 81 | } 82 | 83 | /* 84 | Suppress is for forcing nullable values to non null methods 85 | for copy constructor. 86 | */ 87 | @SuppressWarnings("ConstantConditions") 88 | @Override 89 | public void configure(ConfigurationBuilder builder) { 90 | builder.formatter(configuration.getFormatter()) 91 | .uniqueIdGenerator(configuration.getIdGenerator()) 92 | .validator(configuration.getValidator()) 93 | .locale(configuration.getLocale()); 94 | } 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/impl/DefaultConfigurator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.impl; 18 | 19 | import pl.wavesoftware.eid.api.Configurator; 20 | import pl.wavesoftware.eid.api.ConfigurationBuilder; 21 | 22 | /** 23 | * @author Krzysztof Suszynski 24 | * @since 2.0.0 25 | */ 26 | final class DefaultConfigurator implements Configurator { 27 | @Override 28 | public void configure(ConfigurationBuilder configuration) { 29 | configuration 30 | .formatter(new DefaultFormatter(configuration.getFutureConfiguration())) 31 | .uniqueIdGenerator(new DefaultUniqueIdGenerator()); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/impl/DefaultEidMessage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.impl; 18 | 19 | import pl.wavesoftware.eid.api.Configuration; 20 | import pl.wavesoftware.eid.api.Eid; 21 | import pl.wavesoftware.eid.api.EidMessage; 22 | 23 | import java.io.Serializable; 24 | 25 | /** 26 | * @author Krzysztof Suszynski 27 | * @since 2.0.0 28 | */ 29 | final class DefaultEidMessage implements EidMessage, Serializable { 30 | private static final long serialVersionUID = 20181029192322L; 31 | 32 | private final EidTextRepresentation represntation; 33 | 34 | DefaultEidMessage( 35 | Eid eid, 36 | Configuration configuration, 37 | CharSequence messageFormat, 38 | Object[] arguments 39 | ) { 40 | this.represntation = new EidTextRepresentation( 41 | eid, 42 | new TextMessage(configuration, messageFormat, arguments), 43 | configuration 44 | ); 45 | } 46 | 47 | @Override 48 | public int length() { 49 | return represntation.get().length(); 50 | } 51 | 52 | @Override 53 | public char charAt(int index) { 54 | return represntation.get().charAt(index); 55 | } 56 | 57 | @Override 58 | public CharSequence subSequence(int start, int end) { 59 | return represntation.get().subSequence(start, end); 60 | } 61 | 62 | @Override 63 | public Eid getEid() { 64 | return represntation.getEid(); 65 | } 66 | 67 | @Override 68 | public String toString() { 69 | return represntation.get(); 70 | } 71 | 72 | @Override 73 | public CharSequence getFormattedMessage() { 74 | return represntation.getTextMessage().get(); 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/impl/DefaultFormatter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.impl; 18 | 19 | import pl.wavesoftware.eid.api.Configuration; 20 | import pl.wavesoftware.eid.api.Eid; 21 | import pl.wavesoftware.eid.api.Formatter; 22 | 23 | import javax.annotation.Nullable; 24 | import java.util.Locale; 25 | 26 | /** 27 | * @author Krzysztof Suszynski 28 | * @since 2.0.0 29 | */ 30 | final class DefaultFormatter implements Formatter { 31 | 32 | private static final String FORMAT = "[%s]<%s>"; 33 | private static final String REF_FORMAT = "[%s|%s]<%s>"; 34 | private static final String MSG_FORMAT = "%s => %s"; 35 | 36 | private final Configuration configuration; 37 | 38 | DefaultFormatter(Configuration configuration) { 39 | this.configuration = configuration; 40 | } 41 | 42 | @Override 43 | public String format(Eid eid) { 44 | String fmt; 45 | Object[] params; 46 | if (eid.getRef() == null) { 47 | fmt = FORMAT; 48 | params = new Object[]{ 49 | eid.getId(), 50 | eid.getUnique() 51 | }; 52 | } else { 53 | fmt = REF_FORMAT; 54 | params = new Object[]{ 55 | eid.getId(), 56 | eid.getRef(), 57 | eid.getUnique() 58 | }; 59 | } 60 | java.util.Formatter formatter = getStringFormatter() 61 | .format(fmt, params); 62 | return formatter.toString(); 63 | } 64 | 65 | @Override 66 | public String format(Eid eid, String message) { 67 | return getStringFormatter() 68 | .format( 69 | MSG_FORMAT, 70 | format(eid), 71 | message 72 | ).toString(); 73 | } 74 | 75 | private java.util.Formatter getStringFormatter() { 76 | @Nullable 77 | Locale locale = configuration.getLocale(); 78 | if (locale != null) { 79 | return new java.util.Formatter(locale); 80 | } else { 81 | return new java.util.Formatter(); 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/impl/DefaultUniqueIdGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.impl; 18 | 19 | import pl.wavesoftware.eid.api.UniqueIdGenerator; 20 | 21 | import java.util.Random; 22 | 23 | /** 24 | * @author Krzysztof Suszynski 25 | */ 26 | final class DefaultUniqueIdGenerator implements UniqueIdGenerator { 27 | 28 | private static final int BASE36 = 36; 29 | private static final int MIN = 60466176; 30 | 31 | private final Random random; 32 | 33 | DefaultUniqueIdGenerator() { 34 | this.random = getUnsecuredFastRandom(); 35 | } 36 | 37 | @Override 38 | public String generateUniqId() { 39 | int calc = random.nextInt(Integer.MAX_VALUE - MIN) + MIN; 40 | return Integer.toString(calc, BASE36); 41 | } 42 | 43 | /* 44 | Using random for speed, security of generating random unique id is 45 | not important. 46 | */ 47 | @SuppressWarnings("squid:S2245") 48 | private static Random getUnsecuredFastRandom() { 49 | return new Random(System.currentTimeMillis()); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/impl/EidFactoriesImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.impl; 18 | 19 | import pl.wavesoftware.eid.api.Binding; 20 | import pl.wavesoftware.eid.api.EidFactories; 21 | import pl.wavesoftware.eid.api.EidFactory; 22 | import pl.wavesoftware.eid.api.EidMessageFactory; 23 | import pl.wavesoftware.eid.api.LazyFactory; 24 | 25 | /** 26 | * @author Krzysztof Suszynski 27 | * @since 2018-12-17 28 | */ 29 | final class EidFactoriesImpl implements EidFactories { 30 | private final EidMessageFactory eidMessageFactory; 31 | private final LazyFactory lazyFactory; 32 | private final EidFactory eidFactory; 33 | 34 | EidFactoriesImpl(Binding binding) { 35 | eidMessageFactory = new EidMessageFactoryImpl(binding); 36 | lazyFactory = new LazyFactoryImpl(); 37 | eidFactory = new EidFactoryImpl(); 38 | } 39 | 40 | @Override 41 | public EidMessageFactory getMessageFactory() { 42 | return eidMessageFactory; 43 | } 44 | 45 | @Override 46 | public LazyFactory getLazyFactory() { 47 | return lazyFactory; 48 | } 49 | 50 | @Override 51 | public EidFactory getEidFactory() { 52 | return eidFactory; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/impl/EidFactoryImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.impl; 18 | 19 | import pl.wavesoftware.eid.DefaultEid; 20 | import pl.wavesoftware.eid.api.EidFactory; 21 | import pl.wavesoftware.eid.api.Eid; 22 | 23 | /** 24 | * @author Krzysztof Suszynski 25 | * @since 2018-12-17 26 | */ 27 | final class EidFactoryImpl implements EidFactory { 28 | @Override 29 | public Eid create(CharSequence id) { 30 | return new DefaultEid(id); 31 | } 32 | 33 | @Override 34 | public Eid create(CharSequence id, CharSequence ref) { 35 | return new DefaultEid(id, ref); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/impl/EidMessageFactoryImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.impl; 18 | 19 | import pl.wavesoftware.eid.api.Binding; 20 | import pl.wavesoftware.eid.api.Eid; 21 | import pl.wavesoftware.eid.api.EidMessage; 22 | import pl.wavesoftware.eid.api.EidMessageFactory; 23 | 24 | /** 25 | * @author Krzysztof Suszynski 26 | * @since 2018-12-17 27 | */ 28 | final class EidMessageFactoryImpl implements EidMessageFactory { 29 | private final Binding binding; 30 | 31 | EidMessageFactoryImpl(Binding binding) { 32 | this.binding = binding; 33 | } 34 | 35 | @Override 36 | public EidMessage create( 37 | Eid eid, 38 | CharSequence messageTemplate, 39 | Object[] templateArguments 40 | ) { 41 | return new DefaultEidMessage( 42 | eid, 43 | binding.getConfigurationSystem().getConfiguration(), 44 | messageTemplate, 45 | templateArguments 46 | ); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/impl/EidTextRepresentation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.impl; 18 | 19 | import pl.wavesoftware.eid.api.Configuration; 20 | import pl.wavesoftware.eid.api.EidContainer; 21 | import pl.wavesoftware.eid.api.Eid; 22 | import pl.wavesoftware.eid.api.Supplier; 23 | 24 | import java.io.Serializable; 25 | 26 | /** 27 | * @author Krzysztof Suszynski 28 | * @since 2.0.0 29 | */ 30 | final class EidTextRepresentation implements EidContainer, Serializable { 31 | private static final long serialVersionUID = 20181029231519L; 32 | 33 | private final Eid eid; 34 | private final TextMessage textMessage; 35 | private final SerializableLazy actual; 36 | 37 | EidTextRepresentation( 38 | final Eid eid, 39 | final TextMessage textMessage, 40 | final Configuration configuration 41 | ) { 42 | this.eid = eid; 43 | this.textMessage = textMessage; 44 | this.actual = SerializableLazy.serializableOf(new Supplier() { 45 | @Override 46 | public String get() { 47 | return configuration.getFormatter() 48 | .format(eid, textMessage.get()); 49 | } 50 | }); 51 | } 52 | 53 | @Override 54 | public Eid getEid() { 55 | return eid; 56 | } 57 | 58 | TextMessage getTextMessage() { 59 | return textMessage; 60 | } 61 | 62 | String get() { 63 | return actual.get(); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/impl/InternalChecks.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.impl; 18 | 19 | import javax.annotation.Nullable; 20 | 21 | /** 22 | * @author Krzysztof Suszynski 23 | * @since 2.0.0 24 | */ 25 | final class InternalChecks { 26 | private InternalChecks() { 27 | // nothing here 28 | } 29 | 30 | @SuppressWarnings("squid:S1695") 31 | static T checkNotNull(@Nullable T value, String message) { 32 | if (value == null) { 33 | throw new IllegalArgumentException(message); 34 | } 35 | return value; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/impl/Lazy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.impl; 18 | 19 | import pl.wavesoftware.eid.api.Supplier; 20 | 21 | import static pl.wavesoftware.eid.impl.InternalChecks.checkNotNull; 22 | 23 | 24 | /** 25 | * @author Krzysztof Suszynski 26 | * @since 2.0.0 27 | */ 28 | class Lazy implements Supplier { 29 | private volatile Supplier supplier; 30 | private T value; 31 | 32 | Lazy(Supplier supplier) { 33 | this.supplier = checkNotNull(supplier, "20181124:004511"); 34 | } 35 | 36 | protected Lazy() { 37 | // nothing 38 | } 39 | 40 | private Lazy(T value) { 41 | this.value = value; 42 | } 43 | 44 | static Lazy of(Supplier supplier) { 45 | return new Lazy(supplier); 46 | } 47 | 48 | static Lazy of(R value) { 49 | return new Lazy(value); 50 | } 51 | 52 | @Override 53 | public T get() { 54 | if (supplier != null) { 55 | synchronized (this) { 56 | if (supplier != null) { 57 | T calculated = supplier.get(); 58 | supplier = null; 59 | value = calculated; 60 | } 61 | } 62 | } 63 | return value; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/impl/LazyFactoryImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.impl; 18 | 19 | import pl.wavesoftware.eid.api.LazyFactory; 20 | import pl.wavesoftware.eid.api.SerializableSupplier; 21 | import pl.wavesoftware.eid.api.Supplier; 22 | 23 | import java.io.Serializable; 24 | 25 | /** 26 | * @author Krzysztof Suszynski 27 | * @since 2018-12-17 28 | */ 29 | final class LazyFactoryImpl implements LazyFactory { 30 | @Override 31 | public SerializableSupplier lazy(Supplier supplier) { 32 | return SerializableLazy.serializableOf(supplier); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/impl/MessageSupplier.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.impl; 18 | 19 | import pl.wavesoftware.eid.api.Configuration; 20 | import pl.wavesoftware.eid.api.Supplier; 21 | 22 | import javax.annotation.Nullable; 23 | import java.text.MessageFormat; 24 | import java.text.SimpleDateFormat; 25 | import java.util.Locale; 26 | import java.util.TimeZone; 27 | 28 | /** 29 | * @author Krzysztof Suszynski 30 | * @since 2.0.0 31 | */ 32 | final class MessageSupplier 33 | implements Supplier { 34 | 35 | private final Configuration configuration; 36 | private final CharSequence messageFormat; 37 | private final Object[] arguments; 38 | 39 | MessageSupplier( 40 | Configuration configuration, 41 | CharSequence messageFormat, 42 | Object[] arguments 43 | ) { 44 | this.configuration = configuration; 45 | this.messageFormat = messageFormat; 46 | this.arguments = arguments.clone(); 47 | } 48 | 49 | @Override 50 | public String get() { 51 | return getFormatter().format(arguments); 52 | } 53 | 54 | private MessageFormat getFormatter() { 55 | @Nullable 56 | Locale locale = configuration.getLocale(); 57 | MessageFormat format; 58 | if (locale == null) { 59 | format = new MessageFormat(messageFormat.toString()); 60 | } else { 61 | format = new MessageFormat(messageFormat.toString(), locale); 62 | } 63 | return ensureTimeZone(format); 64 | } 65 | 66 | private MessageFormat ensureTimeZone(MessageFormat messageFormat) { 67 | @Nullable 68 | TimeZone zone = configuration.getTimeZone(); 69 | if (zone != null) { 70 | Object[] formats = messageFormat.getFormats(); 71 | for (Object format : formats) { 72 | if (format instanceof SimpleDateFormat) { 73 | ((SimpleDateFormat) format).setTimeZone(zone); 74 | } 75 | } 76 | } 77 | return messageFormat; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/impl/MutableConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.impl; 18 | 19 | import pl.wavesoftware.eid.api.ConfigurationBuilder; 20 | import pl.wavesoftware.eid.api.Configuration; 21 | 22 | /** 23 | * @author Krzysztof Suszynski 24 | * @since 2.0.0 25 | */ 26 | interface MutableConfiguration 27 | extends Configuration, ConfigurationBuilder { 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/impl/SerializableLazy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.impl; 18 | 19 | import pl.wavesoftware.eid.api.SerializableSupplier; 20 | import pl.wavesoftware.eid.api.Supplier; 21 | 22 | import java.io.IOException; 23 | import java.io.ObjectOutputStream; 24 | import java.io.Serializable; 25 | 26 | /** 27 | * @author Krzysztof Suszynski 28 | * @since 2018-11-24 29 | */ 30 | final class SerializableLazy 31 | extends Lazy 32 | implements SerializableSupplier { 33 | 34 | private static final long serialVersionUID = 20181124011908L; 35 | private volatile T serializable; 36 | 37 | private SerializableLazy(Supplier supplier) { 38 | super(supplier); 39 | } 40 | 41 | static SerializableLazy serializableOf(Supplier supplier) { 42 | return new SerializableLazy(supplier); 43 | } 44 | 45 | @Override 46 | public T get() { 47 | if (serializable == null) { 48 | synchronized (this) { 49 | if (serializable == null) { 50 | serializable = super.get(); 51 | } 52 | } 53 | } 54 | return serializable; 55 | } 56 | 57 | /** 58 | * Ensures that the value is evaluated before serialization. 59 | * 60 | * @param stream An object serialization stream. 61 | * @throws java.io.IOException If an error occurs writing to the stream. 62 | */ 63 | private void writeObject(ObjectOutputStream stream) throws IOException { 64 | // evaluates the values if it isn't evaluated yet! 65 | get(); 66 | stream.defaultWriteObject(); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/impl/TextMessage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.impl; 18 | 19 | import pl.wavesoftware.eid.api.Configuration; 20 | 21 | import java.io.Serializable; 22 | 23 | /** 24 | * @author Krzysztof Suszynski 25 | * @since 2.0.0 26 | */ 27 | final class TextMessage implements Serializable { 28 | private static final long serialVersionUID = 20181029231527L; 29 | 30 | private final SerializableLazy message; 31 | 32 | TextMessage( 33 | Configuration configuration, 34 | CharSequence messageFormat, 35 | Object[] arguments 36 | ) { 37 | message = SerializableLazy.serializableOf(new MessageSupplier( 38 | configuration, messageFormat, arguments 39 | )); 40 | } 41 | 42 | String get() { 43 | return message.get(); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/impl/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | /** 18 | * @author Krzysztof Suszynski 19 | * @since 2.0.0 20 | */ 21 | @ReturnTypesAreNonnullByDefault 22 | @ParametersAreNonnullByDefault 23 | package pl.wavesoftware.eid.impl; 24 | 25 | import pl.wavesoftware.eid.api.ReturnTypesAreNonnullByDefault; 26 | 27 | import javax.annotation.ParametersAreNonnullByDefault; 28 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Wave Software 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 | /** 18 | * @author Krzysztof Suszynski 19 | * @since 2018-10-25 20 | */ 21 | @ReturnTypesAreNonnullByDefault 22 | @ParametersAreNonnullByDefault 23 | package pl.wavesoftware.eid; 24 | 25 | import pl.wavesoftware.eid.api.ReturnTypesAreNonnullByDefault; 26 | 27 | import javax.annotation.ParametersAreNonnullByDefault; 28 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/system/BindingChooser.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.system; 18 | 19 | import pl.wavesoftware.eid.api.Binding; 20 | import pl.wavesoftware.eid.impl.BindingImpl; 21 | 22 | import java.io.Serializable; 23 | 24 | /** 25 | * @author Krzysztof Suszynski 26 | * @since 2.0.0 27 | */ 28 | final class BindingChooser implements Serializable { 29 | private static final long serialVersionUID = 20181218003610L; 30 | 31 | Binding chooseImplementation(Iterable bindings) { 32 | Binding best = null; 33 | for (Binding bond : bindings) { 34 | if (best == null || !isCoreImplementation(bond)) { 35 | best = bond; 36 | } 37 | } 38 | assert best != null : "20181106:000523"; 39 | return best; 40 | } 41 | 42 | private static boolean isCoreImplementation(Binding bond) { 43 | return bond.getClass() == BindingImpl.class; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/system/EidModule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.system; 18 | 19 | import pl.wavesoftware.eid.api.Binding; 20 | 21 | import java.util.ServiceLoader; 22 | 23 | /** 24 | * Represents a EID library module, and it's configuration binding. 25 | *

26 | * Utilizing {@link #getBinding()} method it is possible to reconfigure this 27 | * module to perform differently then the default behavior. 28 | * 29 | * @author Krzysztof Suszynski 30 | * @since 2.0.0 31 | */ 32 | public enum EidModule { 33 | MODULE; 34 | 35 | private final Binding binding; 36 | 37 | EidModule() { 38 | ServiceLoader loader = ServiceLoader.load(Binding.class); 39 | BindingChooser chooser = new BindingChooser(); 40 | binding = chooser.chooseImplementation(loader); 41 | } 42 | 43 | /** 44 | * Provides a binding to Eid library. It can be used to reconfigure 45 | * configuration programmatically. 46 | * 47 | * @return a binding interface 48 | */ 49 | public Binding getBinding() { 50 | return binding; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/system/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | /** 18 | * @author Krzysztof Suszynski 19 | * @since 2.0.0 20 | */ 21 | @ParametersAreNonnullByDefault 22 | @ReturnTypesAreNonnullByDefault 23 | package pl.wavesoftware.eid.system; 24 | 25 | import pl.wavesoftware.eid.api.ReturnTypesAreNonnullByDefault; 26 | 27 | import javax.annotation.ParametersAreNonnullByDefault; 28 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/utils/EidExecutions.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.utils; 18 | 19 | import pl.wavesoftware.eid.api.Eid; 20 | import pl.wavesoftware.eid.exceptions.EidRuntimeException; 21 | 22 | import static pl.wavesoftware.eid.utils.EidUtil.ensureEid; 23 | 24 | /** 25 | *

Functional try to execute blocks

26 | * Using functional blocks to handle operations, that are intended to operate 27 | * properly, simplify the code and makes it more readable. It's also good way to 28 | * deal with untested, uncovered {@code catch} blocks. It's easy and gives 29 | * developers nice way of dealing with countless operations that suppose to work 30 | * as intended. 31 | *

32 | * Example: 33 | *

 34 |  * InputStream is = EidExecutions.tryToExecute(new UnsafeSupplier<InputStream>() {
 35 |  *     @Override
 36 |  *     public InputStream get() throws IOException {
 37 |  *         return this.getClass().getClassLoader()
 38 |  *             .getResourceAsStream("project.properties");
 39 |  *     }
 40 |  * }, "20150718:121521");
 41 |  * 
42 | * 43 | * @author Krzysztof Suszynski 44 | * @since 2.0.0 45 | */ 46 | public final class EidExecutions { 47 | 48 | private EidExecutions() { 49 | // nothing here 50 | } 51 | 52 | /** 53 | * Tries to execute code in given unsafe supplier code block, and if 54 | * exception is thrown, it will gets rethrown as a {@link EidRuntimeException} 55 | * with eid given as a argument. This is because this exception is 56 | * treated as a software bug! 57 | *

58 | * Example: 59 | *

 60 |      * Document doc = EidExecutions.tryToExecute(new UnsafeSupplier<Document>}() {
 61 |      *     @Override
 62 |      *     public Document get() throws SAXException, IOException {
 63 |      *         DocumentBuilder docBuilder = ...
 64 |      *         return docBuilder.parse(new InputSource(reader));
 65 |      *     }
 66 |      * }, new Eid("20150718:121521"));
 67 |      * 
68 | * 69 | * @param return type 70 | * @param supplier unsafe supplier code to be executed within a try-catch 71 | * block 72 | * @param eid unique developer identifier from date for ex.: 73 | * "20150716:123200" 74 | * @return A block of code return type, if exception is not thrown 75 | * @throws EidRuntimeException if code block thrown any exception, which 76 | * in that case is wrapped in EidRuntimeException 77 | */ 78 | public static R tryToExecute( 79 | final UnsafeSupplier supplier, 80 | final Eid eid 81 | ) { 82 | try { 83 | return supplier.get(); 84 | } catch (Exception throwable) { 85 | throw new EidRuntimeException(ensureEid(eid), throwable); 86 | } 87 | } 88 | 89 | /** 90 | * Tries to execute code in given unsafe procedure code block, and if 91 | * exception is thrown, it will gets rethrown as a {@link EidRuntimeException} 92 | * with eid given as a argument. This is because this exception is treated 93 | * as a software bug! 94 | *

95 | * Example: 96 | *

 97 |      * EidExecutions.tryToExecute(new UnsafeProcedure() {
 98 |      *     @Override
 99 |      *     public void execute() throws SQLException {
100 |      *         stmt.executeQuery(userPersist);
101 |      *     }
102 |      * }, new Eid("20151117:184627"));
103 |      * 
104 | * 105 | * @param procedure unsafe procedure code to be executed within a try-catch 106 | * block 107 | * @param eid unique developer identifier from date for ex.: 108 | * "20150716:123200" 109 | * @throws EidRuntimeException if code block thrown any exception, which in 110 | * that case is wrapped in EidRuntimeException 111 | */ 112 | public static void tryToExecute( 113 | final UnsafeProcedure procedure, 114 | final Eid eid 115 | ) { 116 | try { 117 | procedure.execute(); 118 | } catch (Exception throwable) { 119 | throw new EidRuntimeException(ensureEid(eid), throwable); 120 | } 121 | } 122 | 123 | /** 124 | * For more info in JavaDoc see {@link 125 | * EidExecutions#tryToExecute(UnsafeSupplier, Eid)} 126 | *

127 | * Please, note that for performance reasons, Eid is not evaluated until 128 | * it's needed. If you are using {@code Validator}, 129 | * please use {@link #tryToExecute(UnsafeSupplier, Eid)} instead. 130 | * 131 | * @param return type 132 | * @param supplier unsafe supplier code to be executed within a try-catch 133 | * block 134 | * @param eid unique developer identifier from date for ex.: 135 | * "20150716:123200" 136 | * @return A block of code return type, if exception is not thrown 137 | * @throws EidRuntimeException if code block thrown any exception, which in 138 | * that case is wrapped in EidRuntimeException 139 | * @see EidExecutions#tryToExecute(UnsafeSupplier, Eid) 140 | */ 141 | public static R tryToExecute( 142 | final UnsafeSupplier supplier, 143 | final String eid 144 | ) { 145 | try { 146 | return supplier.get(); 147 | } catch (Exception throwable) { 148 | throw new EidRuntimeException(ensureEid(eid), throwable); 149 | } 150 | } 151 | 152 | /** 153 | * For more info in JavaDoc see {@link 154 | * EidExecutions#tryToExecute(UnsafeProcedure, Eid)} 155 | *

156 | * Please, note that for performance reasons, Eid is not evaluated until 157 | * it's needed. If you are using {@code Validator}, 158 | * please use {@link #tryToExecute(UnsafeProcedure, Eid)} instead. 159 | * 160 | * @param procedure unsafe procedure code to be executed within a try-catch 161 | * block 162 | * @param eid unique developer identifier from date for ex.: 163 | * "20150716:123200" 164 | * @throws EidRuntimeException if code block thrown any exception, which in 165 | * that case is wrapped in EidRuntimeException 166 | * @see EidExecutions#tryToExecute(UnsafeProcedure, Eid) 167 | */ 168 | public static void tryToExecute( 169 | final UnsafeProcedure procedure, 170 | final String eid 171 | ) { 172 | try { 173 | procedure.execute(); 174 | } catch (Exception throwable) { 175 | throw new EidRuntimeException(ensureEid(eid), throwable); 176 | } 177 | } 178 | } 179 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/utils/EidUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.utils; 18 | 19 | import pl.wavesoftware.eid.api.Eid; 20 | import pl.wavesoftware.eid.system.EidModule; 21 | 22 | import javax.annotation.Nullable; 23 | 24 | /** 25 | * @author Krzysztof Suszynski 26 | * @since 2018-10-29 27 | */ 28 | final class EidUtil { 29 | private EidUtil() { 30 | // nothing here 31 | } 32 | 33 | static Eid ensureEid(@Nullable Eid eid) { 34 | if (eid == null) { 35 | return EidModule.MODULE 36 | .getBinding() 37 | .getFactories() 38 | .getEidFactory() 39 | .create("20160329:132823", "EID-NULL"); 40 | } 41 | return eid; 42 | } 43 | 44 | static Eid ensureEid(@Nullable String eid) { 45 | if (eid == null) { 46 | return EidModule.MODULE 47 | .getBinding() 48 | .getFactories() 49 | .getEidFactory() 50 | .create("20160329:133052", "EID-NULL"); 51 | } 52 | return EidModule.MODULE 53 | .getBinding() 54 | .getFactories() 55 | .getEidFactory() 56 | .create(eid); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/utils/UnsafeProcedure.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.utils; 18 | 19 | import pl.wavesoftware.eid.api.Eid; 20 | 21 | /** 22 | * This unsafe procedure can be used to execute a code block that can throw 23 | * some checked Exception, that you would like not to process, because they 24 | * are unrecoverable bugs. 25 | *

26 | * To be used with 27 | * {@link EidExecutions#tryToExecute(UnsafeProcedure, Eid)} or 28 | * {@link EidExecutions#tryToExecute(UnsafeProcedure, String)} methods. 29 | */ 30 | public interface UnsafeProcedure { 31 | /** 32 | * Executes a procedure code that can throw a checked exception to be cough 33 | * 34 | * @throws Exception this exception should be set to concrete one ex. IOException 35 | */ 36 | @SuppressWarnings("squid:S00112") 37 | void execute() throws Exception; 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/utils/UnsafeSupplier.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.utils; 18 | 19 | import pl.wavesoftware.eid.api.Eid; 20 | 21 | /** 22 | * This unsafe supplier can be used to execute a code block that needs to 23 | * return some value and can throw some checked Exception, that you would like 24 | * not to process, because they are unrecoverable bugs. 25 | *

26 | * To be used with 27 | * {@link EidExecutions#tryToExecute(UnsafeSupplier, Eid)} or 28 | * {@link EidExecutions#tryToExecute(UnsafeSupplier, String)} methods 29 | * 30 | * @param a return type from unsafe supplier 31 | */ 32 | public interface UnsafeSupplier { 33 | /** 34 | * Executes a supplier function that can throw a checked exception to be cough 35 | * 36 | * @return a return value from unsafe supplier function 37 | * @throws Exception this exception should be set to concrete one ex. IOException 38 | */ 39 | @SuppressWarnings("squid:S00112") 40 | T get() throws Exception; 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/pl/wavesoftware/eid/utils/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Wave Software 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 | /** 18 | * @author Krzysztof Suszynski 19 | * @since 2016-03-26 20 | */ 21 | @javax.annotation.ParametersAreNonnullByDefault 22 | @ReturnTypesAreNonnullByDefault 23 | package pl.wavesoftware.eid.utils; 24 | 25 | import pl.wavesoftware.eid.api.ReturnTypesAreNonnullByDefault; 26 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/services/pl.wavesoftware.eid.api.Binding: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2018 Wave Software 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 | pl.wavesoftware.eid.impl.BindingImpl 18 | -------------------------------------------------------------------------------- /src/test/groovy/verify-sonar-issues.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | import groovy.json.JsonSlurper 18 | 19 | String skip = properties.getAt('sonar.skip') 20 | if (skip == 'true') { 21 | log.info('sonar.skip = true: Skipping Sonar issues file analysis.') 22 | return 23 | } 24 | String issueFile = properties.getAt('sonar.issues.file') 25 | filepath = new File(issueFile) 26 | log.info('Sonar Issues file: ' + filepath) 27 | JsonSlurper slurper = new JsonSlurper() 28 | String contents = filepath.getText('UTF-8') 29 | def json = slurper.parseText(contents) 30 | 31 | def major = 0 32 | def critical = 0 33 | def blocker = 0 34 | def report = [] 35 | 36 | json.issues.each { 37 | issue = it 38 | if (issue.isNew && issue.status == "OPEN") { 39 | switch (issue.severity) { 40 | case 'MAJOR': 41 | major++ 42 | report << issue 43 | break 44 | case 'CRITICAL': 45 | critical++ 46 | report << issue 47 | break 48 | case 'BLOCKER': 49 | blocker++ 50 | report << issue 51 | break 52 | } 53 | } 54 | } 55 | red = major + critical + blocker 56 | if (red > 0) { 57 | msg = 'There are ' + red + ' new issue(s) that have severity of MAJOR or above (BLOCKER or CRITICAL)!' 58 | log.error(msg) 59 | log.error('') 60 | if (blocker) { 61 | log.error('Blocker: +' + blocker) 62 | } 63 | if (critical) { 64 | log.error('Critical: +' + critical) 65 | } 66 | if (major) { 67 | log.error('Major: +' + major) 68 | } 69 | fail(msg) 70 | } else { 71 | log.info('No new issues have been found') 72 | } 73 | -------------------------------------------------------------------------------- /src/test/java/pl/wavesoftware/eid/ConfigurationContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid; 18 | 19 | import pl.wavesoftware.eid.api.Configurator; 20 | import pl.wavesoftware.eid.system.EidModule; 21 | 22 | import java.io.Closeable; 23 | 24 | /** 25 | * @author Krzysztof Suszynski 26 | * @since 2018-12-18 27 | */ 28 | final class ConfigurationContext implements Closeable { 29 | 30 | private final Configurator saved; 31 | 32 | ConfigurationContext(Configurator configurator) { 33 | this.saved = EidModule.MODULE 34 | .getBinding() 35 | .getConfigurationSystem() 36 | .configure(configurator); 37 | } 38 | 39 | @Override 40 | public void close() { 41 | EidModule.MODULE 42 | .getBinding() 43 | .getConfigurationSystem() 44 | .configure(saved); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/test/java/pl/wavesoftware/eid/ConfiguratorRule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid; 18 | 19 | import org.junit.rules.TestRule; 20 | import org.junit.runner.Description; 21 | import org.junit.runners.model.Statement; 22 | import pl.wavesoftware.eid.api.Configurator; 23 | 24 | /** 25 | * @author Krzysztof Suszynski 26 | * @since 2018-12-17 27 | */ 28 | public class ConfiguratorRule implements TestRule { 29 | 30 | private final Configurator configurator; 31 | 32 | public ConfiguratorRule(Configurator configurator) { 33 | this.configurator = configurator; 34 | } 35 | 36 | @Override 37 | public Statement apply(final Statement base, Description description) { 38 | return new Statement() { 39 | @Override 40 | public void evaluate() throws Throwable { 41 | ConfigurationContext context = 42 | new ConfigurationContext(configurator); 43 | try { 44 | base.evaluate(); 45 | } finally { 46 | context.close(); 47 | } 48 | } 49 | }; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/test/java/pl/wavesoftware/eid/ConstantUniqueIdRule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid; 18 | 19 | import pl.wavesoftware.eid.api.ConfigurationBuilder; 20 | import pl.wavesoftware.eid.api.Configurator; 21 | import pl.wavesoftware.eid.api.UniqueIdGenerator; 22 | 23 | /** 24 | * @author Krzysztof Suszynski 25 | * @since 2.0.0 26 | */ 27 | public final class ConstantUniqueIdRule extends ConfiguratorRule { 28 | 29 | public ConstantUniqueIdRule(final String constantId) { 30 | super(new Configurator() { 31 | @Override 32 | public void configure(ConfigurationBuilder configuration) { 33 | configuration.uniqueIdGenerator(new UniqueIdGenerator() { 34 | @Override 35 | public String generateUniqId() { 36 | return constantId; 37 | } 38 | }); 39 | } 40 | }); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/test/java/pl/wavesoftware/eid/DisableValidatorState.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid; 18 | 19 | import org.openjdk.jmh.annotations.Scope; 20 | import org.openjdk.jmh.annotations.Setup; 21 | import org.openjdk.jmh.annotations.State; 22 | import org.openjdk.jmh.annotations.TearDown; 23 | import org.slf4j.Logger; 24 | import org.slf4j.LoggerFactory; 25 | import pl.wavesoftware.eid.api.ConfigurationBuilder; 26 | import pl.wavesoftware.eid.api.Configurator; 27 | 28 | /** 29 | * @author Krzysztof Suszynski 30 | * @since 2018-12-18 31 | */ 32 | @State(Scope.Benchmark) 33 | public class DisableValidatorState { 34 | private static final Logger LOGGER = 35 | LoggerFactory.getLogger(DisableValidatorState.class); 36 | private ConfigurationContext context; 37 | 38 | @Setup 39 | public void setup() { 40 | context = 41 | new ConfigurationContext(new Configurator() { 42 | @Override 43 | public void configure(ConfigurationBuilder configuration) { 44 | configuration.validator(null); 45 | LOGGER.debug("Disable validation of Eid numbers"); 46 | } 47 | }); 48 | } 49 | 50 | @TearDown 51 | public void tearDown() { 52 | context.close(); 53 | LOGGER.debug("Restoring configuration context"); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/test/java/pl/wavesoftware/eid/EidIT.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Wave Software 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 | package pl.wavesoftware.eid; 18 | 19 | import org.junit.ClassRule; 20 | import org.junit.Test; 21 | import org.junit.rules.RuleChain; 22 | import org.openjdk.jmh.annotations.Benchmark; 23 | import org.openjdk.jmh.annotations.Mode; 24 | import org.openjdk.jmh.infra.Blackhole; 25 | import org.openjdk.jmh.results.RunResult; 26 | import org.openjdk.jmh.runner.Runner; 27 | import org.openjdk.jmh.runner.options.Options; 28 | import org.openjdk.jmh.runner.options.OptionsBuilder; 29 | import org.openjdk.jmh.runner.options.TimeValue; 30 | import org.slf4j.Logger; 31 | import org.slf4j.LoggerFactory; 32 | import pl.wavesoftware.eid.exceptions.EidRuntimeException; 33 | import pl.wavesoftware.testing.JavaAgentSkip; 34 | import pl.wavesoftware.testing.JavaVersion; 35 | import pl.wavesoftware.testing.JmhCleaner; 36 | import pl.wavesoftware.testing.JvmArgs; 37 | 38 | import java.util.Collection; 39 | import java.util.Date; 40 | import java.util.concurrent.TimeUnit; 41 | 42 | import static org.assertj.core.api.Assertions.assertThat; 43 | 44 | /** 45 | * @author Krzysztof Suszynski 46 | * @since 2016-03-24 47 | */ 48 | public class EidIT { 49 | 50 | private static final int PERCENT = 100; 51 | private static final int OPERATIONS = 1000; 52 | private static final Logger LOG = LoggerFactory.getLogger(EidIT.class); 53 | private static final double SPEED_THRESHOLD = 54 | JavaVersion.get().greaterOrEqual(JavaVersion.of(8)) 55 | ? 0.9d 56 | : 0.5d; 57 | 58 | @ClassRule 59 | public static RuleChain chain = RuleChain 60 | .outerRule(new JmhCleaner(EidIT.class)) 61 | .around(JavaAgentSkip.ifActive()); 62 | 63 | @Test 64 | public void benchmark() throws Exception { 65 | Options opt = new OptionsBuilder() 66 | .include(this.getClass().getName() + ".*") 67 | .mode(Mode.Throughput) 68 | .timeUnit(TimeUnit.MILLISECONDS) 69 | .operationsPerInvocation(OPERATIONS) 70 | .warmupTime(TimeValue.seconds(1)) 71 | .warmupIterations(2) 72 | .measurementTime(TimeValue.seconds(1)) 73 | .measurementIterations(5) 74 | .threads(4) 75 | .forks(1) 76 | .shouldFailOnError(true) 77 | .shouldDoGC(true) 78 | .jvmArgs(JvmArgs.get()) 79 | .build(); 80 | 81 | Runner runner = new Runner(opt); 82 | Collection results = runner.run(); 83 | assertThat(results).hasSize(2); 84 | 85 | RunResult control = getRunResultByName(results, "control"); 86 | RunResult eid = getRunResultByName(results, "eid"); 87 | assertThat(control).isNotNull(); 88 | assertThat(eid).isNotNull(); 89 | 90 | double controlScore = control.getAggregatedResult().getPrimaryResult().getScore(); 91 | double eidScore = eid.getAggregatedResult().getPrimaryResult().getScore(); 92 | 93 | String title = "method speed quotient to the control sample"; 94 | String eidTitle = String.format("%s %s should be at least %.2f%%", "#eid()", 95 | title, SPEED_THRESHOLD * PERCENT); 96 | 97 | double eidTimes = eidScore / controlScore; 98 | 99 | LOG.info(String.format("Control sample method time per operation: %" + 100 | ".2f ops / msec", controlScore)); 101 | LOG.info(String.format("#eid() method time per operation: %" + 102 | ".2f ops / msec", eidScore)); 103 | LOG.info(String.format("%s and is %.2f%%", eidTitle, eidTimes * PERCENT)); 104 | 105 | assertThat(eidTimes).as(eidTitle).isGreaterThanOrEqualTo(SPEED_THRESHOLD); 106 | } 107 | 108 | @Benchmark 109 | public void control(Blackhole bh) { 110 | for (int i = 0; i < OPERATIONS; i++) { 111 | bh.consume(new Date()); 112 | } 113 | } 114 | 115 | @Benchmark 116 | public void eid(Blackhole bh, DisableValidatorState state) { 117 | for (int i = 0; i < OPERATIONS; i++) { 118 | bh.consume(new DefaultEid("20160330:144947")); 119 | } 120 | } 121 | 122 | private static RunResult getRunResultByName(Collection results, String name) { 123 | String fullName = String.format("%s.%s", EidIT.class.getName(), name); 124 | for (RunResult result : results) { 125 | if (result.getParams().getBenchmark().equals(fullName)) { 126 | return result; 127 | } 128 | } 129 | throw new EidRuntimeException("20160324:225412", "Invalid name: " + name); 130 | } 131 | 132 | } 133 | -------------------------------------------------------------------------------- /src/test/java/pl/wavesoftware/eid/EidTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Wave Software 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 pl.wavesoftware.eid; 17 | 18 | import org.junit.Rule; 19 | import org.junit.Test; 20 | import org.junit.rules.ExpectedException; 21 | import pl.wavesoftware.eid.api.Eid; 22 | import pl.wavesoftware.eid.api.EidMessage; 23 | 24 | import java.util.ArrayList; 25 | import java.util.Collection; 26 | import java.util.HashSet; 27 | import java.util.List; 28 | import java.util.Set; 29 | import java.util.concurrent.Callable; 30 | import java.util.concurrent.ExecutionException; 31 | import java.util.concurrent.ExecutorService; 32 | import java.util.concurrent.Executors; 33 | import java.util.concurrent.Future; 34 | 35 | import static org.assertj.core.api.Assertions.assertThat; 36 | 37 | /** 38 | * @author Krzysztof Suszynski 39 | * @since 2.0.0 40 | */ 41 | public class EidTest { 42 | 43 | @Rule 44 | public final ExpectedException thrown = ExpectedException.none(); 45 | 46 | @Test 47 | public void invalidNullEid() { 48 | // given 49 | String eid = null; 50 | 51 | // then 52 | thrown.expect(IllegalArgumentException.class); 53 | thrown.expectMessage("Exception ID can't be null"); 54 | 55 | // when 56 | DefaultEid.eid(eid); 57 | } 58 | 59 | @Test 60 | public void invalidEid() { 61 | // given 62 | String eid = "1234"; 63 | 64 | // then 65 | thrown.expect(IllegalArgumentException.class); 66 | thrown.expectMessage("Invalid ID given as an Exception ID: 1234"); 67 | 68 | // when 69 | DefaultEid.eid(eid); 70 | } 71 | 72 | @Test 73 | public void testToString() { 74 | // given 75 | DefaultEid instance = new DefaultEid("20150718:012917"); 76 | String expResult = "[20150718:012917]"; 77 | // when 78 | String result = instance.toString(); 79 | // then 80 | assertThat(result).contains(expResult); 81 | assertThat(result).matches("^\\[20150718:012917\\]<[a-zA-Z0-9_-]+>$"); 82 | } 83 | 84 | @Test 85 | public void testToString_Ref() { 86 | // given 87 | DefaultEid instance = new DefaultEid("20150718:013056", "ORA-38101"); 88 | String expResult = "[20150718:013056|ORA-38101]"; 89 | // when 90 | String result = instance.toString(); 91 | // then 92 | assertThat(result).contains(expResult); 93 | } 94 | 95 | @Test 96 | public void testGetId() { 97 | // given 98 | DefaultEid instance = new DefaultEid("20150718:013209"); 99 | String expResult = "20150718:013209"; 100 | // when 101 | String result = instance.getId(); 102 | // then 103 | assertThat(result).isEqualTo(expResult); 104 | } 105 | 106 | @Test 107 | public void testGetRef() { 108 | // given 109 | DefaultEid instance = new DefaultEid("20150718:013314", "ORA-38105"); 110 | String expResult = "ORA-38105"; 111 | // when 112 | String result = instance.getRef(); 113 | // then 114 | assertThat(result).isEqualTo(expResult); 115 | } 116 | 117 | @Test 118 | public void testGetRef_Null() { 119 | // given 120 | Eid instance = DefaultEid.eid("20150718:013236"); 121 | // when 122 | String result = instance.getRef(); 123 | // then 124 | assertThat(result).isNull(); 125 | } 126 | 127 | @Test 128 | public void testGetUniq() { 129 | String id = "20150718:013443"; 130 | DefaultEid instance = new DefaultEid(id); 131 | DefaultEid instance2 = new DefaultEid(id); 132 | String result = instance.getUnique(); 133 | String result2 = instance2.getUnique(); 134 | assertThat(result).isNotEmpty(); 135 | assertThat(result).isNotEqualTo(result2); 136 | } 137 | 138 | @Test 139 | public void message() { 140 | // given 141 | String code = "20151117:192211"; 142 | DefaultEid eid = new DefaultEid(code); 143 | int filesNumber = 18; 144 | 145 | // when 146 | EidMessage message = eid.message("Files: {0}", filesNumber); 147 | 148 | // then 149 | assertThat(message).contains("[20151117:192211]<", "> => Files: 18"); 150 | } 151 | 152 | @Test 153 | public void raceTheSun() throws InterruptedException, ExecutionException { 154 | // given 155 | final DefaultEid eid = new DefaultEid("20181114:223748"); 156 | ExecutorService executorService = Executors.newFixedThreadPool(4); 157 | Callable task = new Callable() { 158 | @Override 159 | public String call() { 160 | return eid.getUnique(); 161 | } 162 | }; 163 | Collection> tasks = 164 | new ArrayList>(20); 165 | for (int i = 0; i < 20; i++) { 166 | tasks.add(task); 167 | } 168 | executorService.invokeAll(tasks); 169 | 170 | // when 171 | List> executed = executorService.invokeAll(tasks); 172 | 173 | // then 174 | Set collected = new HashSet(); 175 | for (Future future : executed) { 176 | collected.add(future.get()); 177 | } 178 | assertThat(collected).hasSize(1); 179 | } 180 | 181 | } 182 | -------------------------------------------------------------------------------- /src/test/java/pl/wavesoftware/eid/api/TestConfigurator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.api; 18 | 19 | import java.util.Locale; 20 | import java.util.TimeZone; 21 | import java.util.regex.Pattern; 22 | 23 | /** 24 | * @author Krzysztof Suszynski 25 | * @since 2.0.0 26 | */ 27 | public final class TestConfigurator implements Configurator { 28 | 29 | @Override 30 | public void configure(ConfigurationBuilder configuration) { 31 | configuration 32 | .locale(Locale.ENGLISH) 33 | .timezone(TimeZone.getTimeZone("GMT")) 34 | .validator(new PseudoDateValidator()); 35 | } 36 | 37 | private static final class PseudoDateValidator implements Validator { 38 | private final Pattern pattern = Pattern.compile("^\\d{8}:\\d{6}$"); 39 | 40 | @Override 41 | public boolean isValid(CharSequence id) { 42 | return pattern.matcher(id).matches(); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/test/java/pl/wavesoftware/eid/exceptions/A.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.exceptions; 18 | 19 | /** 20 | * @author Krzysztof Suszynski 21 | * @since 2018-12-16 22 | */ 23 | class A { 24 | @Override 25 | public String toString() { 26 | return "a"; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/test/java/pl/wavesoftware/eid/exceptions/AssertionsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Wave Software 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 | package pl.wavesoftware.eid.exceptions; 18 | 19 | import org.junit.Rule; 20 | import org.junit.Test; 21 | import org.junit.rules.ExpectedException; 22 | import pl.wavesoftware.eid.DefaultEid; 23 | 24 | /** 25 | * @author Krzysztof Suszyński 26 | * @since 21.07.15 27 | */ 28 | public class AssertionsTest { 29 | 30 | @Rule 31 | public final ExpectedException thrown = ExpectedException.none(); 32 | 33 | @Test 34 | public void testJdkAssertions() { 35 | // then 36 | thrown.expect(AssertionError.class); 37 | 38 | // when 39 | assert getTestNumber() < 3 : new DefaultEid("20150721:101958"); 40 | } 41 | 42 | private int getTestNumber() { 43 | return 3 * 5; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/test/java/pl/wavesoftware/eid/exceptions/EidIllegalArgumentExceptionTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.exceptions; 18 | 19 | import org.junit.Rule; 20 | import org.junit.Test; 21 | import pl.wavesoftware.eid.ConstantUniqueIdRule; 22 | import pl.wavesoftware.eid.DefaultEid; 23 | import pl.wavesoftware.eid.api.Eid; 24 | import pl.wavesoftware.eid.api.EidMessage; 25 | 26 | import java.io.IOException; 27 | 28 | import static org.assertj.core.api.Assertions.assertThat; 29 | 30 | /** 31 | * @author Krzysztof Suszynski 32 | * @since 2.0.0 33 | */ 34 | public class EidIllegalArgumentExceptionTest { 35 | 36 | @Rule 37 | public ConstantUniqueIdRule uniqueIdRule = new ConstantUniqueIdRule( 38 | "d3m0" 39 | ); 40 | 41 | @Test 42 | public void constructOfCharSequence() { 43 | // given 44 | String eid = "20181215:000127"; 45 | 46 | // when 47 | EidRuntimeException ex = new EidIllegalArgumentException(eid); 48 | 49 | // then 50 | assertThat(ex).hasNoCause() 51 | .hasMessage("[20181215:000127]"); 52 | } 53 | 54 | @Test 55 | public void constructOfCharSequenceAndMessage() { 56 | // given 57 | String eid = "20181215:000410"; 58 | String message = "This is message"; 59 | 60 | // when 61 | EidRuntimeException ex = new EidIllegalArgumentException(eid, message); 62 | 63 | // then 64 | assertThat(ex).hasNoCause() 65 | .hasMessage("[20181215:000410] => This is message"); 66 | } 67 | 68 | @Test 69 | public void constructOfEidMessage() { 70 | // given 71 | DefaultEid eid = new DefaultEid("20181216:175048"); 72 | EidMessage message = eid.message( 73 | "This is {0} message", new A() 74 | ); 75 | 76 | // when 77 | EidRuntimeException ex = new EidIllegalArgumentException(message); 78 | 79 | // then 80 | assertThat(ex) 81 | .hasNoCause() 82 | .hasMessage("[20181216:175048] => This is a message"); 83 | } 84 | 85 | @Test 86 | public void constructOfCharSequenceAndMessageAndCause() { 87 | // given 88 | String eid = "20181215:001337"; 89 | String message = "This is message"; 90 | Throwable cause = getCause(); 91 | 92 | // when 93 | EidRuntimeException ex = new EidIllegalArgumentException( 94 | eid, message, cause 95 | ); 96 | 97 | // then 98 | assertThat(ex) 99 | .hasCauseInstanceOf(IOException.class) 100 | .hasMessage("[20181215:001337] => This is message"); 101 | } 102 | 103 | @Test 104 | public void constructOfEidMessageAndCause() { 105 | // given 106 | DefaultEid eid = new DefaultEid("20181215:000410"); 107 | EidMessage message = eid.message( 108 | "This is {0} message", new A() 109 | ); 110 | Throwable cause = getCause(); 111 | 112 | // when 113 | EidRuntimeException ex = new EidIllegalArgumentException(message, cause); 114 | 115 | // then 116 | assertThat(ex) 117 | .hasCauseInstanceOf(IOException.class) 118 | .hasMessage("[20181215:000410] => This is a message"); 119 | } 120 | 121 | @Test 122 | public void constructOfCharSequenceAndCause() { 123 | // given 124 | String eid = "20181216:175040"; 125 | Throwable cause = getCause(); 126 | 127 | // when 128 | EidRuntimeException ex = new EidIllegalArgumentException(eid, cause); 129 | 130 | // then 131 | assertThat(ex) 132 | .hasCauseInstanceOf(IOException.class) 133 | .hasMessage("[20181216:175040] => A cause"); 134 | } 135 | 136 | @Test 137 | public void constructOfEid() { 138 | // given 139 | Eid eid = DefaultEid.eid("20181216:180054"); 140 | 141 | // when 142 | EidRuntimeException ex = new EidIllegalArgumentException(eid); 143 | 144 | // then 145 | assertThat(ex) 146 | .hasNoCause() 147 | .hasMessage("[20181216:180054]"); 148 | } 149 | 150 | @Test 151 | public void constructOfEidAndMessage() { 152 | // given 153 | Eid eid = DefaultEid.eid("20181216:180212"); 154 | String message = "A simple message"; 155 | 156 | // when 157 | EidRuntimeException ex = new EidIllegalArgumentException(eid, message); 158 | 159 | // then 160 | assertThat(ex) 161 | .hasNoCause() 162 | .hasMessage("[20181216:180212] => A simple message"); 163 | } 164 | 165 | @Test 166 | public void constructOfEidAndMessageAndCause() { 167 | // given 168 | Eid eid = DefaultEid.eid("20181216:180355"); 169 | String message = "A simple message"; 170 | Throwable cause = getCause(); 171 | 172 | // when 173 | EidRuntimeException ex = new EidIllegalArgumentException( 174 | eid, message, cause 175 | ); 176 | 177 | // then 178 | assertThat(ex) 179 | .hasCauseInstanceOf(IOException.class) 180 | .hasMessage("[20181216:180355] => A simple message"); 181 | } 182 | 183 | @Test 184 | public void constructOfEidAndCause() { 185 | // given 186 | Eid eid = DefaultEid.eid("20181216:191804"); 187 | Throwable cause = getCause(); 188 | 189 | // when 190 | EidRuntimeException ex = new EidIllegalArgumentException( 191 | eid, cause 192 | ); 193 | 194 | // then 195 | assertThat(ex) 196 | .hasCauseInstanceOf(IOException.class) 197 | .hasMessage("[20181216:191804] => A cause"); 198 | } 199 | 200 | private static IOException getCause() { 201 | return new IOException("A cause"); 202 | } 203 | 204 | } 205 | -------------------------------------------------------------------------------- /src/test/java/pl/wavesoftware/eid/exceptions/EidIllegalStateExceptionTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.exceptions; 18 | 19 | import org.junit.Rule; 20 | import org.junit.Test; 21 | import pl.wavesoftware.eid.ConstantUniqueIdRule; 22 | import pl.wavesoftware.eid.DefaultEid; 23 | import pl.wavesoftware.eid.api.Eid; 24 | import pl.wavesoftware.eid.api.EidMessage; 25 | 26 | import java.io.IOException; 27 | 28 | import static org.assertj.core.api.Assertions.assertThat; 29 | 30 | /** 31 | * @author Krzysztof Suszynski 32 | * @since 2018-12-16 33 | */ 34 | public class EidIllegalStateExceptionTest { 35 | @Rule 36 | public ConstantUniqueIdRule uniqueIdRule = new ConstantUniqueIdRule( 37 | "e9b1" 38 | ); 39 | 40 | @Test 41 | public void constructOfCharSequence() { 42 | // given 43 | String eid = "20181216:192816"; 44 | 45 | // when 46 | EidRuntimeException ex = new EidIllegalStateException(eid); 47 | 48 | // then 49 | assertThat(ex).hasNoCause() 50 | .hasMessage("[20181216:192816]"); 51 | } 52 | 53 | @Test 54 | public void constructOfCharSequenceAndMessage() { 55 | // given 56 | String eid = "20181216:192843"; 57 | String message = "This is message"; 58 | 59 | // when 60 | EidRuntimeException ex = new EidIllegalStateException(eid, message); 61 | 62 | // then 63 | assertThat(ex).hasNoCause() 64 | .hasMessage("[20181216:192843] => This is message"); 65 | } 66 | 67 | @Test 68 | public void constructOfEidMessage() { 69 | // given 70 | DefaultEid eid = new DefaultEid("20181216:192906"); 71 | EidMessage message = eid.message( 72 | "This is {0} message", new A() 73 | ); 74 | 75 | // when 76 | EidRuntimeException ex = new EidIllegalStateException(message); 77 | 78 | // then 79 | assertThat(ex) 80 | .hasNoCause() 81 | .hasMessage("[20181216:192906] => This is a message"); 82 | } 83 | 84 | @Test 85 | public void constructOfCharSequenceAndMessageAndCause() { 86 | // given 87 | String eid = "20181216:192917"; 88 | String message = "This is message"; 89 | Throwable cause = getCause(); 90 | 91 | // when 92 | EidRuntimeException ex = new EidIllegalStateException( 93 | eid, message, cause 94 | ); 95 | 96 | // then 97 | assertThat(ex) 98 | .hasCauseInstanceOf(IOException.class) 99 | .hasMessage("[20181216:192917] => This is message"); 100 | } 101 | 102 | @Test 103 | public void constructOfEidMessageAndCause() { 104 | // given 105 | DefaultEid eid = new DefaultEid("20181216:192930"); 106 | EidMessage message = eid.message( 107 | "This is {0} message", new A() 108 | ); 109 | Throwable cause = getCause(); 110 | 111 | // when 112 | EidRuntimeException ex = new EidIllegalStateException(message, cause); 113 | 114 | // then 115 | assertThat(ex) 116 | .hasCauseInstanceOf(IOException.class) 117 | .hasMessage("[20181216:192930] => This is a message"); 118 | } 119 | 120 | @Test 121 | public void constructOfCharSequenceAndCause() { 122 | // given 123 | String eid = "20181216:192938"; 124 | Throwable cause = getCause(); 125 | 126 | // when 127 | EidRuntimeException ex = new EidIllegalStateException(eid, cause); 128 | 129 | // then 130 | assertThat(ex) 131 | .hasCauseInstanceOf(IOException.class) 132 | .hasMessage("[20181216:192938] => A cause"); 133 | } 134 | 135 | @Test 136 | public void constructOfEid() { 137 | // given 138 | Eid eid = DefaultEid.eid("20181216:192947"); 139 | 140 | // when 141 | EidRuntimeException ex = new EidIllegalStateException(eid); 142 | 143 | // then 144 | assertThat(ex) 145 | .hasNoCause() 146 | .hasMessage("[20181216:192947]"); 147 | } 148 | 149 | @Test 150 | public void constructOfEidAndMessage() { 151 | // given 152 | Eid eid = DefaultEid.eid("20181216:192956"); 153 | String message = "A simple message"; 154 | 155 | // when 156 | EidRuntimeException ex = new EidIllegalStateException(eid, message); 157 | 158 | // then 159 | assertThat(ex) 160 | .hasNoCause() 161 | .hasMessage("[20181216:192956] => A simple message"); 162 | } 163 | 164 | @Test 165 | public void constructOfEidAndMessageAndCause() { 166 | // given 167 | Eid eid = DefaultEid.eid("20181216:193003"); 168 | String message = "A simple message"; 169 | Throwable cause = getCause(); 170 | 171 | // when 172 | EidRuntimeException ex = new EidIllegalStateException( 173 | eid, message, cause 174 | ); 175 | 176 | // then 177 | assertThat(ex) 178 | .hasCauseInstanceOf(IOException.class) 179 | .hasMessage("[20181216:193003] => A simple message"); 180 | } 181 | 182 | @Test 183 | public void constructOfEidAndCause() { 184 | // given 185 | Eid eid = DefaultEid.eid("20181216:193015"); 186 | Throwable cause = getCause(); 187 | 188 | // when 189 | EidRuntimeException ex = new EidIllegalStateException( 190 | eid, cause 191 | ); 192 | 193 | // then 194 | assertThat(ex) 195 | .hasCauseInstanceOf(IOException.class) 196 | .hasMessage("[20181216:193015] => A cause"); 197 | } 198 | 199 | private static Throwable getCause() { 200 | return new IOException("A cause"); 201 | } 202 | } 203 | -------------------------------------------------------------------------------- /src/test/java/pl/wavesoftware/eid/exceptions/EidIndexOutOfBoundsExceptionTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Wave Software 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 | package pl.wavesoftware.eid.exceptions; 18 | 19 | import org.hamcrest.CoreMatchers; 20 | import org.junit.Rule; 21 | import org.junit.Test; 22 | import org.junit.rules.ExpectedException; 23 | import pl.wavesoftware.eid.ConstantUniqueIdRule; 24 | import pl.wavesoftware.eid.DefaultEid; 25 | import pl.wavesoftware.eid.api.Eid; 26 | 27 | import static org.hamcrest.CoreMatchers.containsString; 28 | import static org.hamcrest.CoreMatchers.nullValue; 29 | import static org.junit.internal.matchers.ThrowableMessageMatcher.hasMessage; 30 | 31 | /** 32 | * @author Krzysztof Suszynski 33 | * @since 2015-11-19 34 | */ 35 | public class EidIndexOutOfBoundsExceptionTest { 36 | 37 | @Rule 38 | public final ExpectedException thrown = ExpectedException.none(); 39 | 40 | @Rule 41 | public final ConstantUniqueIdRule uniqueIdRule = new ConstantUniqueIdRule( 42 | "deadfa11" 43 | ); 44 | 45 | private final String causeString = "Index seams to be invalid"; 46 | 47 | @Test 48 | public void throwWithEidWithRefAndCause() { 49 | // given 50 | String eid = "20151119:103158"; 51 | String ref = "MS+1233"; 52 | 53 | // then 54 | thrown.expectCause(hasMessage(containsString(causeString))); 55 | thrown.expectCause(CoreMatchers.instanceOf(ArrayIndexOutOfBoundsException.class)); 56 | thrown.expect(EidIndexOutOfBoundsException.class); 57 | thrown.expectMessage( 58 | "[20151119:103158|MS+1233] => " + 59 | "Index seams to be invalid" 60 | ); 61 | 62 | // when 63 | throw new EidIndexOutOfBoundsException(new DefaultEid(eid, ref), getCause()); 64 | } 65 | 66 | @Test 67 | public void throwWithEidWithRef() { 68 | // given 69 | String eid = "20151119:103217"; 70 | String ref = "MS+1233"; 71 | 72 | // then 73 | thrown.expect(EidIndexOutOfBoundsException.class); 74 | thrown.expectMessage("[20151119:103217|MS+1233]"); 75 | 76 | // when 77 | throw new EidIndexOutOfBoundsException(new DefaultEid(eid, ref)); 78 | } 79 | 80 | @Test 81 | public void throwWithStringAndCause() { 82 | // given 83 | String eid = "20151119:103232"; 84 | 85 | // then 86 | thrown.expectCause(hasMessage(containsString(causeString))); 87 | thrown.expectCause(CoreMatchers.instanceOf(ArrayIndexOutOfBoundsException.class)); 88 | thrown.expect(EidIndexOutOfBoundsException.class); 89 | thrown.expectMessage("[20151119:103232] => " + 90 | "Index seams to be invalid"); 91 | 92 | // when 93 | throw new EidIndexOutOfBoundsException(eid, getCause()); 94 | } 95 | 96 | @Test 97 | public void throwWithEidAndCause() { 98 | // given 99 | String eidNum = "20151119:103245"; 100 | DefaultEid eid = new DefaultEid(eidNum); 101 | 102 | // then 103 | thrown.expectCause(hasMessage(containsString(causeString))); 104 | thrown.expectCause(CoreMatchers.instanceOf( 105 | ArrayIndexOutOfBoundsException.class 106 | )); 107 | thrown.expect(EidIndexOutOfBoundsException.class); 108 | thrown.expectMessage("[20151119:103245] => " + 109 | "Index seams to be invalid"); 110 | 111 | // when 112 | throw new EidIndexOutOfBoundsException(eid, getCause()); 113 | } 114 | 115 | @Test 116 | public void throwWithString() { 117 | // given 118 | String eid = "20181216:233656"; 119 | 120 | // then 121 | thrown.expectCause(nullValue(Throwable.class)); 122 | thrown.expect(EidIndexOutOfBoundsException.class); 123 | thrown.expectMessage("[20181216:233656]"); 124 | 125 | // when 126 | throw new EidIndexOutOfBoundsException(eid); 127 | } 128 | 129 | @Test 130 | public void throwWithStringAndString() { 131 | // given 132 | String eid = "20181216:233958"; 133 | String message = "This is a message"; 134 | 135 | // then 136 | thrown.expectCause(nullValue(Throwable.class)); 137 | thrown.expect(EidIndexOutOfBoundsException.class); 138 | thrown.expectMessage("[20181216:233958] => " + 139 | "This is a message"); 140 | 141 | // when 142 | throw new EidIndexOutOfBoundsException(eid, message); 143 | } 144 | 145 | @Test 146 | public void throwWithEidMessage() { 147 | // given 148 | String eid = "20181216:235124"; 149 | String message = "This is {0} message"; 150 | 151 | // then 152 | thrown.expectCause(nullValue(Throwable.class)); 153 | thrown.expect(EidIndexOutOfBoundsException.class); 154 | thrown.expectMessage("[20181216:235124] => " + 155 | "This is a message"); 156 | 157 | // when 158 | throw new EidIndexOutOfBoundsException( 159 | DefaultEid.eid(eid).message(message, new A()) 160 | ); 161 | } 162 | 163 | @Test 164 | public void throwWithStringStringAndCause() { 165 | // given 166 | String eid = "20181216:235554"; 167 | String message = "This is a message"; 168 | 169 | // then 170 | thrown.expect(EidIndexOutOfBoundsException.class); 171 | thrown.expectMessage("[20181216:235554] => " + 172 | "This is a message"); 173 | thrown.expectCause(CoreMatchers.instanceOf( 174 | ArrayIndexOutOfBoundsException.class 175 | )); 176 | 177 | // when 178 | throw new EidIndexOutOfBoundsException( 179 | eid, message, getCause() 180 | ); 181 | } 182 | 183 | @Test 184 | public void throwWithEidMessageAndCause() { 185 | // given 186 | String eid = "20181216:235729"; 187 | String message = "This is {0} message"; 188 | 189 | // then 190 | thrown.expect(EidIndexOutOfBoundsException.class); 191 | thrown.expectMessage("[20181216:235729] => " + 192 | "This is a message"); 193 | thrown.expectCause(CoreMatchers.instanceOf( 194 | ArrayIndexOutOfBoundsException.class 195 | )); 196 | 197 | // when 198 | throw new EidIndexOutOfBoundsException( 199 | DefaultEid.eid(eid).message(message, new A()), getCause() 200 | ); 201 | } 202 | 203 | @Test 204 | public void throwWithEidAndMessage() { 205 | // given 206 | Eid eid = DefaultEid.eid("20181217:001519"); 207 | String message = "This is a message"; 208 | 209 | // then 210 | thrown.expect(EidIndexOutOfBoundsException.class); 211 | thrown.expectMessage("[20181217:001519] => " + 212 | "This is a message"); 213 | thrown.expectCause(nullValue(Throwable.class)); 214 | 215 | // when 216 | throw new EidIndexOutOfBoundsException( 217 | eid, message 218 | ); 219 | } 220 | 221 | @Test 222 | public void throwWithEidAndMessageAndCause() { 223 | // given 224 | Eid eid = DefaultEid.eid("20181217:001727"); 225 | String message = "This is a message"; 226 | 227 | // then 228 | thrown.expect(EidIndexOutOfBoundsException.class); 229 | thrown.expectMessage("[20181217:001727] => " + 230 | "This is a message"); 231 | thrown.expectCause(CoreMatchers.instanceOf( 232 | ArrayIndexOutOfBoundsException.class 233 | )); 234 | 235 | // when 236 | throw new EidIndexOutOfBoundsException( 237 | eid, message, getCause() 238 | ); 239 | } 240 | 241 | private Throwable getCause() { 242 | return new ArrayIndexOutOfBoundsException(causeString); 243 | } 244 | } 245 | -------------------------------------------------------------------------------- /src/test/java/pl/wavesoftware/eid/exceptions/EidNullPointerExceptionTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Wave Software 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 | package pl.wavesoftware.eid.exceptions; 18 | 19 | import org.hamcrest.CoreMatchers; 20 | import org.junit.Rule; 21 | import org.junit.Test; 22 | import org.junit.rules.ExpectedException; 23 | import pl.wavesoftware.eid.ConstantUniqueIdRule; 24 | import pl.wavesoftware.eid.DefaultEid; 25 | import pl.wavesoftware.eid.api.Eid; 26 | import pl.wavesoftware.eid.api.EidMessage; 27 | 28 | import static org.hamcrest.CoreMatchers.containsString; 29 | import static org.hamcrest.CoreMatchers.nullValue; 30 | import static org.junit.internal.matchers.ThrowableMessageMatcher.hasMessage; 31 | 32 | /** 33 | * @author Krzysztof Suszynski 34 | * @since 2015-11-19 35 | */ 36 | public class EidNullPointerExceptionTest { 37 | 38 | private final String constUniq = "deadcafe"; 39 | private final String causeString = "A cause"; 40 | 41 | @Rule 42 | public ExpectedException thrown = ExpectedException.none(); 43 | @Rule 44 | public ConstantUniqueIdRule uniqueIdRule = 45 | new ConstantUniqueIdRule(constUniq); 46 | 47 | @Test 48 | public void throwWithString() { 49 | // given 50 | String eid = "20181217:220721"; 51 | 52 | // then 53 | thrown.expectCause(nullValue(Throwable.class)); 54 | thrown.expect(EidNullPointerException.class); 55 | thrown.expectMessage("[20181217:220721]"); 56 | 57 | // when 58 | throw new EidNullPointerException(eid); 59 | } 60 | 61 | @Test 62 | public void throwWithStringAndCause() { 63 | // given 64 | String eid = "20151119:101810"; 65 | 66 | // then 67 | thrown.expectCause(hasMessage(containsString(causeString))); 68 | thrown.expectCause(CoreMatchers.instanceOf( 69 | UnsupportedOperationException.class 70 | )); 71 | thrown.expect(EidNullPointerException.class); 72 | thrown.expectMessage("[20151119:101810] => A cause"); 73 | 74 | // when 75 | throw new EidNullPointerException(eid, getCause()); 76 | } 77 | 78 | @Test 79 | public void throwWithEidAndCause() { 80 | // given 81 | String eidNum = "20151119:102150"; 82 | DefaultEid eid = new DefaultEid(eidNum); 83 | 84 | // then 85 | thrown.expectCause(hasMessage(containsString(causeString))); 86 | thrown.expectCause(CoreMatchers.instanceOf(UnsupportedOperationException.class)); 87 | thrown.expect(EidNullPointerException.class); 88 | thrown.expectMessage("[20151119:102150] => A cause"); 89 | 90 | // when 91 | throw new EidNullPointerException(eid, getCause()); 92 | } 93 | 94 | @Test 95 | public void throwWithEidAndNullCause() { 96 | // given 97 | String eidNum = "20181217:222651"; 98 | DefaultEid eid = new DefaultEid(eidNum); 99 | Throwable cause = null; 100 | 101 | // then 102 | thrown.expectCause(nullValue(Throwable.class)); 103 | thrown.expect(EidNullPointerException.class); 104 | thrown.expectMessage("[20181217:222651]"); 105 | 106 | // when 107 | throw new EidNullPointerException(eid, cause); 108 | } 109 | 110 | @Test 111 | public void throwWithEidAndRefAndCause() { 112 | // given 113 | String eid = "20151119:100854"; 114 | String ref = "PL-9584"; 115 | 116 | // then 117 | thrown.expectCause(hasMessage(containsString(causeString))); 118 | thrown.expectCause(CoreMatchers.instanceOf( 119 | UnsupportedOperationException.class 120 | )); 121 | thrown.expect(EidNullPointerException.class); 122 | thrown.expectMessage("[20151119:100854|PL-9584] => A cause"); 123 | 124 | // when 125 | throw new EidNullPointerException(new DefaultEid(eid, ref), getCause()); 126 | } 127 | 128 | @Test 129 | public void throwWithEid() { 130 | // given 131 | String eid = "20151119:100854"; 132 | String ref = "PL-9584"; 133 | 134 | // then 135 | thrown.expect(EidNullPointerException.class); 136 | thrown.expectMessage("[20151119:100854|PL-9584]"); 137 | 138 | // when 139 | throw new EidNullPointerException(new DefaultEid(eid, ref)); 140 | } 141 | 142 | @Test 143 | public void throwWithEidMessageAndCause() { 144 | // given 145 | String eid = "20181217:220935"; 146 | EidMessage message = DefaultEid.eid(eid) 147 | .message("This is {0} message", new A()); 148 | 149 | // then 150 | thrown.expectCause(hasMessage(containsString(causeString))); 151 | thrown.expectCause(CoreMatchers.instanceOf( 152 | UnsupportedOperationException.class 153 | )); 154 | thrown.expect(EidNullPointerException.class); 155 | thrown.expectMessage( 156 | "[20181217:220935] => This is a message" 157 | ); 158 | 159 | // when 160 | throw new EidNullPointerException(message, getCause()); 161 | } 162 | 163 | @Test 164 | public void throwWithEidAndMessage() { 165 | // given 166 | Eid eid = DefaultEid.eid("20181217:221725"); 167 | String message = "This is a message"; 168 | 169 | // then 170 | thrown.expectCause(nullValue(Throwable.class)); 171 | thrown.expect(EidNullPointerException.class); 172 | thrown.expectMessage( 173 | "[20181217:221725] => This is a message" 174 | ); 175 | 176 | // when 177 | throw new EidNullPointerException(eid, message); 178 | } 179 | 180 | @Test 181 | public void throwWithEidAndMessageAndCause() { 182 | // given 183 | DefaultEid eid = new DefaultEid("20181217:221901"); 184 | String message = "This is a message"; 185 | 186 | // then 187 | thrown.expectCause(hasMessage(containsString(causeString))); 188 | thrown.expectCause(CoreMatchers.instanceOf( 189 | UnsupportedOperationException.class 190 | )); 191 | thrown.expect(EidNullPointerException.class); 192 | thrown.expectMessage( 193 | "[20181217:221901] => This is a message" 194 | ); 195 | 196 | // when 197 | throw new EidNullPointerException(eid, message, getCause()); 198 | } 199 | 200 | private Throwable getCause() { 201 | return new UnsupportedOperationException(causeString); 202 | } 203 | } 204 | -------------------------------------------------------------------------------- /src/test/java/pl/wavesoftware/eid/exceptions/EidRuntimeExceptionTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Wave Software 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 | package pl.wavesoftware.eid.exceptions; 18 | 19 | import org.hamcrest.CoreMatchers; 20 | import org.junit.Rule; 21 | import org.junit.Test; 22 | import org.junit.rules.ExpectedException; 23 | 24 | import javax.naming.NamingException; 25 | import java.util.UnknownFormatConversionException; 26 | 27 | import static org.hamcrest.CoreMatchers.containsString; 28 | import static org.hamcrest.CoreMatchers.is; 29 | import static org.junit.internal.matchers.ThrowableMessageMatcher.hasMessage; 30 | 31 | /** 32 | * @author Krzysztof Suszyński 33 | * @since 2015-10-07 34 | */ 35 | public class EidRuntimeExceptionTest { 36 | 37 | @Rule 38 | public ExpectedException thrown = ExpectedException.none(); 39 | 40 | @Test 41 | public void testGetMessage() { 42 | // then 43 | thrown.expect(EidRuntimeException.class); 44 | thrown.expectCause(CoreMatchers.instanceOf(NamingException.class)); 45 | thrown.expectCause(hasMessage(is((String) null))); 46 | thrown.expectMessage(containsString("20151007:212217")); 47 | thrown.expectMessage(containsString( 48 | "javax.naming.NamingException [Root exception is java.util" + 49 | ".UnknownFormatConversionException: Conversion = Invalid for" + 50 | " unit test]" 51 | )); 52 | 53 | // given 54 | Throwable original = new UnknownFormatConversionException("Invalid for unit test"); 55 | NamingException cause = new NamingException(null); 56 | cause.setRootCause(original); 57 | throw new EidRuntimeException("20151007:212217", cause); 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/test/java/pl/wavesoftware/eid/exceptions/ExceptionsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Wave Software 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 pl.wavesoftware.eid.exceptions; 17 | 18 | import org.assertj.core.util.Lists; 19 | import org.junit.Test; 20 | import org.junit.runner.RunWith; 21 | import org.junit.runners.Parameterized; 22 | import org.junit.runners.Parameterized.Parameters; 23 | import pl.wavesoftware.eid.DefaultEid; 24 | import pl.wavesoftware.eid.api.Eid; 25 | 26 | import java.lang.reflect.Constructor; 27 | import java.lang.reflect.InvocationTargetException; 28 | import java.util.Arrays; 29 | import java.util.List; 30 | 31 | import static org.assertj.core.api.Assertions.assertThat; 32 | 33 | /** 34 | * 35 | * @author Krzysztof Suszyński 36 | */ 37 | @SuppressWarnings("unused") 38 | @RunWith(Parameterized.class) 39 | public class ExceptionsTest { 40 | 41 | @Parameters(name = "{index}: {0}({1})") 42 | public static Iterable data() { 43 | List argsList = getArguments(); 44 | List parameters = Lists.newArrayList(); 45 | for (Class exception : getExceptions()) { 46 | for (Object[] args : argsList) { 47 | addParameters(exception, parameters, args); 48 | } 49 | } 50 | return parameters; 51 | } 52 | 53 | private static List getArguments() { 54 | String message = "A testing message"; 55 | Throwable cause = new InterruptedException(message); 56 | CharSequence eid = "20150718:112954"; 57 | Eid id = new DefaultEid(eid); 58 | List arguments = Lists.newArrayList(); 59 | arguments.add(new Object[]{eid, message, cause}); 60 | arguments.add(new Object[]{eid, cause}); 61 | arguments.add(new Object[]{eid, message}); 62 | arguments.add(new Object[]{id, cause}); 63 | arguments.add(new Object[]{id}); 64 | return arguments; 65 | } 66 | 67 | private static String argumentsTypesToString(Object[] args) { 68 | StringBuilder sb = new StringBuilder(); 69 | for (Class argumentType : getArgumentsTypes(args)) { 70 | sb.append(argumentType.getSimpleName()).append(", "); 71 | } 72 | return sb.substring(0, sb.length() - 2); 73 | } 74 | 75 | private static Class[] getArgumentsTypes(Object[] args) { 76 | List> classes = Lists.newArrayList(); 77 | for (Object arg : args) { 78 | classes.add(guessType(arg)); 79 | } 80 | Class[] empty = new Class[0]; 81 | return classes.toArray(empty); 82 | } 83 | 84 | private static Class guessType(Object arg) { 85 | return Throwable.class.isAssignableFrom(arg.getClass()) 86 | ? Throwable.class 87 | : Eid.class.isAssignableFrom(arg.getClass()) 88 | ? Eid.class 89 | : arg.getClass(); 90 | } 91 | 92 | @SuppressWarnings("unchecked") 93 | private static Iterable> getExceptions() { 94 | return Arrays.asList( 95 | EidRuntimeException.class, 96 | EidIllegalArgumentException.class, 97 | EidIllegalStateException.class, 98 | EidIndexOutOfBoundsException.class, 99 | EidNullPointerException.class 100 | ); 101 | } 102 | 103 | private static void addParameters( 104 | Class eidClass, 105 | List parameters, 106 | Object[] args 107 | ) { 108 | try { 109 | Class[] types = getArgumentsTypes(args); 110 | if (types[0] == String.class) { 111 | types[0] = CharSequence.class; 112 | } 113 | Constructor constructor = 114 | eidClass.getDeclaredConstructor(types); 115 | parameters.add(new Object[]{ 116 | eidClass.getSimpleName(), 117 | argumentsTypesToString(args), 118 | eidClass, 119 | constructor, 120 | args 121 | }); 122 | } catch (NoSuchMethodException ex) { 123 | throw new RuntimeException(ex); 124 | } catch (SecurityException ex) { 125 | throw new RuntimeException(ex); 126 | } 127 | } 128 | 129 | private final Class eidClass; 130 | 131 | 132 | private final Constructor constructor; 133 | 134 | private final Object[] arguments; 135 | 136 | @SuppressWarnings("unchecked") 137 | public ExceptionsTest( 138 | String classSimpleName, 139 | String argsClasses, 140 | Class eidClass, 141 | Constructor constructor, 142 | Object... arguments 143 | ) { 144 | 145 | assertThat(classSimpleName).isNotEmpty(); 146 | assertThat(argsClasses).isNotEmpty(); 147 | this.eidClass = eidClass; 148 | this.constructor = (Constructor) constructor; 149 | this.arguments = arguments; 150 | } 151 | 152 | @Test 153 | public void testMessage() { 154 | // given 155 | EidRuntimeException exception = construct(); 156 | boolean hasCause = exception.getCause() != null; 157 | 158 | // then 159 | assertThat(exception).hasMessageContaining("20150718:112954"); 160 | if (hasCause) { 161 | assertThat(exception).hasMessageContaining("A testing message"); 162 | } 163 | } 164 | 165 | @Test 166 | public void testConstruction() { 167 | // given 168 | EidRuntimeException exception = construct(); 169 | 170 | // then 171 | assertThat(exception).isNotNull(); 172 | assertThat(exception).isExactlyInstanceOf(eidClass); 173 | } 174 | 175 | @SuppressWarnings("unchecked") 176 | private T construct() { 177 | try { 178 | return (T) constructor.newInstance(arguments); 179 | } catch (InstantiationException ex) { 180 | throw new RuntimeException(ex); 181 | } catch (IllegalAccessException ex) { 182 | throw new RuntimeException(ex); 183 | } catch (IllegalArgumentException ex) { 184 | throw new RuntimeException(ex); 185 | } catch (InvocationTargetException ex) { 186 | throw new RuntimeException(ex); 187 | } 188 | } 189 | } 190 | -------------------------------------------------------------------------------- /src/test/java/pl/wavesoftware/eid/impl/BindingImplTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.impl; 18 | 19 | import org.junit.Test; 20 | import pl.wavesoftware.eid.api.Binding; 21 | import pl.wavesoftware.eid.api.SerializableSupplier; 22 | import pl.wavesoftware.eid.api.Supplier; 23 | 24 | import static org.assertj.core.api.Assertions.assertThat; 25 | 26 | /** 27 | * @author Krzysztof Suszynski 28 | * @since 2018-11-30 29 | */ 30 | public class BindingImplTest { 31 | 32 | @Test 33 | public void testLazy() { 34 | // given 35 | Binding binding = new BindingImpl(); 36 | 37 | // when 38 | SerializableSupplier lazy = 39 | binding.getFactories().getLazyFactory().lazy(new Supplier() { 40 | @Override 41 | public String get() { 42 | return "Alice"; 43 | } 44 | }); 45 | String result = lazy.get(); 46 | 47 | // then 48 | assertThat(result).isEqualTo("Alice"); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/test/java/pl/wavesoftware/eid/impl/DefaultEidMessageTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.impl; 18 | 19 | import org.junit.Test; 20 | import pl.wavesoftware.eid.DefaultEid; 21 | import pl.wavesoftware.eid.api.EidMessage; 22 | 23 | import java.util.Date; 24 | 25 | import static org.assertj.core.api.Assertions.assertThat; 26 | 27 | /** 28 | * @author Krzysztof Suszynski 29 | * @since 2.0.0 30 | */ 31 | public class DefaultEidMessageTest { 32 | 33 | @Test 34 | public void testToString() { 35 | // given 36 | DefaultEid eid = new DefaultEid("20181114:225421"); 37 | EidMessage message = eid.message( 38 | "An example message with {0,date} {0,time}", 39 | new Date(1024000L) 40 | ); 41 | 42 | // when 43 | String repr = message.toString(); 44 | CharSequence fomatted = message.getFormattedMessage(); 45 | char forthChar = message.charAt(32); 46 | int len = message.length(); 47 | CharSequence sub = message.subSequence(35, 38); 48 | 49 | // then 50 | assertThat(fomatted).isEqualTo("An example message with Jan 1, 1970 12:17:04 AM"); 51 | assertThat(repr).contains( 52 | "[20181114:225421]<", 53 | "> => An example message with Jan 1, 1970 12:17:04 AM" 54 | ); 55 | assertThat(forthChar).isEqualTo('e'); 56 | assertThat(len).isEqualTo(76); 57 | assertThat(sub).isEqualTo("mpl"); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/test/java/pl/wavesoftware/eid/impl/DefaultFormatterTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.impl; 18 | 19 | import org.junit.Rule; 20 | import org.junit.Test; 21 | import pl.wavesoftware.eid.ConstantUniqueIdRule; 22 | import pl.wavesoftware.eid.DefaultEid; 23 | import pl.wavesoftware.eid.api.Configuration; 24 | import pl.wavesoftware.eid.api.Formatter; 25 | 26 | import static org.assertj.core.api.Assertions.assertThat; 27 | 28 | /** 29 | * @author Krzysztof Suszynski 30 | * @since 2018-12-03 31 | */ 32 | public class DefaultFormatterTest { 33 | 34 | @Rule 35 | public ConstantUniqueIdRule uniqueIdRule = new ConstantUniqueIdRule( 36 | "deadcafe" 37 | ); 38 | 39 | @Test 40 | public void format() { 41 | // given 42 | Configuration configuration = new ConfigurationImpl(); 43 | Formatter formatter = new DefaultFormatter(configuration); 44 | 45 | // when 46 | String formatted = formatter.format(new DefaultEid("20181203:224055")); 47 | 48 | // then 49 | assertThat(formatted).isEqualTo("[20181203:224055]"); 50 | } 51 | 52 | @Test 53 | public void formatWithMessage() { 54 | // given 55 | Configuration configuration = new ConfigurationImpl(); 56 | Formatter formatter = new DefaultFormatter(configuration); 57 | 58 | // when 59 | String formatted = formatter.format( 60 | new DefaultEid("20181203:224137"), 61 | "a message" 62 | ); 63 | 64 | // then 65 | assertThat(formatted).isEqualTo("[20181203:224137] => a message"); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/test/java/pl/wavesoftware/eid/impl/DefaultUniqueIdGeneratorTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.impl; 18 | 19 | import org.junit.Test; 20 | import pl.wavesoftware.eid.api.UniqueIdGenerator; 21 | 22 | import static org.assertj.core.api.Assertions.*; 23 | 24 | /** 25 | * @author Krzysztof Suszynski 26 | * @since 2.0.0 27 | */ 28 | public class DefaultUniqueIdGeneratorTest { 29 | 30 | @Test 31 | public void testGenerateUniqId() { 32 | // given 33 | UniqueIdGenerator generator = new DefaultUniqueIdGenerator(); 34 | String id; 35 | 36 | for (int i = 0; i < 1000000; i++) { 37 | // when 38 | id = generator.generateUniqId(); 39 | 40 | // then 41 | assertThat(id).hasSize(6); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/pl/wavesoftware/eid/impl/EidTextRepresentationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.impl; 18 | 19 | import org.junit.Test; 20 | import pl.wavesoftware.eid.DefaultEid; 21 | import pl.wavesoftware.eid.api.Configuration; 22 | import pl.wavesoftware.eid.system.EidModule; 23 | 24 | import java.io.ByteArrayInputStream; 25 | import java.io.ByteArrayOutputStream; 26 | import java.io.IOException; 27 | import java.io.ObjectInputStream; 28 | import java.io.ObjectOutputStream; 29 | import java.util.Date; 30 | 31 | import static org.assertj.core.api.Assertions.assertThat; 32 | 33 | /** 34 | * @author Krzysztof Suszynski 35 | * @since 2018-11-14 36 | */ 37 | public class EidTextRepresentationTest { 38 | 39 | @Test 40 | public void passivation() throws IOException, ClassNotFoundException { 41 | // given 42 | final DefaultEid eid = new DefaultEid("20181114:231205"); 43 | Configuration configuration = EidModule.MODULE 44 | .getBinding() 45 | .getConfigurationSystem() 46 | .getConfiguration(); 47 | TextMessage textMessage = new TextMessage( 48 | configuration, 49 | "Other {0}", 50 | new Object[]{new Date(3095000L)} 51 | ); 52 | final EidTextRepresentation representation = new EidTextRepresentation( 53 | eid, 54 | textMessage, 55 | configuration 56 | ); 57 | ByteArrayOutputStream byteArray = new ByteArrayOutputStream(); 58 | ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArray); 59 | 60 | // when 61 | objectOutputStream.writeObject(representation); 62 | ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( 63 | byteArray.toByteArray() 64 | ); 65 | ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream); 66 | EidTextRepresentation repr2 = (EidTextRepresentation) objectInputStream.readObject(); 67 | 68 | // then 69 | assertThat(repr2.get()).isEqualTo(representation.get()); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/test/java/pl/wavesoftware/eid/impl/InternalChecksTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.impl; 18 | 19 | import org.junit.Rule; 20 | import org.junit.Test; 21 | import org.junit.rules.ExpectedException; 22 | 23 | import java.lang.reflect.Constructor; 24 | import java.lang.reflect.InvocationTargetException; 25 | 26 | import static org.assertj.core.api.Assertions.assertThat; 27 | 28 | /** 29 | * @author Krzysztof Suszynski 30 | * @since 2018-12-03 31 | */ 32 | public class InternalChecksTest { 33 | 34 | @Rule 35 | public ExpectedException expectedException = ExpectedException.none(); 36 | 37 | @Test 38 | public void instantinate() throws NoSuchMethodException, 39 | IllegalAccessException, InvocationTargetException, 40 | InstantiationException { 41 | // when 42 | Constructor constr = 43 | InternalChecks.class.getDeclaredConstructor(); 44 | constr.setAccessible(true); 45 | InternalChecks checks = constr.newInstance(); 46 | 47 | // then 48 | assertThat(checks).isNotNull(); 49 | } 50 | 51 | @Test 52 | public void testCheckNotNull() { 53 | // given 54 | String eid = "20181203:223812"; 55 | String val = System.getProperty("non-existing-key-in-system"); 56 | 57 | // then 58 | expectedException.expect(IllegalArgumentException.class); 59 | expectedException.expectMessage(eid); 60 | 61 | // when 62 | InternalChecks.checkNotNull(val, eid); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/test/java/pl/wavesoftware/eid/impl/MessageSupplierTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.impl; 18 | 19 | import org.junit.Test; 20 | import pl.wavesoftware.eid.api.Configuration; 21 | 22 | import static org.assertj.core.api.Assertions.assertThat; 23 | 24 | /** 25 | * @author Krzysztof Suszynski 26 | * @since 2018-12-03 27 | */ 28 | public class MessageSupplierTest { 29 | 30 | @Test 31 | public void testGet() { 32 | // given 33 | Configuration configuration = new ConfigurationImpl(); 34 | MessageSupplier supplier = new MessageSupplier( 35 | configuration, "{0} -> 11", new Object[] { ".5" } 36 | ); 37 | 38 | // when 39 | String message = supplier.get(); 40 | 41 | // then 42 | assertThat(message).isEqualTo(".5 -> 11"); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/pl/wavesoftware/eid/impl/TestBinding.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.impl; 18 | 19 | import pl.wavesoftware.eid.api.Binding; 20 | import pl.wavesoftware.eid.api.ConfigurationSystem; 21 | import pl.wavesoftware.eid.api.EidFactories; 22 | 23 | /** 24 | * @author Krzysztof Suszynski 25 | * @since 2.0.0 26 | */ 27 | public final class TestBinding implements Binding { 28 | 29 | private final Binding impl = new BindingImpl(); 30 | 31 | @Override 32 | public ConfigurationSystem getConfigurationSystem() { 33 | return impl.getConfigurationSystem(); 34 | } 35 | 36 | @Override 37 | public EidFactories getFactories() { 38 | return impl.getFactories(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/test/java/pl/wavesoftware/eid/system/BindingChooserTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.system; 18 | 19 | import org.junit.Test; 20 | import pl.wavesoftware.eid.api.Binding; 21 | import pl.wavesoftware.eid.impl.BindingImpl; 22 | import pl.wavesoftware.eid.impl.TestBinding; 23 | 24 | import java.util.ArrayList; 25 | import java.util.Arrays; 26 | import java.util.Collections; 27 | import java.util.List; 28 | 29 | import static org.assertj.core.api.Assertions.*; 30 | 31 | /** 32 | * @author Krzysztof Suszynski 33 | * @since 2018-12-18 34 | */ 35 | public class BindingChooserTest { 36 | 37 | @Test 38 | public void testChooseImplementation() { 39 | // given 40 | BindingChooser chooser = new BindingChooser(); 41 | Binding testBinding = new TestBinding(); 42 | Binding binding = new BindingImpl(); 43 | List bindings = Arrays.asList( 44 | testBinding, 45 | binding 46 | ); 47 | Collections.shuffle(bindings); 48 | 49 | // when 50 | 51 | Binding chosen = chooser.chooseImplementation(bindings); 52 | 53 | // then 54 | assertThat(chosen).isSameAs(testBinding); 55 | } 56 | 57 | @Test 58 | public void testChooseImplementationOnSingle() { 59 | // given 60 | BindingChooser chooser = new BindingChooser(); 61 | 62 | // when 63 | Binding binding = new BindingImpl(); 64 | Binding chosen = chooser.chooseImplementation(Collections.singletonList( 65 | binding 66 | )); 67 | 68 | // then 69 | assertThat(chosen).isSameAs(binding); 70 | } 71 | 72 | @Test(expected = AssertionError.class) 73 | public void testChooseImplementationOnEmpty() { 74 | // given 75 | BindingChooser chooser = new BindingChooser(); 76 | 77 | // when 78 | Binding chosen = chooser.chooseImplementation(new ArrayList()); 79 | 80 | // then 81 | assertThat(chosen).isNull(); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/test/java/pl/wavesoftware/eid/utils/EidExecutionsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.eid.utils; 18 | 19 | import org.junit.Rule; 20 | import org.junit.Test; 21 | import org.junit.rules.ExpectedException; 22 | import pl.wavesoftware.eid.DefaultEid; 23 | import pl.wavesoftware.eid.exceptions.EidRuntimeException; 24 | 25 | import java.text.ParseException; 26 | 27 | import static org.assertj.core.api.Assertions.assertThat; 28 | import static org.hamcrest.CoreMatchers.equalTo; 29 | import static org.hamcrest.CoreMatchers.isA; 30 | import static org.junit.internal.matchers.ThrowableMessageMatcher.hasMessage; 31 | 32 | /** 33 | * @author Krzysztof Suszynski 34 | * @since 2.0.0 35 | */ 36 | public class EidExecutionsTest { 37 | 38 | @Rule 39 | public final ExpectedException thrown = ExpectedException.none(); 40 | 41 | private final String eid = "20181029:230523"; 42 | 43 | @Test 44 | public void testTryToExecute_UnsafeProcedure_String() { 45 | // given 46 | final String causeMessage = "An error occured while parsing JSON document at char 178"; 47 | UnsafeProcedure procedure = new UnsafeProcedure() { 48 | @Override 49 | public void execute() throws ParseException { 50 | throw new ParseException(causeMessage, 178); 51 | } 52 | }; 53 | // then 54 | thrown.expect(EidRuntimeException.class); 55 | thrown.expectCause(isA(ParseException.class)); 56 | thrown.expectCause(hasMessage(equalTo(causeMessage))); 57 | // when 58 | EidExecutions.tryToExecute(procedure, eid); 59 | } 60 | 61 | @Test 62 | public void testTryToExecute_UnsafeProcedure_String_Ok() { 63 | // given 64 | UnsafeProcedure procedure = new UnsafeProcedure() { 65 | @Override 66 | public void execute() { 67 | // nothing special here, for unit test 68 | } 69 | }; 70 | // when 71 | EidExecutions.tryToExecute(procedure, eid); 72 | // then 73 | assertThat(procedure).isNotNull(); 74 | } 75 | 76 | @Test 77 | public void testTryToExecute_UnsafeProcedure_Eid() { 78 | // given 79 | final String causeMessage = "An error occured while parsing JSON document at char 178"; 80 | UnsafeProcedure procedure = new UnsafeProcedure() { 81 | @Override 82 | public void execute() throws ParseException { 83 | throw new ParseException(causeMessage, 178); 84 | } 85 | }; 86 | // then 87 | thrown.expect(EidRuntimeException.class); 88 | thrown.expectCause(isA(ParseException.class)); 89 | thrown.expectCause(hasMessage(equalTo(causeMessage))); 90 | // when 91 | EidExecutions.tryToExecute(procedure, new DefaultEid(eid)); 92 | } 93 | 94 | @Test 95 | public void testTryToExecute_UnsafeProcedure_Eid_Ok() { 96 | // given 97 | UnsafeProcedure procedure = new UnsafeProcedure() { 98 | @Override 99 | public void execute() { 100 | // nothing special here, for unit test 101 | } 102 | }; 103 | // when 104 | EidExecutions.tryToExecute(procedure, new DefaultEid(eid)); 105 | // then 106 | assertThat(procedure).isNotNull(); 107 | } 108 | 109 | @Test 110 | public void testTryToExecute_UnsafeSupplier_String() { 111 | // given 112 | final String causeMessage = "An error occured while parsing JSON document at char 178"; 113 | UnsafeSupplier supplier = new UnsafeSupplier() { 114 | 115 | @Override 116 | public String get() throws ParseException { 117 | throw new ParseException(causeMessage, 178); 118 | } 119 | }; 120 | // then 121 | thrown.expect(EidRuntimeException.class); 122 | thrown.expectCause(isA(ParseException.class)); 123 | thrown.expectCause(hasMessage(equalTo(causeMessage))); 124 | // when 125 | EidExecutions.tryToExecute(supplier, eid); 126 | } 127 | 128 | @Test 129 | public void testTryToExecute_UnsafeSupplier_String_Ok() { 130 | // given 131 | final String returning = "An answer to universe and everything"; 132 | UnsafeSupplier supplier = new UnsafeSupplier() { 133 | @Override 134 | public String get() throws ParseException { 135 | return returning; 136 | } 137 | }; 138 | // when 139 | String answer = EidExecutions.tryToExecute(supplier, eid); 140 | // then 141 | assertThat(supplier).isNotNull(); 142 | assertThat(answer).isNotNull().isNotEmpty().isEqualTo(returning); 143 | } 144 | 145 | @Test 146 | public void testTryToExecute_UnsafeSupplier_Eid() { 147 | // given 148 | final String causeMessage = "An error occured while parsing JSON document at char 178"; 149 | UnsafeSupplier supplier = new UnsafeSupplier() { 150 | 151 | @Override 152 | public String get() throws ParseException { 153 | throw new ParseException(causeMessage, 178); 154 | } 155 | }; 156 | // then 157 | thrown.expect(EidRuntimeException.class); 158 | thrown.expectCause(isA(ParseException.class)); 159 | thrown.expectCause(hasMessage(equalTo(causeMessage))); 160 | // when 161 | EidExecutions.tryToExecute(supplier, new DefaultEid(eid)); 162 | } 163 | 164 | @Test 165 | public void testTryToExecute_UnsafeSupplier_Eid_Ok() { 166 | // given 167 | final String returning = "An answer to universe and everything"; 168 | UnsafeSupplier supplier = new UnsafeSupplier() { 169 | @Override 170 | public String get() throws ParseException { 171 | return returning; 172 | } 173 | }; 174 | // when 175 | String answer = EidExecutions.tryToExecute(supplier, new DefaultEid(eid)); 176 | // then 177 | assertThat(supplier).isNotNull(); 178 | assertThat(answer).isNotNull().isNotEmpty().isEqualTo(returning); 179 | } 180 | } 181 | -------------------------------------------------------------------------------- /src/test/java/pl/wavesoftware/testing/JavaAgentSkip.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Wave Software 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 | package pl.wavesoftware.testing; 18 | 19 | import org.junit.Assume; 20 | import org.junit.rules.TestRule; 21 | import org.junit.runner.Description; 22 | import org.junit.runners.model.Statement; 23 | 24 | import java.lang.management.ManagementFactory; 25 | import java.lang.management.RuntimeMXBean; 26 | import java.util.List; 27 | 28 | /** 29 | * @author Krzysztof Suszynski 30 | * @since 29.03.16 31 | */ 32 | public final class JavaAgentSkip implements TestRule { 33 | 34 | public static final String JAVAAGENT = "-javaagent:"; 35 | private final boolean ifActive; 36 | 37 | private JavaAgentSkip(boolean ifActive) { 38 | this.ifActive = ifActive; 39 | } 40 | 41 | public static JavaAgentSkip ifActive() { 42 | return new JavaAgentSkip(true); 43 | } 44 | 45 | public static JavaAgentSkip ifNotActive() { 46 | return new JavaAgentSkip(false); 47 | } 48 | 49 | @Override 50 | public Statement apply(final Statement base, Description description) { 51 | return new Statement() { 52 | @Override 53 | public void evaluate() throws Throwable { 54 | Assume.assumeTrue("Skipping test due to JavaAgentSkip rule that is set to " + ifActive, assumeValue()); 55 | base.evaluate(); 56 | } 57 | }; 58 | } 59 | 60 | private boolean assumeValue() { 61 | return isAgentThere() != ifActive; 62 | } 63 | 64 | private boolean isAgentThere() { 65 | RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean(); 66 | List arguments = runtimeMxBean.getInputArguments(); 67 | boolean agent = false; 68 | for (String argument : arguments) { 69 | if (argument.startsWith(JAVAAGENT)) { 70 | agent = true; 71 | break; 72 | } 73 | } 74 | return agent; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/test/java/pl/wavesoftware/testing/JavaVersion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.testing; 18 | 19 | /** 20 | * @author Krzysztof Suszynski 21 | * @since 2018-12-29 22 | */ 23 | public final class JavaVersion implements Comparable { 24 | private final int major; 25 | 26 | private JavaVersion(int major) { 27 | this.major = major; 28 | } 29 | 30 | public static JavaVersion get() { 31 | return new JavaVersion(getMajor()); 32 | } 33 | 34 | public static JavaVersion of(int major) { 35 | return new JavaVersion(major); 36 | } 37 | 38 | private static int getMajor() { 39 | String version = System.getProperty("java.version"); 40 | if (version.startsWith("1.")) { 41 | version = version.substring(2); 42 | } 43 | // Allow these formats: 44 | // 1.8.0_72-ea 45 | // 9-ea 46 | // 9 47 | // 9.0.1 48 | int dotPos = version.indexOf('.'); 49 | int dashPos = version.indexOf('-'); 50 | return Integer.parseInt(version.substring( 51 | 0, 52 | dotPos > -1 53 | ? dotPos 54 | : dashPos > -1 55 | ? dashPos : 1 56 | )); 57 | } 58 | 59 | public boolean greaterOrEqual(JavaVersion other) { 60 | return compareTo(other) >= 0; 61 | } 62 | 63 | @Override 64 | public int compareTo(JavaVersion other) { 65 | return this.major - other.major; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/test/java/pl/wavesoftware/testing/JmhCleaner.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Wave Software 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 | package pl.wavesoftware.testing; 18 | 19 | import org.apache.commons.io.FileUtils; 20 | import org.junit.Test; 21 | import org.junit.rules.ExternalResource; 22 | 23 | import java.io.File; 24 | import java.io.IOException; 25 | import java.lang.reflect.Method; 26 | import java.lang.reflect.Modifier; 27 | import java.net.URISyntaxException; 28 | 29 | /** 30 | * @author Krzysztof Suszynski 31 | * @since 25.03.16 32 | */ 33 | public class JmhCleaner extends ExternalResource { 34 | private static final String GENERATED_TEST_SOURCES = "generated-test-sources"; 35 | private static final String TEST_ANNOTATIONS = "test-annotations"; 36 | private final Class testClass; 37 | 38 | public JmhCleaner(Class testClass) { 39 | this.testClass = validateTestClass(testClass); 40 | } 41 | 42 | private Class validateTestClass(Class testClass) { 43 | boolean hasTests = false; 44 | for (Method method : testClass.getDeclaredMethods()) { 45 | Test annot = method.getAnnotation(Test.class); 46 | if (Modifier.isPublic(method.getModifiers()) && !Modifier.isStatic(method.getModifiers()) && annot != null) { 47 | hasTests = true; 48 | break; 49 | } 50 | } 51 | if (!hasTests) { 52 | throw new IllegalArgumentException("You need to pass a test class to constructor of JmhCleaner!!"); 53 | } 54 | return testClass; 55 | } 56 | 57 | @Override 58 | protected void after() { 59 | try { 60 | cleanup(); 61 | } catch (IOException ex) { 62 | throw new RuntimeException(ex); 63 | } catch (URISyntaxException ex) { 64 | throw new RuntimeException(ex); 65 | } 66 | } 67 | 68 | private void cleanup() throws IOException, URISyntaxException { 69 | String location = testClass.getProtectionDomain().getCodeSource().getLocation().toURI().getPath(); 70 | File file = new File(location).getCanonicalFile().getParentFile(); 71 | File testAnnotationsPath = resolve(file, GENERATED_TEST_SOURCES, TEST_ANNOTATIONS); 72 | if (!testAnnotationsPath.isDirectory()) { 73 | return; 74 | } 75 | FileUtils.deleteDirectory(testAnnotationsPath); 76 | } 77 | 78 | private File resolve(File parent, String... paths) { 79 | StringBuilder sb = new StringBuilder(parent.getPath()); 80 | for (String path : paths) { 81 | sb.append(File.separator).append(path); 82 | } 83 | return new File(sb.toString()); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/test/java/pl/wavesoftware/testing/JvmArgs.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Wave Software 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 | package pl.wavesoftware.testing; 18 | 19 | /** 20 | * @author Krzysztof Suszynski 21 | * @since 2018-12-29 22 | */ 23 | public final class JvmArgs { 24 | 25 | private static final String[] PRE_RELEASE_8 = new String[] { 26 | "-server", "-Xms256m", "-Xmx256m", 27 | "-XX:PermSize=128m", "-XX:MaxPermSize=128m", 28 | "-XX:+UseParNewGC" 29 | }; 30 | 31 | private static final String[] RELEASE_8_PLUS = new String[] { 32 | "-server", "-Xms256m", "-Xmx256m", 33 | "-XX:+UseParallelGC" 34 | }; 35 | 36 | private JvmArgs() { 37 | // nothing here 38 | } 39 | 40 | public static String[] get() { 41 | JavaVersion version = JavaVersion.get(); 42 | return version.greaterOrEqual(JavaVersion.of(8)) 43 | ? RELEASE_8_PLUS : PRE_RELEASE_8; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/test/resources/META-INF/services/pl.wavesoftware.eid.api.Binding: -------------------------------------------------------------------------------- 1 | pl.wavesoftware.eid.impl.TestBinding 2 | -------------------------------------------------------------------------------- /src/test/resources/META-INF/services/pl.wavesoftware.eid.api.Configurator: -------------------------------------------------------------------------------- 1 | pl.wavesoftware.eid.api.TestConfigurator 2 | -------------------------------------------------------------------------------- /src/test/resources/logback-test.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 22 | 23 | 24 | 25 | %highlight(%-5level) %magenta(%d{HH:mm:ss.SSS}) %cyan(%logger{36}) - %msg%n 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | --------------------------------------------------------------------------------