${frontPost.title}
${frontPost.content.nl2br()}
The last error in the previous article was due to an error in the assertion assertEquals(1, Post.count()); and the number of Posts obtained was not 1. Before running the Test, there was data in the table
You can add the following method to run the Test Clear data before
@Before
public void setup() {
Fixtures.deleteAll();
}
1. Write complex test cases
Edit/test/data.yml
# User(bob ):
# email: bob@Gmail.com
# passWord: secret
# fullname: Bob
Replace the content with http://play-framework.herokuapp.com/zh/files/data.yml
Add test cases
@Test
public void fullTest() {
Fixtures.loadModels("data.yml");
// Count things
assertEquals(2, User.count());
assertEquals(3, Post .count());
assertEquals(3, Comment.count());
// Try to connect as users
assertNotNull(User.connect("bob@gmail.com", "secret"));
assertNotNull (User.connect("jeff@gmail.com", "secret"));
assertNull(User.connect("jeff@gmail.com", "badpassword"));
assertNull(User.connect("tom@ gmail.com", "secret"));
// Find all of Bob's posts
List
.fetch() ;
assertEquals(2, bobPosts.size());
// Find all comments related to Bob's posts
List
"bob@gmail.com ").fetch();
assertEquals(3, bobComments.size());
// Find the most recent post
Post frontPost = Post.find("order by postedAt desc").first();
assertNotNull (frontPost);
assertEquals("About the model layer", frontPost.title);
// Check that this post has two comments
assertEquals(2, frontPost.comments.size());
// Post a new comment
frontPost.addComment("Jim", "Hello guys");
assertEquals(3, frontPost.comments.size());
assertEquals(4, Comment.count());
}
Regarding how to use data.yml, you can refer to http://play-framework.herokuapp.com/zh/yaml
2. Initialize data
Start creating the first page of the application. This page will display recent posts, as well as a list of older articles.
We need one thing before developing the first screen. Create test data. One way to inject default data into your blog is to load the file at application load time. To do this, we will create a bootstrap job.
Create Bootstrap.java
package models;
import play.*;
import play.jobs.*;
import play.test.*;
@OnapplicationStart
public class Bootstrap extends Job {
public void doJob( ) {
// Check if the database is empty
if (User.count() == 0) {
Fixtures.loadModels("initial-data.yml");
}
}
}
initial-data .yml uses the content of data.yml to create the default data
@OnApplicationStart identification method to run when the application starts
3. Develop the homepage
Modify the index() method of Application.java
public static void index () {
Post frontPost = Post.find("order by postedAt desc").first();
List
.fetch( 10);
render(frontPost, olderPosts);
}
Modify Application/index.html
#{extends 'main.html' /}
#{set title:'Home' /}
#{if frontPost}
4. Open the site
The above is the content of PlayFramework to completely implement an APP (4). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!
JVM works by converting Java code into machine code and managing resources. 1) Class loading: Load the .class file into memory. 2) Runtime data area: manage memory area. 3) Execution engine: interpret or compile execution bytecode. 4) Local method interface: interact with the operating system through JNI.
JVM enables Java to run across platforms. 1) JVM loads, validates and executes bytecode. 2) JVM's work includes class loading, bytecode verification, interpretation execution and memory management. 3) JVM supports advanced features such as dynamic class loading and reflection.
Java applications can run on different operating systems through the following steps: 1) Use File or Paths class to process file paths; 2) Set and obtain environment variables through System.getenv(); 3) Use Maven or Gradle to manage dependencies and test. Java's cross-platform capabilities rely on the JVM's abstraction layer, but still require manual handling of certain operating system-specific features.
Java requires specific configuration and tuning on different platforms. 1) Adjust JVM parameters, such as -Xms and -Xmx to set the heap size. 2) Choose the appropriate garbage collection strategy, such as ParallelGC or G1GC. 3) Configure the Native library to adapt to different platforms. These measures can enable Java applications to perform best in various environments.
OSGi,ApacheCommonsLang,JNA,andJVMoptionsareeffectiveforhandlingplatform-specificchallengesinJava.1)OSGimanagesdependenciesandisolatescomponents.2)ApacheCommonsLangprovidesutilityfunctions.3)JNAallowscallingnativecode.4)JVMoptionstweakapplicationbehav
JVMmanagesgarbagecollectionacrossplatformseffectivelybyusingagenerationalapproachandadaptingtoOSandhardwaredifferences.ItemploysvariouscollectorslikeSerial,Parallel,CMS,andG1,eachsuitedfordifferentscenarios.Performancecanbetunedwithflagslike-XX:NewRa
Java code can run on different operating systems without modification, because Java's "write once, run everywhere" philosophy is implemented by Java virtual machine (JVM). As the intermediary between the compiled Java bytecode and the operating system, the JVM translates the bytecode into specific machine instructions to ensure that the program can run independently on any platform with JVM installed.
The compilation and execution of Java programs achieve platform independence through bytecode and JVM. 1) Write Java source code and compile it into bytecode. 2) Use JVM to execute bytecode on any platform to ensure the code runs across platforms.
AI-powered app for creating realistic nude photos
Online AI tool for removing clothes from photos.
Undress images for free
AI clothes remover
Swap faces in any video effortlessly with our completely free AI face swap tool!
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
Useful JavaScript development tools
God-level code editing software (SublimeText3)
Visual web development tools
The most popular open source editor
${oldPost.comments.size()?:'no'}
comment${oldPost.comments.size().pluralize()}
#{if oldPost.comments}
- latest by ${oldPost.comments[0].author}
#{/if}