Sunday, 4 June 2023

PlantUML: Add methods, fields to a class in class diagram

Using the symbol : followed by the field’s or method’s name we can define methods, fields.

 

defineFieldsMethods.txt

@startuml

class Object
Object : Object clone()
Object : boolean equals(Object obj)

interface List
List : add(E e)
List : add(int index, E element)
List : clear()
List : Object[] toArray()

class ArrayList
ArrayList : {static} Long MAX_LIST_SIZE = 1234567

List <|-- ArrayList
Object <|-- ArrayList

@enduml

 

Above snippet generate below diagram.

 

 


There is another better way for the programmers to define fields, methods.

 

defineFieldsMethods2.txt

@startuml

class Object {
	Object clone()
	boolean equals(Object obj)
}

interface List{
	void add(E e)
	void add(int index, E element)
	void clear()
	Object[] toArray()
}

class ArrayList{
	MAX_LIST_SIZE : {static} Long = 1234567
}

List <|-- ArrayList
Object <|-- ArrayList

@enduml

Above snippet generate below diagram.





 

Previous                                                    Next                                                    Home

PlantUML: Arrow to specify more meaning to the relationship label

Using the arrow (< or >), we can clearly specify which object act on other object.

 

Example

Person --> Address : lives at >
Car --> Person : < owns

labelArrow.txt

@startuml

title "Class Composition"

class Person
class Address
class Car

Person --> Address : lives at >
Car --> Person : < owns

@enduml

Above snippet generate below diagram.




 

 

Previous                                                    Next                                                    Home

Playwright: Get the locator by placeholder text

page.getByPlaceholder() method return the locate the input elements by the placeholder text.

 

Signature

Locator getByPlaceholder(String text)
Locator getByPlaceholder(String text, GetByPlaceholderOptions options)
Locator getByPlaceholder(Pattern text)
Locator getByPlaceholder(Pattern text, GetByPlaceholderOptions options)

 

Example

ElementHandle elementHandle = page.getByPlaceholder("Enter your email id").elementHandle();

 

Find the below working application.

 

placeHolderDemo.html

 

<!DOCTYPE html>
<html>

<head> </head>

<body>

	<form onsubmit="return false;">

		<table>
			<tr>
				<td> <label for="email"><b>Email</b></label> </td>
				<td><input type="text" placeholder="Enter your email id" name="email" id="email" required> </td>
			</tr>

			<tr>
				<td> <label for="password"><b>Password</b></label></td>
				<td><input type="password" placeholder="Enter Password" name="password" id="password" required> </td>
			</tr>

			<tr>
				<td> <label for="confirm-password"><b>Repeat Password</b></label> </td>
				<td><input type="password" placeholder="Repeat Password" name=confirm-password" id="confirm-password"
						required></td>
			</tr>

			<tr>
				<td> <input type="submit" value="submit" onclick="display()"></td>
			</tr>


		</table>

	</form>

	<p id="result"></p>

	<script>
		function display() {
			let result = document.getElementById("result");
			let userEmail = document.querySelector('input[id="email"]').value;
			result.innerHTML = `<b>Your email :  ${userEmail} is registered</b>`
		}
	</script>

</body>

</html>

 

FileUtil.java

package com.sample.app.util;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class FileUtil {

	public static String resourceAsString(String resourceName) throws IOException {
		ClassLoader classLoader = FileUtil.class.getClassLoader();
		URL url = classLoader.getResource(resourceName);
		if (url == null) {
			return null;
		}

		URLConnection urlConnection = url.openConnection();

		urlConnection.setUseCaches(false);

		try (InputStreamReader inputStreamReader = new InputStreamReader(urlConnection.getInputStream())) {
			char[] buffer = new char[1048];
			StringBuilder builder = new StringBuilder();

			int count = -1;
			while ((count = inputStreamReader.read(buffer, 0, buffer.length)) != -1) {
				builder.append(buffer, 0, count);
			}

			return builder.toString();
		}

	}
}

 

GetLocatorByPlaceHolder.java

package com.sample.app.locators;

import java.io.File;
import java.io.IOException;

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.ElementHandle;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import com.sample.app.util.FileUtil;

public class GetLocatorByPlaceHolder {
	public static void main(String[] args) throws IOException {
		try (Playwright playwright = Playwright.create()) {

			Browser browser = playwright.chromium()
					.launch(new BrowserType.LaunchOptions().setHeadless(false).setSlowMo(100));

			final String content = FileUtil.resourceAsString("locators" + File.separator + "placeHolderDemo.html");

			Page page = browser.newPage();
			page.setContent(content);

			ElementHandle elementHandle = page.getByPlaceholder("Enter your email id").elementHandle();
			String name = elementHandle.getAttribute("name");
			String id = elementHandle.getAttribute("id");
			String placeholder = elementHandle.getAttribute("placeholder");

			System.out.println("name : " + name);
			System.out.println("id : " + id);
			System.out.println("placeholder : " + placeholder);
		}
	}
}

 

Output

name : email
id : email
placeholder : Enter your email id

 

 

Previous                                                 Next                                                 Home

Micronaut: Customize the running port of the server

Micronaut application runs on the port 8080 by default. You can customize this to a specific port with below configuration.

micronaut:
  server:
    port: 8888

 

Find the below working application.

 

Step 1: Create new maven project ‘micronaut-rest-change-port’.

 

Step 2: Update pom.xml with maven dependencies.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sample.app</groupId>
    <artifactId>micronaut-rest-change-port</artifactId>
    <version>0.1</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>io.micronaut</groupId>
        <artifactId>micronaut-parent</artifactId>
        <version>3.7.3</version>
    </parent>

    <properties>
        <packaging>jar</packaging>
        <jdk.version>11</jdk.version>
        <release.version>11</release.version>
        <micronaut.version>3.7.3</micronaut.version>
        <micronaut.runtime>netty</micronaut.runtime>
        <exec.mainClass>com.sample.app.App</exec.mainClass>
    </properties>

    <repositories>
        <repository>
            <id>central</id>
            <url>https://repo.maven.apache.org/maven2</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>io.micronaut</groupId>
            <artifactId>micronaut-inject</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.micronaut</groupId>
            <artifactId>micronaut-validation</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.micronaut</groupId>
            <artifactId>micronaut-http-client</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.micronaut</groupId>
            <artifactId>micronaut-http-server-netty</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.micronaut</groupId>
            <artifactId>micronaut-jackson-databind</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>jakarta.annotation</groupId>
            <artifactId>jakarta.annotation-api</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>io.micronaut.test</groupId>
            <artifactId>micronaut-test-junit5</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>io.micronaut.build</groupId>
                <artifactId>micronaut-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <!-- Uncomment to enable incremental compilation -->
                    <!-- <useIncrementalCompilation>false</useIncrementalCompilation> -->

                    <annotationProcessorPaths
                        combine.children="append">
                        <path>
                            <groupId>io.micronaut</groupId>
                            <artifactId>micronaut-http-validation</artifactId>
                            <version>${micronaut.version}</version>
                        </path>
                    </annotationProcessorPaths>

                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

 

Step 3: Create application.yml file under src/main/resources folder.

 

application.yml
micronaut:
  server:
    port: 8888

 

Step 4: Define WelcomeController class.

 

WelcomeController.java

package com.sample.app.controller;

import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;

@Controller("/welcome")
public class WelcomeController {

    @Get(produces = MediaType.TEXT_PLAIN)
    public String index() {
        return "Welcome to Micronaut app!!!!";
    }
}

 

Step 5: Define main application class.

 

App.java

package com.sample.app;

import io.micronaut.runtime.Micronaut;

public class App {

    public static void main(String[] args) {
        Micronaut.run(App.class);
    }
}

 

Total project structure looks like below.

 



 

Build the project using mvn package command.

Navigate to the folder where pom.xml is located and execute the command ‘mvn package’.

 

Upon command successful execution, you can see the jar file ‘micronaut-rest-change-port-0.1.jar’ in project target folder.

 

$ls ./target/
classes
generated-sources
generated-test-sources
maven-archiver
maven-status
micronaut-rest-change-port-0.1.jar
original-micronaut-rest-change-port-0.1.jar
test-classes

Execute below command to run the application.

java -jar ./target/micronaut-rest-change-port-0.1.jar

Open the url ‘http://localhost:8888/welcome’ in browser, you will see below kind of screen.

 


 

You can download this application from this link.



Previous                                                    Next                                                    Home