Donnerstag, 24. September 2020

Test TinyMCE Editor with Protractor

If you want to test TinyMCE editors with Protractor you have to work around the problem with iframes first. To do this you have to switch to the TinyMCE iframe using switch, send keys and then switch back to the parent. The following method shows an example of what is necessary:

static async sendKeysToTinyMce(tinyMCEiFrameLocator: Locator, value: string, clearField: boolean = true) {

  browser.waitForAngularEnabled(false);
browser.switchTo().frame(element(tinyMCEiFrameLocator).getWebElement());
const body = element(by.id('tinymce'));
if (clearField) {
await body.clear();
}
await body.click();
await body.sendKeys(value);
browser.switchTo().defaultContent();
browser.waitForAngularEnabled(true);
}

Dienstag, 16. Februar 2016

Jenkins: Select Maven Project version depending on the previously selected artifact



If you want to build a property selector in Jenkins, which allows you to select an artifact and a depending artifact version, you can’t do that with default Jenkins property selectors. The following tutorial makes use of the “Active Choice” plugin, which allows a dynamic generation of selection lists and access to the other parameter fields in Jenkins. 




First we build an artifact selector to select the artifact. We do this with an “Active Choice Parameter” and a static list. Later we can replace the static list dynamically with requested artifacts from maven repository. Use the following groovy script to build the static list, and set property Name to “artifactId”:


Now we can add an “Active Choice Reactive Parameter” selector. Referenced Parameter must beset to “artifactId” to get access to the first defined property field. The following groovy script reads all available versions of artifact from maven repo and creates a selectable list.




You can copy the groovy script from here:

import java.util.regex.Matcher
import java.util.regex.Pattern
import javax.xml.xpath.*

// set target url to artifact versions in you maven repo
URL targetUrl = new URL("https://mvn.havre.de/repo/de/havre/" + artifactId + "/");

// resolve HTML version list
HttpURLConnection con = (HttpURLConnection) targetUrl.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Authorization", "Basic ***********************");
int responseCode = con.getResponseCode();

// pars HTML to a simple list
if (responseCode == HttpURLConnection.HTTP_OK) {
               BufferedReader inStream = new BufferedReader(new InputStreamReader(con.getInputStream()));
               String inputLine;
               StringBuffer response = new StringBuffer();
               while ((inputLine = inStream.readLine()) != null) {
                              response.append(inputLine);
               }
               inStream.close();

               Pattern TAG_REGEX = Pattern.compile("<text>([0-9]*\\.[0-9]*\\.[0-9]*)</text>");
               List tagValues = new ArrayList();
               final Matcher matcher = TAG_REGEX.matcher(response.toString());
               while (matcher.find()) {
                              tagValues.add(matcher.group(1));
               }
        Collections.reverse(tagValues);
        return tagValues;
} else {
               return['GET ' + targetUrl + ' not worked: ' + responseCode ];
}


 That’s it. Now you can use the "artifactId" and "artifactVersion" property for your build script. E.g. to deploy the concert artifact to an application server.

Mittwoch, 5. August 2015

Debug Tomcat Listener Startup



On startup Tomcat usually does not log detailed information about a failed listener start. Put the following file into the root of class path to enable better logging in catalina.out:


logging.properties

org.apache.catalina.core.ContainerBase.[Catalina].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].handlers = java.util.logging.ConsoleHandler

Montag, 19. Januar 2015

Proguard android obfuscation

To prevent your android app from decompilation and analyzation it is highly recommended to obfuscate the APK file before deployment to play store. Proguard is the proposed tool for this job and already contained in the SDK.

To use Proguard with an Android application a special configuration is required. The JDC already contains an example for a simple Android app. But in combination with Roboguice dependency injection or Gson JSON serialization an extended configuration is required.

Roboguice

If you choose Roboguice for dependency injection in you android app, I recommend to use only the obfuscation functions of Proguard. Also make sure the Roboguice classes are not obfuscated and attributes are not changed. Try the following example configuration:

-ignorewarnings
-dontshrink
-keep class roboguice.** { *; }

-keepattributes *Annotation*
-keep public class roboguice.** { *; }

-keep class **.Finalizer
-keepclassmembers class ** { *** startFinalizer( ... ); }

-keepclassmembers class * {
  void *(**On*Event);
}

-keepattributes **

GSON JSON serialization

The serialization of java objects makes often problems in combination with obfuscation. The produce a consistent object serialization it is required to prevent attribute names of objects and enumerations from obfuscation.


One possibility is to use @SerializedName annotation do set a static name for an attribute, which is not affected from obfuscation.

class TestObject {
  
  @SerializedName("staticName") 
  private String testField;
}

An other solution is the usage of a marker interface e.g. GsonSerializable:

public interface GsonSerializable {}


Use this interface to mark classes and enumerations which should be serialized by Gson. The following pro guard configuration ensures that the deserialize objects are always serialized in the same way:

# keep GsonSerializable interface, it would be thrown away by proguard since it is empty
-keep class de.havre.copymeter.model.GsonSerializable

# member fields of serialized classes, including enums that implement this interface
-keepclassmembers class * implements de.havre.copymeter.model.GsonSerializable {
    <fields>;
}

# also keep names of these classes. not required, but just in case.
-keepnames class * implements de.havre.copymeter.model.GsonSerializable

Links

https://sites.google.com/site/gson/
https://github.com/roboguice/roboguice/
http://proguard.sourceforge.net/

Mittwoch, 3. Dezember 2014

Bean Cloner




Sometimes you need a deep copy of bean structure but no clone constructors are available. Cloner offers a fast reflection based methods to clone object structures. It is also possible to integrate Cloner into Spring.


Code Snippet:

class CloneTest {
  private static final Cloner CLONER = new Cloner(); 
  
  public static void main(String[] args) {
    Object original = new Object();
    Object clone = CLONER.deepClone(tds);
  }
}


Details:
https://code.google.com/p/cloning/wiki/Usage

Maven:
 <dependency>
    <groupId>uk.com.robust-it</groupId>
    <artifactId>cloning</artifactId>
    <version>1.7.4</version>
</dependency>

Dienstag, 17. Juni 2014

Generating JAXB Beans from XSD with Maven

The following example shows a simple maven plugin configuration to generate Java beans from a XSD. I use the “maven-jaxb22-plugin” because it allows a simple integration of plugins to adjust the Bean generation. Leave the following arguments if you don’t need the feature:
  • Xsetters - Generates setters to beans
  • XtoString - Generates common to string method to beans  
  • Xequals - Generates a equals method to beans  
  • XhashCode - Generates a hashcode method to beans 
  • Xfluent-api - Generates a fluent API to beans

1:  <plugin>  
2:   <groupId>org.jvnet.jaxb2.maven2</groupId>  
3:   <artifactId>maven-jaxb22-plugin</artifactId>  
4:   <executions>  
5:    <execution>  
6:     <id>Generating Asset Admin Api Beans</id>  
7:     <goals>  
8:      <goal>generate</goal>  
9:     </goals>  
10:     <configuration>  
11:      <encoding>UTF-8</encoding>  
12:      <forceRegenerate>true</forceRegenerate>  
13:      <schemaDirectory>${basedir}/src/main/resources/xsd/</schemaDirectory>  
14:      <generatePackage>de.lehavre.demo</generatePackage>  
15:      <schemaIncludes>  
16:       <include>model1.xsd</include>  
17:       <include>model2.xsd</include>  
18:      </schemaIncludes>  
19:      <extension>true</extension>  
20:      <args>  
21:       <arg>-Xsetters</arg>  
22:       <arg>-XtoString</arg>  
23:       <arg>-Xequals</arg>  
24:       <arg>-XhashCode</arg>  
25:       <arg>-Xfluent-api</arg>  
26:      </args>  
27:      <plugins>  
28:       <plugin>  
29:        <groupId>org.jvnet.jaxb2_commons</groupId>  
30:        <artifactId>jaxb2-basics</artifactId>  
31:       </plugin>  
32:       <plugin>  
33:        <groupId>net.java.dev.jaxb2-commons</groupId>  
34:        <artifactId>jaxb-fluent-api</artifactId>  
35:        <version>2.1.8</version>  
36:       </plugin>  
37:      </plugins>  
38:     </configuration>  
39:    </execution>  
40:   </executions>  
41:  </plugin>  

Generating Fluent Builder with IntelliJ



The JavaBean getter and setter usage is often a bit laborious. I like to use a fluent API, but I don't like to implement these methods direly in the bean class. There is a nice IntelliJ Plugin named "Builder-Genarer", which generates a Builder from POJO.


Just open class and press ALT-SHIFT-B

Plugin: http://plugins.jetbrains.com/plugin/6585