Skip to content

Commit

Permalink
Merge pull request #2 from silentsoft/dev
Browse files Browse the repository at this point in the history
Update version to 2.1.0
  • Loading branch information
silentsoft authored Jan 31, 2020
2 parents 9732cf6 + 334ac18 commit db6844d
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 22 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# OSS Notice File Generator

![release](https://img.shields.io/badge/release-2.0.0-blue.svg)
![release](https://img.shields.io/badge/release-2.1.0-blue.svg)
[![Build Status](https://travis-ci.org/silentsoft/oss-notice-file-generator.svg?branch=master)](https://travis-ci.org/silentsoft/oss-notice-file-generator)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=silentsoft_oss-notice-file-generator&metric=alert_status)](https://sonarcloud.io/dashboard?id=silentsoft_oss-notice-file-generator)
[![HitCount](http://hits.dwyl.io/silentsoft/oss-notice-file-generator.svg)](http://hits.dwyl.io/silentsoft/oss-notice-file-generator)
Expand All @@ -19,7 +19,7 @@
<dependency>
<groupId>org.silentsoft.oss</groupId>
<artifactId>notice-file-generator</artifactId>
<version>2.0.0</version>
<version>2.1.0</version>
</dependency>
</dependencies>
```
Expand All @@ -29,8 +29,8 @@
public static void main(String[] args) {
System.out.println("--------START OF THE NOTICE FILE--------");

String markdown = NoticeFileGenerator.newInstance("{{Your Product Name}}", "{{Your Organization Name}}")
.addText("This product includes software developed by {{Your Organization Name}}.")
String markdown = NoticeFileGenerator.newInstance("{{Your Product Name}}", "{{Owner}}")
.addText("This product includes software developed by {{Owner}}.")
.addText("This product includes software developed by The Apache Software Foundation (http://www.apache.org/).")
.addLibrary("first-3rd-party", "1.2.3", "https://github.com/silentsoft/first-3rd-party", new ApacheLicense2())
.addLibrary("second-3rd-party", "1.2.3", "https://github.com/silentsoft/second-3rd-party", new MITLicense())
Expand All @@ -46,9 +46,9 @@ public static void main(String[] args) {

--------START OF THE NOTICE FILE--------
# {{Your Product Name}}
Copyright (c) {{Your Organization Name}}. All rights reserved.
Copyright (c) {{Owner}}. All rights reserved.

This product includes software developed by {{Your Organization Name}}.
This product includes software developed by {{Owner}}.

This product includes software developed by The Apache Software Foundation (http://www.apache.org/).

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.silentsoft.oss</groupId>
<artifactId>notice-file-generator</artifactId>
<version>2.0.0</version>
<version>2.1.0</version>
<properties>
<!-- Generic properties (Warning : DO NOT REMOVE) -->
<java.version>1.8</java.version>
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/silentsoft/oss/License.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,19 @@ public String toString() {
buffer.append("```");
return buffer.toString();
}

@Override
public int hashCode() {
return toString().hashCode();
}

@Override
public boolean equals(Object obj) {
if (getClass().isInstance(obj)) {
return toString().equals(getClass().cast(obj).toString());
}

return super.equals(obj);
}

}
31 changes: 21 additions & 10 deletions src/main/java/org/silentsoft/oss/NoticeFileGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public class NoticeFileGenerator {

private String productName;
private String organization;
private String owner;
private List<String> texts;
private List<Library> libraries;

Expand All @@ -17,20 +17,23 @@ private NoticeFileGenerator() {

private NoticeFileGenerator(NoticeFileBuilder builder) {
this.productName = builder.productName;
this.organization = builder.organization;
this.owner = builder.owner;
this.texts = builder.texts;
this.libraries = builder.libraries;
}

public String generate() {
return generate(productName, organization, texts.toArray(new String[0]), libraries.toArray(new Library[0]));
return generate(productName, owner, texts.toArray(new String[0]), libraries.toArray(new Library[0]));
}

public static String generate(String productName, String organization, String[] texts, Library[] libraries) {
public static String generate(String productName, String owner, String[] texts, Library[] libraries) {
StringBuffer buffer = new StringBuffer();

buffer.append(String.format("# %s\r\n", productName));
buffer.append(String.format("Copyright (c) %s. All rights reserved.\r\n\r\n", organization));
if (owner != null && "".equals(owner.trim()) == false) {
buffer.append(String.format("Copyright (c) %s. All rights reserved.\r\n", owner));
}
buffer.append("\r\n");

for (String text : texts) {
buffer.append(String.format("%s\r\n\r\n", text));
Expand All @@ -45,19 +48,27 @@ public static String generate(String productName, String organization, String[]
return buffer.toString().trim();
}

public static NoticeFileBuilder newInstance(String productName, String organization) {
return new NoticeFileBuilder(productName, organization);
public static NoticeFileBuilder newInstance(String productName) {
return new NoticeFileBuilder(productName);
}

public static NoticeFileBuilder newInstance(String productName, String owner) {
return new NoticeFileBuilder(productName, owner);
}

public static class NoticeFileBuilder {
private String productName;
private String organization;
private String owner;
private List<String> texts;
private List<Library> libraries;

private NoticeFileBuilder(String productName, String organization) {
private NoticeFileBuilder(String productName) {
this(productName, null);
}

private NoticeFileBuilder(String productName, String owner) {
this.productName = productName;
this.organization = organization;
this.owner = owner;
this.texts = new LinkedList<>();
this.libraries = new LinkedList<>();
}
Expand Down
57 changes: 52 additions & 5 deletions src/test/java/org/silentsoft/oss/NoticeFileGeneratorTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package org.silentsoft.oss;

import java.util.function.Function;
import java.util.stream.Stream;

import org.junit.Assert;
import org.junit.Test;
import org.silentsoft.oss.license.ApacheLicense2;
import org.silentsoft.oss.license.MITLicense;

public class NoticeFileGeneratorTest {

Expand All @@ -23,18 +27,61 @@ public void templateTest() {

@Test
public void generateTest() {
StringBuffer buffer = new StringBuffer();
buffer.append("# {{Your Product Name}}");
buffer.append("\r\n");
buffer.append("Copyright (c) {{Your Organization}}. All rights reserved.");
StringBuffer case1 = new StringBuffer();
case1.append("# {{Your Product Name}}");

Assert.assertEquals(buffer.toString(), NoticeFileGenerator.newInstance("{{Your Product Name}}", "{{Your Organization}}").generate().trim());
Assert.assertEquals(case1.toString(), NoticeFileGenerator.newInstance("{{Your Product Name}}").generate().trim());

StringBuffer case2 = new StringBuffer();
case2.append("# {{Your Product Name}}");
case2.append("\r\n");
case2.append("Copyright (c) {{Your Organization}}. All rights reserved.");

Assert.assertEquals(case2.toString(), NoticeFileGenerator.newInstance("{{Your Product Name}}", "{{Your Organization}}").generate().trim());
}

@Test
public void noticeEqualsTest() {
String a = NoticeFileGenerator.newInstance("{{Your Product Name}}").generate();
String b = NoticeFileGenerator.newInstance("{{Your Product Name}}", "").generate();
String c = NoticeFileGenerator.newInstance("{{Your Product Name}}", null).generate();

Assert.assertTrue(a.equals(b));
Assert.assertTrue(b.equals(c));
}

@Test
public void addLibraryTest() {
String a = NoticeFileGenerator.newInstance("{{Your Product Name}}", "{{Your Organization}}").addLibrary("name version", "url", new ApacheLicense2()).generate();
String b = NoticeFileGenerator.newInstance("{{Your Product Name}}", "{{Your Organization}}").addLibrary("name", "version", "url", new ApacheLicense2()).generate();

Assert.assertTrue(a.equals(b));
}

@Test
public void licenseEqualsTest() {
License a = new ApacheLicense2();
License b = new ApacheLicense2();
License c = new MITLicense();

Assert.assertTrue(a.equals(b));
Assert.assertFalse(b.equals(c));
}

@Test
public void licenseStreamDistinctTest() {
Function<License[], String> texts = (licenses) -> {
StringBuffer buffer = new StringBuffer();
Stream.of(licenses).distinct().forEach((license) -> {
buffer.append(String.format("%s\r\n", license.toString()));
});

return buffer.toString();
};

String a = texts.apply(new License[] {new ApacheLicense2()});
String b = texts.apply(new License[] {new ApacheLicense2(), new ApacheLicense2()});

Assert.assertTrue(a.equals(b));
}

Expand Down

0 comments on commit db6844d

Please sign in to comment.