Fix criticals sonar issues

Signed-off-by: SirWayne <dennis.melzer@bosch-si.com>
This commit is contained in:
SirWayne
2016-08-05 10:51:11 +02:00
parent 54ce3ad8e0
commit 88e5bdf8f4
6 changed files with 57 additions and 102 deletions

View File

@@ -14,7 +14,7 @@ import java.net.ServerSocket;
/** /**
* *
* * Look for a free port.
*/ */
public class FreePortFileWriter { public class FreePortFileWriter {
@@ -24,7 +24,9 @@ public class FreePortFileWriter {
/** /**
* @param from * @param from
* port range from (start point)
* @param to * @param to
* port range to (end point)
*/ */
public FreePortFileWriter(final int from, final int to, final String filePortPath) { public FreePortFileWriter(final int from, final int to, final String filePortPath) {
this.from = from; this.from = from;
@@ -51,23 +53,20 @@ public class FreePortFileWriter {
portFile.getParentFile().mkdirs(); portFile.getParentFile().mkdirs();
if (portFile.exists()) { if (portFile.exists()) {
return false; return false;
} else {
boolean isFree = false;
final ServerSocket sock = new ServerSocket();
sock.setReuseAddress(true);
sock.bind(new InetSocketAddress(port));
if (portFile.createNewFile()) {
portFile.deleteOnExit();
isFree = true;
}
sock.close();
// is free:
return isFree;
// We rely on an exception thrown to determine availability or
// not availability.
} }
} catch (final Exception e) { boolean isFree = false;
// not free. final ServerSocket sock = new ServerSocket();
sock.setReuseAddress(true);
sock.bind(new InetSocketAddress(port));
if (portFile.createNewFile()) {
portFile.deleteOnExit();
isFree = true;
}
sock.close();
return isFree;
// We rely on an exception thrown to determine availability or
// not availability and don't want to log the exception.
} catch (@SuppressWarnings({ "squid:S2221", "squid:S1166" }) final Exception e) {
return false; return false;
} }
} }

View File

@@ -223,7 +223,7 @@ public abstract class AbstractIntegrationTest implements EnvironmentAware {
createTestdatabaseAndStart(); createTestdatabaseAndStart();
} }
private static void createTestdatabaseAndStart() { private static synchronized void createTestdatabaseAndStart() {
if ("MYSQL".equals(System.getProperty("spring.jpa.database"))) { if ("MYSQL".equals(System.getProperty("spring.jpa.database"))) {
tesdatabase = new CIMySqlTestDatabase(); tesdatabase = new CIMySqlTestDatabase();
tesdatabase.before(); tesdatabase.before();

View File

@@ -13,11 +13,14 @@ import java.util.List;
import org.eclipse.hawkbit.cache.TenantAwareCacheManager; import org.eclipse.hawkbit.cache.TenantAwareCacheManager;
import org.eclipse.hawkbit.repository.SystemManagement; import org.eclipse.hawkbit.repository.SystemManagement;
import org.eclipse.hawkbit.security.SystemSecurityContext; import org.eclipse.hawkbit.security.SystemSecurityContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
public class JpaTestRepositoryManagement implements TestRepositoryManagement { public class JpaTestRepositoryManagement implements TestRepositoryManagement {
private static final Logger LOGGER = LoggerFactory.getLogger(JpaTestRepositoryManagement.class);
@Autowired @Autowired
private TenantAwareCacheManager cacheManager; private TenantAwareCacheManager cacheManager;
@@ -28,6 +31,7 @@ public class JpaTestRepositoryManagement implements TestRepositoryManagement {
private SystemManagement systemManagement; private SystemManagement systemManagement;
@Override @Override
@Transactional
public void clearTestRepository() { public void clearTestRepository() {
deleteAllRepos(); deleteAllRepos();
cacheManager.getDirectCacheNames().forEach(name -> cacheManager.getDirectCache(name).clear()); cacheManager.getDirectCacheNames().forEach(name -> cacheManager.getDirectCache(name).clear());
@@ -43,7 +47,7 @@ public class JpaTestRepositoryManagement implements TestRepositoryManagement {
return null; return null;
}); });
} catch (final Exception e) { } catch (final Exception e) {
e.printStackTrace(); LOGGER.error("Error hile delete tenant", e);
} }
}); });
} }

View File

@@ -10,10 +10,10 @@ package org.eclipse.hawkbit.repository.test.util;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.security.SecureRandom;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Random;
import java.util.UUID; import java.util.UUID;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
@@ -191,16 +191,17 @@ public class TestdataFactory {
public DistributionSet createDistributionSet(final String prefix, final String version, public DistributionSet createDistributionSet(final String prefix, final String version,
final boolean isRequiredMigrationStep) { final boolean isRequiredMigrationStep) {
final SoftwareModule appMod = softwareManagement.createSoftwareModule(entityFactory.generateSoftwareModule( final SoftwareModule appMod = softwareManagement.createSoftwareModule(
findOrCreateSoftwareModuleType(SM_TYPE_APP, Integer.MAX_VALUE), prefix + SM_TYPE_APP, entityFactory.generateSoftwareModule(findOrCreateSoftwareModuleType(SM_TYPE_APP, Integer.MAX_VALUE),
version + "." + new Random().nextInt(100), LOREM.words(20), prefix + " vendor Limited, California")); prefix + SM_TYPE_APP, version + "." + new SecureRandom().nextInt(100), LOREM.words(20),
prefix + " vendor Limited, California"));
final SoftwareModule runtimeMod = softwareManagement final SoftwareModule runtimeMod = softwareManagement
.createSoftwareModule(entityFactory.generateSoftwareModule(findOrCreateSoftwareModuleType(SM_TYPE_RT), .createSoftwareModule(entityFactory.generateSoftwareModule(findOrCreateSoftwareModuleType(SM_TYPE_RT),
prefix + "app runtime", version + "." + new Random().nextInt(100), LOREM.words(20), prefix + "app runtime", version + "." + new SecureRandom().nextInt(100), LOREM.words(20),
prefix + " vendor GmbH, Stuttgart, Germany")); prefix + " vendor GmbH, Stuttgart, Germany"));
final SoftwareModule osMod = softwareManagement final SoftwareModule osMod = softwareManagement
.createSoftwareModule(entityFactory.generateSoftwareModule(findOrCreateSoftwareModuleType(SM_TYPE_OS), .createSoftwareModule(entityFactory.generateSoftwareModule(findOrCreateSoftwareModuleType(SM_TYPE_OS),
prefix + " Firmware", version + "." + new Random().nextInt(100), LOREM.words(20), prefix + " Firmware", version + "." + new SecureRandom().nextInt(100), LOREM.words(20),
prefix + " vendor Limited Inc, California")); prefix + " vendor Limited Inc, California"));
return distributionSetManagement return distributionSetManagement

View File

@@ -28,16 +28,12 @@ import org.springframework.security.core.context.SecurityContextHolder;
public class WithSpringAuthorityRule implements TestRule { public class WithSpringAuthorityRule implements TestRule {
/*
* (non-Javadoc)
*
* @see org.junit.rules.TestRule#apply(org.junit.runners.model.Statement,
* org.junit.runner.Description)
*/
@Override @Override
public Statement apply(final Statement base, final Description description) { public Statement apply(final Statement base, final Description description) {
return new Statement() { return new Statement() {
@Override @Override
// throwable comes from jnuit evaluate signature
@SuppressWarnings("squid:S00112")
public void evaluate() throws Throwable { public void evaluate() throws Throwable {
final SecurityContext oldContext = before(description); final SecurityContext oldContext = before(description);
try { try {
@@ -49,7 +45,7 @@ public class WithSpringAuthorityRule implements TestRule {
}; };
} }
private SecurityContext before(final Description description) throws Throwable { private SecurityContext before(final Description description) {
final SecurityContext oldContext = SecurityContextHolder.getContext(); final SecurityContext oldContext = SecurityContextHolder.getContext();
WithUser annotation = description.getAnnotation(WithUser.class); WithUser annotation = description.getAnnotation(WithUser.class);
if (annotation == null) { if (annotation == null) {
@@ -63,19 +59,14 @@ public class WithSpringAuthorityRule implements TestRule {
} }
return oldContext; return oldContext;
} }
/**
* @param annotation
*/
private void setSecurityContext(final WithUser annotation) { private void setSecurityContext(final WithUser annotation) {
SecurityContextHolder.setContext(new SecurityContext() { SecurityContextHolder.setContext(new SecurityContext() {
/**
*
*/
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@Override @Override
public void setAuthentication(final Authentication authentication) { public void setAuthentication(final Authentication authentication) {
// nothing todo
} }
@Override @Override
@@ -113,7 +104,8 @@ public class WithSpringAuthorityRule implements TestRule {
if (addPermission) { if (addPermission) {
allPermissions.add(permissionName); allPermissions.add(permissionName);
} }
} catch (IllegalArgumentException | IllegalAccessException e) { // don't want to log this exceptions.
} catch (@SuppressWarnings("squid:S1166") IllegalArgumentException | IllegalAccessException e) {
// nope // nope
} }
} }
@@ -129,18 +121,17 @@ public class WithSpringAuthorityRule implements TestRule {
private void after(final SecurityContext oldContext) { private void after(final SecurityContext oldContext) {
SecurityContextHolder.setContext(oldContext); SecurityContextHolder.setContext(oldContext);
} }
/** /**
* Clears the current security context. * Clears the current security context.
*/ */
public void clear() public void clear() {
{
SecurityContextHolder.clearContext(); SecurityContextHolder.clearContext();
} }
/** /**
* @param callable * @param callable
* @return * @return the callable result
* @throws Exception * @throws Exception
*/ */
public <T> T runAsPrivileged(final Callable<T> callable) throws Exception { public <T> T runAsPrivileged(final Callable<T> callable) throws Exception {
@@ -151,7 +142,7 @@ public class WithSpringAuthorityRule implements TestRule {
* *
* @param withUser * @param withUser
* @param callable * @param callable
* @return * @return callable result
* @throws Exception * @throws Exception
*/ */
public <T> T runAs(final WithUser withUser, final Callable<T> callable) throws Exception { public <T> T runAs(final WithUser withUser, final Callable<T> callable) throws Exception {
@@ -170,7 +161,7 @@ public class WithSpringAuthorityRule implements TestRule {
public static WithUser withUser(final String principal, final String... authorities) { public static WithUser withUser(final String principal, final String... authorities) {
return withUserAndTenant(principal, "default", true, true, authorities); return withUserAndTenant(principal, "default", true, true, authorities);
} }
public static WithUser withUser(final String principal, final boolean allSpPermision, final String... authorities) { public static WithUser withUser(final String principal, final boolean allSpPermision, final String... authorities) {
return withUserAndTenant(principal, "default", true, allSpPermision, authorities); return withUserAndTenant(principal, "default", true, allSpPermision, authorities);
} }
@@ -185,6 +176,15 @@ public class WithSpringAuthorityRule implements TestRule {
public static WithUser withUserAndTenant(final String principal, final String tenant, public static WithUser withUserAndTenant(final String principal, final String tenant,
final boolean autoCreateTenant, final boolean allSpPermission, final String... authorities) { final boolean autoCreateTenant, final boolean allSpPermission, final String... authorities) {
return createWithUser(principal, tenant, autoCreateTenant, allSpPermission, authorities);
}
private static WithUser privilegedUser() {
return createWithUser("bumlux", "default", true, true, new String[] { "ROLE_CONTROLLER" });
}
private static WithUser createWithUser(final String principal, final String tenant, final boolean autoCreateTenant,
final boolean allSpPermission, final String... authorities) {
return new WithUser() { return new WithUser() {
@Override @Override
@@ -214,7 +214,7 @@ public class WithSpringAuthorityRule implements TestRule {
@Override @Override
public String[] removeFromAllPermission() { public String[] removeFromAllPermission() {
return null; return new String[0];
} }
@Override @Override
@@ -222,65 +222,10 @@ public class WithSpringAuthorityRule implements TestRule {
return tenant; return tenant;
} }
/*
* (non-Javadoc)
*
* @see org.eclipse.hawkbit.WithUser#autoCreateTenant()
*/
@Override @Override
public boolean autoCreateTenant() { public boolean autoCreateTenant() {
return autoCreateTenant; return autoCreateTenant;
} }
}; };
} }
private static WithUser privilegedUser() {
return new WithUser() {
@Override
public Class<? extends Annotation> annotationType() {
return WithUser.class;
}
@Override
public String principal() {
return "bumlux";
}
@Override
public String credentials() {
return null;
}
@Override
public String[] authorities() {
return new String[] { "ROLE_CONTROLLER" };
}
@Override
public boolean allSpPermissions() {
return true;
}
@Override
public String[] removeFromAllPermission() {
return null;
}
@Override
public String tenantId() {
return "default";
}
/*
* (non-Javadoc)
*
* @see org.eclipse.hawkbit.WithUser#autoCreateTenant()
*/
@Override
public boolean autoCreateTenant() {
return true;
}
};
}
} }

View File

@@ -43,6 +43,9 @@ import com.vaadin.ui.themes.ValoTheme;
/** /**
* Abstract class for target/ds tag token layout. * Abstract class for target/ds tag token layout.
*
* @param <T>
* the special entity
*/ */
public abstract class AbstractTagToken<T extends BaseEntity> implements Serializable { public abstract class AbstractTagToken<T extends BaseEntity> implements Serializable {
@@ -75,6 +78,9 @@ public abstract class AbstractTagToken<T extends BaseEntity> implements Serializ
@Autowired @Autowired
protected ManagementUIState managementUIState; protected ManagementUIState managementUIState;
// BaseEntity implements Serializable so this entity is serializable. Maybe
// a sonar bug
@SuppressWarnings("squid:S1948")
protected T selectedEntity; protected T selectedEntity;
@PostConstruct @PostConstruct