Skip to content
Permalink
Browse files

[JENKINS-50216] - Cleanup codestyle and re-enable Checkstyle

  • Loading branch information
oleg-nenashev committed Apr 17, 2018
1 parent 902af17 commit bb9c2cc5ca5940b2043ee35bf6afb3ac3c7c68af
@@ -143,8 +143,6 @@
<jenkins.version>1.653</jenkins.version>
<google.api.version>1.22.0</google.api.version>
<java.level>7</java.level>
<!-- TODO: Currently checkstyle fails even for the 'master' branch code. It also prohibits code not Copyrighted by Google-->
<checkstyle.skip>true</checkstyle.skip>
</properties>

<dependencies>
@@ -79,7 +79,9 @@ public RemotableGoogleCredentials(
throw new GeneralSecurityException(
Messages.RemotableGoogleCredentials_NoAccessToken(), e);
}
this.accessToken = mock ? "MOCKED" : checkNotNull(credential.getAccessToken());
this.accessToken = mock
? "MOCKED"
: checkNotNull(credential.getAccessToken());
this.expiration = new DateTime().plusSeconds(
checkNotNull(credential.getExpiresInSeconds()).intValue());
}
@@ -111,7 +113,8 @@ public String getUsername() {

/**
* Gets expiration time of the credentials token.
* @return Expiration time. {@code null} if time is not set, Token will be considered as expired.
* @return Expiration time. {@code null} if time is not set,
* token will be considered as expired.
* @since TODO
*/
@CheckForNull
@@ -122,7 +125,8 @@ public DateTime getTokenExpirationTime() {
/**
* Check if token is expired.
* @return {@code true} if token is expired.
* If {@link #expiration} deserialization fails, the token is always expired
* If {@link #expiration} deserialization fails,
* the token is always expired
* @since TODO
*/
public boolean isTokenExpired() {
@@ -1,5 +1,24 @@
/*
* Copyright 2018 CloudBees, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.jenkins.plugins.util;

import java.util.logging.Logger;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
@@ -9,20 +28,16 @@
import hudson.init.InitMilestone;
import hudson.init.Initializer;
import jenkins.model.Jenkins;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.slf4j.event.Level;

import java.util.TimeZone;
import java.util.logging.Logger;

/**
* Performs JEP-200-safe conversion of {@link DateTime} classes.
* @author Oleg Nenashev
* @since TODO
* @since TODO(oleg_nenashev): Add once the target release is defined.
*/
public class JodaDateTimeConverter implements Converter {

private static final Logger LOGGER = Logger.getLogger(JodaDateTimeConverter.class.getName());
private static final Logger LOGGER = Logger.getLogger(
JodaDateTimeConverter.class.getName());

@Initializer(before = InitMilestone.PLUGINS_LISTED)
public static void initConverter() {
@@ -31,28 +46,40 @@ public static void initConverter() {
}

@Override
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
DateTime value = (DateTime)source;
writer.addAttribute("millis", Long.toString(value.getMillis()));
writer.addAttribute("timezone", value.getChronology().getZone().getID());
public void marshal(Object source, HierarchicalStreamWriter writer,
MarshallingContext context) {
DateTime value = (DateTime) source;
writer.addAttribute("millis",
Long.toString(value.getMillis()));
writer.addAttribute("timezone",
value.getChronology().getZone().getID());
}

@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
public Object unmarshal(HierarchicalStreamReader reader,
UnmarshallingContext context) {
String millis = reader.getAttribute("millis");
if (millis != null) { // new format
String timezone = reader.getAttribute("timezone");
Long val = Long.parseLong(millis);
DateTimeZone tz = timezone != null ? DateTimeZone.forID(timezone) : null;
DateTimeZone tz = timezone != null
? DateTimeZone.forID(timezone) : null;
return new DateTime(val, tz);
}

//TODO: this thing may not work for other ISOChronology implementations, but in such case we will get null
//TODO(oleg-nenashev): this thing may not work for other
// ISOChronology implementations, but in such case we will get null.
// Null will enforce the token refresh

// Old format
// <iMillis>1523889095013</iMillis>
// <iChronology class="org.joda.time.chrono.ISOChronology" resolves-to="org.joda.time.chrono.ISOChronology$Stub" serialization="custom">
// <iChronology class="org.joda.time.chrono.ISOChronology"
// resolves-to="org.joda.time.chrono.ISOChronology$Stub"
// serialization="custom">
// <org.joda.time.chrono.ISOChronology_-Stub>
// <org.joda.time.tz.CachedDateTimeZone resolves-to="org.joda.time.DateTimeZone$Stub" serialization="custom">
// <org.joda.time.tz.CachedDateTimeZone
// resolves-to="org.joda.time.DateTimeZone$Stub"
// serialization="custom">
// <org.joda.time.DateTimeZone_-Stub>
// <string>Europe/Zurich</string>
// </org.joda.time.DateTimeZone_-Stub>
@@ -86,8 +113,9 @@ public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext co
return new DateTime(val, tz);
}

//TODO: throw something meaningful?
throw new IllegalStateException("Unsupported format for: " + DateTime.class + " in " + context.currentObject());
//TODO(oleg-nenashev): throw something meaningful?
throw new IllegalStateException("Unsupported format for: " +
DateTime.class + " in " + context.currentObject());
}

@Override
@@ -22,9 +22,12 @@
import java.util.Arrays;
import java.util.Collection;

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import hudson.XmlFile;
import jenkins.model.Jenkins;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import org.joda.time.DateTime;
import org.junit.Rule;
import org.junit.Test;
@@ -33,11 +36,9 @@
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import hudson.XmlFile;
import jenkins.model.Jenkins;

/**
* Tests which involve real Jenkins instance.
@@ -61,30 +62,41 @@ public void checkSerializationRoundtrip() throws Exception {

GoogleCredential googleCredential = new GoogleCredential();
googleCredential.setExpiresInSeconds(120L);
GoogleRobotCredentials creds = new GoogleRobotCredentialsTest.FakeGoogleCredentials(
GoogleRobotCredentials creds =
new GoogleRobotCredentialsTest.FakeGoogleCredentials(
"myproject", googleCredential);

RemotableGoogleCredentials credentials = new RemotableGoogleCredentials(creds, new Requirement(), new GoogleRobotCredentialsModule(), true);
RemotableGoogleCredentials credentials =
new RemotableGoogleCredentials(creds, new Requirement(),
new GoogleRobotCredentialsModule(), true);
xml.write(credentials);

// now Reload it
RemotableGoogleCredentials read = (RemotableGoogleCredentials) xml.read();
assertFalse("Deserialized token should not be considered as expired", read.isTokenExpired());
RemotableGoogleCredentials read =
(RemotableGoogleCredentials) xml.read();
assertFalse(
"Deserialized token should not be considered as expired",
read.isTokenExpired());
}

@Test
@Issue("JENKINS-50216")
public void shouldDeserializeOldFormat() throws Exception {
File file = new File(tmp.getRoot(), "remotableGoogleCredentials.xml");
try (InputStream istream = RemotableGoogleCredentialsTest.class.getResourceAsStream("jodaDateTimeXML.xml");
try (InputStream istream =
RemotableGoogleCredentialsTest.class.getResourceAsStream(
"jodaDateTimeXML.xml");
OutputStream ostream = new FileOutputStream(file)) {
org.apache.commons.io.IOUtils.copy(istream, ostream);
}

XmlFile xml = new XmlFile(Jenkins.XSTREAM2, file);
RemotableGoogleCredentials read = (RemotableGoogleCredentials) xml.read();
RemotableGoogleCredentials read =
(RemotableGoogleCredentials) xml.read();
DateTime expiration = read.getTokenExpirationTime();
assertNotNull("Expiration token should be read from the Old XML format", expiration);
assertNotNull(
"Expiration token should be read from the Old XML format",
expiration);
assertEquals(1523889095013L, expiration.getMillis());
assertEquals("Europe/Zurich", expiration.getZone().getID());
}
@@ -93,16 +105,20 @@ public void shouldDeserializeOldFormat() throws Exception {
@Issue("JENKINS-50216")
public void shouldDeserializeBrokenXMLAsNull() throws Exception {
File file = new File(tmp.getRoot(), "remotableGoogleCredentials.xml");
try (InputStream istream = RemotableGoogleCredentialsTest.class.getResourceAsStream("jodaDateTimeBroken.xml");
try (InputStream istream =
RemotableGoogleCredentialsTest.class.getResourceAsStream(
"jodaDateTimeBroken.xml");
OutputStream ostream = new FileOutputStream(file)) {
org.apache.commons.io.IOUtils.copy(istream, ostream);
}

XmlFile xml = new XmlFile(Jenkins.XSTREAM2, file);
RemotableGoogleCredentials read = (RemotableGoogleCredentials) xml.read();
RemotableGoogleCredentials read =
(RemotableGoogleCredentials) xml.read();
DateTime expiration = read.getTokenExpirationTime();
assertNull("Expiration token should be null", expiration);
assertTrue("Deserialized token should be considered as expired", read.isTokenExpired());
assertTrue("Deserialized token should be considered as expired",
read.isTokenExpired());
}

private static class Requirement extends GoogleOAuth2ScopeRequirement {

0 comments on commit bb9c2cc

Please sign in to comment.