1 /*
   2  * Copyright (c) 2007, 2018, 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 package vm.oom.limit;
  24 
  25 import java.util.*;
  26 import nsk.share.gc.*;
  27 import nsk.share.gc.gp.*;
  28 import nsk.share.TestFailure;
  29 import vm.oom.OOMTraceTest;
  30 
  31 public class OOMLimitAnonymousTraceTest extends OOMTraceTest implements GarbageProducerAware {
  32 
  33     private GarbageProducer garbageProducer;
  34 
  35     private class InnerClassB {
  36 
  37         public void createB() {
  38             Object o = garbageProducer.create(Long.MAX_VALUE);
  39         }
  40     }
  41 
  42     private class InnerClassA {
  43 
  44         public InnerClassA() {
  45             new InnerClassB().createB();
  46         }
  47     }
  48 
  49     @Override
  50     protected boolean isAlwaysOOM() {
  51         return false;
  52     }
  53 
  54     protected OutOfMemoryError createOOM() {
  55         try {
  56             new InnerClassA();
  57             return null;
  58         } catch (OutOfMemoryError e) {
  59             return e;
  60         }
  61     }
  62 
  63     protected void initExpectedTrace() {
  64         super.initExpectedTrace();
  65         addExpectedTraceElement(InnerClassA.class.getName(), "<init>", false);
  66         addExpectedTraceElement(InnerClassB.class.getName(), "createB", false);
  67         addExpectedTraceElement(null, "create", false);
  68     }
  69 
  70     public final void setGarbageProducer(GarbageProducer garbageProducer) {
  71         this.garbageProducer = garbageProducer;
  72     }
  73 
  74     public static void main(String[] args) {
  75         GC.runTest(new OOMLimitAnonymousTraceTest(), args);
  76     }
  77 }