├── src
├── test
│ ├── resources
│ │ ├── META-INF
│ │ │ └── services
│ │ │ │ ├── pl.wavesoftware.eid.api.Binding
│ │ │ │ └── pl.wavesoftware.eid.api.Configurator
│ │ └── logback-test.xml
│ ├── java
│ │ └── pl
│ │ │ └── wavesoftware
│ │ │ ├── eid
│ │ │ ├── exceptions
│ │ │ │ ├── A.java
│ │ │ │ ├── AssertionsTest.java
│ │ │ │ ├── EidRuntimeExceptionTest.java
│ │ │ │ ├── EidIllegalStateExceptionTest.java
│ │ │ │ ├── EidIllegalArgumentExceptionTest.java
│ │ │ │ ├── ExceptionsTest.java
│ │ │ │ ├── EidNullPointerExceptionTest.java
│ │ │ │ └── EidIndexOutOfBoundsExceptionTest.java
│ │ │ ├── impl
│ │ │ │ ├── TestBinding.java
│ │ │ │ ├── DefaultUniqueIdGeneratorTest.java
│ │ │ │ ├── MessageSupplierTest.java
│ │ │ │ ├── BindingImplTest.java
│ │ │ │ ├── DefaultEidMessageTest.java
│ │ │ │ ├── InternalChecksTest.java
│ │ │ │ ├── DefaultFormatterTest.java
│ │ │ │ └── EidTextRepresentationTest.java
│ │ │ ├── ConfigurationContext.java
│ │ │ ├── ConstantUniqueIdRule.java
│ │ │ ├── api
│ │ │ │ └── TestConfigurator.java
│ │ │ ├── ConfiguratorRule.java
│ │ │ ├── DisableValidatorState.java
│ │ │ ├── system
│ │ │ │ └── BindingChooserTest.java
│ │ │ ├── EidIT.java
│ │ │ ├── EidTest.java
│ │ │ └── utils
│ │ │ │ └── EidExecutionsTest.java
│ │ │ └── testing
│ │ │ ├── JvmArgs.java
│ │ │ ├── JavaVersion.java
│ │ │ ├── JavaAgentSkip.java
│ │ │ └── JmhCleaner.java
│ └── groovy
│ │ └── verify-sonar-issues.groovy
└── main
│ ├── resources
│ └── META-INF
│ │ └── services
│ │ └── pl.wavesoftware.eid.api.Binding
│ └── java
│ └── pl
│ └── wavesoftware
│ └── eid
│ ├── api
│ ├── package-info.java
│ ├── EidContainer.java
│ ├── SerializableSupplier.java
│ ├── Supplier.java
│ ├── UniqueIdGenerator.java
│ ├── EidMessage.java
│ ├── LazyFactory.java
│ ├── EidFactories.java
│ ├── Formatter.java
│ ├── Validator.java
│ ├── EidFactory.java
│ ├── ConfigurationSystem.java
│ ├── EidMessageFactory.java
│ ├── Configurator.java
│ ├── ReturnTypesAreNonnullByDefault.java
│ ├── Binding.java
│ ├── Configuration.java
│ ├── ConfigurationBuilder.java
│ └── Eid.java
│ ├── utils
│ ├── package-info.java
│ ├── UnsafeProcedure.java
│ ├── UnsafeSupplier.java
│ ├── EidUtil.java
│ └── EidExecutions.java
│ ├── exceptions
│ ├── package-info.java
│ ├── EidNullPointerException.java
│ └── EidIllegalStateException.java
│ ├── package-info.java
│ ├── impl
│ ├── package-info.java
│ ├── MutableConfiguration.java
│ ├── InternalChecks.java
│ ├── LazyFactoryImpl.java
│ ├── EidFactoryImpl.java
│ ├── DefaultConfigurator.java
│ ├── TextMessage.java
│ ├── BindingImpl.java
│ ├── EidMessageFactoryImpl.java
│ ├── DefaultUniqueIdGenerator.java
│ ├── EidFactoriesImpl.java
│ ├── Lazy.java
│ ├── EidTextRepresentation.java
│ ├── SerializableLazy.java
│ ├── DefaultEidMessage.java
│ ├── MessageSupplier.java
│ ├── DefaultFormatter.java
│ ├── ConfigurationSystemImpl.java
│ └── ConfigurationImpl.java
│ ├── system
│ ├── package-info.java
│ ├── BindingChooser.java
│ └── EidModule.java
│ └── DefaultEid.java
├── .gitignore
├── .editorconfig
├── .travis.yml
└── .circleci
└── config.yml
/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 |
--------------------------------------------------------------------------------
/.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 |
--------------------------------------------------------------------------------
/.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 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/.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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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 |
--------------------------------------------------------------------------------
/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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 | *
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/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/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 |
--------------------------------------------------------------------------------
/.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 |
--------------------------------------------------------------------------------
/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/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 | *
30 | *
An explicit nullness annotation
31 | *
The method overrides a method in a superclass (in which case the
32 | * annotation of the corresponding parameter in the superclass applies)
33 | *
there is a default parameter annotation applied to a more tightly
34 | * nested element.
35 | *
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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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 | *
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/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/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/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/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/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