1 /*
   2  * Copyright (c) 2011, 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 /**
  26  * @test
  27  * @bug 6357214
  28  * @summary Hotspot server compiler gets integer comparison wrong
  29  *
  30  * @run main/othervm/timeout=60 -DshowAll=ffo -DeventID=444 compiler.c2.Test6357214
  31  */
  32 
  33 package compiler.c2;
  34 
  35 // The test hangs after few iterations before the fix. So it fails if timeout.
  36 public class Test6357214 {
  37     static class MyResult {
  38         public boolean next() {
  39             return true;
  40         }
  41 
  42         public String getString(String in) {
  43             if (in.equals("id"))
  44                 return "idFoo";
  45             if (in.equals("contentKey"))
  46                 return "ckFoo";
  47             return "Foo";
  48         }
  49 
  50         public int getInt(String in) {
  51             if (in.equals("processingComplete"))
  52                 return 0;
  53             return 1;
  54         }
  55 
  56         public byte[] getBytes(String in) {
  57             byte[] arr = null;
  58             if (in.equals("content")) {
  59                 arr = new byte[65536];
  60                 byte j = 32;
  61                 for (int i=0; i<65536; i++) {
  62                     arr[i] = j;
  63                     if (++j == 127)
  64                         j=32;
  65                 }
  66             }
  67             return arr;
  68         }
  69     }
  70 
  71     public static volatile boolean bollocks = true;
  72     public String create(String context) throws Exception {
  73 
  74         //
  75         // Extract HTTP parameters
  76         //
  77 
  78         boolean showAll = System.getProperty("showAll") != null;
  79           String eventID = System.getProperty("eventID");
  80           String eventContentKey = System.getProperty("cKey");
  81         //
  82         // Build ContentStaging query based on eventID or eventContentKey
  83         //
  84 
  85         String sql = "select id, processingComplete, contentKey, content "
  86                    + "from   ContentStaging cs, ContentStagingKey csk "
  87                    + "where  cs.eventContentKey = csk.eventContentKey ";
  88 
  89         if (eventID != null) {
  90             sql += "and id = " + eventID;
  91         }
  92         else if (eventContentKey != null) {
  93             sql += "and cs.eventContentKey = '"
  94                 +  eventContentKey
  95                 +  "' having id = max(id)";
  96         }
  97         else {
  98             throw new Exception("Need eventID or eventContentKey");
  99         }
 100 
 101         //
 102         // This factory builds a static panel, there is no JSP
 103         //
 104 
 105         StringBuffer html = new StringBuffer();
 106 
 107         try {
 108 
 109                 MyResult result = new MyResult();
 110             if (result.next()) {
 111 
 112                 eventID = result.getString("id");
 113                 int processingComplete = result.getInt("processingComplete");
 114                 String contentKey = result.getString("contentKey");
 115                 byte[] bytes = result.getBytes("content");
 116 
 117                 //
 118                 // Print content status and associated controls
 119                 //
 120 
 121                 html.append("<br/><font class=\"small\">");
 122                 html.append("Status: ");
 123                 switch (processingComplete) {
 124                     case  0 :
 125                     case  1 : html.append("PENDING"); break;
 126                     case  2 : html.append(contentKey); break;
 127                     case  3 : html.append(eventID); break;
 128                     default : html.append("UNKNONW");
 129                 }
 130                 html.append("</font><br/>");
 131 
 132                 //
 133                 // Print at most 20Kb of content unless "showAll" is set
 134                 //
 135 
 136                 int limit = showAll ? Integer.MAX_VALUE : 1024 * 20;
 137                 System.out.println(limit);
 138                 html.append("<pre>");
 139                 for (int i = 0; bytes != null && i < bytes.length; i++) {
 140                     char c = (char) bytes[i];
 141                     switch (c) {
 142                         case '<' : html.append("&lt;");  break;
 143                         case '>' : html.append("&gt;");  break;
 144                         case '&' : html.append("&amp;"); break;
 145                         default  : html.append(c);
 146                     }
 147 
 148                     if (i > limit) {
 149                         while (bollocks);
 150                         // System.out.println("i is " + i);
 151                         // System.out.println("limit is " + limit);
 152                         html.append("...\n</pre>");
 153                         html.append(eventID);
 154                         html.append("<pre>");
 155                         break;
 156                     }
 157                 }
 158                 html.append("</pre>");
 159             }
 160         }
 161         catch (Exception exception) {
 162             throw exception;
 163         }
 164         finally {
 165             html.append("Oof!!");
 166         }
 167         String ret = html.toString();
 168         System.out.println("Returning string length = "+ ret.length());
 169         return ret;
 170     }
 171 
 172     public static void main(String[] args) throws Exception {
 173                 int length=0;
 174 
 175                 for (int i = 0; i < 100; i++) {
 176                         length = new Test6357214().create("boo").length();
 177                         System.out.println(length);
 178                 }
 179     }
 180 }
 181