|  | 
|  | 1 | +// Licensed to the Software Freedom Conservancy (SFC) under one | 
|  | 2 | +// or more contributor license agreements.  See the NOTICE file | 
|  | 3 | +// distributed with this work for additional information | 
|  | 4 | +// regarding copyright ownership.  The SFC licenses this file | 
|  | 5 | +// to you under the Apache License, Version 2.0 (the | 
|  | 6 | +// "License"); you may not use this file except in compliance | 
|  | 7 | +// with the License.  You may obtain a copy of the License at | 
|  | 8 | +// | 
|  | 9 | +//   http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 10 | +// | 
|  | 11 | +// Unless required by applicable law or agreed to in writing, | 
|  | 12 | +// software distributed under the License is distributed on an | 
|  | 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | 
|  | 14 | +// KIND, either express or implied.  See the License for the | 
|  | 15 | +// specific language governing permissions and limitations | 
|  | 16 | +// under the License. | 
|  | 17 | + | 
|  | 18 | +package org.openqa.selenium.concurrent; | 
|  | 19 | + | 
|  | 20 | +import static org.assertj.core.api.Assertions.assertThat; | 
|  | 21 | + | 
|  | 22 | +import java.util.concurrent.atomic.AtomicInteger; | 
|  | 23 | +import org.junit.jupiter.api.Tag; | 
|  | 24 | +import org.junit.jupiter.api.Test; | 
|  | 25 | + | 
|  | 26 | +@Tag("UnitTests") | 
|  | 27 | +public class LazyTest { | 
|  | 28 | +  private final AtomicInteger counter = new AtomicInteger(0); | 
|  | 29 | + | 
|  | 30 | +  @Test | 
|  | 31 | +  void trivialCase() { | 
|  | 32 | +    Lazy expression = Lazy.lazy(() -> "constant"); | 
|  | 33 | +    assertThat(expression.get()).isEqualTo("constant"); | 
|  | 34 | +    assertThat(expression.getIfInitialized()).contains("constant"); | 
|  | 35 | +  } | 
|  | 36 | + | 
|  | 37 | +  @Test | 
|  | 38 | +  void getIfInitialized_returnsNothing_ifNotInitializedYet() { | 
|  | 39 | +    Lazy expression = Lazy.lazy(() -> "value#" + counter.incrementAndGet()); | 
|  | 40 | +    assertThat(expression.getIfInitialized()).isEmpty(); | 
|  | 41 | +  } | 
|  | 42 | + | 
|  | 43 | +  @Test | 
|  | 44 | +  void lazyEvaluatedExpression() { | 
|  | 45 | +    Lazy expression = Lazy.lazy(() -> "value#" + counter.incrementAndGet()); | 
|  | 46 | +    assertThat(expression.get()).isEqualTo("value#1"); | 
|  | 47 | +    assertThat(expression.get()).isEqualTo("value#1"); | 
|  | 48 | +    assertThat(expression.getIfInitialized()).contains("value#1"); | 
|  | 49 | +    assertThat(expression.getIfInitialized()).contains("value#1"); | 
|  | 50 | +  } | 
|  | 51 | + | 
|  | 52 | +  @Test | 
|  | 53 | +  void differentLazyInstances_produce_differentValues() { | 
|  | 54 | +    Lazy expression1 = Lazy.lazy(() -> "one#" + counter.incrementAndGet()); | 
|  | 55 | +    Lazy expression2 = Lazy.lazy(() -> "two#" + counter.incrementAndGet()); | 
|  | 56 | +    assertThat(expression1.get()).isEqualTo("one#1"); | 
|  | 57 | +    assertThat(expression1.getIfInitialized()).contains("one#1"); | 
|  | 58 | +    assertThat(expression2.getIfInitialized()).isEmpty(); | 
|  | 59 | + | 
|  | 60 | +    assertThat(expression2.get()).isEqualTo("two#2"); | 
|  | 61 | +    assertThat(expression2.getIfInitialized()).contains("two#2"); | 
|  | 62 | + | 
|  | 63 | +    assertThat(expression1.get()).isEqualTo("one#1"); | 
|  | 64 | +    assertThat(expression2.get()).isEqualTo("two#2"); | 
|  | 65 | +  } | 
|  | 66 | +} | 
0 commit comments