Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java 8/Junit 5 Migration #45

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
75e16f0
Change dictionary definition for Delegated-IPv6-Prefix
gribnut May 26, 2020
328c8cc
Fix parsing of Ipv6PrefixAttribute to allow for prefixes smaller than…
gribnut May 26, 2020
7155e86
Add seancfoley IPAddress library for better parsing of IPv6 addresses…
gribnut May 26, 2020
e7f7736
Merge branch 'master' of https://github.com/ctran/TinyRadius into fix…
gribnut May 27, 2020
9594458
Merge commit '9b226fdb743b83bef025b20d75e4eea0c819aa25'
gribnut Jul 4, 2020
c681eef
Merge commit 'b1be739fbe7814c2a2db99a1a38c20689911f9a3'
gribnut Jul 4, 2020
66cd2f1
Merge commit 'b780c224d033f1a714937673a10cc2c751264fef'
gribnut Oct 19, 2021
4fa68b4
Merge branch 'master' of https://github.com/ctran/TinyRadius
gribnut Dec 22, 2021
98cc2f5
Add maven clean and compiler plugins
gribnut Dec 22, 2021
5805989
Bump junit to 4.3.2
gribnut Dec 22, 2021
1b3092f
Remove un-needed public declaration of interface methods
gribnut Dec 22, 2021
a78ca06
Convert C# style byte array to Java convention
gribnut Dec 22, 2021
23c8cf6
Use StandardCharsets.UTF_8 constant instead of hardcoded string value.
gribnut Dec 22, 2021
523512c
Set checks/types for raw maps, lists, sets, etc.
gribnut Dec 23, 2021
439413c
Code cleanup for Java 8 compiler
gribnut Dec 23, 2021
f03e8ef
Additional code cleanup for Java 8 compiler
gribnut Dec 23, 2021
e609db4
Convert raw Class type for AttributeType and add type checking
gribnut Dec 26, 2021
88009c9
Use auto resource management for try block of RadiusClient/Datagram s…
gribnut Dec 26, 2021
938463f
Use Java 11 with release 8 for compiling in CI
gribnut Dec 27, 2021
2d44925
Migrate to Junit 5
gribnut Dec 27, 2021
e01952f
Add some unit tests for Attribute objects
gribnut Dec 27, 2021
b48a42e
Set version to 1.2.0
gribnut Dec 27, 2021
e29cc60
Merge branch 'master' of https://github.com/ctran/TinyRadius
gribnut Jan 15, 2022
748e49d
Swapped LinkedList for ArrayList and use getDeclaredConstructor per s…
gribnut Jan 16, 2022
6b6b011
Remove unused param attributeType from DictionaryParser per sonatype …
gribnut Jan 16, 2022
45a8f85
Synchronize methods reading serverSocket in RadiusClient (sonatype li…
gribnut Jan 16, 2022
d24c740
Synchronize methods reading serverSocket in RadiusClient (sonatype li…
gribnut Jan 16, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 1.8
- name: Set up JDK 11
uses: actions/setup-java@v2
with:
java-version: 8
distribution: adopt
java-version: 11
distribution: temurin
- name: Build with Maven
run: mvn -V -B package --file pom.xml
- name: Calculate dependency tree
Expand Down
30 changes: 26 additions & 4 deletions 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.tinyradius</groupId>
<artifactId>tinyradius</artifactId>
<version>1.1.1-SNAPSHOT</version>
<version>1.2.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>TinyRadius Java Radius Library</name>
<description>
Expand Down Expand Up @@ -43,9 +43,9 @@
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>

Expand All @@ -72,6 +72,28 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<showDeprecation>true</showDeprecation>
<compilerArgs>-Xlint:unchecked</compilerArgs>
<!-- Release directive requires mvn 3.6 and Java 9 or later -->
<release>8</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<!-- JUnit 5 requires Surefire version 2.22.0 or higher -->
<version>2.22.2</version>
</plugin>
</plugins>
</build>
<distributionManagement>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/tinyradius/attribute/IntegerAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public String getAttributeValue() {
return name;
}
// Radius uses only unsigned values....
return Long.toString(((long)value & 0xffffffffl));
return Long.toString(((long)value & 0xffffffffL));
}

/**
Expand All @@ -82,7 +82,7 @@ public void setAttributeValue(String value) {
if (at != null) {
Integer val = at.getEnumeration(value);
if (val != null) {
setAttributeValue(val.intValue());
setAttributeValue(val);
return;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/tinyradius/attribute/IpAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public IpAttribute(int type, long ipNum) {
* @see org.tinyradius.attribute.RadiusAttribute#getAttributeValue()
*/
public String getAttributeValue() {
StringBuffer ip = new StringBuffer();
StringBuilder ip = new StringBuilder();
byte[] data = getAttributeData();
if (data == null || data.length != 4)
throw new RuntimeException("ip attribute: expected 4 bytes attribute data");
Expand Down Expand Up @@ -107,6 +107,7 @@ public long getIpAsLong() {
* Sets the IP number represented by this IpAttribute
* as a 32 bit unsigned number.
* @param ip
* ip address
*/
public void setIpAsLong(long ip) {
byte[] data = new byte[4];
Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/tinyradius/attribute/Ipv6Attribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*/
package org.tinyradius.attribute;

import java.util.StringTokenizer;
import java.net.Inet6Address;
import java.net.UnknownHostException;

Expand Down
6 changes: 2 additions & 4 deletions src/main/java/org/tinyradius/attribute/RadiusAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ public void setAttributeValue(String value) {
* Gets the value of this attribute as a string.
*
* @return value
* @exception RadiusException
* if the value is invalid
*/
public String getAttributeValue() {
return RadiusUtil.getHexString(getAttributeData());
Expand Down Expand Up @@ -168,7 +166,7 @@ public byte[] writeAttribute() {
/**
* Reads in this attribute from the passed byte array.
*
* @param data
* @param data Raw Attribute data
*/
public void readAttribute(byte[] data, int offset, int length) throws RadiusException {
if (length < 2)
Expand Down Expand Up @@ -234,7 +232,7 @@ public static RadiusAttribute createRadiusAttribute(Dictionary dictionary, int v
AttributeType at = dictionary.getAttributeTypeByCode(vendorId, attributeType);
if (at != null && at.getAttributeClass() != null) {
try {
attribute = (RadiusAttribute) at.getAttributeClass().newInstance();
attribute = at.getAttributeClass().getDeclaredConstructor().newInstance();
}
catch (Exception e) {
// error instantiating class - should not occur
Expand Down
14 changes: 3 additions & 11 deletions src/main/java/org/tinyradius/attribute/StringAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
package org.tinyradius.attribute;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

/**
* This class represents a Radius attribute which only
Expand Down Expand Up @@ -36,11 +36,7 @@ public StringAttribute(int type, String value) {
* @return a string
*/
public String getAttributeValue() {
try {
return new String(getAttributeData(), "UTF-8");
} catch (UnsupportedEncodingException uee) {
return new String(getAttributeData());
}
return new String(getAttributeData(), StandardCharsets.UTF_8);
}

/**
Expand All @@ -50,11 +46,7 @@ public String getAttributeValue() {
public void setAttributeValue(String value) {
if (value == null)
throw new NullPointerException("string value not set");
try {
setAttributeData(value.getBytes("UTF-8"));
} catch (UnsupportedEncodingException uee) {
setAttributeData(value.getBytes());
}
setAttributeData(value.getBytes(StandardCharsets.UTF_8));
}

}
42 changes: 18 additions & 24 deletions src/main/java/org/tinyradius/attribute/VendorSpecificAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.tinyradius.dictionary.AttributeType;
import org.tinyradius.dictionary.Dictionary;
Expand Down Expand Up @@ -50,6 +48,7 @@ public VendorSpecificAttribute(int vendorId) {
* Sets the vendor ID of the child attributes.
*
* @param childVendorId
* vendor ID of the child attributes
*/
public void setChildVendorId(int childVendorId) {
this.childVendorId = childVendorId;
Expand All @@ -73,8 +72,7 @@ public int getChildVendorId() {
*/
public void setDictionary(Dictionary dictionary) {
super.setDictionary(dictionary);
for (Iterator i = subAttributes.iterator(); i.hasNext();) {
RadiusAttribute attr = (RadiusAttribute) i.next();
for (RadiusAttribute attr : subAttributes) {
attr.setDictionary(dictionary);
}
}
Expand All @@ -87,7 +85,7 @@ public void setDictionary(Dictionary dictionary) {
*/
public void addSubAttribute(RadiusAttribute attribute) {
if (attribute.getVendorId() != getChildVendorId())
throw new IllegalArgumentException("sub attribut has incorrect vendor ID");
throw new IllegalArgumentException("sub attribute has incorrect vendor ID");

subAttributes.add(attribute);
}
Expand Down Expand Up @@ -137,24 +135,23 @@ public void removeSubAttribute(RadiusAttribute attribute) {
*
* @return List of RadiusAttribute objects
*/
public List getSubAttributes() {
public List<RadiusAttribute> getSubAttributes() {
return subAttributes;
}

/**
* Returns all sub-attributes of this attribut which have the given type.
* Returns all sub-attributes of this attribute which have the given type.
*
* @param attributeType
* type of sub-attributes to get
* @return list of RadiusAttribute objects, does not return null
*/
public List getSubAttributes(int attributeType) {
public List<RadiusAttribute> getSubAttributes(int attributeType) {
if (attributeType < 1 || attributeType > 255)
throw new IllegalArgumentException("sub-attribute type out of bounds");

LinkedList result = new LinkedList();
for (Iterator i = subAttributes.iterator(); i.hasNext();) {
RadiusAttribute a = (RadiusAttribute) i.next();
ArrayList<RadiusAttribute> result = new ArrayList<>();
for (RadiusAttribute a : subAttributes) {
if (attributeType == a.getAttributeType())
result.add(a);
}
Expand All @@ -169,17 +166,17 @@ public List getSubAttributes(int attributeType) {
* sub-attribute type
* @return RadiusAttribute object or null if there is no such sub-attribute
* @throws RuntimeException
* if there are multiple occurences of the
* if there are multiple occurrences of the
* requested sub-attribute type
*/
public RadiusAttribute getSubAttribute(int type) {
List attrs = getSubAttributes(type);
List<RadiusAttribute> attrs = getSubAttributes(type);
if (attrs.size() > 1)
throw new RuntimeException("multiple sub-attributes of requested type " + type);
else if (attrs.size() == 0)
return null;
else
return (RadiusAttribute) attrs.get(0);
return attrs.get(0);
}

/**
Expand All @@ -188,11 +185,10 @@ else if (attrs.size() == 0)
* @param type
* attribute type name
* @return RadiusAttribute object or null if there is no such attribute
* @throws RadiusException
* @throws RuntimeException
* if the attribute occurs multiple times
*/
public RadiusAttribute getSubAttribute(String type) throws RadiusException {
public RadiusAttribute getSubAttribute(String type) {
if (type == null || type.length() == 0)
throw new IllegalArgumentException("type name is empty");

Expand All @@ -218,7 +214,7 @@ public RadiusAttribute getSubAttribute(String type) throws RadiusException {
* @throws RuntimeException
* attribute occurs multiple times
*/
public String getSubAttributeValue(String type) throws RadiusException {
public String getSubAttributeValue(String type) {
RadiusAttribute attr = getSubAttribute(type);
if (attr == null) {
return null;
Expand All @@ -241,8 +237,7 @@ public byte[] writeAttribute() {

// write sub-attributes
try {
for (Iterator i = subAttributes.iterator(); i.hasNext();) {
RadiusAttribute a = (RadiusAttribute) i.next();
for (RadiusAttribute a : subAttributes) {
bos.write(a.writeAttribute());
}
}
Expand Down Expand Up @@ -305,7 +300,7 @@ public void readAttribute(byte[] data, int offset, int length) throws RadiusExce
if (pos != vsaLen)
throw new RadiusException("Vendor-Specific attribute malformed");

subAttributes = new ArrayList(count);
subAttributes = new ArrayList<>(count);
pos = 0;
while (pos < vsaLen) {
int subtype = data[(offset + 6) + pos] & 0x0ff;
Expand All @@ -327,7 +322,7 @@ private static int unsignedByteToInt(byte b) {
* @see org.tinyradius.attribute.RadiusAttribute#toString()
*/
public String toString() {
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
sb.append("Vendor-Specific: ");
int vendorId = getChildVendorId();
String vendorName = getDictionary().getVendorName(vendorId);
Expand All @@ -341,8 +336,7 @@ public String toString() {
sb.append("vendor ID ");
sb.append(vendorId);
}
for (Iterator i = getSubAttributes().iterator(); i.hasNext();) {
RadiusAttribute attr = (RadiusAttribute) i.next();
for (RadiusAttribute attr : getSubAttributes()) {
sb.append("\n");
sb.append(attr.toString());
}
Expand All @@ -352,7 +346,7 @@ public String toString() {
/**
* Sub attributes. Only set if isRawData == false.
*/
private List subAttributes = new ArrayList();
private List<RadiusAttribute> subAttributes = new ArrayList<>();

/**
* Vendor ID of sub-attributes.
Expand Down
Loading