Saturday, April 10, 2021

Gradle test project

HOWTO

GitHub repo

Initialize project 

[dave@dave git]$ cd gradle-demo/
[dave@dave gradle-demo]$ gradle init

Select type of project to generate:
  1: basic
  2: application
  3: library
  4: Gradle plugin
Enter selection (default: basic) [1..4] 2

Select implementation language:
  1: C++
  2: Groovy
  3: Java
  4: Kotlin
  5: Scala
  6: Swift
Enter selection (default: Java) [1..6] 3

Split functionality across multiple subprojects?:
  1: no - only one application project
  2: yes - application and library projects
Enter selection (default: no - only one application project) [1..2] 2

Select build script DSL:
  1: Groovy
  2: Kotlin
Enter selection (default: Groovy) [1..2] 1

Project name (default: gradle-demo): 
Source package (default: gradle.demo): 

> Task :init
Get more help with your project: https://docs.gradle.org/7.0/samples/sample_building_java_applications_multi_project.html

BUILD SUCCESSFUL in 41s
2 actionable tasks: 2 executed

Project structure
[dave@dave gradle-demo]$ find
.
./gradle
./gradle/wrapper
./gradle/wrapper/gradle-wrapper.jar
./gradle/wrapper/gradle-wrapper.properties
./settings.gradle
./.gradle
./.gradle/configuration-cache
./.gradle/configuration-cache/gc.properties
./.gradle/7.0
./.gradle/7.0/fileChanges
./.gradle/7.0/fileChanges/last-build.bin
./.gradle/7.0/gc.properties
./.gradle/7.0/dependencies-accessors
./.gradle/7.0/dependencies-accessors/gc.properties
./.gradle/7.0/dependencies-accessors/dependencies-accessors.lock
./.gradle/7.0/vcsMetadata-1
./.gradle/7.0/executionHistory
./.gradle/7.0/executionHistory/executionHistory.lock
./.gradle/7.0/executionHistory/executionHistory.bin
./.gradle/7.0/fileHashes
./.gradle/7.0/fileHashes/fileHashes.lock
./.gradle/7.0/fileHashes/fileHashes.bin
./.gradle/buildOutputCleanup
./.gradle/buildOutputCleanup/buildOutputCleanup.lock
./.gradle/buildOutputCleanup/outputFiles.bin
./.gradle/buildOutputCleanup/cache.properties
./.gradle/checksums
./.gradle/checksums/checksums.lock
./.gradle/vcs-1
./.gradle/vcs-1/gc.properties
./gradlew.bat
./buildSrc
./buildSrc/build.gradle
./buildSrc/src
./buildSrc/src/main
./buildSrc/src/main/groovy
./buildSrc/src/main/groovy/gradle.demo.java-common-conventions.gradle
./buildSrc/src/main/groovy/gradle.demo.java-library-conventions.gradle
./buildSrc/src/main/groovy/gradle.demo.java-application-conventions.gradle

./app
./app/build.gradle
./app/src
./app/src/main
./app/src/main/java
./app/src/main/java/gradle
./app/src/main/java/gradle/demo
./app/src/main/java/gradle/demo/app
./app/src/main/java/gradle/demo/app/MessageUtils.java
./app/src/main/java/gradle/demo/app/App.java
./app/src/main/resources
./app/src/test
./app/src/test/java
./app/src/test/java/gradle
./app/src/test/java/gradle/demo
./app/src/test/java/gradle/demo/app
./app/src/test/java/gradle/demo/app/MessageUtilsTest.java
./app/src/test/resources
./.gitattributes
./utilities
./utilities/build.gradle
./utilities/src
./utilities/src/main
./utilities/src/main/java
./utilities/src/main/java/gradle
./utilities/src/main/java/gradle/demo
./utilities/src/main/java/gradle/demo/utilities
./utilities/src/main/java/gradle/demo/utilities/SplitUtils.java
./utilities/src/main/java/gradle/demo/utilities/StringUtils.java
./utilities/src/main/java/gradle/demo/utilities/JoinUtils.java
./utilities/src/main/resources
./utilities/src/test
./utilities/src/test/resources
./gradlew
./README.md
./list
./list/build.gradle
./list/src
./list/src/main
./list/src/main/java
./list/src/main/java/gradle
./list/src/main/java/gradle/demo
./list/src/main/java/gradle/demo/list
./list/src/main/java/gradle/demo/list/LinkedList.java
./list/src/main/resources
./list/src/test
./list/src/test/java
./list/src/test/java/gradle
./list/src/test/java/gradle/demo
./list/src/test/java/gradle/demo/list
./list/src/test/java/gradle/demo/list/LinkedListTest.java
./list/src/test/resources
./.gitignore

Main
[dave@dave gradle-demo]$ more settings.gradle 
/*
 * This file was generated by the Gradle 'init' task.
 *
 * The settings file is used to specify which projects to include in your build.
 *
 * Detailed information about configuring a multi-project build in Gradle can be found
 * in the user manual at https://docs.gradle.org/7.0/userguide/multi_project_builds.html
 */

rootProject.name = 'gradle-demo'
include('app', 'list', 'utilities')

Run tests
[dave@dave gradle-demo]$ ./gradlew check
Downloading https://services.gradle.org/distributions/gradle-7.0-bin.zip
..........10%...........20%...........30%..........40%...........50%...........60%...........70%..........80%...........90%...........100%

BUILD SUCCESSFUL in 32s
9 actionable tasks: 9 executed

Run application
[dave@dave gradle-demo]$ ./gradlew run

> Task :app:run
Hello World!

BUILD SUCCESSFUL in 989ms
6 actionable tasks: 1 executed, 5 up-to-date

Build
[dave@dave gradle-demo]$ ./gradlew build

BUILD SUCCESSFUL in 970ms
13 actionable tasks: 4 executed, 9 up-to-date

Publish
[dave@dave gradle-demo]$ ./gradlew build --scan

BUILD SUCCESSFUL in 4s
13 actionable tasks: 13 up-to-date

Publishing a build scan to scans.gradle.com requires accepting the Gradle Terms of Service defined at https://gradle.com/terms-of-service. Do you accept these terms? [yes, no] yes

Gradle Terms of Service accepted.

Publishing build scan...
https://gradle.com/s/v7pfqpbcuendg

Monday, April 5, 2021

LDAP search

HOWTO

 

LDAP test server

LDAP Server Information (read-only access):

Server: ldap.forumsys.com  
Port: 389

Bind DN: cn=read-only-admin,dc=example,dc=com
Bind Password: password

All user passwords are password.

 

 Search using test  server

[dave@dave git]$ ldapsearch -W -h ldap.forumsys.com -D "cn=read-only-admin,dc=example,dc=com" -b "dc=example,dc=com"  "uid=Newton" 
Enter LDAP Password: 
# extended LDIF
#
# LDAPv3
# base <dc=example,dc=com> with scope subtree
# filter: uid=Newton
# requesting: ALL
#

# newton, example.com
dn: uid=newton,dc=example,dc=com
sn: Newton
objectClass: inetOrgPerson
objectClass: organizationalPerson
objectClass: person
objectClass: top
uid: newton
mail: newton@ldap.forumsys.com
cn: Isaac Newton

# search result
search: 2
result: 0 Success

# numResponses: 2
# numEntries: 1

Select attributes
[dave@dave git]$ ldapsearch -W -h ldap.forumsys.com -D "cn=read-only-admin,dc=example,dc=com" -b "dc=example,dc=com"  "uid=Newton" mail
Enter LDAP Password: 
# extended LDIF
#
# LDAPv3
# base <dc=example,dc=com> with scope subtree
# filter: uid=Newton
# requesting: mail 
#

# newton, example.com
dn: uid=newton,dc=example,dc=com
mail: newton@ldap.forumsys.com

# search result
search: 2
result: 0 Success

# numResponses: 2
# numEntries: 1

Search using Java https://github.com/dveselka/java-tutorial/blob/master/src/main/java/ldap/TestLDAP.java
package ldap;

import java.util.ArrayList;
import java.util.Properties;


import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.Attribute;
import javax.naming.Context;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

public class TestLDAP {
    private static final String CONTEXT_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";

    private InitialDirContext ctx;
    private String basedn;
    private SearchControls ctrls;

    private TestLDAP(String url, String user, String password, String b) throws NamingException {
        ctx = TestLDAP.doConnect(url, user, password);
        basedn = b.length() > 0 ? "," + b : b;
        ctrls = new SearchControls();
    }

    public ArrayList<Attributes> search(String base, String filter) throws NamingException {
        NamingEnumeration<SearchResult> ne = ctx.search(base + basedn, filter, ctrls);
        ArrayList<Attributes> arr = new ArrayList<>();
        while (ne.hasMore()) {
            arr.add(ne.next().getAttributes());
        }
        return arr;
    }

    public Attributes get(String base) throws NamingException {
        return ctx.getAttributes(base + basedn);
    }

    private static InitialDirContext doConnect(String url, String user, String password) throws NamingException {
        Properties props = new Properties();
        props.put(Context.INITIAL_CONTEXT_FACTORY, CONTEXT_FACTORY);
        props.put(Context.PROVIDER_URL, url);
        props.put(Context.SECURITY_PRINCIPAL, user);
        props.put(Context.SECURITY_CREDENTIALS, password);

        return new InitialDirContext(props);
    }

    public static TestLDAP connect(String url, String user, String pass, String basedn) throws NamingException {
        return new TestLDAP(url, user, pass, basedn);
    }

    public static boolean testConnection(String url, String user, String password) {
        try {
            return TestLDAP.doConnect(url, user, password) != null;
        } catch (Exception e) {
            return false;
        }
    }

    public static void main(String[] args) {
        try {
            TestLDAP testLDAP = new TestLDAP("ldap://ldap.forumsys.com", "cn=read-only-admin,dc=example,dc=com",
                    "password", "dc=example,dc=com");
            Attributes attributes = testLDAP.get("uid=Newton");

            NamingEnumeration<? extends Attribute> allAttributes = attributes.getAll();
            while (allAttributes.hasMore()) {
                Attribute attribute = allAttributes.next();
                System.out.println(attribute.getID() + "=" + attribute.get().toString());
            }

        } catch (NamingException e) {
            e.printStackTrace();
        }
    }
}
Run code in Eclipse