1 /*
   2  * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @run testng ModuleReferenceTest
  27  * @summary Basic tests for java.lang.module.ModuleReference
  28  */
  29 
  30 import java.lang.module.ModuleDescriptor;
  31 import java.lang.module.ModuleReader;
  32 import java.lang.module.ModuleReference;
  33 import java.net.URI;
  34 
  35 import org.testng.annotations.Test;
  36 import static org.testng.Assert.*;
  37 
  38 @Test
  39 public class ModuleReferenceTest {
  40 
  41     private ModuleReference newModuleReference(ModuleDescriptor descriptor, URI uri) {
  42         return new ModuleReference(descriptor, uri) {
  43             @Override
  44             public ModuleReader open() {
  45                 throw new UnsupportedOperationException();
  46             }
  47         };
  48     }
  49 
  50     public void testBasic() throws Exception {
  51         ModuleDescriptor descriptor
  52             = ModuleDescriptor.module("m")
  53                 .exports("p")
  54                 .exports("q")
  55                 .contains("p.internal")
  56                 .build();
  57 
  58         URI uri = URI.create("module:/m");
  59 
  60         ModuleReference mref = newModuleReference(descriptor, uri);
  61 
  62         assertTrue(mref.descriptor().equals(descriptor));
  63         assertTrue(mref.location().get().equals(uri));
  64     }
  65 
  66     @Test(expectedExceptions = { NullPointerException.class })
  67     public void testNullDescriptor() throws Exception {
  68         URI location = URI.create("module:/m");
  69         newModuleReference(null, location);
  70     }
  71 
  72     public void testNullLocation() {
  73         ModuleDescriptor descriptor
  74             = ModuleDescriptor.module("m")
  75                 .exports("p")
  76                 .build();
  77         ModuleReference mref = newModuleReference(descriptor, null);
  78         assertTrue(!mref.location().isPresent());
  79     }
  80 
  81 }