diff --git a/google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystem.java b/google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystem.java index 60a39fb5a817..4f031c92f740 100644 --- a/google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystem.java +++ b/google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystem.java @@ -27,6 +27,7 @@ import java.net.URISyntaxException; import java.nio.file.FileStore; import java.nio.file.FileSystem; +import java.nio.file.FileSystems; import java.nio.file.Path; import java.nio.file.PathMatcher; import java.nio.file.WatchService; @@ -210,13 +211,9 @@ public Set supportedFileAttributeViews() { return SUPPORTED_VIEWS; } - /** - * Throws {@link UnsupportedOperationException} because this feature hasn't been implemented yet. - */ @Override public PathMatcher getPathMatcher(String syntaxAndPattern) { - // TODO(#813): Implement me. - throw new UnsupportedOperationException(); + return FileSystems.getDefault().getPathMatcher(syntaxAndPattern); } /** diff --git a/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemTest.java b/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemTest.java index ec856447b96a..d7d5b346c376 100644 --- a/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemTest.java +++ b/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemTest.java @@ -34,6 +34,7 @@ import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.PathMatcher; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; @@ -159,4 +160,27 @@ public void testListFiles() throws IOException { assertThat(got).containsExactlyElementsIn(goodPaths); } } + + @Test + public void testMatcher() throws IOException { + try (FileSystem fs = CloudStorageFileSystem.forBucket("bucket")) { + String pattern1 = "glob:*.java"; + PathMatcher javaFileMatcher = fs.getPathMatcher(pattern1); + assertMatches(fs, javaFileMatcher, "a.java", true); + assertMatches(fs, javaFileMatcher, "a.text", false); + assertMatches(fs, javaFileMatcher, "folder/c.java", true); + assertMatches(fs, javaFileMatcher, "d", false); + + String pattern2 = "glob:*.{java,text}"; + PathMatcher javaAndTextFileMatcher = fs.getPathMatcher(pattern2); + assertMatches(fs, javaAndTextFileMatcher, "a.java", true); + assertMatches(fs, javaAndTextFileMatcher, "a.text", true); + assertMatches(fs, javaAndTextFileMatcher, "folder/c.java", true); + assertMatches(fs, javaAndTextFileMatcher, "d", false); + } + } + + private void assertMatches(FileSystem fs, PathMatcher matcher, String toMatch, boolean expected) { + assertThat(matcher.matches(fs.getPath(toMatch).getFileName())).isEqualTo(expected); + } } diff --git a/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingHandler.java b/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingHandler.java index df80d361d9a8..ecb2706d9528 100644 --- a/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingHandler.java +++ b/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingHandler.java @@ -363,6 +363,7 @@ private LogEntry entryFor(LogRecord record) { LogEntry.Builder builder = LogEntry.newBuilder(Payload.StringPayload.of(payload)) .addLabel("levelName", level.getName()) .addLabel("levelValue", String.valueOf(level.intValue())) + .setTimestamp(record.getMillis()) .setSeverity(severityFor(level)); for (Enhancer enhancer : enhancers) { diff --git a/google-cloud-logging/src/test/java/com/google/cloud/logging/AsyncLoggingHandlerTest.java b/google-cloud-logging/src/test/java/com/google/cloud/logging/AsyncLoggingHandlerTest.java index 495430c23d75..8ea02628309f 100644 --- a/google-cloud-logging/src/test/java/com/google/cloud/logging/AsyncLoggingHandlerTest.java +++ b/google-cloud-logging/src/test/java/com/google/cloud/logging/AsyncLoggingHandlerTest.java @@ -65,6 +65,7 @@ public void testPublish() { .setSeverity(Severity.DEBUG) .addLabel("levelName", "FINEST") .addLabel("levelValue", String.valueOf(Level.FINEST.intValue())) + .setTimestamp(123456789L) .build(); EasyMock.expect(logging.writeAsync(ImmutableList.of(entry), WriteOption.logName(LOG_NAME), WriteOption.resource(DEFAULT_RESOURCE))).andReturn(FUTURE); @@ -72,6 +73,8 @@ public void testPublish() { Handler handler = new AsyncLoggingHandler(LOG_NAME, options); handler.setLevel(Level.ALL); handler.setFormatter(new TestFormatter()); - handler.publish(new LogRecord(Level.FINEST, MESSAGE)); + LogRecord record = new LogRecord(Level.FINEST, MESSAGE); + record.setMillis(123456789L); + handler.publish(record); } } diff --git a/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java b/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java index 6b593e665fa2..1c19d4d40922 100644 --- a/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java +++ b/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java @@ -47,6 +47,7 @@ public class LoggingHandlerTest { .setSeverity(Severity.DEBUG) .addLabel("levelName", "FINEST") .addLabel("levelValue", String.valueOf(Level.FINEST.intValue())) + .setTimestamp(123456789L) .build(); private static final LogEntry FINEST_ENHANCED_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE)) .setSeverity(Severity.DEBUG) @@ -58,61 +59,73 @@ public class LoggingHandlerTest { .setSeverity(Severity.DEBUG) .addLabel("levelName", "FINER") .addLabel("levelValue", String.valueOf(Level.FINER.intValue())) + .setTimestamp(123456789L) .build(); private static final LogEntry FINE_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE)) .setSeverity(Severity.DEBUG) .addLabel("levelName", "FINE") .addLabel("levelValue", String.valueOf(Level.FINE.intValue())) + .setTimestamp(123456789L) .build(); private static final LogEntry CONFIG_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE)) .setSeverity(Severity.INFO) .addLabel("levelName", "CONFIG") .addLabel("levelValue", String.valueOf(Level.CONFIG.intValue())) + .setTimestamp(123456789L) .build(); private static final LogEntry INFO_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE)) .setSeverity(Severity.INFO) .addLabel("levelName", "INFO") .addLabel("levelValue", String.valueOf(Level.INFO.intValue())) + .setTimestamp(123456789L) .build(); private static final LogEntry WARNING_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE)) .setSeverity(Severity.WARNING) .addLabel("levelName", "WARNING") .addLabel("levelValue", String.valueOf(Level.WARNING.intValue())) + .setTimestamp(123456789L) .build(); private static final LogEntry SEVERE_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE)) .setSeverity(Severity.ERROR) .addLabel("levelName", "SEVERE") .addLabel("levelValue", String.valueOf(Level.SEVERE.intValue())) + .setTimestamp(123456789L) .build(); private static final LogEntry DEBUG_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE)) .setSeverity(Severity.DEBUG) .addLabel("levelName", "DEBUG") .addLabel("levelValue", String.valueOf(LoggingLevel.DEBUG.intValue())) + .setTimestamp(123456789L) .build(); private static final LogEntry NOTICE_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE)) .setSeverity(Severity.NOTICE) .addLabel("levelName", "NOTICE") .addLabel("levelValue", String.valueOf(LoggingLevel.NOTICE.intValue())) + .setTimestamp(123456789L) .build(); private static final LogEntry ERROR_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE)) .setSeverity(Severity.ERROR) .addLabel("levelName", "ERROR") .addLabel("levelValue", String.valueOf(LoggingLevel.ERROR.intValue())) + .setTimestamp(123456789L) .build(); private static final LogEntry CRITICAL_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE)) .setSeverity(Severity.CRITICAL) .addLabel("levelName", "CRITICAL") .addLabel("levelValue", String.valueOf(LoggingLevel.CRITICAL.intValue())) + .setTimestamp(123456789L) .build(); private static final LogEntry ALERT_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE)) .setSeverity(Severity.ALERT) .addLabel("levelName", "ALERT") .addLabel("levelValue", String.valueOf(LoggingLevel.ALERT.intValue())) + .setTimestamp(123456789L) .build(); private static final LogEntry EMERGENCY_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE)) .setSeverity(Severity.EMERGENCY) .addLabel("levelName", "EMERGENCY") .addLabel("levelValue", String.valueOf(LoggingLevel.EMERGENCY.intValue())) + .setTimestamp(123456789L) .build(); private Logging logging; @@ -136,6 +149,13 @@ public void setUp() { public void afterClass() { EasyMock.verify(logging, options); } + + + private static LogRecord newLogRecord(Level level, String message) { + LogRecord record = new LogRecord(level, message); + record.setMillis(123456789L); + return record; + } @Test public void testPublishLevels() { @@ -185,20 +205,20 @@ public void testPublishLevels() { handler.setLevel(Level.ALL); handler.setFormatter(new TestFormatter()); // default levels - handler.publish(new LogRecord(Level.FINEST, MESSAGE)); - handler.publish(new LogRecord(Level.FINER, MESSAGE)); - handler.publish(new LogRecord(Level.FINE, MESSAGE)); - handler.publish(new LogRecord(Level.CONFIG, MESSAGE)); - handler.publish(new LogRecord(Level.INFO, MESSAGE)); - handler.publish(new LogRecord(Level.WARNING, MESSAGE)); - handler.publish(new LogRecord(Level.SEVERE, MESSAGE)); + handler.publish(newLogRecord(Level.FINEST, MESSAGE)); + handler.publish(newLogRecord(Level.FINER, MESSAGE)); + handler.publish(newLogRecord(Level.FINE, MESSAGE)); + handler.publish(newLogRecord(Level.CONFIG, MESSAGE)); + handler.publish(newLogRecord(Level.INFO, MESSAGE)); + handler.publish(newLogRecord(Level.WARNING, MESSAGE)); + handler.publish(newLogRecord(Level.SEVERE, MESSAGE)); // Logging levels - handler.publish(new LogRecord(LoggingLevel.DEBUG, MESSAGE)); - handler.publish(new LogRecord(LoggingLevel.NOTICE, MESSAGE)); - handler.publish(new LogRecord(LoggingLevel.ERROR, MESSAGE)); - handler.publish(new LogRecord(LoggingLevel.CRITICAL, MESSAGE)); - handler.publish(new LogRecord(LoggingLevel.ALERT, MESSAGE)); - handler.publish(new LogRecord(LoggingLevel.EMERGENCY, MESSAGE)); + handler.publish(newLogRecord(LoggingLevel.DEBUG, MESSAGE)); + handler.publish(newLogRecord(LoggingLevel.NOTICE, MESSAGE)); + handler.publish(newLogRecord(LoggingLevel.ERROR, MESSAGE)); + handler.publish(newLogRecord(LoggingLevel.CRITICAL, MESSAGE)); + handler.publish(newLogRecord(LoggingLevel.ALERT, MESSAGE)); + handler.publish(newLogRecord(LoggingLevel.EMERGENCY, MESSAGE)); } @Test @@ -213,7 +233,7 @@ public void testPublishCustomResource() { Handler handler = new LoggingHandler(LOG_NAME, options, resource); handler.setLevel(Level.ALL); handler.setFormatter(new TestFormatter()); - handler.publish(new LogRecord(Level.FINEST, MESSAGE)); + handler.publish(newLogRecord(Level.FINEST, MESSAGE)); } @Test @@ -260,7 +280,7 @@ public void testReportFlushError() { handler.setLevel(Level.ALL); handler.setErrorManager(errorManager); handler.setFormatter(new TestFormatter()); - handler.publish(new LogRecord(Level.FINEST, MESSAGE)); + handler.publish(newLogRecord(Level.FINEST, MESSAGE)); EasyMock.verify(errorManager); } @@ -273,7 +293,7 @@ public void testReportFormatError() { ErrorManager errorManager = EasyMock.createStrictMock(ErrorManager.class); errorManager.error(null, ex, ErrorManager.FORMAT_FAILURE); EasyMock.expectLastCall().once(); - LogRecord record = new LogRecord(Level.FINEST, MESSAGE); + LogRecord record = newLogRecord(Level.FINEST, MESSAGE); EasyMock.expect(formatter.format(record)).andThrow(ex); EasyMock.replay(errorManager, formatter); Handler handler = new LoggingHandler(LOG_NAME, options); @@ -296,12 +316,12 @@ public void testFlushSize() { handler.setLevel(Level.ALL); handler.setFlushSize(6); handler.setFormatter(new TestFormatter()); - handler.publish(new LogRecord(Level.FINEST, MESSAGE)); - handler.publish(new LogRecord(Level.FINER, MESSAGE)); - handler.publish(new LogRecord(Level.FINE, MESSAGE)); - handler.publish(new LogRecord(Level.CONFIG, MESSAGE)); - handler.publish(new LogRecord(Level.INFO, MESSAGE)); - handler.publish(new LogRecord(Level.WARNING, MESSAGE)); + handler.publish(newLogRecord(Level.FINEST, MESSAGE)); + handler.publish(newLogRecord(Level.FINER, MESSAGE)); + handler.publish(newLogRecord(Level.FINE, MESSAGE)); + handler.publish(newLogRecord(Level.CONFIG, MESSAGE)); + handler.publish(newLogRecord(Level.INFO, MESSAGE)); + handler.publish(newLogRecord(Level.WARNING, MESSAGE)); } @Test @@ -318,12 +338,12 @@ public void testFlushLevel() { handler.setFlushSize(100); handler.setFlushLevel(Level.WARNING); handler.setFormatter(new TestFormatter()); - handler.publish(new LogRecord(Level.FINEST, MESSAGE)); - handler.publish(new LogRecord(Level.FINER, MESSAGE)); - handler.publish(new LogRecord(Level.FINE, MESSAGE)); - handler.publish(new LogRecord(Level.CONFIG, MESSAGE)); - handler.publish(new LogRecord(Level.INFO, MESSAGE)); - handler.publish(new LogRecord(Level.WARNING, MESSAGE)); + handler.publish(newLogRecord(Level.FINEST, MESSAGE)); + handler.publish(newLogRecord(Level.FINER, MESSAGE)); + handler.publish(newLogRecord(Level.FINE, MESSAGE)); + handler.publish(newLogRecord(Level.CONFIG, MESSAGE)); + handler.publish(newLogRecord(Level.INFO, MESSAGE)); + handler.publish(newLogRecord(Level.WARNING, MESSAGE)); } @Test @@ -334,13 +354,18 @@ public void testAddHandler() { WriteOption.resource(DEFAULT_RESOURCE)); EasyMock.expectLastCall().andReturn(Futures.immediateFuture(null)); EasyMock.replay(options, logging); - LoggingHandler handler = new LoggingHandler(LOG_NAME, options); + LoggingHandler handler = new LoggingHandler(LOG_NAME, options) { + @Override + public void close() { + // Make close NOOP to avoid mock close exception + } + }; handler.setLevel(Level.ALL); handler.setFormatter(new TestFormatter()); Logger logger = Logger.getLogger(getClass().getName()); logger.setLevel(Level.ALL); LoggingHandler.addHandler(logger, handler); - logger.finest(MESSAGE); + logger.log(newLogRecord(Level.FINEST, MESSAGE)); } @Test @@ -356,7 +381,7 @@ public void testClose() throws Exception { Handler handler = new LoggingHandler(LOG_NAME, options); handler.setLevel(Level.ALL); handler.setFormatter(new TestFormatter()); - handler.publish(new LogRecord(Level.FINEST, MESSAGE)); + handler.publish(newLogRecord(Level.FINEST, MESSAGE)); handler.close(); handler.close(); }