Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  *
0009  *    http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  */
0017 
0018 package org.apache.spark.api.java;
0019 
0020 import org.junit.Assert;
0021 import org.junit.Test;
0022 
0023 /**
0024  * Tests {@link Optional}.
0025  */
0026 public class OptionalSuite {
0027 
0028   @Test
0029   public void testEmpty() {
0030     Assert.assertFalse(Optional.empty().isPresent());
0031     Assert.assertNull(Optional.empty().orNull());
0032     Assert.assertEquals("foo", Optional.empty().or("foo"));
0033     Assert.assertEquals("foo", Optional.empty().orElse("foo"));
0034   }
0035 
0036   @Test(expected = NullPointerException.class)
0037   public void testEmptyGet() {
0038     Optional.empty().get();
0039   }
0040 
0041   @Test
0042   public void testAbsent() {
0043     Assert.assertFalse(Optional.absent().isPresent());
0044     Assert.assertNull(Optional.absent().orNull());
0045     Assert.assertEquals("foo", Optional.absent().or("foo"));
0046     Assert.assertEquals("foo", Optional.absent().orElse("foo"));
0047   }
0048 
0049   @Test(expected = NullPointerException.class)
0050   public void testAbsentGet() {
0051     Optional.absent().get();
0052   }
0053 
0054   @Test
0055   public void testOf() {
0056     Assert.assertTrue(Optional.of(1).isPresent());
0057     Assert.assertNotNull(Optional.of(1).orNull());
0058     Assert.assertEquals(Integer.valueOf(1), Optional.of(1).get());
0059     Assert.assertEquals(Integer.valueOf(1), Optional.of(1).or(2));
0060     Assert.assertEquals(Integer.valueOf(1), Optional.of(1).orElse(2));
0061   }
0062 
0063   @Test(expected = NullPointerException.class)
0064   public void testOfWithNull() {
0065     Optional.of(null);
0066   }
0067 
0068   @Test
0069   public void testOfNullable() {
0070     Assert.assertTrue(Optional.ofNullable(1).isPresent());
0071     Assert.assertNotNull(Optional.ofNullable(1).orNull());
0072     Assert.assertEquals(Integer.valueOf(1), Optional.ofNullable(1).get());
0073     Assert.assertEquals(Integer.valueOf(1), Optional.ofNullable(1).or(2));
0074     Assert.assertEquals(Integer.valueOf(1), Optional.ofNullable(1).orElse(2));
0075     Assert.assertFalse(Optional.ofNullable(null).isPresent());
0076     Assert.assertNull(Optional.ofNullable(null).orNull());
0077     Assert.assertEquals(Integer.valueOf(2), Optional.<Integer>ofNullable(null).or(2));
0078     Assert.assertEquals(Integer.valueOf(2), Optional.<Integer>ofNullable(null).orElse(2));
0079   }
0080 
0081   @Test
0082   public void testFromNullable() {
0083     Assert.assertTrue(Optional.fromNullable(1).isPresent());
0084     Assert.assertNotNull(Optional.fromNullable(1).orNull());
0085     Assert.assertEquals(Integer.valueOf(1), Optional.fromNullable(1).get());
0086     Assert.assertEquals(Integer.valueOf(1), Optional.fromNullable(1).or(2));
0087     Assert.assertEquals(Integer.valueOf(1), Optional.fromNullable(1).orElse(2));
0088     Assert.assertFalse(Optional.fromNullable(null).isPresent());
0089     Assert.assertNull(Optional.fromNullable(null).orNull());
0090     Assert.assertEquals(Integer.valueOf(2), Optional.<Integer>fromNullable(null).or(2));
0091     Assert.assertEquals(Integer.valueOf(2), Optional.<Integer>fromNullable(null).orElse(2));
0092   }
0093 
0094 }