Skip to content

Commit

Permalink
Merge branch 'master' into flexMonitoredResource
Browse files Browse the repository at this point in the history
  • Loading branch information
gregw committed Jan 18, 2017
2 parents 3185bc2 + 0010dd8 commit 30c0019
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -210,13 +211,9 @@ public Set<String> 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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,16 @@ 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);
EasyMock.replay(options, logging);
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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();
}
Expand Down

0 comments on commit 30c0019

Please sign in to comment.