1 /*
   2  * Copyright (c) 2010, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package test;
  27 
  28 import java.lang.annotation.Annotation;
  29 import java.lang.module.*;
  30 import java.lang.reflect.Module;
  31 import java.io.*;
  32 import java.util.*;
  33 import com.foo.ArrayTypes;
  34 import com.foo.ArrayTypesWithDefault;
  35 import com.foo.Stooge;
  36 import org.bar.ScalarTypes;
  37 import org.bar.ScalarTypesWithDefault;
  38 import org.bar.Point;
  39 import org.openjdk.jigsaw.*;
  40 
  41 /*
  42  * @summary Test reading of annotations from the input module-info.class
  43  * This references test/java/lang/annotation/UnitTest.java.
  44  *
  45  */
  46 public class ModuleAnnotationTest {
  47     private static JigsawModuleSystem ms = JigsawModuleSystem.instance();
  48     private static int failCount = 0;
  49     private static void fail(String test) {
  50         System.out.println("Failure: " + test);
  51         failCount++;
  52     }
  53 
  54     public static void main(String[] argv) throws Exception {
  55         File libPath = new File(argv[0]);
  56         Library lb = SimpleLibrary.open(libPath, false);
  57 
  58         List<ModuleId> mids = lb.findModuleIds("test.foo.bar");
  59         if (mids.size() != 1)
  60             throw new RuntimeException("Installed modules: " + mids);
  61 
  62         System.out.println("Installed module : " + mids);
  63 
  64         for (ModuleId mid : mids) {
  65             loadModule(lb, mid);
  66         }
  67 
  68         if (failCount > 0) {
  69            throw new RuntimeException("Test failed: " + failCount);
  70         }
  71     }
  72 
  73     static void loadModule(Library lb, ModuleId mid) throws Exception {
  74         ModuleInfo mi = lb.readModuleInfo(mid);
  75         if (mi == null)
  76             throw new RuntimeException(mi + ": Can't read module-info");
  77 
  78         System.out.format("Module %s%n", mi.toString());
  79         Class<?> cls = Class.forName("foo.bar.Main");
  80         Module module = cls.getModule();
  81         System.out.format("Class %s from %s%n", cls, module.getModuleId());
  82         checkModule(module);
  83     }
  84 
  85     static void checkModule(Module module) throws Exception {
  86         if (module.isAnnotationPresent(ScalarTypes.class) &&
  87             module.isAnnotationPresent(ArrayTypes.class)) {
  88             checkScalarTypes(module.getAnnotation(ScalarTypes.class), module);
  89             checkArrayTypes(module.getAnnotation(ArrayTypes.class), module);
  90         } else if (module.isAnnotationPresent(ScalarTypesWithDefault.class) &&
  91                    module.isAnnotationPresent(ArrayTypesWithDefault.class)) {
  92             checkScalarTypes(module.getAnnotation(ScalarTypesWithDefault.class), module);
  93             checkArrayTypes(module.getAnnotation(ArrayTypesWithDefault.class), module);
  94         } else {
  95             throw new RuntimeException("Expected annotation is missing in " + module.getModuleId());
  96         }
  97     }
  98 
  99     static void checkScalarTypes(ScalarTypes st, Module m) {
 100         if (!(st.b()    == 1            &&
 101               st.s()    == 2            &&
 102               st.i()    == 3            &&
 103               st.l()    == 4L           &&
 104               st.c()    == '5'          &&
 105               st.f()    == 6.0f         &&
 106               st.d()    == 7.0          &&
 107               st.bool() == true         &&
 108               st.str().equals("custom") &&
 109               st.e()    == Stooge.MOE   &&
 110               st.a().x() == 1 && st.a().y() == 2))
 111             fail(m.getModuleId() + ": unexpected ScalarTypes");
 112 
 113         Class<?> cls = st.cls();
 114         if (cls != java.util.Map.class) {
 115             fail(m.getModuleId() + ": ScalarTypes.cls() returns unexpected value " + cls);
 116         }
 117     }
 118 
 119     static void checkScalarTypes(ScalarTypesWithDefault st, Module m) {
 120         if (!(st.b()    == 11            &&
 121               st.s()    == 12            &&
 122               st.i()    == 13            &&
 123               st.l()    == 14L           &&
 124               st.c()    == 'V'           &&
 125               st.f()    == 16.0f         &&
 126               st.d()    == 17.0          &&
 127               st.bool() == false         &&
 128               st.str().equals("default") &&
 129               st.e()    == Stooge.LARRY  &&
 130               st.a().x() == 11 && st.a().y() == 12))
 131             fail(m.getModuleId() + ": unexpected ScalarTypesWithDefault");
 132 
 133         Class<?> cls = st.cls();
 134         if (cls != java.util.Deque.class) {
 135             fail(m.getModuleId() + ": ScalarTypesWithDefault.cls() returns unexpected value " + cls);
 136         }
 137     }
 138 
 139     static void checkArrayTypes(ArrayTypes at, Module m) {
 140         if (!(at.b()[0]    == 1            && at.b()[1]    == 2            &&
 141               at.s()[0]    == 2            && at.s()[1]    == 3            &&
 142               at.i()[0]    == 3            && at.i()[1]    == 4            &&
 143               at.l()[0]    == 4L           && at.l()[1]    == 5L           &&
 144               at.c()[0]    == '5'          && at.c()[1]    == '6'          &&
 145               at.f()[0]    == 6.0f         && at.f()[1]    == 7.0f         &&
 146               at.d()[0]    == 7.0          && at.d()[1]    == 8.0          &&
 147               at.bool()[0] == true         && at.bool()[1] == false        &&
 148               at.str()[0].equals("custom") && at.str()[1].equals("paint")  &&
 149               at.e()[0]    == Stooge.MOE   && at.e()[1]    == Stooge.CURLY &&
 150               at.a()[0].x() == 1 && at.a()[0].y() == 2 && at.a()[1].x() == 3 && at.a()[1].y() == 4 &&
 151               at.b().length==2    && at.s().length==2   && at.i().length==2 &&
 152               at.l().length==2    && at.c().length==2   && at.d().length==2 &&
 153               at.bool().length==2 && at.str().length==2 && at.a().length==2))
 154             fail(m.getModuleId() + ": unexpected ArrayTypes");
 155 
 156         Class<?>[] cls = at.cls();
 157         if (!(cls.length == 2 &&
 158               cls[0] == java.util.Map.class &&
 159               cls[1] == java.util.Set.class))
 160             fail(m.getModuleId() + ": ArrayTypes.cls() returns " + Arrays.toString(cls));
 161     }
 162 
 163     static void checkArrayTypes(ArrayTypesWithDefault at, Module m) {
 164         if (!(at.b()[0]    == 11            &&
 165               at.s()[0]    == 12            &&
 166               at.i()[0]    == 13            &&
 167               at.l()[0]    == 14L           &&
 168               at.c()[0]    == 'V'           &&
 169               at.f()[0]    == 16.0f         &&
 170               at.d()[0]    == 17.0          &&
 171               at.bool()[0] == false         &&
 172               at.str()[0].equals("default") &&
 173               at.e()[0]    == Stooge.LARRY  &&
 174               at.a()[0].x() == 11 && at.a()[0].y() == 12 &&
 175               at.b().length==1    && at.s().length==1   && at.i().length==1 &&
 176               at.l().length==1    && at.c().length==1   && at.d().length==1 &&
 177               at.bool().length==1 && at.str().length==1))
 178             fail(m.getModuleId() + ": unexpected ArrayTypesWithDefault");
 179 
 180         Class<?>[] cls = at.cls();
 181         if (!(cls.length == 2 &&
 182               cls[0] == java.util.Deque.class &&
 183               cls[1] == java.util.Queue.class))
 184             fail(m.getModuleId() + ": ArrayTypesWithDefault.cls() returns " + Arrays.toString(cls));
 185     }
 186 }
--- EOF ---