How do I use j2html without rendering everything - java

I am converting my html rendering code to use j2html. Whilst I like the library it is not easy for me to convert all the code in one go so sometimes I may convert the outer html to use j2html but not able to convert the inner html to j2html at the same time. So I would like j2html to be able to accept text passed to it as already rendered, but it always re-renders it so
System.out.println(p("<b>the bridge</b>"));
returns
<p><b>the bridge</b></p>
is there a way I get it to output
<p><b>the bridge</b></p>
Full Test Case
import j2html.tags.Text;
import static j2html.TagCreator.b;
import static j2html.TagCreator.p;
public class HtmlTest
{
public static void main(String[] args)
{
System.out.println(p(b("the bridge")));
System.out.println(p("<b>the bridge</b>"));
}
}

import static j2html.TagCreator.b;
import static j2html.TagCreator.p;
import static j2html.TagCreator.rawHtml;
public class HtmlTest
{
public static void main(String[] args)
{
System.out.println(p(b("the bridge")));
System.out.println(p(rawHtml("<b>the bridge</b>")));
}
}
Result:
<p><b>the bridge</b></p>
<p><b>the bridge</b></p>

In j2html 1.1.0 you can disable text-escaping by writing
Config.textEscaper = text -> text;
Be careful though..

Related

Java code to find operating system

I am very new at coding in Java and I want to write a code to find out my operating system through a dialogue box (Windows). I wrote this code but I am not sure how to completely implement it.
Please let me know what I am doing wrong in simple terms as I am a beginner.
import java.awt.*;
import javax.swing.*;
public class JavaTut {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null,System.getProperty("os.name").list(System.out));
Toolkit.getDefaultToolkit().beep();
}
}
Instead of System.getProperty("os.name").list(System.out), use System.getProperty("os.name").
Example main method:
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, System.getProperty("os.name"));
}

jsoup Not Catching Full Webpage

Trying to make a super simple bit of code using jsoup to see if a webpage contains a specific word (checks to see the availability of a class to take in-residence). jsoup seems to be catching lots of the webpage specified, but won't capture all of it - specifically the area I'm interested in. Is there a reason for this? Am I doing something wrong?
import org.jsoup.Jsoup;
import java.io.IOException;
import org.jsoup.nodes.Document;
public class main {
public static void main(String[] args) throws IOException{
String html = Jsoup.connect("https://www.afit.edu/CE/Course_Desc.cfm?p=WENG%20481").maxBodySize(0).get().html();
System.out.print(html);
if (html.contains("Resident")) {
System.out.print("\nAVAILABLE!");
}
}
}

html2image configuration in java

I would to ask about html2image, i have some codes like this
import gui.ava.html.image.generator.HtmlImageGenerator;
import java.io.File;
public class html2image {
public static void main(String[] args) {
HtmlImageGenerator imageGenerator = new HtmlImageGenerator();
imageGenerator.loadHtml("<b>Hello World!</b> Please goto <a title=\"Goto Google\" href=\"http://www.google.com\">Google</a>.");
imageGenerator.saveAsImage("hello-world.png");
imageGenerator.saveAsHtmlWithMap("hello-world.html", "hello-world.png");
}
}
But seems like after i call this java, its not appear any image or output image, maybe i miss some configuration, anyone can help ?
Try to give the absolute path in imageGenerator.saveAsImage as otherwise the file will be generated in the working directory.

FEST: How to use the NoExitSecurityManager properly?

I starting to use FEST to help me to perform unit test on my Java Swing GUI.
For now, I managed to get through the documentation (mostly deprecated) and help me by looking at the Javadoc and the code.
Right now I am stuck on a problem while using the NoExitSecurityManager. The documentation is quite out dated but we can understand the big lines of it.
I simply try to test if my "Quit" MenuItem is working well in my GUI. So, I need to block the System.exit(0) and map the exit status of the program to a JUnit test.
Here is a simplified code I use to perform the test (the tested class is GraphicalUserInterface).
import org.junit.Test;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.fest.swing.annotation.RunsInEDT;
import org.fest.swing.edt.GuiQuery;
import org.fest.swing.edt.GuiActionRunner;
import org.fest.swing.fixture.FrameFixture;
import org.fest.swing.junit.testcase.FestSwingJUnitTestCase;
import org.fest.swing.security.NoExitSecurityManagerInstaller;
public class GraphicalUserInterfaceTest extends FestSwingJUnitTestCase {
private static FrameFixture gui;
private static NoExitSecurityManagerInstaller noExitSecurityManagerInstaller;
#BeforeClass
public static void setUpBeforeClass() throws Exception {
NoExitSecurityManagerInstaller.installNoExitSecurityManager(new ExpectExitSuccess());
}
#AfterClass
public static void tearDownAfterClass() throws Exception {
noExitSecurityManagerInstaller.uninstall();
}
#Override
protected void onSetUp() {
gui = new FrameFixture(robot(), createNewGUI());
gui.show();
}
#RunsInEDT
private GraphicalUserInterface createNewGUI() {
return GuiActionRunner.execute(new GuiQuery<GraphicalUserInterface>() {
protected GraphicalUserInterface executeInEDT() {
return new GraphicalUserInterface();
}
});
}
#Test
public final void testFileMenuQuitMenuItem() {
gui.menuItemWithPath("File", "Quit").click();
}
}
The ExitCallHook are coded like this (you can guess the other one easily).
import static org.junit.Assert.assertTrue;
import org.fest.swing.security.ExitCallHook;
public final class ExpectExitSuccess implements ExitCallHook {
#Override
public void exitCalled(int status) {
assertTrue(status == 0);
}
}
All the tests are performed well and everything seems to be ok except that I get a java.lang.NullPointerException at the end.
So, I wonder what did I do wrong (or what can I improve to not get this nullpointer exception at the end of the test).
I found the solution in the code. In fact, the proper way to do it is the following:
#Test
public final void testFileMenuQuitMenuItem() {
NoExitSecurityManagerInstaller noExitSecurityManagerInstaller =
NoExitSecurityManagerInstaller.installNoExitSecurityManager(new ExpectExitSuccess());
gui.menuItemWithPath("File", "Quit").click();
noExitSecurityManagerInstaller.uninstall();
}
This way prevent to pollute each test with a NoExitSecurityManager.

How to inject method its return value?

I am not even sure if this is possible, but I came up with this idea because of this question: Change private static final field using Java reflection
So this is the scenario:
public static String test() {
return "test test test";
}
What I want to do this let the test() method return "worked" instead of "test test test".
Does anybody have an idea how to accomplish this?
EDIT:
I downloaded powermock and tried this:
package test;
import org.powermock.api.easymock.PowerMock;
public class InjectorTest {
public static void main(final String[] args) {
System.out.println("Before the injection:");
System.out.println(test());
PowerMock.mockStatic(InjectorTest.class);
PowerMock.doReturn("worked").when(InjectorTest.class, "test");
System.out.println("After the injection:");
System.out.println(test());
}
public static String test() {
return "did not work :(";
}
}
But eclipse gives this error: The method doReturn(String) is undefined for the type PowerMock
Did I download the wrong one or was that a bad sample code?
Because I don't want more votedowns, WHY I want to do this?
I want to inject Minecraft in that way that it doesn't uses the user.home but a relative URI.
In this way I can make a pre-installed portable Minecraft for an USB stick for school :D
I think PowerMock can do this. (Java reflection under the hood)
With PowerMock, your code would look like this:
public static void main(String[] args) {
PowerMock.mockStatic(Foo.class);
PowerMock.doReturn("worked").when(Foo.class, "test");
System.out.println(Foo.test());
}
Credits to #RC for code above

Categories