Skip to content

Commit

Permalink
Unit test to verify JSEmbeddingProvider behavior. Requires JavaScript…
Browse files Browse the repository at this point in the history
… support modules to pass.
  • Loading branch information
Jaroslav Tulach committed Feb 23, 2018
1 parent feeab51 commit bca3514
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 4 deletions.
2 changes: 1 addition & 1 deletion editor.htmlui/nbproject/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
is.eager=true
javac.source=1.7
javac.compilerargs=-Xlint -Xlint:-serial

requires.nb.javac=true
7 changes: 6 additions & 1 deletion editor.htmlui/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,19 @@
<compile-dependency/>
</test-dependency>
<test-dependency>
<code-name-base>org.netbeans.libs.testng</code-name-base>
<code-name-base>org.netbeans.libs.junit4</code-name-base>
<compile-dependency/>
</test-dependency>
<test-dependency>
<code-name-base>org.netbeans.modules.java.hints.test</code-name-base>
<recursive/>
<compile-dependency/>
</test-dependency>
<test-dependency>
<code-name-base>org.netbeans.modules.nbjunit</code-name-base>
<recursive/>
<compile-dependency/>
</test-dependency>
<test-dependency>
<code-name-base>org.netbeans.modules.parsing.nb</code-name-base>
<recursive/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.editor.htmlui;

import java.io.OutputStream;
import javax.swing.text.StyledDocument;
import junit.framework.Test;
import org.netbeans.api.java.source.CompilationController;
import org.netbeans.api.java.source.JavaSource;
import org.netbeans.api.java.source.Task;
import org.netbeans.api.lexer.Token;
import org.netbeans.api.lexer.TokenHierarchy;
import org.netbeans.api.lexer.TokenSequence;
import org.netbeans.junit.NbModuleSuite;
import org.netbeans.junit.NbTestCase;
import org.openide.cookies.EditorCookie;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileSystem;
import org.openide.filesystems.FileUtil;

public class ColoringJsbTest extends NbTestCase {

public ColoringJsbTest(String name) {
super(name);
}

public static Test suite() {
return NbModuleSuite.createConfiguration(ColoringJsbTest.class).gui(false).suite();
}

public void testRun() throws Exception {
FileSystem fs = FileUtil.createMemoryFileSystem();
FileObject jsb = fs.getRoot().createData("JSB.java");
String code = ""
+ "import net.java.html.js.JavaScriptBody;\n"
+ "class JSB {\n"
+ " @JavaScriptBody(args = {}, body= \"if (true) return 42;\")\n"
+ " public static native void method();\n"
+ "}\n";
try (OutputStream os = jsb.getOutputStream()) {
os.write(code.getBytes());
}

EditorCookie ec = jsb.getLookup().lookup(EditorCookie.class);
StyledDocument doc = ec.openDocument();
assertNotNull("Document found", doc);
final JavaSource source = JavaSource.forDocument(doc);
assertNotNull("Source found", source);

source.runUserActionTask(new Task<CompilationController>() {
@Override
public void run(CompilationController info) throws Exception {
info.toPhase(JavaSource.Phase.RESOLVED);
JSEmbeddingProvider.colorizeJSB(info);
}
}, false);

TokenHierarchy<StyledDocument> h = TokenHierarchy.get(doc);
TokenSequence<?> seq = h.tokenSequence();
seq.moveStart();
for (;;) {
final Token<?> t = seq.token();
if (t != null && t.text() != null && t.text().toString().contains("if (true) return 42")) {
fail("Found string token, and we shouldn't: " + t.text() + "\n" + seq);
}
if (!seq.moveNext()) {
break;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
package org.netbeans.modules.editor.htmlui;

import net.java.html.js.JavaScriptBody;
import org.junit.Test;
import org.netbeans.modules.java.hints.test.api.HintTest;
import org.openide.filesystems.FileUtil;
import org.testng.annotations.Test;

public class JSNI2JavaScriptBodyTest {
@Test
Expand Down
2 changes: 1 addition & 1 deletion java.source.nbjavac/manifest.mf
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ AutoUpdate-Show-In-Client: false
OpenIDE-Module: org.netbeans.modules.java.source.nbjavac
OpenIDE-Module-Implementation-Version: 1
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/java/source/nbjavac/Bundle.properties
OpenIDE-Module-Requires: org.netbeans.modules.nbjavac
OpenIDE-Module-Package-Dependencies: com.sun.tools.javac.util[CouplingAbort]

0 comments on commit bca3514

Please sign in to comment.