|
| 1 | +/* |
| 2 | + * Copyright 2016-2026 Qameta Software Inc |
| 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 io.qameta.allure.test; |
| 17 | + |
| 18 | +import io.qameta.allure.Allure; |
| 19 | +import io.github.benas.randombeans.api.EnhancedRandom; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import java.util.concurrent.atomic.AtomicReference; |
| 23 | + |
| 24 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 25 | +import static org.junit.jupiter.api.Assertions.assertNotSame; |
| 26 | +import static org.junit.jupiter.api.Assertions.assertSame; |
| 27 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 28 | + |
| 29 | +class TestUtilitiesTest { |
| 30 | + |
| 31 | + @Test |
| 32 | + void shouldGenerateStableThreadLocalRandomPerThread() throws Exception { |
| 33 | + final EnhancedRandom mainThread = ThreadLocalEnhancedRandom.current(); |
| 34 | + final AtomicReference<EnhancedRandom> workerThread = new AtomicReference<>(); |
| 35 | + final Thread thread = new Thread(() -> |
| 36 | + workerThread.set(ThreadLocalEnhancedRandom.current()) |
| 37 | + ); |
| 38 | + |
| 39 | + Allure.step("Resolve thread-local random generators on two threads and compare their identities", () -> { |
| 40 | + thread.start(); |
| 41 | + thread.join(); |
| 42 | + Allure.addAttachment( |
| 43 | + "thread-local-random-identities", |
| 44 | + "main=" + System.identityHashCode(mainThread) |
| 45 | + + "\nworker=" + System.identityHashCode(workerThread.get()) |
| 46 | + ); |
| 47 | + assertSame(mainThread, ThreadLocalEnhancedRandom.current()); |
| 48 | + assertNotSame(mainThread, workerThread.get()); |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void shouldGenerateExpectedRandomTestDataShapes() { |
| 54 | + final String name = TestData.randomName(); |
| 55 | + final String id = TestData.randomId(); |
| 56 | + final String value = TestData.randomString(16); |
| 57 | + |
| 58 | + assertEquals(10, name.length()); |
| 59 | + assertEquals(10, id.length()); |
| 60 | + assertEquals(16, value.length()); |
| 61 | + assertTrue(name.matches("[A-Za-z]+")); |
| 62 | + assertTrue(id.matches("[A-Za-z0-9]+")); |
| 63 | + assertTrue(value.matches("[A-Za-z0-9]+")); |
| 64 | + } |
| 65 | +} |
0 commit comments