nashorn/test/script/basic/javaadapter.js
author martin
Wed, 10 Dec 2014 09:23:00 -0800
changeset 28057 1a47ceecdba5
parent 24778 2ff5d7041566
permissions -rw-r--r--
8065804: JEP 171: Clarifications/corrections for fence intrinsics Reviewed-by: dholmes, shade, dl, psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16205
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
     1
/*
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 16205
diff changeset
     4
 *
16205
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 16205
diff changeset
     8
 *
16205
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    13
 * accompanied this code).
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 16205
diff changeset
    14
 *
16205
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 16205
diff changeset
    18
 *
16205
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    21
 * questions.
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    22
 */
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    23
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    24
/**
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    25
 * Test to check JavaAdapter constructor.
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    26
 *
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    27
 * @test
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    28
 * @run
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    29
 */
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    30
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    31
try {
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    32
    load("nashorn:mozilla_compat.js");
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    33
} catch (e) {
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    34
}
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    35
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    36
// try various JavaAdapter cases
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    37
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    38
// Single interface
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    39
var runReached = false;
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    40
var r = new JavaAdapter(java.lang.Runnable) {
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    41
    run: function() {
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    42
        runReached = true;
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    43
    }
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    44
};
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    45
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    46
r.run();
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    47
if (! runReached) {
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    48
    fail("run was not called");
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    49
}
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    50
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    51
if (! (r instanceof java.lang.Runnable)) {
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    52
    fail("r is not a Runnable");
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    53
}
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    54
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    55
// Multiple intefaces
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    56
var runReached = false;
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    57
var actionPerformedReached = false;
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    58
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    59
var obj = new JavaAdapter(java.awt.event.ActionListener, java.lang.Runnable) {
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    60
    actionPerformed : function(e) {
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    61
        actionPerformedReached = true;
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    62
    },
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    63
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    64
    run: function() {
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    65
        runReached = true;
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    66
    }
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    67
};
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    68
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    69
obj.actionPerformed(null);
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    70
if (! actionPerformedReached) {
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    71
    fail("actionPerformed was not called");
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    72
}
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    73
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    74
obj.run();
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    75
if (! runReached) {
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    76
    fail("run was not called");
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    77
}
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    78
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    79
if (! (obj instanceof java.lang.Runnable)) {
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    80
    fail("obj is not a Runnable");
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    81
}
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    82
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    83
if (! (obj instanceof java.awt.event.ActionListener)) {
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    84
    fail("obj is not an ActionListener");
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    85
}
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    86
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    87
// Single class
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    88
var obj = new JavaAdapter(java.lang.Object) {
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    89
    toString: function() { return "I am an Object"; }
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    90
};
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    91
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    92
if (! (obj instanceof java.lang.Object)) {
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    93
    fail("obj is not an instance of java.lang.Object");
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    94
}
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    95
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    96
if (obj.toString() != "I am an Object") {
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    97
    fail("Object.toString did not get called");
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    98
}
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
    99
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
   100
// Single class and single interface
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
   101
var runReached = false;
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
   102
var obj = new JavaAdapter(java.lang.Object, java.lang.Runnable) {
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
   103
    run: function() {
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
   104
        runReached = true;
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
   105
    },
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
   106
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
   107
    hashCode: function() {
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
   108
        return 12;
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
   109
    }
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
   110
};
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
   111
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
   112
obj.run();
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
   113
if (! runReached) {
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
   114
    fail("run was not called");
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
   115
}
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
   116
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
   117
if (obj.hashCode() != 12) {
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
   118
    fail("hashCode does not return 12");
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
diff changeset
   119
}